|
6 | 6 |
|
7 | 7 | \*******************************************************************/
|
8 | 8 |
|
9 |
| - |
10 | 9 | #ifndef CPROVER_UTIL_NUMBERING_H
|
11 | 10 | #define CPROVER_UTIL_NUMBERING_H
|
12 | 11 |
|
|
16 | 15 | #include <vector>
|
17 | 16 |
|
18 | 17 | #include <util/invariant.h>
|
| 18 | +#include <util/optional.h> |
19 | 19 |
|
20 |
| -template <typename T> |
21 |
| -// NOLINTNEXTLINE(readability/identifiers) |
22 |
| -class numbering final |
| 20 | +/// \tparam Map a map from a key type to some numeric type |
| 21 | +template <typename Map> |
| 22 | +class template_numberingt final |
23 | 23 | {
|
24 | 24 | public:
|
25 |
| - // NOLINTNEXTLINE(readability/identifiers) |
26 |
| - typedef std::size_t number_type; |
| 25 | + using number_type = typename Map::mapped_type; // NOLINT |
| 26 | + using key_type = typename Map::key_type; // NOLINT |
27 | 27 |
|
28 | 28 | private:
|
29 |
| - typedef std::vector<T> data_typet; |
30 |
| - data_typet data; |
31 |
| - typedef std::map<T, number_type> numberst; |
32 |
| - numberst numbers; |
| 29 | + using data_typet = std::vector<key_type>; // NOLINT |
| 30 | + data_typet data_; |
| 31 | + Map numbers_; |
33 | 32 |
|
34 | 33 | public:
|
35 |
| - // NOLINTNEXTLINE(readability/identifiers) |
36 |
| - typedef typename data_typet::size_type size_type; |
37 |
| - // NOLINTNEXTLINE(readability/identifiers) |
38 |
| - typedef typename data_typet::iterator iterator; |
39 |
| - // NOLINTNEXTLINE(readability/identifiers) |
40 |
| - typedef typename data_typet::const_iterator const_iterator; |
41 |
| - |
42 |
| - number_type number(const T &a) |
| 34 | + using size_type = typename data_typet::size_type; // NOLINT |
| 35 | + using iterator = typename data_typet::iterator; // NOLINT |
| 36 | + using const_iterator = typename data_typet::const_iterator; // NOLINT |
| 37 | + |
| 38 | + number_type number(const key_type &a) |
43 | 39 | {
|
44 |
| - std::pair<typename numberst::const_iterator, bool> result= |
45 |
| - numbers.insert( |
46 |
| - std::pair<T, number_type> |
47 |
| - (a, number_type(numbers.size()))); |
| 40 | + const auto result = numbers_.emplace(a, number_type(numbers_.size())); |
48 | 41 |
|
49 | 42 | if(result.second) // inserted?
|
50 | 43 | {
|
51 |
| - data.push_back(a); |
52 |
| - INVARIANT(data.size()==numbers.size(), "vector sizes must match"); |
| 44 | + data_.emplace_back(a); |
| 45 | + INVARIANT(data_.size() == numbers_.size(), "vector sizes must match"); |
53 | 46 | }
|
54 | 47 |
|
55 | 48 | return (result.first)->second;
|
56 | 49 | }
|
57 | 50 |
|
58 |
| - number_type operator()(const T &a) |
| 51 | + optionalt<number_type> get_number(const key_type &a) const |
59 | 52 | {
|
60 |
| - return number(a); |
| 53 | + const auto it = numbers_.find(a); |
| 54 | + if(it == numbers_.end()) |
| 55 | + { |
| 56 | + return {}; |
| 57 | + } |
| 58 | + return it->second; |
61 | 59 | }
|
62 | 60 |
|
63 |
| - bool get_number(const T &a, number_type &n) const |
| 61 | + void clear() |
64 | 62 | {
|
65 |
| - typename numberst::const_iterator it=numbers.find(a); |
66 |
| - |
67 |
| - if(it==numbers.end()) |
68 |
| - return true; |
69 |
| - |
70 |
| - n=it->second; |
71 |
| - return false; |
| 63 | + data_.clear(); |
| 64 | + numbers_.clear(); |
72 | 65 | }
|
73 | 66 |
|
74 |
| - void clear() |
| 67 | + size_type size() const |
75 | 68 | {
|
76 |
| - data.clear(); |
77 |
| - numbers.clear(); |
| 69 | + return data_.size(); |
78 | 70 | }
|
79 | 71 |
|
80 |
| - size_t size() const { return data.size(); } |
81 |
| - |
82 |
| - T &operator[](size_type t) { return data[t]; } |
83 |
| - const T &operator[](size_type t) const { return data[t]; } |
84 |
| - |
85 |
| - iterator begin() { return data.begin(); } |
86 |
| - const_iterator begin() const { return data.begin(); } |
87 |
| - const_iterator cbegin() const { return data.cbegin(); } |
88 |
| - |
89 |
| - iterator end() { return data.end(); } |
90 |
| - const_iterator end() const { return data.end(); } |
91 |
| - const_iterator cend() const { return data.cend(); } |
92 |
| -}; |
93 |
| - |
94 |
| -template <typename T, class hash_fkt> |
95 |
| -// NOLINTNEXTLINE(readability/identifiers) |
96 |
| -class hash_numbering final |
97 |
| -{ |
98 |
| -public: |
99 |
| - // NOLINTNEXTLINE(readability/identifiers) |
100 |
| - typedef unsigned int number_type; |
101 |
| - |
102 |
| -private: |
103 |
| - typedef std::vector<T> data_typet; |
104 |
| - data_typet data; |
105 |
| - typedef std::unordered_map<T, number_type, hash_fkt> numberst; |
106 |
| - numberst numbers; |
107 |
| - |
108 |
| -public: |
109 |
| - // NOLINTNEXTLINE(readability/identifiers) |
110 |
| - typedef typename data_typet::size_type size_type; |
111 |
| - // NOLINTNEXTLINE(readability/identifiers) |
112 |
| - typedef typename data_typet::iterator iterator; |
113 |
| - // NOLINTNEXTLINE(readability/identifiers) |
114 |
| - typedef typename data_typet::const_iterator const_iterator; |
115 |
| - |
116 |
| - number_type number(const T &a) |
| 72 | + key_type &operator[](size_type t) |
117 | 73 | {
|
118 |
| - std::pair<typename numberst::const_iterator, bool> result= |
119 |
| - numbers.insert( |
120 |
| - std::pair<T, number_type> |
121 |
| - (a, number_type(numbers.size()))); |
122 |
| - |
123 |
| - if(result.second) // inserted? |
124 |
| - { |
125 |
| - this->push_back(a); |
126 |
| - assert(this->size()==numbers.size()); |
127 |
| - } |
128 |
| - |
129 |
| - return (result.first)->second; |
| 74 | + return data_[t]; |
130 | 75 | }
|
131 |
| - |
132 |
| - bool get_number(const T &a, number_type &n) const |
| 76 | + const key_type &operator[](size_type t) const |
133 | 77 | {
|
134 |
| - typename numberst::const_iterator it=numbers.find(a); |
135 |
| - |
136 |
| - if(it==numbers.end()) |
137 |
| - return true; |
138 |
| - |
139 |
| - n=it->second; |
140 |
| - return false; |
| 78 | + return data_[t]; |
141 | 79 | }
|
142 | 80 |
|
143 |
| - void clear() |
| 81 | + iterator begin() |
144 | 82 | {
|
145 |
| - data.clear(); |
146 |
| - numbers.clear(); |
| 83 | + return data_.begin(); |
| 84 | + } |
| 85 | + const_iterator begin() const |
| 86 | + { |
| 87 | + return data_.begin(); |
| 88 | + } |
| 89 | + const_iterator cbegin() const |
| 90 | + { |
| 91 | + return data_.cbegin(); |
147 | 92 | }
|
148 | 93 |
|
149 |
| - template <typename U> |
150 |
| - void push_back(U &&u) { data.push_back(std::forward<U>(u)); } |
151 |
| - |
152 |
| - T &operator[](size_type t) { return data[t]; } |
153 |
| - const T &operator[](size_type t) const { return data[t]; } |
154 |
| - |
155 |
| - T &at(size_type t) { return data.at(t); } |
156 |
| - const T &at(size_type t) const { return data.at(t); } |
157 |
| - |
158 |
| - size_type size() const { return data.size(); } |
| 94 | + iterator end() |
| 95 | + { |
| 96 | + return data_.end(); |
| 97 | + } |
| 98 | + const_iterator end() const |
| 99 | + { |
| 100 | + return data_.end(); |
| 101 | + } |
| 102 | + const_iterator cend() const |
| 103 | + { |
| 104 | + return data_.cend(); |
| 105 | + } |
| 106 | +}; |
159 | 107 |
|
160 |
| - iterator begin() { return data.begin(); } |
161 |
| - const_iterator begin() const { return data.begin(); } |
162 |
| - const_iterator cbegin() const { return data.cbegin(); } |
| 108 | +template <typename Key> |
| 109 | +using numbering = template_numberingt<std::map<Key, std::size_t>>; // NOLINT |
163 | 110 |
|
164 |
| - iterator end() { return data.end(); } |
165 |
| - const_iterator end() const { return data.end(); } |
166 |
| - const_iterator cend() const { return data.cend(); } |
167 |
| -}; |
| 111 | +template <typename Key, typename Hash> |
| 112 | +using hash_numbering = // NOLINT |
| 113 | + template_numberingt<std::unordered_map<Key, std::size_t, Hash>>; |
168 | 114 |
|
169 | 115 | #endif // CPROVER_UTIL_NUMBERING_H
|
0 commit comments