diff options
Diffstat (limited to 'identifier.h')
-rw-r--r-- | identifier.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/identifier.h b/identifier.h new file mode 100644 index 0000000..74d83ce --- /dev/null +++ b/identifier.h | |||
@@ -0,0 +1,59 @@ | |||
1 | #ifndef IDENTIFIER_H_D7EE5679 | ||
2 | #define IDENTIFIER_H_D7EE5679 | ||
3 | |||
4 | #include <map> | ||
5 | #include <vector> | ||
6 | |||
7 | template <typename T> | ||
8 | class identifier { | ||
9 | public: | ||
10 | |||
11 | using value_type = T; | ||
12 | |||
13 | private: | ||
14 | |||
15 | using vector_type = std::vector<value_type>; | ||
16 | |||
17 | public: | ||
18 | |||
19 | using key_type = typename vector_type::size_type; | ||
20 | |||
21 | key_type add(const value_type& val) | ||
22 | { | ||
23 | auto it = ids_.find(val); | ||
24 | |||
25 | if (it == std::end(ids_)) | ||
26 | { | ||
27 | key_type ret = ids_.size(); | ||
28 | ids_[val] = ret; | ||
29 | |||
30 | uniq_.push_back(val); | ||
31 | |||
32 | return ret; | ||
33 | } else { | ||
34 | return it->second; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | void compile() | ||
39 | { | ||
40 | ids_.clear(); | ||
41 | } | ||
42 | |||
43 | inline const value_type& get(key_type i) const | ||
44 | { | ||
45 | return uniq_.at(i); | ||
46 | } | ||
47 | |||
48 | inline key_type size() const | ||
49 | { | ||
50 | return uniq_.size(); | ||
51 | } | ||
52 | |||
53 | private: | ||
54 | |||
55 | std::map<value_type, key_type> ids_; | ||
56 | vector_type uniq_; | ||
57 | }; | ||
58 | |||
59 | #endif /* end of include guard: IDENTIFIER_H_D7EE5679 */ | ||