CppCommon  1.0.4.1
C++ Common Library
flatmap.inl
Go to the documentation of this file.
1 
9 namespace CppCommon {
10 
11 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
12 inline FlatMap<TKey, TValue, TCompare, TAllocator>::FlatMap(size_t capacity, const TCompare& compare, const TAllocator& allocator)
13  : _compare(compare), _container(allocator)
14 {
16 }
17 
18 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
19 template <class InputIterator>
20 inline FlatMap<TKey, TValue, TCompare, TAllocator>::FlatMap(InputIterator first, InputIterator last, bool unused, size_t capacity, const TCompare& compare, const TAllocator& allocator)
21  : FlatMap(capacity, compare, allocator)
22 {
23  insert(first, last);
24 }
25 
26 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
28  : FlatMap(flatmap.capacity(), flatmap._compare, flatmap._container.get_allocator())
29 {
30  for (const auto& item : flatmap)
31  insert(item);
32 }
33 
34 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
35 inline FlatMap<TKey, TValue, TCompare, TAllocator>::FlatMap(const FlatMap& flatmap, size_t capacity)
36  : FlatMap(capacity, flatmap._compare, flatmap._container.get_allocator())
37 {
38  for (const auto& item : flatmap)
39  insert(item);
40 }
41 
42 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
44 {
45  clear();
46  reserve(flatmap.size());
47  for (const auto& item : flatmap)
48  insert(item);
49  return *this;
50 }
51 
52 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
54 {
55  iterator it = lower_bound(key);
56  if ((it != end()) && compare(key, it->first))
57  return end();
58  return it;
59 }
60 
61 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
63 {
64  const_iterator it = lower_bound(key);
65  if ((it != end()) && compare(key, it->first))
66  return end();
67  return it;
68 }
69 
70 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
72 {
73  return std::lower_bound(begin(), end(), key, [this](auto key1, auto key2) { return this->compare(key1, key2); });
74 }
75 
76 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
78 {
79  return std::lower_bound(begin(), end(), key, [this](auto key1, auto key2) { return this->compare(key1, key2); });
80 }
81 
82 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
84 {
85  return std::upper_bound(begin(), end(), key, [this](auto key1, auto key2) { return this->compare(key1, key2); });
86 }
87 
88 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
90 {
91  return std::upper_bound(begin(), end(), key, [this](auto key1, auto key2) { return this->compare(key1, key2); });
92 }
93 
94 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
95 inline std::pair<typename FlatMap<TKey, TValue, TCompare, TAllocator>::iterator, typename FlatMap<TKey, TValue, TCompare, TAllocator>::iterator> FlatMap<TKey, TValue, TCompare, TAllocator>::equal_range(const TKey& key) noexcept
96 {
97  return std::equal_range(begin(), end(), key, [this](auto key1, auto key2) { return this->compare(key1, key2); });
98 }
99 
100 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
101 inline std::pair<typename FlatMap<TKey, TValue, TCompare, TAllocator>::const_iterator, typename FlatMap<TKey, TValue, TCompare, TAllocator>::const_iterator> FlatMap<TKey, TValue, TCompare, TAllocator>::equal_range(const TKey& key) const noexcept
102 {
103  return std::equal_range(begin(), end(), key, [this](auto key1, auto key2) { return this->compare(key1, key2); });
104 }
105 
106 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
108 {
109  auto it = find(key);
110  if (it == end())
111  throw std::out_of_range("Item with the given key was not found in the flat map!");
112 
113  return it->second;
114 }
115 
116 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
118 {
119  auto it = find(key);
120  if (it == end())
121  throw std::out_of_range("Item with the given key was not found in the flat map!");
122 
123  return it->second;
124 }
125 
126 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
127 inline std::pair<typename FlatMap<TKey, TValue, TCompare, TAllocator>::iterator, bool> FlatMap<TKey, TValue, TCompare, TAllocator>::insert(const value_type& item)
128 {
129  return emplace_internal(item.first, item.second);
130 }
131 
132 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
133 inline std::pair<typename FlatMap<TKey, TValue, TCompare, TAllocator>::iterator, bool> FlatMap<TKey, TValue, TCompare, TAllocator>::insert(value_type&& item)
134 {
135  return emplace_internal(item.first, std::move(item.second));
136 }
137 
138 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
140 {
141  return emplace_hint_internal(position, item.first, item.second);
142 }
143 
144 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
146 {
147  return emplace_hint_internal(position, item.first, std::move(item.second));
148 }
149 
150 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
151 template <class InputIterator>
152 inline void FlatMap<TKey, TValue, TCompare, TAllocator>::insert(InputIterator first, InputIterator last)
153 {
154  for (auto it = first; it != last; ++it)
155  insert(*it);
156 }
157 
158 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
159 template <typename... Args>
160 inline std::pair<typename FlatMap<TKey, TValue, TCompare, TAllocator>::iterator, bool> FlatMap<TKey, TValue, TCompare, TAllocator>::emplace(Args&&... args)
161 {
162  return emplace_internal(std::forward<Args>(args)...);
163 }
164 
165 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
166 template <typename... Args>
168 {
169  return emplace_hint_internal(position, std::forward<Args>(args)...);
170 }
171 
172 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
174 {
175  auto it = find(key);
176  if (it == end())
177  return 0;
178 
179  _container.erase(it);
180  return 1;
181 }
182 
183 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
185 {
186  iterator result(position);
187  ++result;
188  _container.erase(position);
189  return result;
190 }
191 
192 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
194 {
195  iterator result(last);
196  _container.erase(first, last);
197  return result;
198 }
199 
200 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
201 template <typename... Args>
202 inline std::pair<typename FlatMap<TKey, TValue, TCompare, TAllocator>::iterator, bool> FlatMap<TKey, TValue, TCompare, TAllocator>::emplace_internal(const TKey& key, Args&&... args)
203 {
204  bool found = true;
205  iterator it = lower_bound(key);
206  if ((it == end()) || compare(key, it->first))
207  {
208  it = _container.emplace(it, std::make_pair(key, TValue(std::forward<Args>(args)...)));
209  found = false;
210  }
211  return std::make_pair(it, !found);
212 }
213 
214 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
215 template <typename... Args>
216 inline typename FlatMap<TKey, TValue, TCompare, TAllocator>::iterator FlatMap<TKey, TValue, TCompare, TAllocator>::emplace_hint_internal(const const_iterator& position, const TKey& key, Args&&... args)
217 {
218  if (((position == begin()) || compare((position - 1)->first, key)) && ((position == end()) || compare(key, position.first)))
219  return _container.emplace(position, std::make_pair(key, TValue(std::forward<Args>(args)...)));
220  return emplace_internal(key, std::forward<Args>(args)...).first;
221 }
222 
223 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
225 {
226  using std::swap;
227  swap(_compare, flatmap._compare);
228  swap(_container, flatmap._container);
229 }
230 
231 template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
233 {
234  flatmap1.swap(flatmap2);
235 }
236 
237 } // namespace CppCommon
Flat map container.
Definition: flatmap.h:33
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
void swap(FlatMap &flatmap) noexcept
Swap two instances.
Definition: flatmap.inl:224
std::pair< TKey, TValue > value_type
Definition: flatmap.h:38
std::vector< value_type, TAllocator >::const_iterator const_iterator
Definition: flatmap.h:46
std::pair< iterator, bool > insert(const value_type &item)
Insert a new item into the flat map.
Definition: flatmap.inl:127
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
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
TValue mapped_type
Definition: flatmap.h:37
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
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::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
FlatMap & operator=(const FlatMap &flatmap)
Definition: flatmap.inl:43
size_t erase(const TKey &key)
Erase the item with the given key from the flat map.
Definition: flatmap.inl:173
std::pair< iterator, bool > emplace(Args &&... args)
Emplace a new item into the flat map.
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
C++ Common project definitions.
Definition: token_bucket.h:15
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition: filecache.inl:23
void swap(FlatMap< TKey, TValue, TCompare, TAllocator > &flatmap1, FlatMap< TKey, TValue, TCompare, TAllocator > &flatmap2) noexcept
Definition: flatmap.inl:232