libhatchet
Loading...
Searching...
No Matches
hxflat_map.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
9
10#include "libhatchet.h"
11
12// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
13#if HX_USE_MACROS_WITH_MODULE
14#error Header does not provide macros alone.
15#endif
16
17#include "hxallocator.hpp"
18#include "hxkey.hpp"
19#include "hxsort.hpp"
20
21HX_NS_BEGIN_
22
50template<typename key_t_,
51 typename mapped_t_,
52 typename compare_t_=hxkey_less_t<key_t_>,
53 bool multi_t_=true,
56public:
57 using key_t = key_t_;
58 using mapped_t = mapped_t_;
59 using compare_t = compare_t_;
60
66 public:
68 const_iterator(void) : m_map_(hxnull), m_index_(0) { }
69
73 const const_iterator& operator*(void) const { return *this; }
74
78
82
85 const_iterator operator+(hxsize_t n_) const { const_iterator t_(*this); t_ += n_; return t_; }
86
89 const_iterator operator-(hxsize_t n_) const { const_iterator t_(*this); t_ -= n_; return t_; }
90
94
97 const_iterator operator[](hxsize_t n_) const { return operator+(n_); }
98
101
103 const_iterator operator++(int) { const_iterator t_(*this); operator++(); return t_; }
104
106 const_iterator& operator--(void) { return operator+=(-1); }
107
109 const_iterator operator--(int) { const_iterator t_(*this); operator--(); return t_; }
110
113 bool operator==(const const_iterator& x_) const;
114
117 bool operator<(const const_iterator& x_) const;
118
121 bool operator>(const const_iterator& x_) const { return x_ < *this; }
122
125 bool operator<=(const const_iterator& x_) const { return !(x_ < *this); }
126
129 bool operator>=(const const_iterator& x_) const { return !(*this < x_); }
130
131#if HX_CPLUSPLUS < 202002L
134 bool operator!=(const const_iterator& x_) const;
135#endif
136
139 const key_t_& key(void) const;
140
143 const mapped_t_& value(void) const;
144
145 protected:
147 friend class hxflat_map;
148 const_iterator(const hxflat_map* map_, hxsize_t index_)
149 : m_map_(map_), m_index_(index_) { }
150 const hxflat_map* m_map_;
151 hxsize_t m_index_;
153 };
154
157 class iterator : public const_iterator {
158 public:
160 iterator(void) { }
161
165 const iterator& operator*(void) const { return *this; }
166
170
174
177 iterator operator+(hxsize_t n_) const { iterator t_(*this); t_ += n_; return t_; }
178
181 iterator operator-(hxsize_t n_) const { iterator t_(*this); t_ -= n_; return t_; }
182
186
189 iterator operator[](hxsize_t n_) const { return operator+(n_); }
190
193
195 iterator operator++(int) { iterator t_(*this); const_iterator::operator++(); return t_; }
196
199
201 iterator operator--(int) { iterator t_(*this); const_iterator::operator--(); return t_; }
202
205 mapped_t_& value(void) const;
206
207 protected:
209 friend class hxflat_map;
210 iterator(hxflat_map* map_, hxsize_t index_) : const_iterator(map_, index_) { }
212 };
213
216 explicit hxflat_map(void);
217
221 hxflat_map(const hxflat_map& x_) noexcept;
222
226 hxflat_map(hxflat_map&& x_) noexcept;
227
229 ~hxflat_map(void) noexcept;
230
234 void operator=(const hxflat_map& x_) noexcept;
235
239 template<hxsize_t capacity_x_>
240 void operator=(const hxflat_map<key_t_, mapped_t_, compare_t_, multi_t_,
241 capacity_x_>& x_) noexcept;
242
246 void operator=(hxflat_map&& x_) noexcept;
247
252
257
259 const_iterator begin(void) const { return const_iterator(this, 0); }
260
262 iterator begin(void) { return iterator(this, 0); }
263
265 hxattr_nodiscard hxsize_t capacity(void) const { return m_keys_.capacity(); }
266
269 const_iterator cbegin(void) const { return const_iterator(this, 0); }
270
272 const_iterator cend(void) const { return const_iterator(this, m_size_); }
273
275 void clear(void) noexcept;
276
279 hxattr_nodiscard hxsize_t count(const key_t_& key_) const;
280
282 hxattr_nodiscard bool empty(void) const { return m_size_ == 0; }
283
285 const_iterator end(void) const { return const_iterator(this, m_size_); }
286
288 iterator end(void) { return iterator(this, m_size_); }
289
293 template<hxsize_t capacity_x_>
294 hxattr_nodiscard bool equal(const hxflat_map<key_t_, mapped_t_, compare_t_,
295 multi_t_, capacity_x_>& x_) const;
296
300 hxsize_t erase(const key_t_& key_) noexcept;
301
305 iterator erase(iterator pos_) noexcept;
306
311 hxattr_nodiscard const mapped_t_* find(const key_t_& key_) const;
312
315 hxattr_nodiscard mapped_t_* find(const key_t_& key_);
316
318 hxattr_nodiscard bool full(void) const { return m_size_ == m_keys_.capacity(); }
319
324
328
335 iterator insert(const key_t_& key_, const mapped_t_& mapped_) noexcept;
336
340 iterator insert(const key_t_& key_, mapped_t_&& mapped_) noexcept;
341
345 template<hxsize_t capacity_x_>
346 hxattr_nodiscard bool less(const hxflat_map<key_t_, mapped_t_, compare_t_,
347 multi_t_, capacity_x_>& x_) const;
348
352 hxattr_nodiscard const_iterator lower_bound(const key_t_& key_) const;
353
357
359 hxattr_nodiscard hxsize_t max_size(void) const { return this->capacity(); }
360
366 void reserve(hxsize_t cap_,
368 hxalignment_t alignment_=hxalignment) noexcept;
369
371 hxattr_nodiscard hxsize_t size(void) const { return m_size_; }
372
375 void swap(hxflat_map& x_) noexcept;
376
380 hxattr_nodiscard const_iterator upper_bound(const key_t_& key_) const;
381
385
386private:
388 template<typename, typename, typename, bool, hxsize_t> friend class hxflat_map;
389
390 template<typename mapped_u_>
391 iterator insert_at_(hxsize_t index_, const key_t_& key_, mapped_u_&& mapped_) noexcept;
392
393 hxsize_t m_size_;
397};
398
399// Without the "requires" keyword these end up being ambiguous.
400#if HX_CPLUSPLUS >= 202002L
403template<typename key_t_, typename mapped_t_, typename compare_t_, bool multi_t_,
404 hxsize_t capacity_x_, hxsize_t capacity_y_>
410
414template<typename key_t_, typename mapped_t_, typename compare_t_, bool multi_t_,
415 hxsize_t capacity_x_, hxsize_t capacity_y_>
421
425template<typename key_t_, typename mapped_t_, typename compare_t_, bool multi_t_>
431
432#endif // HX_CPLUSPLUS >= 202002L
433#include "detail/hxflat_map.inl"
434HX_NS_END_
hxallocator<1+> - Provides static allocation when capacity is greater than zero.
Definition hxallocator.hpp:25
const_iterator - A random-access iterator over key-value pairs.
Definition hxflat_map.hpp:65
const_iterator operator+(hxsize_t n_) const
Returns an iterator advanced by n positions.
Definition hxflat_map.hpp:85
const_iterator & operator--(void)
Retreats the iterator to the previous element.
Definition hxflat_map.hpp:106
bool operator==(const const_iterator &x_) const
Compares two iterators for equality.
const_iterator operator[](hxsize_t n_) const
Returns a copy of the iterator offset by n positions.
Definition hxflat_map.hpp:97
const_iterator & operator++(void)
Advances the iterator to the next element.
Definition hxflat_map.hpp:100
bool operator>=(const const_iterator &x_) const
Returns true when this iterator is not ordered before x.
Definition hxflat_map.hpp:129
const const_iterator & operator*(void) const
Returns a const reference to this iterator, allowing use as a proxy element.
Definition hxflat_map.hpp:73
bool operator>(const const_iterator &x_) const
Returns true when this iterator is ordered after x.
Definition hxflat_map.hpp:121
const_iterator & operator-=(hxsize_t n_)
Moves the iterator back by n positions.
Definition hxflat_map.hpp:81
bool operator<(const const_iterator &x_) const
Returns true when this iterator is ordered before x.
const_iterator operator--(int)
Retreats the iterator to the previous element (post-decrement).
Definition hxflat_map.hpp:109
const mapped_t_ & value(void) const
Returns the mapped value at the current position.
const_iterator operator-(hxsize_t n_) const
Returns an iterator retreated by n positions.
Definition hxflat_map.hpp:89
const_iterator operator++(int)
Advances the iterator to the next element (post-increment).
Definition hxflat_map.hpp:103
const key_t_ & key(void) const
Returns the key at the current position.
bool operator<=(const const_iterator &x_) const
Returns true when this iterator is not ordered after x.
Definition hxflat_map.hpp:125
hxsize_t operator-(const const_iterator &x_) const
Returns the signed distance between two iterators.
const_iterator & operator+=(hxsize_t n_)
Advances the iterator by n positions.
const_iterator(void)
Constructs an end iterator.
Definition hxflat_map.hpp:68
iterator - A mutable random-access iterator that allows modifying the mapped value.
Definition hxflat_map.hpp:157
iterator & operator+=(hxsize_t n_)
Advances the iterator by n positions.
Definition hxflat_map.hpp:169
iterator operator++(int)
Advances the iterator to the next element (post-increment).
Definition hxflat_map.hpp:195
mapped_t_ & value(void) const
Returns the mapped value at the current position, allowing mutation.
iterator operator+(hxsize_t n_) const
Returns an iterator advanced by n positions.
Definition hxflat_map.hpp:177
iterator(void)
Constructs an end iterator.
Definition hxflat_map.hpp:160
iterator & operator-=(hxsize_t n_)
Moves the iterator back by n positions.
Definition hxflat_map.hpp:173
iterator operator[](hxsize_t n_) const
Returns a copy of the iterator offset by n positions.
Definition hxflat_map.hpp:189
iterator & operator++(void)
Advances the iterator to the next element.
Definition hxflat_map.hpp:192
iterator & operator--(void)
Retreats the iterator to the previous element.
Definition hxflat_map.hpp:198
iterator operator--(int)
Retreats the iterator to the previous element (post-decrement).
Definition hxflat_map.hpp:201
hxsize_t operator-(const const_iterator &x_) const
Returns the signed distance from x to this position.
Definition hxflat_map.hpp:185
iterator operator-(hxsize_t n_) const
Returns an iterator retreated by n positions.
Definition hxflat_map.hpp:181
const iterator & operator*(void) const
Returns a const reference to this iterator as a proxy element.
Definition hxflat_map.hpp:165
hxflat_map - A sorted associative container that stores keys and mapped values in two parallel arrays...
Definition hxflat_map.hpp:55
iterator upper_bound(const key_t_ &key_)
Non-const version of upper_bound.
const_iterator end(void) const
Returns a const iterator pointing past the last element.
Definition hxflat_map.hpp:285
iterator begin(void)
Returns an iterator pointing to the first element.
Definition hxflat_map.hpp:262
bool full(void) const
Returns true if the map has reached its capacity.
Definition hxflat_map.hpp:318
hxflat_map(hxflat_map &&x_) noexcept
Move constructs from a temporary hxflat_map.
hxsize_t capacity(void) const
Returns the number of elements the map can hold.
Definition hxflat_map.hpp:265
const_iterator begin(void) const
Returns a const iterator pointing to the first element.
Definition hxflat_map.hpp:259
iterator insert(const key_t_ &key_, const mapped_t_ &mapped_) noexcept
Inserts a key-value pair.
void reserve(hxsize_t cap_, hxsystem_allocator_t allocator_=hxsystem_allocator_current, hxalignment_t alignment_=hxalignment) noexcept
Allocates storage for cap keys and values.
key_t_ key_t
Definition hxflat_map.hpp:57
const mapped_t_ * find(const key_t_ &key_) const
Returns a const pointer to the mapped value for key, or hxnull if the key is not found.
iterator end(void)
Returns an iterator pointing past the last element.
Definition hxflat_map.hpp:288
compare_t_ compare_t
Definition hxflat_map.hpp:59
iterator erase(iterator pos_) noexcept
Removes the element at the iterator position and destroys the key-value pair.
mapped_t_ mapped_t
Definition hxflat_map.hpp:58
hxflat_map(void)
Constructs an empty map.
void swap(hxflat_map &x_) noexcept
Swaps the contents with x.
mapped_t_ * find(const key_t_ &key_)
Non-const version of find.
iterator lower_bound(const key_t_ &key_)
Non-const version of lower_bound.
hxsize_t max_size(void) const
Returns the capacity of the map.
Definition hxflat_map.hpp:359
bool equal(const hxflat_map< key_t_, mapped_t_, compare_t_, multi_t_, capacity_x_ > &x_) const
Returns true if this map and x contain the same key-value pairs in the same order using hxkey_equal o...
hxsize_t count(const key_t_ &key_) const
Returns the number of elements with the given key.
bool empty(void) const
Checks if the map contains no elements.
Definition hxflat_map.hpp:282
void operator=(const hxflat_map< key_t_, mapped_t_, compare_t_, multi_t_, capacity_x_ > &x_) noexcept
Cross-capacity copy assignment.
const_iterator lower_bound(const key_t_ &key_) const
Returns an iterator to the first element whose key is not ordered before key.
iterator insert(const key_t_ &key_, mapped_t_ &&mapped_) noexcept
Move-value overload of insert.
const_iterator cbegin(void) const
Returns a const iterator pointing to the first element (alias for begin).
Definition hxflat_map.hpp:269
void operator=(hxflat_map &&x_) noexcept
Move assigns from a temporary map using swap.
hxsize_t erase(const key_t_ &key_) noexcept
Removes all elements with the given key and destroys their key-value pairs.
hxsize_t size(void) const
Returns the number of elements in the map.
Definition hxflat_map.hpp:371
hxflat_map(const hxflat_map &x_) noexcept
Copy constructs from another hxflat_map.
iterator get(hxsize_t index_)
Non-const version of get.
const_iterator operator[](hxsize_t index_) const
Returns a const iterator to the element at index.
~hxflat_map(void) noexcept
Destructs the map and destroys all key-value pairs.
bool less(const hxflat_map< key_t_, mapped_t_, compare_t_, multi_t_, capacity_x_ > &x_) const
Returns true if this map compares less than x lexicographically, using hxkey_equal and hxkey_less on ...
const_iterator get(hxsize_t index_) const
Returns a const iterator to the element at index, or an end iterator if index is out of range.
iterator operator[](hxsize_t index_)
Returns an iterator to the element at index.
void operator=(const hxflat_map &x_) noexcept
Assigns the contents of x to this map.
const_iterator cend(void) const
Returns a const iterator pointing past the last element.
Definition hxflat_map.hpp:272
void clear(void) noexcept
Removes all elements and destroys their key-value pairs.
const_iterator upper_bound(const key_t_ &key_) const
Returns an iterator to the first element whose key is ordered after key.
hxkey_less_t<T> - A constexpr callable that invokes hxkey_less.
Definition hxkey.hpp:131
Similar to std::allocator.
HX_NS_BEGIN_ constexpr hxsize_t hxallocator_dynamic_capacity
hxallocator_dynamic_capacity - A capacity value that allows for dynamic allocation.
Definition hxallocator.hpp:20
void hxswap(hxflat_map< key_t_, mapped_t_, compare_t_, multi_t_, hxallocator_dynamic_capacity > &x_, hxflat_map< key_t_, mapped_t_, compare_t_, multi_t_, hxallocator_dynamic_capacity > &y_) noexcept
void hxswap(hxflat_map<K,V>& x, hxflat_map<K,V>& y) - Exchanges the contents of x and y.
Definition hxflat_map.hpp:426
bool hxkey_equal(const hxflat_map< key_t_, mapped_t_, compare_t_, multi_t_, capacity_x_ > &x_, const hxflat_map< key_t_, mapped_t_, compare_t_, multi_t_, capacity_y_ > &y_)
bool hxkey_equal(hxflat_map<K,V>& x, hxflat_map<K,V>& y) - Compares the contents of x and y for equiv...
Definition hxflat_map.hpp:405
bool hxkey_less(const hxflat_map< key_t_, mapped_t_, compare_t_, multi_t_, capacity_x_ > &x_, const hxflat_map< key_t_, mapped_t_, compare_t_, multi_t_, capacity_y_ > &y_)
bool hxkey_less(hxflat_map<K,V>& x, hxflat_map<K,V>& y) - Compares the contents of x and y lexicograp...
Definition hxflat_map.hpp:416
User-overloadable key-equal, key-less, and key-hash functions.
@ hxsystem_allocator_current
hxsystem_allocator_current - Use current allocation scope.
Definition hxmemory_manager.h:82
int hxsystem_allocator_t
hxsystem_allocator_t - This is extendable by the application.
Definition hxmemory_manager.h:77
unsigned int hxalignment_t
hxalignment_t - A positive integer power of 2 for aligning allocations.
Definition hxmemory_manager.h:64
constexpr hxalignment_t hxalignment
hxalignment - The default alignment.
Definition hxmemory_manager.h:69
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:120
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
#define hxattr_flatten
hxattr_flatten - Inline every call inside a function's body into it, recursively, regardless of the c...
Definition hxsettings.h:82
Standard sorting utilities.
void hxswap_memcpy(T &x_, T &y_)
hxswap_memcpy - Exchanges the contents of x and y using memcpy and a stack temporary.
Definition hxutility.h:354
Provides core macros, memory management and feature detection.
#define hxnull
hxnull - The null pointer value for a given pointer type represented by the numeric constant 0.
Definition libhatchet.h:277
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:209