66
77\*******************************************************************/
88
9-
109#ifndef CPROVER_UTIL_NUMBERING_H
1110#define CPROVER_UTIL_NUMBERING_H
1211
1817#include < util/invariant.h>
1918#include < util/optional.h>
2019
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
2423{
2524public:
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
2727
2828private:
29- using data_typet = std::vector<T >; // NOLINT
29+ using data_typet = std::vector<key_type >; // NOLINT
3030 data_typet data_;
31- using numberst = std::map<T, number_type>; // NOLINT
32- numberst numbers_;
31+ Map numbers_;
3332
3433public:
3534 using size_type = typename data_typet::size_type; // NOLINT
3635 using iterator = typename data_typet::iterator; // NOLINT
3736 using const_iterator = typename data_typet::const_iterator; // NOLINT
3837
39- number_type number (const T &a)
38+ number_type number (const key_type &a)
4039 {
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 ()));
4341
4442 if (result.second ) // inserted?
4543 {
46- data_.push_back (a);
44+ data_.emplace_back (a);
4745 INVARIANT (data_.size () == numbers_.size (), " vector sizes must match" );
4846 }
4947
5048 return (result.first )->second ;
5149 }
5250
53- optionalt<number_type> get_number (const T &a) const
51+ optionalt<number_type> get_number (const key_type &a) const
5452 {
5553 const auto it = numbers_.find (a);
5654 if (it == numbers_.end ())
@@ -66,16 +64,16 @@ class numbering final
6664 numbers_.clear ();
6765 }
6866
69- size_t size () const
67+ size_type size () const
7068 {
7169 return data_.size ();
7270 }
7371
74- T &operator [](size_type t)
72+ key_type &operator [](size_type t)
7573 {
7674 return data_[t];
7775 }
78- const T &operator [](size_type t) const
76+ const key_type &operator [](size_type t) const
7977 {
8078 return data_[t];
8179 }
@@ -107,109 +105,11 @@ class numbering final
107105 }
108106};
109107
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
145110
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>>;
214114
215115#endif // CPROVER_UTIL_NUMBERING_H
0 commit comments