aboutsummaryrefslogtreecommitdiff
path: root/butl/string-table
blob: a1d60c3ab35dea18740cde46ceaee1c6d6aea89e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// file      : butl/string-table -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUTL_STRING_TABLE
#define BUTL_STRING_TABLE

#include <vector>
#include <string>
#include <unordered_map>

#include <butl/export>

#include <butl/multi-index>

namespace butl
{
  // A pool of strings and, optionally, other accompanying data in which
  // each entry is assigned an individual index (or id) of type I (e.g.,
  // uint8_t, uint16_t, etc., depending on how many entries are expected).
  // Index value 0 is reserved to indicate the "no entry" condition.
  //
  template <typename I, typename D>
  struct string_table_element
  {
    const I i;
    const D d;
  };

  template <typename I>
  struct string_table_element<I, std::string>
  {
    const I i;
    const std::string d;
  };

  // For custom data the options are to call the data member 'key' or to
  // specialize this traits.
  //
  template <typename D>
  struct string_table_traits
  {
    static const std::string&
    key (const D& d) {return d.key;}
  };

  template <>
  struct string_table_traits<std::string>
  {
    static const std::string&
    key (const std::string& d) {return d;}
  };

  template <typename I, typename D = std::string>
  struct string_table
  {
    // Insert new entry unless one already exists.
    //
    I
    insert (const D&);

    // Find existing.
    //
    I
    find (const std::string& k) const
    {
      auto i (map_.find (key_type (&k)));
      return i != map_.end () ? i->second.i : 0;
    }

    // Reverse lookup.
    //
    const D&
    operator[] (I i) const {assert (i > 0); return vec_[i - 1]->second.d;}

    I
    size () const {return static_cast<I> (vec_.size ());}

    bool
    empty () const {return vec_.empty ();}

    void
    clear () {vec_.clear (); map_.clear ();}

  private:
    using key_type = butl::map_key<std::string>;
    using value_type = string_table_element<I, D>;
    using map_type = std::unordered_map<key_type, value_type>;
    using traits = string_table_traits<D>;

    map_type map_;
    std::vector<typename map_type::const_iterator> vec_;
  };
}

#include <butl/string-table.txx>

#endif // BUTL_STRING_TABLE