Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_flat_map.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
67#ifndef TPL_FLAT_MAP_H
68#define TPL_FLAT_MAP_H
69
70#include <algorithm>
71#include <initializer_list>
72#include <iterator>
73#include <type_traits>
74#include <utility>
75
76#include <ah-errors.H>
77#include <ahFunction.H>
78#include <htlist.H>
79#include <tpl_memArray.H>
80#include <tpl_sort_utils.H>
81
82namespace Aleph {
83
149template <typename Key, typename T, class Compare = Aleph::less<Key>>
151{
154 Compare cmp_;
155
156 // Index of the first key not less than k (lower bound position).
157 size_t lower_idx(const Key &k) const
158 {
159 size_t lo = 0, hi = keys_.size();
160 while (lo < hi)
161 {
162 const size_t mid = lo + ((hi - lo) >> 1);
163 if (cmp_(keys_(mid), k))
164 lo = mid + 1;
165 else
166 hi = mid;
167 }
168 return lo;
169 }
170
171 // Index of the first key greater than k (upper bound position).
172 size_t upper_idx(const Key &k) const
173 {
174 size_t lo = 0, hi = keys_.size();
175 while (lo < hi)
176 {
177 const size_t mid = lo + ((hi - lo) >> 1);
178 if (not cmp_(k, keys_(mid)))
179 lo = mid + 1;
180 else
181 hi = mid;
182 }
183 return lo;
184 }
185
186 // True if pos holds a key equivalent to k. Assumes pos == lower_idx(k).
187 bool match_at(size_t pos, const Key &k) const
188 {
189 return pos < keys_.size() and not cmp_(k, keys_(pos));
190 }
191
192 // True if keys and values can be moved without throwing. A shift or a
193 // relocation then always completes, and the only way an insertion can
194 // fail is an allocation that leaves the entries untouched.
195 static constexpr bool nothrow_relocation =
196 std::is_nothrow_move_constructible_v<Key> and
197 std::is_nothrow_move_assignable_v<Key> and
198 std::is_nothrow_move_constructible_v<T> and
199 std::is_nothrow_move_assignable_v<T>;
200
201 // Runs op, which moves entries inside the arrays. If such a move can
202 // throw, an exception may leave an entry moved from, duplicated or apart
203 // from its value. The map is then emptied, which restores its invariants
204 // as std::flat_map does, and the exception continues.
205 template <class Op>
206 void keeping_invariants(Op &&op)
207 {
208 if constexpr (nothrow_relocation)
209 op();
210 else
211 try
212 {
213 op();
214 }
215 catch (...)
216 {
217 empty();
218 throw;
219 }
220 }
221
222 // Moves the entries of arr to fresh, which is empty and has room for
223 // them, and leaves fresh as the buffer of arr.
224 template <class U>
225 static void relocate(MemArray<U> &arr, MemArray<U> &fresh)
226 {
227 const size_t n = arr.size();
228 fresh.putn(n);
229 U *from = arr.get_ptr();
230 U *to = fresh.get_ptr();
231 for (size_t i = 0; i < n; ++i)
232 to[i] = std::move(from[i]);
233 arr.swap(fresh);
234 }
235
236 // Gives arr room for cap entries. If the entries cannot throw when moved,
237 // MemArray::reserve() either completes or fails before it moves any.
238 // Otherwise the new buffer is obtained here first, so that a cap out of
239 // range or an allocation that fails leaves the entries untouched; only a
240 // move that throws afterwards is left to keeping_invariants().
241 template <class U>
242 void grow_array(MemArray<U> &arr, const size_t cap)
243 {
244 if (cap <= arr.capacity())
245 return;
246
247 if constexpr (nothrow_relocation)
248 arr.reserve(cap);
249 else
250 {
251 MemArray<U> fresh(cap);
253 {
254 relocate(arr, fresh);
255 });
256 }
257 }
258
259 // Gives both arrays room for cap entries. Nothing changes but the
260 // capacity, so if the second array fails the map still holds its entries
261 // even though the first one has already grown.
262 void grow_to(const size_t cap)
263 {
264 grow_array(keys_, cap);
265 grow_array(vals_, cap);
266 }
267
268 // Opens one slot at pos in both arrays. They have room for it: growing
269 // is a matter of grow_to(), which runs before any logical size change.
270 void make_room(const size_t pos)
271 {
272 keys_.putn(1);
273 vals_.putn(1);
274 Key *kp = keys_.get_ptr();
275 T *vp = vals_.get_ptr();
276 for (size_t i = keys_.size() - 1; i > pos; --i)
277 {
278 kp[i] = std::move(kp[i - 1]);
279 vp[i] = std::move(vp[i - 1]);
280 }
281 }
282
283 // Stores (k, v) in a new slot at pos. Both are built before the arrays
284 // change, so they may be entries of this map: opening the slot shifts the
285 // entries and may free the buffers they live in. Building them first also
286 // means that a copy that throws leaves the map untouched.
287 template <class K, class V>
288 void insert_at(const size_t pos, K &&k, V &&v)
289 {
290 Key key(std::forward<K>(k));
291 T val(std::forward<V>(v));
292
293 // A full array doubles. One emptied by a move has no storage at all and
294 // starts again at the minimum, as MemArray does on its own.
295 if (const size_t n = keys_.size(); n == keys_.capacity() or n == vals_.capacity())
296 grow_to(std::max(n + 1, MemArray<Key>::Min_Dim));
297
299 {
300 make_room(pos);
301 keys_.get_ptr()[pos] = std::move(key);
302 vals_.get_ptr()[pos] = std::move(val);
303 });
304 }
305
306 // Drops the last slot of both arrays. If the entries cannot throw when
307 // moved, a shrinking reallocation can only fail before it moves anything,
308 // and that failure is ignored: the slot is already gone. Otherwise the
309 // exception may come from an entry and is left to keeping_invariants().
311 {
312 if constexpr (nothrow_relocation)
313 {
314 try
315 {
316 (void) keys_.get();
317 }
318 catch (...)
319 { /* shrink failed; logical removal already done */
320 }
321 try
322 {
323 (void) vals_.get();
324 }
325 catch (...)
326 { /* shrink failed; logical removal already done */
327 }
328 }
329 else
330 {
331 (void) keys_.get();
332 (void) vals_.get();
333 }
334 }
335
336 // Removes the slot at pos from both arrays.
337 void remove_at(const size_t pos)
338 {
340 {
341 Key *kp = keys_.get_ptr();
342 T *vp = vals_.get_ptr();
343 const size_t last = keys_.size() - 1;
344 for (size_t i = pos; i < last; ++i)
345 {
346 kp[i] = std::move(kp[i + 1]);
347 vp[i] = std::move(vp[i + 1]);
348 }
349 drop_last();
350 });
351 }
352
353 // Sorts freshly loaded parallel arrays by key and drops duplicate keys,
354 // keeping the first occurrence of each equivalence class.
356 {
357 const size_t m = keys_.size();
359 Key *kp = keys_.get_ptr();
360 T *vp = vals_.get_ptr();
361 for (size_t i = 0; i < m; ++i)
362 tmp.put(std::pair<Key, T>(std::move(kp[i]), std::move(vp[i])));
363
364 std::pair<Key, T> *tp = tmp.get_ptr();
365 timsort(tp, m, [this](const auto &a, const auto &b)
366 {
367 return cmp_(a.first, b.first);
368 });
369
370 size_t out = 0;
371 for (size_t i = 0; i < m; ++i)
372 if (out == 0 or cmp_(kp[out - 1], tp[i].first))
373 {
374 kp[out] = std::move(tp[i].first);
375 vp[out] = std::move(tp[i].second);
376 ++out;
377 }
378 const size_t excess = m - out;
379 if (excess > 0)
380 {
381 try { (void) keys_.get(excess); } catch (const std::bad_alloc &)
382 { /* shrink failed */ }
383 try { (void) vals_.get(excess); } catch (const std::bad_alloc &)
384 { /* shrink failed */ }
385 }
386 }
387
388public:
389 using Item_Type = std::pair<Key, T>;
390 using Key_Type = Key;
391 using key_type = Key;
392 using mapped_type = T;
393 using value_type = std::pair<Key, T>;
394 using key_compare = Compare;
395 using size_type = size_t;
396
407 template <bool IsConst>
409 {
410 using VPtr = std::conditional_t<IsConst, const T *, T *>;
411
412 const Key *k_ = nullptr;
413 VPtr v_ = nullptr;
414
415 public:
418 {
419 const Key &first;
420 std::conditional_t<IsConst, const T &, T &> second;
421 };
422
424 struct pointer
425 {
429 {
430 return &ref;
431 }
432 };
433
434 using iterator_category = std::random_access_iterator_tag;
435 using value_type = std::pair<Key, T>;
436 using difference_type = std::ptrdiff_t;
437
439 basic_iterator() = default;
440
442 basic_iterator(const Key *k, VPtr v) noexcept : k_(k), v_(v) {}
443
445 template <bool B> requires (IsConst and not B)
446 basic_iterator(const basic_iterator<B> &it) noexcept : k_(it.key_ptr()), v_(it.val_ptr())
447 {}
448
450 const Key *key_ptr() const noexcept
451 {
452 return k_;
453 }
454
456 VPtr val_ptr() const noexcept
457 {
458 return v_;
459 }
460
462 reference operator * () const noexcept
463 {
464 return {*k_, *v_};
465 }
466
468 pointer operator ->() const noexcept
469 {
470 return {{*k_, *v_}};
471 }
472
475 {
476 return {*(k_ + i), *(v_ + i)};
477 }
478
481 {
482 ++k_;
483 ++v_;
484 return *this;
485 }
486
489 {
490 basic_iterator ret = *this;
491 ++*this;
492 return ret;
493 }
494
497 {
498 --k_;
499 --v_;
500 return *this;
501 }
502
505 {
506 basic_iterator ret = *this;
507 --*this;
508 return ret;
509 }
510
513 {
514 k_ += i;
515 v_ += i;
516 return *this;
517 }
518
521 {
522 return *this += -i;
523 }
524
527 {
528 basic_iterator ret = *this;
529 return ret += i;
530 }
531
534 {
535 basic_iterator ret = *this;
536 return ret -= i;
537 }
538
541 {
542 return k_ - it.key_ptr();
543 }
544
546 bool operator == (const basic_iterator &it) const noexcept
547 {
548 return k_ == it.key_ptr();
549 }
550
552 bool operator != (const basic_iterator &it) const noexcept
553 {
554 return k_ != it.key_ptr();
555 }
556
558 bool operator < (const basic_iterator &it) const noexcept
559 {
560 return k_ < it.key_ptr();
561 }
562
564 bool operator <= (const basic_iterator &it) const noexcept
565 {
566 return k_ <= it.key_ptr();
567 }
568
570 bool operator > (const basic_iterator &it) const noexcept
571 {
572 return k_ > it.key_ptr();
573 }
574
576 bool operator >= (const basic_iterator &it) const noexcept
577 {
578 return k_ >= it.key_ptr();
579 }
580 };
581
584
591 explicit FlatMap(size_t cap = MemArray<Key>::Min_Dim) : keys_(cap), vals_(cap) {}
592
599 explicit FlatMap(const Compare &cmp, size_t cap = MemArray<Key>::Min_Dim)
600 : keys_(cap), vals_(cap), cmp_(cmp)
601 {}
602
617 template <class It>
618 // Mirrors the loop below instead of std::input_iterator: this map's own
619 // iterators yield a proxy with no common reference with std::pair, so
620 // they fail std::indirectly_readable while working fine here.
621 requires requires(It first, It last, MemArray<Key> & ks, MemArray<T> & vs,
622 const std::remove_reference_t<std::iter_reference_t<It>> & p)
623 {
624 static_cast<bool>(first != last);
625 ++first;
626 ks.put(p.first);
627 vs.put(p.second);
628 }
629 FlatMap(It first, It last, const Compare &cmp = Compare())
630 : keys_(MemArray<Key>::Min_Dim), vals_(MemArray<T>::Min_Dim), cmp_(cmp)
631 {
632 for (; first != last; ++first)
633 {
634 const auto &p = *first;
635 keys_.put(p.first);
636 vals_.put(p.second);
637 }
639 }
640
647 FlatMap(std::initializer_list<std::pair<Key, T>> l, const Compare &cmp = Compare())
648 : FlatMap(l.begin(), l.end(), cmp)
649 {}
650
652 FlatMap(const FlatMap &) = default;
653
655 FlatMap(FlatMap &&) noexcept = default;
656
666 FlatMap &operator = (const FlatMap &m)
667 {
668 if (this != &m)
669 {
670 FlatMap tmp(m);
671 swap(tmp);
672 }
673 return *this;
674 }
675
677 FlatMap &operator = (FlatMap &&) noexcept = default;
678
679 ~FlatMap() = default;
680
684 void swap(FlatMap &m) noexcept(std::is_nothrow_swappable_v<Compare>)
685 {
686 keys_.swap(m.keys_);
687 vals_.swap(m.vals_);
688 std::swap(cmp_, m.cmp_);
689 }
690
691 // -- capacity -------------------------------------------------------------
692
694 [[nodiscard]] size_t size() const noexcept
695 {
696 return keys_.size();
697 }
698
700 [[nodiscard]] bool is_empty() const noexcept
701 {
702 return keys_.is_empty();
703 }
704
706 [[nodiscard]] size_t capacity() const noexcept
707 {
708 return keys_.capacity();
709 }
710
720 void reserve(size_t cap)
721 {
722 grow_to(cap);
723 }
724
729 void empty() noexcept
730 {
731 keys_.empty();
732 vals_.empty();
733 }
734
736 void clear() noexcept
737 {
738 empty();
739 }
740
741 // -- lookup ---------------------------------------------------------------
742
748 iterator find(const Key &k)
749 {
750 const size_t pos = lower_idx(k);
751 return match_at(pos, k) ? begin() + pos : end();
752 }
753
755 const_iterator find(const Key &k) const
756 {
757 const size_t pos = lower_idx(k);
758 return match_at(pos, k) ? begin() + pos : end();
759 }
760
766 [[nodiscard]] bool contains(const Key &k) const
767 {
768 return match_at(lower_idx(k), k);
769 }
770
776 [[nodiscard]] size_t count(const Key &k) const
777 {
778 return contains(k) ? 1 : 0;
779 }
780
787 {
788 return begin() + lower_idx(k);
789 }
790
792 const_iterator lower_bound(const Key &k) const
793 {
794 return begin() + lower_idx(k);
795 }
796
803 {
804 return begin() + upper_idx(k);
805 }
806
808 const_iterator upper_bound(const Key &k) const
809 {
810 return begin() + upper_idx(k);
811 }
812
818 std::pair<iterator, iterator> equal_range(const Key &k)
819 {
820 return {lower_bound(k), upper_bound(k)};
821 }
822
824 std::pair<const_iterator, const_iterator> equal_range(const Key &k) const
825 {
826 return {lower_bound(k), upper_bound(k)};
827 }
828
835 T &at(const Key &k)
836 {
837 const size_t pos = lower_idx(k);
838 ah_out_of_range_error_if(not match_at(pos, k)) << "FlatMap::at(): key not found";
839 return vals_(pos);
840 }
841
843 const T &at(const Key &k) const
844 {
845 const size_t pos = lower_idx(k);
846 ah_out_of_range_error_if(not match_at(pos, k)) << "FlatMap::at(): key not found";
847 return vals_(pos);
848 }
849
856 T &operator [] (const Key &k)
857 {
858 const size_t pos = lower_idx(k);
859 if (match_at(pos, k))
860 return vals_(pos);
861 insert_at(pos, k, T());
862 return vals_(pos);
863 }
864
874 {
875 const size_t pos = lower_idx(k);
876 if (match_at(pos, k))
877 return vals_(pos);
878 insert_at(pos, std::move(k), T());
879 return vals_(pos);
880 }
881
882 // -- positional access ----------------------------------------------------
883
890 const Key &nth_key(size_t i) const
891 {
892 return keys_[i];
893 }
894
901 T &nth_value(size_t i)
902 {
903 return vals_[i];
904 }
905
907 const T &nth_value(size_t i) const
908 {
909 return vals_[i];
910 }
911
912 // -- modifiers ------------------------------------------------------------
913
926 std::pair<iterator, bool> insert(const Key &k, const T &v)
927 {
928 const size_t pos = lower_idx(k);
929 if (match_at(pos, k))
930 return {begin() + pos, false};
931 insert_at(pos, k, v);
932 return {begin() + pos, true};
933 }
934
936 std::pair<iterator, bool> insert(Key &&k, T &&v)
937 {
938 const size_t pos = lower_idx(k);
939 if (match_at(pos, k))
940 return {begin() + pos, false};
941 insert_at(pos, std::move(k), std::move(v));
942 return {begin() + pos, true};
943 }
944
950 std::pair<iterator, bool> insert(const std::pair<Key, T> &p)
951 {
952 return insert(p.first, p.second);
953 }
954
956 std::pair<iterator, bool> insert(std::pair<Key, T> &&p)
957 {
958 return insert(std::move(p.first), std::move(p.second));
959 }
960
969 std::pair<iterator, bool> insert_or_assign(const Key &k, const T &v)
970 {
971 const size_t pos = lower_idx(k);
972 if (match_at(pos, k))
973 {
974 vals_(pos) = v;
975 return {begin() + pos, false};
976 }
977 insert_at(pos, k, v);
978 return {begin() + pos, true};
979 }
980
982 std::pair<iterator, bool> insert_or_assign(Key &&k, T &&v)
983 {
984 const size_t pos = lower_idx(k);
985 if (match_at(pos, k))
986 {
987 vals_(pos) = std::move(v);
988 return {begin() + pos, false};
989 }
990 insert_at(pos, std::move(k), std::move(v));
991 return {begin() + pos, true};
992 }
993
1004 template <class... KArgs>
1005 std::pair<iterator, bool> emplace(KArgs &&...kargs)
1006 {
1007 return insert(Key(std::forward<KArgs>(kargs)...), T());
1008 }
1009
1019 size_t erase(const Key &k)
1020 {
1021 const size_t pos = lower_idx(k);
1022 if (not match_at(pos, k))
1023 return 0;
1024 remove_at(pos);
1025 return 1;
1026 }
1027
1035 {
1036 const size_t pos = it.key_ptr() - keys_.get_ptr();
1037 ah_out_of_range_error_if(pos >= size()) << "FlatMap::erase(): iterator out of range";
1038 remove_at(pos);
1039 return begin() + pos;
1040 }
1041
1042 // -- iteration ------------------------------------------------------------
1043
1045 iterator begin() noexcept
1046 {
1047 return iterator(keys_.get_ptr(), vals_.get_ptr());
1048 }
1049
1051 iterator end() noexcept
1052 {
1053 return iterator(keys_.get_ptr() + size(), vals_.get_ptr() + size());
1054 }
1055
1057 const_iterator begin() const noexcept
1058 {
1059 return const_iterator(keys_.get_ptr(), vals_.get_ptr());
1060 }
1061
1063 const_iterator end() const noexcept
1064 {
1065 return const_iterator(keys_.get_ptr() + size(), vals_.get_ptr() + size());
1066 }
1067
1069 const_iterator cbegin() const noexcept
1070 {
1071 return begin();
1072 }
1073
1075 const_iterator cend() const noexcept
1076 {
1077 return end();
1078 }
1079
1081 const Key *keys_data() const noexcept
1082 {
1083 return keys_.get_ptr();
1084 }
1085
1087 T *values_data() noexcept
1088 {
1089 return vals_.get_ptr();
1090 }
1091
1093 const T *values_data() const noexcept
1094 {
1095 return vals_.get_ptr();
1096 }
1097
1104 {
1105 DynList<Key> ret;
1106 const Key *p = keys_.get_ptr();
1107 for (size_t i = 0; i < size(); ++i)
1108 ret.append(p[i]);
1109 return ret;
1110 }
1111
1118 {
1119 DynList<T> ret;
1120 const T *p = vals_.get_ptr();
1121 for (size_t i = 0; i < size(); ++i)
1122 ret.append(p[i]);
1123 return ret;
1124 }
1125
1135 template <class Operation>
1136 bool traverse(Operation operation)
1137 {
1138 const Key *kp = keys_.get_ptr();
1139 T *vp = vals_.get_ptr();
1140 const size_t m = size();
1141 for (size_t i = 0; i < m; ++i)
1142 if (not operation(kp[i], vp[i]))
1143 return false;
1144 return true;
1145 }
1146
1153 template <class Operation>
1154 bool traverse(Operation operation) const
1155 {
1156 const Key *kp = keys_.get_ptr();
1157 const T *vp = vals_.get_ptr();
1158 const size_t m = size();
1159 for (size_t i = 0; i < m; ++i)
1160 if (not operation(kp[i], vp[i]))
1161 return false;
1162 return true;
1163 }
1164
1165 // -- comparison -----------------------------------------------------------
1166
1172 bool operator == (const FlatMap &m) const
1173 {
1174 return size() == m.size() and
1175 std::equal(keys_.get_ptr(), keys_.get_ptr() + size(), m.keys_.get_ptr()) and
1176 std::equal(vals_.get_ptr(), vals_.get_ptr() + size(), m.vals_.get_ptr());
1177 }
1178
1180 bool operator != (const FlatMap &m) const
1181 {
1182 return not (*this == m);
1183 }
1184};
1185
1186} // end namespace Aleph
1187
1188#endif // TPL_FLAT_MAP_H
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
Standard functor implementations and comparison objects.
size_t size_t int32_t * out
Definition ca-c-api.h:120
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Random-access proxy iterator over (key, value) entries.
reference operator*() const noexcept
Dereference to the (key, value) proxy.
VPtr val_ptr() const noexcept
Cursor into the value array (internal use).
basic_iterator & operator++() noexcept
Pre-increment.
basic_iterator()=default
Construct a singular iterator.
basic_iterator operator-(difference_type i) const noexcept
Iterator i positions backward.
std::pair< Key, T > value_type
Materialized entry type.
bool operator==(const basic_iterator &it) const noexcept
Equality: same position.
std::ptrdiff_t difference_type
Signed distance type.
const Key * key_ptr() const noexcept
Cursor into the key array (internal use).
pointer operator->() const noexcept
Member access through the proxy (e.g. it->second).
basic_iterator(const Key *k, VPtr v) noexcept
Construct from parallel key/value cursors (internal use).
bool operator<=(const basic_iterator &it) const noexcept
Ordering by position.
reference operator[](difference_type i) const noexcept
Proxy for the entry i positions away.
std::conditional_t< IsConst, const T *, T * > VPtr
bool operator>=(const basic_iterator &it) const noexcept
Ordering by position.
std::random_access_iterator_tag iterator_category
Category.
bool operator<(const basic_iterator &it) const noexcept
Strict ordering by position.
basic_iterator & operator+=(difference_type i) noexcept
Advance by i positions.
bool operator>(const basic_iterator &it) const noexcept
Strict ordering by position.
basic_iterator(const basic_iterator< B > &it) noexcept
Convert a mutable iterator into a const iterator.
basic_iterator & operator-=(difference_type i) noexcept
Retreat by i positions.
bool operator!=(const basic_iterator &it) const noexcept
Inequality: different position.
basic_iterator operator+(difference_type i) const noexcept
Iterator i positions forward.
basic_iterator & operator--() noexcept
Pre-decrement.
Ordered map stored as two parallel sorted contiguous arrays.
T & operator[](const Key &k)
Access the value mapped to k, inserting a default if absent.
FlatMap(FlatMap &&) noexcept=default
Move constructor. The source is left valid but unspecified.
const Key & nth_key(size_t i) const
Key of the i-th entry in sorted order (checked).
std::pair< iterator, bool > insert(const Key &k, const T &v)
Insert (k, v) if k is not mapped.
iterator upper_bound(const Key &k)
First entry whose key is greater than k.
FlatMap(size_t cap=MemArray< Key >::Min_Dim)
Construct an empty map.
const Key * keys_data() const noexcept
Pointer to the sorted, contiguous key storage. O(1).
size_t upper_idx(const Key &k) const
FlatMap(const Compare &cmp, size_t cap=MemArray< Key >::Min_Dim)
Construct an empty map with a specific comparator.
void reserve(size_t cap)
Reserve capacity for at least cap entries.
iterator erase(const_iterator it)
Remove the entry at iterator it.
const T * values_data() const noexcept
See values_data().
T & nth_value(size_t i)
Value of the i-th entry in sorted order (checked).
void keeping_invariants(Op &&op)
size_t count(const Key &k) const
Count entries with key k (0 or 1).
MemArray< Key > keys_
FlatMap & operator=(const FlatMap &m)
Copy assignment (requires copyable Key and T).
bool contains(const Key &k) const
Test whether key k is mapped.
size_t capacity() const noexcept
Return the capacity of the backing key array. O(1).
const_iterator find(const Key &k) const
See find(const Key & k).
const_iterator lower_bound(const Key &k) const
See lower_bound(const Key & k).
Compare key_compare
STL convention: comparator type.
bool traverse(Operation operation) const
Const traversal in key order while operation returns true.
DynList< Key > keys() const
Copy all keys, in ascending order, into a DynList.
const_iterator cend() const noexcept
Const iterator past the entry with the greatest key. O(1).
T * values_data() noexcept
Pointer to the contiguous value storage (parallel to keys). O(1).
basic_iterator< false > iterator
Mutable-value iterator.
void clear() noexcept
Remove all entries. Alias of empty(). Capacity is kept.
size_t erase(const Key &k)
Remove the entry with key k, if present.
MemArray< T > vals_
const T & nth_value(size_t i) const
See nth_value(size_t i).
std::pair< iterator, bool > insert(Key &&k, T &&v)
See insert(const Key & k, const T & v); moving key and value.
const_iterator end() const noexcept
Const iterator past the entry with the greatest key. O(1).
const_iterator upper_bound(const Key &k) const
See upper_bound(const Key & k).
void empty() noexcept
Remove all entries (Aleph convention).
bool is_empty() const noexcept
Return true if the map holds no entries. O(1).
T & at(const Key &k)
Checked access to the value mapped to k.
void insert_at(const size_t pos, K &&k, V &&v)
std::pair< iterator, bool > insert(std::pair< Key, T > &&p)
See insert(const std::pair<Key, T> & p); moving the pair.
void remove_at(const size_t pos)
size_t size_type
STL convention: size type.
Key Key_Type
Aleph convention: key type.
bool operator!=(const FlatMap &m) const
Inequality: negation of operator==.
void swap(FlatMap &m) noexcept(std::is_nothrow_swappable_v< Compare >)
Swap contents with m in O(1).
static void relocate(MemArray< U > &arr, MemArray< U > &fresh)
size_t lower_idx(const Key &k) const
DynList< T > values() const
Copy all values, in ascending key order, into a DynList.
Key key_type
STL convention: key type.
FlatMap(const FlatMap &)=default
Copy constructor (requires copyable Key and T).
bool match_at(size_t pos, const Key &k) const
bool traverse(Operation operation)
Traverse entries in key order while operation returns true.
void make_room(const size_t pos)
std::pair< iterator, bool > insert(const std::pair< Key, T > &p)
Insert a (key, value) pair if the key is not mapped.
std::pair< Key, T > Item_Type
Aleph convention: element type.
void grow_to(const size_t cap)
void grow_array(MemArray< U > &arr, const size_t cap)
FlatMap(It first, It last, const Compare &cmp=Compare())
Construct from an iterator range of pairs.
const_iterator cbegin() const noexcept
Const iterator to the entry with the smallest key. O(1).
std::pair< iterator, bool > insert_or_assign(Key &&k, T &&v)
See insert_or_assign(const Key & k, const T & v); moving both.
basic_iterator< true > const_iterator
Const-value iterator.
iterator lower_bound(const Key &k)
First entry whose key is not less than k.
void sort_and_unique()
std::pair< const_iterator, const_iterator > equal_range(const Key &k) const
See equal_range(const Key & k).
FlatMap(std::initializer_list< std::pair< Key, T > > l, const Compare &cmp=Compare())
Construct from an initializer list of pairs.
const_iterator begin() const noexcept
Const iterator to the entry with the smallest key. O(1).
bool operator==(const FlatMap &m) const
Equality: same size and pairwise equal keys and values.
T mapped_type
STL convention: mapped type.
iterator begin() noexcept
Iterator to the entry with the smallest key. O(1).
std::pair< iterator, iterator > equal_range(const Key &k)
Range of entries with key equivalent to k.
const T & at(const Key &k) const
See at(const Key & k).
std::pair< iterator, bool > insert_or_assign(const Key &k, const T &v)
Insert (k, v) or overwrite the value if k is mapped.
iterator find(const Key &k)
Find the entry with key k.
size_t size() const noexcept
Return the number of stored entries. O(1).
std::pair< iterator, bool > emplace(KArgs &&...kargs)
Build a key from kargs and insert it with a default value.
iterator end() noexcept
Iterator past the entry with the greatest key. O(1).
std::pair< Key, T > value_type
STL convention: value type.
static constexpr bool nothrow_relocation
Simple, scalable and fast dynamic array.
T & put(const T &item)
Put a copy of item at the end of the sequence.
void reserve(const size_t cap)
Reserves cap cells into the array.
size_t size() const noexcept
Return the number of elements.
void putn(const size_t more)
Reserve more additional logical slots in the array.
T get(const size_t i=1)
Remove i elements from the end.
constexpr size_t capacity() const noexcept
The type of element of array.
bool is_empty() const noexcept
Return true is the array is empty.
void empty() noexcept
Empties the container.
T * get_ptr() noexcept
Return the current base of array.
void swap(MemArray &a) noexcept
Swap in constant time this with a
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
void timsort(T *a, const size_t n, const Compare &cmp=Compare())
Timsort — adaptive, stable, natural merge sort.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Proxy returned by operator->, keeps the reference alive.
reference ref
Materialized reference proxy.
reference * operator->() noexcept
Give access to the members of the proxy.
Proxy returned by operator*: references into the parallel arrays.
const Key & first
The entry's key.
std::conditional_t< IsConst, const T &, T & > second
The entry's value.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
size_t V
static int * k
Simple, scalable, contiguous dynamic array.
Comprehensive sorting algorithms and search utilities for Aleph-w.
DynList< int > l