CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
flatmap.inl
Go to the documentation of this file.
1
9namespace CppCommon {
10
11template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
12inline FlatMap<TKey, TValue, TCompare, TAllocator>::FlatMap(size_t capacity, const TCompare& compare, const TAllocator& allocator)
13 : _compare(compare), _container(allocator)
14{
16}
17
18template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
19template <class InputIterator>
20inline 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
26template <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
34template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
36 : FlatMap(capacity, flatmap._compare, flatmap._container.get_allocator())
37{
38 for (const auto& item : flatmap)
39 insert(item);
40}
41
42template <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
52template <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
61template <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
70template <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
76template <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
82template <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
88template <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
94template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
95inline 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
100template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
101inline 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
106template <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
116template <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
126template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
127inline 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
132template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
133inline 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
138template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
140{
141 return emplace_hint_internal(position, item.first, item.second);
142}
143
144template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
146{
147 return emplace_hint_internal(position, item.first, std::move(item.second));
148}
149
150template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
151template <class InputIterator>
152inline 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
158template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
159template <typename... Args>
160inline 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
165template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
166template <typename... Args>
168{
169 return emplace_hint_internal(position, std::forward<Args>(args)...);
170}
171
172template <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
183template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
185{
186 iterator result(position);
187 ++result;
188 _container.erase(position);
189 return result;
190}
191
192template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
194{
195 iterator result(last);
196 _container.erase(first, last);
197 return result;
198}
199
200template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
201template <typename... Args>
202inline 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
214template <typename TKey, typename TValue, typename TCompare, typename TAllocator>
215template <typename... Args>
216inline 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
223template <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
231template <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
std::pair< TKey, TValue > value_type
Definition flatmap.h:38
friend void swap(FlatMap< UKey, UValue, UCompare, UAllocator > &flatmap1, FlatMap< UKey, UValue, UCompare, UAllocator > &flatmap2) noexcept
std::vector< value_type, TAllocator >::iterator iterator
Definition flatmap.h:45
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 >::const_iterator const_iterator
Definition flatmap.h:46
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
std::pair< iterator, bool > emplace(Args &&... args)
Emplace a new item into the flat map.
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
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.
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition filecache.inl:23