6
6
7
7
\*******************************************************************/
8
8
9
-
10
9
#ifndef CPROVER_UTIL_NUMBERING_H
11
10
#define CPROVER_UTIL_NUMBERING_H
12
11
18
17
#include < util/invariant.h>
19
18
#include < util/optional.h>
20
19
21
- template < typename T>
22
- // NOLINTNEXTLINE(readability/identifiers)
23
- 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
24
23
{
25
24
public:
26
- using number_type = std::size_t ; // NOLINT
25
+ using number_type = typename Map::mapped_type; // NOLINT
26
+ using key_type = typename Map::key_type; // NOLINT
27
27
28
28
private:
29
- using data_typet = std::vector<T >; // NOLINT
29
+ using data_typet = std::vector<key_type >; // NOLINT
30
30
data_typet data_;
31
- using numberst = std::map<T, number_type>; // NOLINT
32
- numberst numbers_;
31
+ Map numbers_;
33
32
34
33
public:
35
34
using size_type = typename data_typet::size_type; // NOLINT
36
35
using iterator = typename data_typet::iterator; // NOLINT
37
36
using const_iterator = typename data_typet::const_iterator; // NOLINT
38
37
39
- number_type number (const T &a)
38
+ number_type number (const key_type &a)
40
39
{
41
- std::pair<typename numberst::const_iterator, bool > result = numbers_.insert (
42
- std::pair<T, number_type>(a, number_type (numbers_.size ())));
40
+ const auto result = numbers_.emplace (a, number_type (numbers_.size ()));
43
41
44
42
if (result.second ) // inserted?
45
43
{
46
- data_.push_back (a);
44
+ data_.emplace_back (a);
47
45
INVARIANT (data_.size () == numbers_.size (), " vector sizes must match" );
48
46
}
49
47
50
48
return (result.first )->second ;
51
49
}
52
50
53
- optionalt<number_type> get_number (const T &a) const
51
+ optionalt<number_type> get_number (const key_type &a) const
54
52
{
55
53
const auto it = numbers_.find (a);
56
54
if (it == numbers_.end ())
@@ -66,16 +64,16 @@ class numbering final
66
64
numbers_.clear ();
67
65
}
68
66
69
- size_t size () const
67
+ size_type size () const
70
68
{
71
69
return data_.size ();
72
70
}
73
71
74
- T &operator [](size_type t)
72
+ key_type &operator [](size_type t)
75
73
{
76
74
return data_[t];
77
75
}
78
- const T &operator [](size_type t) const
76
+ const key_type &operator [](size_type t) const
79
77
{
80
78
return data_[t];
81
79
}
@@ -107,109 +105,11 @@ class numbering final
107
105
}
108
106
};
109
107
110
- template <typename T, class hash_fkt >
111
- // NOLINTNEXTLINE(readability/identifiers)
112
- class hash_numbering final
113
- {
114
- public:
115
- using number_type = unsigned int ; // NOLINT
116
-
117
- private:
118
- using data_typet = std::vector<T>; // NOLINT
119
- data_typet data_;
120
- using numberst = std::unordered_map<T, number_type, hash_fkt>; // NOLINT
121
- numberst numbers_;
122
-
123
- public:
124
- using size_type = typename data_typet::size_type; // NOLINT
125
- using iterator = typename data_typet::iterator; // NOLINT
126
- using const_iterator = typename data_typet::const_iterator; // NOLINT
127
-
128
- number_type number (const T &a)
129
- {
130
- std::pair<typename numberst::const_iterator, bool > result = numbers_.insert (
131
- std::pair<T, number_type>(a, number_type (numbers_.size ())));
132
-
133
- if (result.second ) // inserted?
134
- {
135
- this ->push_back (a);
136
- assert (this ->size () == numbers_.size ());
137
- }
138
-
139
- return (result.first )->second ;
140
- }
141
-
142
- optionalt<number_type> get_number (const T &a) const
143
- {
144
- const auto it = numbers_.find (a);
108
+ template <typename Key>
109
+ using numbering = template_numberingt<std::map<Key, std::size_t >>; // NOLINT
145
110
146
- if (it == numbers_.end ())
147
- {
148
- return {};
149
- }
150
- return it->second ;
151
- }
152
-
153
- void clear ()
154
- {
155
- data_.clear ();
156
- numbers_.clear ();
157
- }
158
-
159
- template <typename U>
160
- void push_back (U &&u)
161
- {
162
- data_.push_back (std::forward<U>(u));
163
- }
164
-
165
- T &operator [](size_type t)
166
- {
167
- return data_[t];
168
- }
169
- const T &operator [](size_type t) const
170
- {
171
- return data_[t];
172
- }
173
-
174
- T &at (size_type t)
175
- {
176
- return data_.at (t);
177
- }
178
- const T &at (size_type t) const
179
- {
180
- return data_.at (t);
181
- }
182
-
183
- size_type size () const
184
- {
185
- return data_.size ();
186
- }
187
-
188
- iterator begin ()
189
- {
190
- return data_.begin ();
191
- }
192
- const_iterator begin () const
193
- {
194
- return data_.begin ();
195
- }
196
- const_iterator cbegin () const
197
- {
198
- return data_.cbegin ();
199
- }
200
-
201
- iterator end ()
202
- {
203
- return data_.end ();
204
- }
205
- const_iterator end () const
206
- {
207
- return data_.end ();
208
- }
209
- const_iterator cend () const
210
- {
211
- return data_.cend ();
212
- }
213
- };
111
+ template <typename Key, typename Hash>
112
+ using hash_numbering = // NOLINT
113
+ template_numberingt<std::unordered_map<Key, std::size_t , Hash>>;
214
114
215
115
#endif // CPROVER_UTIL_NUMBERING_H
0 commit comments