CppCommon  1.0.4.1
C++ Common Library
flatmap.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_CONTAINERS_FLATMAP_H
10 #define CPPCOMMON_CONTAINERS_FLATMAP_H
11 
12 #include <algorithm>
13 #include <cstddef>
14 #include <functional>
15 #include <iterator>
16 #include <limits>
17 #include <stdexcept>
18 #include <vector>
19 
20 namespace CppCommon {
21 
23 
31 template <typename TKey, typename TValue, typename TCompare = std::less<TKey>, typename TAllocator = std::allocator<std::pair<TKey, TValue>>>
32 class FlatMap
33 {
34 public:
35  // Standard container type definitions
36  typedef TKey key_type;
37  typedef TValue mapped_type;
38  typedef std::pair<TKey, TValue> value_type;
40  typedef const value_type& const_reference;
41  typedef value_type* pointer;
42  typedef const value_type* const_pointer;
43  typedef ptrdiff_t difference_type;
44  typedef size_t size_type;
45  typedef typename std::vector<value_type, TAllocator>::iterator iterator;
46  typedef typename std::vector<value_type, TAllocator>::const_iterator const_iterator;
47  typedef typename std::vector<value_type, TAllocator>::reverse_iterator reverse_iterator;
48  typedef typename std::vector<value_type, TAllocator>::const_reverse_iterator const_reverse_iterator;
49 
51 
56  explicit FlatMap(size_t capacity = 128, const TCompare& compare = TCompare(), const TAllocator& allocator = TAllocator());
57  template <class InputIterator>
58  FlatMap(InputIterator first, InputIterator last, bool unused, size_t capacity = 128, const TCompare& compare = TCompare(), const TAllocator& allocator = TAllocator());
59  FlatMap(const FlatMap& flatmap);
60  FlatMap(const FlatMap& flatmap, size_t capacity);
61  FlatMap(FlatMap&&) noexcept = default;
62  ~FlatMap() = default;
63 
64  FlatMap& operator=(const FlatMap& flatmap);
65  FlatMap& operator=(FlatMap&&) noexcept = default;
66 
68  explicit operator bool() const noexcept { return !empty(); }
69 
71  mapped_type& operator[](const TKey& key) { return emplace_internal(key).first->second; }
72 
74  bool empty() const noexcept { return _container.empty(); }
75 
77  size_t capacity() const noexcept { return _container.capacity(); }
79  size_t size() const noexcept { return _container.size(); }
81  size_t max_size() const noexcept { return _container.max_size(); }
82 
84  bool compare(const TKey& key1, const TKey& key2) const noexcept { return _compare(key1, key2); }
85  bool compare(const TKey& key1, const value_type& key2) const noexcept { return _compare(key1, key2.first); }
86  bool compare(const value_type& key1, const TKey& key2) const noexcept { return _compare(key1.first, key2); }
87  bool compare(const value_type& key1, const value_type& key2) const noexcept { return _compare(key1.first, key2.first); }
88 
90  iterator begin() noexcept { return _container.begin(); }
91  const_iterator begin() const noexcept { return _container.begin(); }
92  const_iterator cbegin() const noexcept { return _container.begin(); }
94  iterator end() noexcept { return _container.end(); }
95  const_iterator end() const noexcept { return _container.end(); }
96  const_iterator cend() const noexcept { return _container.end(); }
97 
99  reverse_iterator rbegin() noexcept { return _container.rbegin(); }
100  const_reverse_iterator rbegin() const noexcept { return _container.rbegin(); }
101  const_reverse_iterator crbegin() const noexcept { return _container.rbegin(); }
103  reverse_iterator rend() noexcept { return _container.rend(); }
104  const_reverse_iterator rend() const noexcept { return _container.rend(); }
105  const_reverse_iterator crend() const noexcept { return _container.rend(); }
106 
108  iterator find(const TKey& key) noexcept;
109  const_iterator find(const TKey& key) const noexcept;
110 
112  iterator lower_bound(const TKey& key) noexcept;
113  const_iterator lower_bound(const TKey& key) const noexcept;
115  iterator upper_bound(const TKey& key) noexcept;
116  const_iterator upper_bound(const TKey& key) const noexcept;
117 
119  std::pair<iterator, iterator> equal_range(const TKey& key) noexcept;
120  std::pair<const_iterator, const_iterator> equal_range(const TKey& key) const noexcept;
121 
123  size_t count(const TKey& key) const noexcept { return (find(key) == end()) ? 0 : 1; }
124 
126 
130  mapped_type& at(const TKey& key) noexcept;
132 
136  const mapped_type& at(const TKey& key) const noexcept;
137 
139 
143  std::pair<iterator, bool> insert(const value_type& item);
145 
149  std::pair<iterator, bool> insert(value_type&& item);
151 
156  iterator insert(const const_iterator& position, const value_type& item);
158 
163  iterator insert(const const_iterator& position, value_type&& item);
165 
169  template <class InputIterator>
170  void insert(InputIterator first, InputIterator last);
171 
173 
177  template <typename... Args>
178  std::pair<iterator, bool> emplace(Args&&... args);
180 
185  template <typename... Args>
186  iterator emplace_hint(const const_iterator& position, Args&&... args);
187 
189 
193  size_t erase(const TKey& key);
195 
199  iterator erase(const const_iterator& position);
201 
206  iterator erase(const const_iterator& first, const const_iterator& last);
207 
209 
212  void reserve(size_t count) { _container.reserve(count); }
214 
217  void shrink_to_fit() { _container.shrink_to_fit(); }
218 
220  void clear() noexcept { _container.clear(); }
221 
223  void swap(FlatMap& flatmap) noexcept;
224  template <typename UKey, typename UValue, typename UCompare, typename UAllocator>
226 
227 private:
228  TCompare _compare; // Flat map key comparator
229  std::vector<value_type, TAllocator> _container; // Flat map container
230 
231  template <typename... Args>
232  std::pair<iterator, bool> emplace_internal(const TKey& key, Args&&... args);
233  template <typename... Args>
234  iterator emplace_hint_internal(const const_iterator& position, const TKey& key, Args&&... args);
235 };
236 
239 } // namespace CppCommon
240 
241 #include "flatmap.inl"
242 
243 #endif // CPPCOMMON_CONTAINERS_FLATMAP_H
Flat map container.
Definition: flatmap.h:33
mapped_type & operator[](const TKey &key)
Access to the item with the given key or insert a new one.
Definition: flatmap.h:71
reverse_iterator rend() noexcept
Get the reverse end flat map iterator.
Definition: flatmap.h:103
iterator begin() noexcept
Get the begin flat map iterator.
Definition: flatmap.h:90
FlatMap(size_t capacity=128, const TCompare &compare=TCompare(), const TAllocator &allocator=TAllocator())
Initialize the flat map with a given capacity.
Definition: flatmap.inl:12
const value_type & const_reference
Definition: flatmap.h:40
void swap(FlatMap &flatmap) noexcept
Swap two instances.
Definition: flatmap.inl:224
size_t size_type
Definition: flatmap.h:44
std::pair< TKey, TValue > value_type
Definition: flatmap.h:38
void clear() noexcept
Clear the flat map.
Definition: flatmap.h:220
const_reverse_iterator rbegin() const noexcept
Definition: flatmap.h:100
friend void swap(FlatMap< UKey, UValue, UCompare, UAllocator > &flatmap1, FlatMap< UKey, UValue, UCompare, UAllocator > &flatmap2) noexcept
std::vector< value_type, TAllocator >::const_iterator const_iterator
Definition: flatmap.h:46
const_iterator cbegin() const noexcept
Definition: flatmap.h:92
const_iterator end() const noexcept
Definition: flatmap.h:95
std::pair< iterator, bool > insert(const value_type &item)
Insert a new item into the flat map.
Definition: flatmap.inl:127
bool empty() const noexcept
Is the flat map empty?
Definition: flatmap.h:74
void reserve(size_t count)
Reserve the flat map capacity to fit the given count of items.
Definition: flatmap.h:212
std::vector< value_type, TAllocator >::iterator iterator
Definition: flatmap.h:45
value_type * pointer
Definition: flatmap.h:41
size_t max_size() const noexcept
Get the flat map maximum size.
Definition: flatmap.h:81
const_reverse_iterator rend() const noexcept
Definition: flatmap.h:104
const_reverse_iterator crend() const noexcept
Definition: flatmap.h:105
iterator find(const TKey &key) noexcept
Find the iterator which points to the first item with the given key in the flat map or return end ite...
Definition: flatmap.inl:53
bool compare(const value_type &key1, const value_type &key2) const noexcept
Definition: flatmap.h:87
TValue mapped_type
Definition: flatmap.h:37
const_iterator cend() const noexcept
Definition: flatmap.h:96
const_iterator begin() const noexcept
Definition: flatmap.h:91
void shrink_to_fit()
Requests the flat map to reduce its capacity to fit its size.
Definition: flatmap.h:217
iterator emplace_hint(const const_iterator &position, Args &&... args)
Emplace a new item into the flat map with a position hint.
Definition: flatmap.inl:167
ptrdiff_t difference_type
Definition: flatmap.h:43
bool compare(const value_type &key1, const TKey &key2) const noexcept
Definition: flatmap.h:86
mapped_type & at(const TKey &key) noexcept
Access to the item with the given key or throw std::out_of_range exception.
Definition: flatmap.inl:107
size_t capacity() const noexcept
Get the flat map capacity.
Definition: flatmap.h:77
std::vector< value_type, TAllocator >::const_reverse_iterator const_reverse_iterator
Definition: flatmap.h:48
std::pair< iterator, iterator > equal_range(const TKey &key) noexcept
Find the bounds of a range that includes all the elements in the hash map with the given key.
Definition: flatmap.inl:95
size_t count(const TKey &key) const noexcept
Find the count of items with the given key.
Definition: flatmap.h:123
bool compare(const TKey &key1, const TKey &key2) const noexcept
Compare two items: if the first key is less than the second one?
Definition: flatmap.h:84
value_type & reference
Definition: flatmap.h:39
size_t erase(const TKey &key)
Erase the item with the given key from the flat map.
Definition: flatmap.inl:173
const value_type * const_pointer
Definition: flatmap.h:42
std::pair< iterator, bool > emplace(Args &&... args)
Emplace a new item into the flat map.
bool compare(const TKey &key1, const value_type &key2) const noexcept
Definition: flatmap.h:85
iterator end() noexcept
Get the end flat map iterator.
Definition: flatmap.h:94
iterator upper_bound(const TKey &key) noexcept
Find the iterator which points to the first item with the given key that greater than the given key i...
Definition: flatmap.inl:83
size_t size() const noexcept
Get the flat map size.
Definition: flatmap.h:79
iterator lower_bound(const TKey &key) noexcept
Find the iterator which points to the first item with the given key that not less than the given key ...
Definition: flatmap.inl:71
std::vector< value_type, TAllocator >::reverse_iterator reverse_iterator
Definition: flatmap.h:47
const_reverse_iterator crbegin() const noexcept
Definition: flatmap.h:101
FlatMap(FlatMap &&) noexcept=default
reverse_iterator rbegin() noexcept
Get the reverse begin flat map iterator.
Definition: flatmap.h:99
Flat map container inline implementation.
C++ Common project definitions.
Definition: token_bucket.h:15