Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::FlatMap< Key, T, Compare > Class Template Reference

Ordered map stored as two parallel sorted contiguous arrays. More...

#include <tpl_flat_map.H>

Collaboration diagram for Aleph::FlatMap< Key, T, Compare >:
[legend]

Classes

class  basic_iterator
 Random-access proxy iterator over (key, value) entries. More...
 

Public Types

using Item_Type = std::pair< Key, T >
 Aleph convention: element type.
 
using Key_Type = Key
 Aleph convention: key type.
 
using key_type = Key
 STL convention: key type.
 
using mapped_type = T
 STL convention: mapped type.
 
using value_type = std::pair< Key, T >
 STL convention: value type.
 
using key_compare = Compare
 STL convention: comparator type.
 
using size_type = size_t
 STL convention: size type.
 
using iterator = basic_iterator< false >
 Mutable-value iterator.
 
using const_iterator = basic_iterator< true >
 Const-value iterator.
 

Public Member Functions

 FlatMap (size_t cap=MemArray< Key >::Min_Dim)
 Construct an empty map.
 
 FlatMap (const Compare &cmp, size_t cap=MemArray< Key >::Min_Dim)
 Construct an empty map with a specific comparator.
 
template<class It >
requires requires(It first, It last, MemArray<Key> & ks, MemArray<T> & vs, const std::remove_reference_t<std::iter_reference_t<It>> & p) { static_cast<bool>(first != last); ++first; ks.put(p.first); vs.put(p.second); }
 FlatMap (It first, It last, const Compare &cmp=Compare())
 Construct from an iterator range of pairs.
 
 FlatMap (std::initializer_list< std::pair< Key, T > > l, const Compare &cmp=Compare())
 Construct from an initializer list of pairs.
 
 FlatMap (const FlatMap &)=default
 Copy constructor (requires copyable Key and T).
 
 FlatMap (FlatMap &&) noexcept=default
 Move constructor. The source is left valid but unspecified.
 
FlatMap & operator= (const FlatMap &m)
 Copy assignment (requires copyable Key and T).
 
FlatMap & operator= (FlatMap &&) noexcept=default
 Move assignment. The source is left valid but unspecified.
 
 ~FlatMap ()=default
 
void swap (FlatMap &m) noexcept(std::is_nothrow_swappable_v< Compare >)
 Swap contents with m in O(1).
 
size_t size () const noexcept
 Return the number of stored entries. O(1).
 
bool is_empty () const noexcept
 Return true if the map holds no entries. O(1).
 
size_t capacity () const noexcept
 Return the capacity of the backing key array. O(1).
 
void reserve (size_t cap)
 Reserve capacity for at least cap entries.
 
void empty () noexcept
 Remove all entries (Aleph convention).
 
void clear () noexcept
 Remove all entries. Alias of empty(). Capacity is kept.
 
iterator find (const Key &k)
 Find the entry with key k.
 
const_iterator find (const Key &k) const
 See find(const Key & k).
 
bool contains (const Key &k) const
 Test whether key k is mapped.
 
size_t count (const Key &k) const
 Count entries with key k (0 or 1).
 
iterator lower_bound (const Key &k)
 First entry whose key is not less than k.
 
const_iterator lower_bound (const Key &k) const
 See lower_bound(const Key & k).
 
iterator upper_bound (const Key &k)
 First entry whose key is greater than k.
 
const_iterator upper_bound (const Key &k) const
 See upper_bound(const Key & k).
 
std::pair< iterator, iterator > equal_range (const Key &k)
 Range of entries with key equivalent to k.
 
std::pair< const_iterator, const_iterator > equal_range (const Key &k) const
 See equal_range(const Key & k).
 
T & at (const Key &k)
 Checked access to the value mapped to k.
 
const T & at (const Key &k) const
 See at(const Key & k).
 
T & operator[] (const Key &k)
 Access the value mapped to k, inserting a default if absent.
 
T & operator[] (Key &&k)
 Access the value mapped to k, inserting a default if absent.
 
const Key & nth_key (size_t i) const
 Key of the i-th entry in sorted order (checked).
 
T & nth_value (size_t i)
 Value of the i-th entry in sorted order (checked).
 
const T & nth_value (size_t i) const
 See nth_value(size_t i).
 
std::pair< iterator, bool > insert (const Key &k, const T &v)
 Insert (k, v) if k is not mapped.
 
std::pair< iterator, bool > insert (Key &&k, T &&v)
 See insert(const Key & k, const T & v); moving key and value.
 
std::pair< iterator, bool > insert (const std::pair< Key, T > &p)
 Insert a (key, value) pair if the key is not mapped.
 
std::pair< iterator, bool > insert (std::pair< Key, T > &&p)
 See insert(const std::pair<Key, T> & p); moving the pair.
 
std::pair< iterator, bool > insert_or_assign (const Key &k, const T &v)
 Insert (k, v) or overwrite the value if k is mapped.
 
std::pair< iterator, bool > insert_or_assign (Key &&k, T &&v)
 See insert_or_assign(const Key & k, const T & v); moving both.
 
template<class... KArgs>
std::pair< iterator, bool > emplace (KArgs &&...kargs)
 Build a key from kargs and insert it with a default value.
 
size_t erase (const Key &k)
 Remove the entry with key k, if present.
 
iterator erase (const_iterator it)
 Remove the entry at iterator it.
 
iterator begin () noexcept
 Iterator to the entry with the smallest key. O(1).
 
iterator end () noexcept
 Iterator past the entry with the greatest key. O(1).
 
const_iterator begin () const noexcept
 Const iterator to the entry with the smallest key. O(1).
 
const_iterator end () const noexcept
 Const iterator past the entry with the greatest key. O(1).
 
const_iterator cbegin () const noexcept
 Const iterator to the entry with the smallest key. O(1).
 
const_iterator cend () const noexcept
 Const iterator past the entry with the greatest key. O(1).
 
const Key * keys_data () const noexcept
 Pointer to the sorted, contiguous key storage. O(1).
 
T * values_data () noexcept
 Pointer to the contiguous value storage (parallel to keys). O(1).
 
const T * values_data () const noexcept
 See values_data().
 
DynList< Key > keys () const
 Copy all keys, in ascending order, into a DynList.
 
DynList< T > values () const
 Copy all values, in ascending key order, into a DynList.
 
template<class Operation >
bool traverse (Operation operation)
 Traverse entries in key order while operation returns true.
 
template<class Operation >
bool traverse (Operation operation) const
 Const traversal in key order while operation returns true.
 
bool operator== (const FlatMap &m) const
 Equality: same size and pairwise equal keys and values.
 
bool operator!= (const FlatMap &m) const
 Inequality: negation of operator==.
 

Private Member Functions

size_t lower_idx (const Key &k) const
 
size_t upper_idx (const Key &k) const
 
bool match_at (size_t pos, const Key &k) const
 
template<class Op >
void keeping_invariants (Op &&op)
 
template<class U >
void grow_array (MemArray< U > &arr, const size_t cap)
 
void grow_to (const size_t cap)
 
void make_room (const size_t pos)
 
template<class K , class V >
void insert_at (const size_t pos, K &&k, V &&v)
 
void drop_last ()
 
void remove_at (const size_t pos)
 
void sort_and_unique ()
 

Static Private Member Functions

template<class U >
static void relocate (MemArray< U > &arr, MemArray< U > &fresh)
 

Private Attributes

MemArray< Key > keys_
 
MemArray< T > vals_
 
Compare cmp_
 

Static Private Attributes

static constexpr bool nothrow_relocation
 

Detailed Description

template<typename Key, typename T, class Compare = Aleph::less<Key>>
class Aleph::FlatMap< Key, T, Compare >

Ordered map stored as two parallel sorted contiguous arrays.

Keys live in a sorted MemArray<Key>; the value associated with keys[i] lives at values[i]. Lookups binary-search the key array only. Keys equivalent under Compare are unique.

Template Parameters
KeyType of the keys. Must be default-constructible, move-constructible and move-assignable (from MemArray).
TType of the mapped values. Same base requirements.
CompareStrict weak ordering over Key. Defaults to Aleph::less<Key>.
Iterators
Random-access proxy iterators: *it is a lightweight object with public members first (a const Key &) and second (a T &, or const T & through const_iterator). Any insertion or removal invalidates all iterators.
Aleph conventions
Following the library convention (see Array), empty() clears the container and is_empty() is the emptiness predicate.
Exception Safety
If Key and T can be move-constructed and move-assigned without throwing, insert, insert_or_assign, emplace, operator[] and reserve offer the strong guarantee: the arguments are copied before the arrays change, so the only possible failure afterwards is an allocation that leaves the entries untouched. erase then always removes the entry; a failed shrinking reallocation is ignored.

If moving a key or a value can throw, an exception thrown while entries are shifted or relocated would leave a moved-from or duplicated entry. The map is then emptied, which restores its invariants as std::flat_map does, and the exception is rethrown: the guarantee is basic. This applies to the operations above and to erase.

A failed allocation or a capacity out of range is not such an exception: the insertions and reserve obtain the new buffers before they move any entry, so the map keeps its contents whatever the types. This does not hold for erase when moving can throw: there a shrinking reallocation that fails cannot be told apart from a move that throws, and it empties the map as well.

Copy assignment offers the strong guarantee for every type.

Arguments that belong to the map
A key or a value passed to an insertion may be an entry of the same map, as in m.insert(k, m.at(j)): the value it has on entry is the one stored, also when the insertion reallocates.
Thread Safety
Distinct instances may be used from distinct threads. Concurrent access to the same instance requires external synchronization.
Example
FlatMap<std::string, int> m = {{"b", 2}, {"a", 1}};
m["c"] = 3; // insert
m["a"] += 10; // update
for (auto [k, v] : m) // sorted by key: a, b, c
std::cout << k << " = " << v << "\n";
Ordered map stored as two parallel sorted contiguous arrays.
STL namespace.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k

Definition at line 150 of file tpl_flat_map.H.

Member Typedef Documentation

◆ const_iterator

template<typename Key , typename T , class Compare = Aleph::less<Key>>
using Aleph::FlatMap< Key, T, Compare >::const_iterator = basic_iterator<true>

Const-value iterator.

Definition at line 583 of file tpl_flat_map.H.

◆ Item_Type

template<typename Key , typename T , class Compare = Aleph::less<Key>>
using Aleph::FlatMap< Key, T, Compare >::Item_Type = std::pair<Key, T>

Aleph convention: element type.

Definition at line 389 of file tpl_flat_map.H.

◆ iterator

template<typename Key , typename T , class Compare = Aleph::less<Key>>
using Aleph::FlatMap< Key, T, Compare >::iterator = basic_iterator<false>

Mutable-value iterator.

Definition at line 582 of file tpl_flat_map.H.

◆ key_compare

template<typename Key , typename T , class Compare = Aleph::less<Key>>
using Aleph::FlatMap< Key, T, Compare >::key_compare = Compare

STL convention: comparator type.

Definition at line 394 of file tpl_flat_map.H.

◆ Key_Type

template<typename Key , typename T , class Compare = Aleph::less<Key>>
using Aleph::FlatMap< Key, T, Compare >::Key_Type = Key

Aleph convention: key type.

Definition at line 390 of file tpl_flat_map.H.

◆ key_type

template<typename Key , typename T , class Compare = Aleph::less<Key>>
using Aleph::FlatMap< Key, T, Compare >::key_type = Key

STL convention: key type.

Definition at line 391 of file tpl_flat_map.H.

◆ mapped_type

template<typename Key , typename T , class Compare = Aleph::less<Key>>
using Aleph::FlatMap< Key, T, Compare >::mapped_type = T

STL convention: mapped type.

Definition at line 392 of file tpl_flat_map.H.

◆ size_type

template<typename Key , typename T , class Compare = Aleph::less<Key>>
using Aleph::FlatMap< Key, T, Compare >::size_type = size_t

STL convention: size type.

Definition at line 395 of file tpl_flat_map.H.

◆ value_type

template<typename Key , typename T , class Compare = Aleph::less<Key>>
using Aleph::FlatMap< Key, T, Compare >::value_type = std::pair<Key, T>

STL convention: value type.

Definition at line 393 of file tpl_flat_map.H.

Constructor & Destructor Documentation

◆ FlatMap() [1/6]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
Aleph::FlatMap< Key, T, Compare >::FlatMap ( size_t  cap = MemArray<Key>::Min_Dim)
inlineexplicit

Construct an empty map.

Parameters
capInitial capacity hint (rounded up to a power of two by the backing MemArrays).
Exceptions
std::bad_allocif the initial buffers cannot be allocated.

Definition at line 591 of file tpl_flat_map.H.

◆ FlatMap() [2/6]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
Aleph::FlatMap< Key, T, Compare >::FlatMap ( const Compare &  cmp,
size_t  cap = MemArray<Key>::Min_Dim 
)
inlineexplicit

Construct an empty map with a specific comparator.

Parameters
cmpComparator instance to copy.
capInitial capacity hint.
Exceptions
std::bad_allocif the initial buffers cannot be allocated.

Definition at line 599 of file tpl_flat_map.H.

◆ FlatMap() [3/6]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
template<class It >
requires requires(It first, It last, MemArray<Key> & ks, MemArray<T> & vs, const std::remove_reference_t<std::iter_reference_t<It>> & p) { static_cast<bool>(first != last); ++first; ks.put(p.first); vs.put(p.second); }
Aleph::FlatMap< Key, T, Compare >::FlatMap ( It  first,
It  last,
const Compare &  cmp = Compare() 
)
inline

Construct from an iterator range of pairs.

Loads all entries, then sorts once by key and removes duplicate keys: O(m log m) for m input entries.

Template Parameters
ItIterator whose dereferenced entry exposes first and second convertible to Key and T (a std::pair, or this map's own (key, value) proxy).
Parameters
firstBeginning of the range.
lastEnd of the range.
cmpComparator instance (defaulted).
Exceptions
std::bad_allocif memory allocation fails.
Note
On duplicate (equivalent) keys the first occurrence wins.

Definition at line 629 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::keys_, Aleph::MemArray< T >::put(), Aleph::FlatMap< Key, T, Compare >::sort_and_unique(), and Aleph::FlatMap< Key, T, Compare >::vals_.

◆ FlatMap() [4/6]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
Aleph::FlatMap< Key, T, Compare >::FlatMap ( std::initializer_list< std::pair< Key, T > >  l,
const Compare &  cmp = Compare() 
)
inline

Construct from an initializer list of pairs.

Parameters
lEntries to load; duplicate keys are dropped (first wins).
cmpComparator instance (defaulted).
Exceptions
std::bad_allocif memory allocation fails.

Definition at line 647 of file tpl_flat_map.H.

◆ FlatMap() [5/6]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
Aleph::FlatMap< Key, T, Compare >::FlatMap ( const FlatMap< Key, T, Compare > &  )
default

Copy constructor (requires copyable Key and T).

◆ FlatMap() [6/6]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
Aleph::FlatMap< Key, T, Compare >::FlatMap ( FlatMap< Key, T, Compare > &&  )
defaultnoexcept

Move constructor. The source is left valid but unspecified.

◆ ~FlatMap()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
Aleph::FlatMap< Key, T, Compare >::~FlatMap ( )
default

Member Function Documentation

◆ at() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
T & Aleph::FlatMap< Key, T, Compare >::at ( const Key &  k)
inline

Checked access to the value mapped to k.

Parameters
kKey to search for.
Returns
Mutable reference to the mapped value.
Exceptions
std::out_of_rangeif k is not mapped.
Note
Complexity: O(log n).

Definition at line 835 of file tpl_flat_map.H.

References ah_out_of_range_error_if, k, Aleph::FlatMap< Key, T, Compare >::lower_idx(), Aleph::FlatMap< Key, T, Compare >::match_at(), and Aleph::FlatMap< Key, T, Compare >::vals_.

Referenced by TEST(), TEST(), and TEST().

◆ at() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const T & Aleph::FlatMap< Key, T, Compare >::at ( const Key &  k) const
inline

◆ begin() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const_iterator Aleph::FlatMap< Key, T, Compare >::begin ( ) const
inlinenoexcept

Const iterator to the entry with the smallest key. O(1).

Definition at line 1057 of file tpl_flat_map.H.

References Aleph::MemArray< T >::get_ptr(), Aleph::FlatMap< Key, T, Compare >::keys_, and Aleph::FlatMap< Key, T, Compare >::vals_.

◆ begin() [2/2]

◆ capacity()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
size_t Aleph::FlatMap< Key, T, Compare >::capacity ( ) const
inlinenoexcept

Return the capacity of the backing key array. O(1).

Definition at line 706 of file tpl_flat_map.H.

References Aleph::MemArray< T >::capacity(), and Aleph::FlatMap< Key, T, Compare >::keys_.

◆ cbegin()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const_iterator Aleph::FlatMap< Key, T, Compare >::cbegin ( ) const
inlinenoexcept

Const iterator to the entry with the smallest key. O(1).

Definition at line 1069 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::begin().

◆ cend()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const_iterator Aleph::FlatMap< Key, T, Compare >::cend ( ) const
inlinenoexcept

Const iterator past the entry with the greatest key. O(1).

Definition at line 1075 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::end().

◆ clear()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
void Aleph::FlatMap< Key, T, Compare >::clear ( )
inlinenoexcept

Remove all entries. Alias of empty(). Capacity is kept.

Definition at line 736 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::empty().

◆ contains()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
bool Aleph::FlatMap< Key, T, Compare >::contains ( const Key &  k) const
inline

Test whether key k is mapped.

Parameters
kKey to search for.
Returns
true if an equivalent key is stored.
Note
Complexity: O(log n).

Definition at line 766 of file tpl_flat_map.H.

References k, Aleph::FlatMap< Key, T, Compare >::lower_idx(), and Aleph::FlatMap< Key, T, Compare >::match_at().

Referenced by Aleph::FlatMap< Key, T, Compare >::count(), TEST(), and TEST().

◆ count()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
size_t Aleph::FlatMap< Key, T, Compare >::count ( const Key &  k) const
inline

Count entries with key k (0 or 1).

Parameters
kKey to search for.
Returns
1 if present, 0 otherwise.
Note
Complexity: O(log n).

Definition at line 776 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::contains(), and k.

◆ drop_last()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
void Aleph::FlatMap< Key, T, Compare >::drop_last ( )
inlineprivate

◆ emplace()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
template<class... KArgs>
std::pair< iterator, bool > Aleph::FlatMap< Key, T, Compare >::emplace ( KArgs &&...  kargs)
inline

Build a key from kargs and insert it with a default value.

Convenience over insert(Key(kargs...), T()): the key is constructed once and moved into position; the value is default-constructed.

Template Parameters
KArgsArgument types forwarded to Key's constructor.
Parameters
kargsArguments for the key.
Returns
Pair of (iterator to the entry, insertion flag).
Exceptions
std::bad_allocif a growing reallocation fails.

Definition at line 1005 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::insert().

◆ empty()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
void Aleph::FlatMap< Key, T, Compare >::empty ( )
inlinenoexcept

Remove all entries (Aleph convention).

Capacity is kept.

Note
Following the Aleph container convention (see Array::empty()), this mutates the map; use is_empty() to test for emptiness.

Definition at line 729 of file tpl_flat_map.H.

References Aleph::MemArray< T >::empty(), Aleph::FlatMap< Key, T, Compare >::keys_, and Aleph::FlatMap< Key, T, Compare >::vals_.

Referenced by Aleph::FlatMap< Key, T, Compare >::clear(), and Aleph::FlatMap< Key, T, Compare >::keeping_invariants().

◆ end() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const_iterator Aleph::FlatMap< Key, T, Compare >::end ( ) const
inlinenoexcept

◆ end() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
iterator Aleph::FlatMap< Key, T, Compare >::end ( )
inlinenoexcept

◆ equal_range() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
std::pair< iterator, iterator > Aleph::FlatMap< Key, T, Compare >::equal_range ( const Key &  k)
inline

Range of entries with key equivalent to k.

Parameters
kKey to bound.
Returns
Pair {lower_bound(k), upper_bound(k)} (length 0 or 1).
Note
Complexity: O(log n).

Definition at line 818 of file tpl_flat_map.H.

References k, Aleph::FlatMap< Key, T, Compare >::lower_bound(), and Aleph::FlatMap< Key, T, Compare >::upper_bound().

◆ equal_range() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
std::pair< const_iterator, const_iterator > Aleph::FlatMap< Key, T, Compare >::equal_range ( const Key &  k) const
inline

See equal_range(const Key & k).

Definition at line 824 of file tpl_flat_map.H.

References k, Aleph::FlatMap< Key, T, Compare >::lower_bound(), and Aleph::FlatMap< Key, T, Compare >::upper_bound().

◆ erase() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
size_t Aleph::FlatMap< Key, T, Compare >::erase ( const Key &  k)
inline

Remove the entry with key k, if present.

Parameters
kKey to remove.
Returns
Number of removed entries (0 or 1).
Exceptions
Anyexception thrown by moving an entry; the map is then left empty (see the class notes on exception safety).
Note
Complexity: O(log n) search + O(n) shift. If keys and values cannot throw when moved, the removal itself never throws and a failed shrinking reallocation is ignored.

Definition at line 1019 of file tpl_flat_map.H.

References k, Aleph::FlatMap< Key, T, Compare >::lower_idx(), Aleph::FlatMap< Key, T, Compare >::match_at(), and Aleph::FlatMap< Key, T, Compare >::remove_at().

Referenced by TEST().

◆ erase() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
iterator Aleph::FlatMap< Key, T, Compare >::erase ( const_iterator  it)
inline

Remove the entry at iterator it.

Parameters
itValid dereferenceable iterator into this map.
Returns
Iterator to the entry following the removed one.
Exceptions
std::out_of_rangeif it does not point into the map.
Note
Complexity: O(n) shift. All other iterators are invalidated.

Definition at line 1034 of file tpl_flat_map.H.

References ah_out_of_range_error_if, Aleph::FlatMap< Key, T, Compare >::begin(), Aleph::MemArray< T >::get_ptr(), Aleph::FlatMap< Key, T, Compare >::basic_iterator< IsConst >::key_ptr(), Aleph::FlatMap< Key, T, Compare >::keys_, Aleph::FlatMap< Key, T, Compare >::remove_at(), and Aleph::FlatMap< Key, T, Compare >::size().

◆ find() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
iterator Aleph::FlatMap< Key, T, Compare >::find ( const Key &  k)
inline

Find the entry with key k.

Parameters
kKey to search for.
Returns
Iterator to the matching entry, or end() if absent.
Note
Complexity: O(log n) over the contiguous key array.

Definition at line 748 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::begin(), Aleph::FlatMap< Key, T, Compare >::end(), k, Aleph::FlatMap< Key, T, Compare >::lower_idx(), and Aleph::FlatMap< Key, T, Compare >::match_at().

◆ find() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const_iterator Aleph::FlatMap< Key, T, Compare >::find ( const Key &  k) const
inline

◆ grow_array()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
template<class U >
void Aleph::FlatMap< Key, T, Compare >::grow_array ( MemArray< U > &  arr,
const size_t  cap 
)
inlineprivate

◆ grow_to()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
void Aleph::FlatMap< Key, T, Compare >::grow_to ( const size_t  cap)
inlineprivate

◆ insert() [1/4]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
std::pair< iterator, bool > Aleph::FlatMap< Key, T, Compare >::insert ( const Key &  k,
const T &  v 
)
inline

Insert (k, v) if k is not mapped.

Parameters
kKey to insert.
vValue to associate.
Returns
Pair of (iterator to the entry, true if inserted / false if the key was already mapped — the existing value is kept).
Exceptions
std::bad_allocif a growing reallocation fails.
Anyexception thrown by copying k or v (the map is left untouched) or by moving an entry (see the class notes on exception safety).
Note
Complexity: O(log n) search + O(n) shift. k and v may be entries of this map.

Definition at line 926 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::begin(), Aleph::FlatMap< Key, T, Compare >::insert_at(), k, Aleph::FlatMap< Key, T, Compare >::lower_idx(), and Aleph::FlatMap< Key, T, Compare >::match_at().

Referenced by Aleph::FlatMap< Key, T, Compare >::emplace(), Aleph::FlatMap< Key, T, Compare >::insert(), Aleph::FlatMap< Key, T, Compare >::insert(), TEST(), and TEST().

◆ insert() [2/4]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
std::pair< iterator, bool > Aleph::FlatMap< Key, T, Compare >::insert ( const std::pair< Key, T > &  p)
inline

Insert a (key, value) pair if the key is not mapped.

Parameters
pPair to insert.
Returns
Pair of (iterator to the entry, insertion flag).
Exceptions
std::bad_allocif a growing reallocation fails.

Definition at line 950 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::insert().

◆ insert() [3/4]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
std::pair< iterator, bool > Aleph::FlatMap< Key, T, Compare >::insert ( Key &&  k,
T &&  v 
)
inline

◆ insert() [4/4]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
std::pair< iterator, bool > Aleph::FlatMap< Key, T, Compare >::insert ( std::pair< Key, T > &&  p)
inline

See insert(const std::pair<Key, T> & p); moving the pair.

Definition at line 956 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::insert().

◆ insert_at()

◆ insert_or_assign() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
std::pair< iterator, bool > Aleph::FlatMap< Key, T, Compare >::insert_or_assign ( const Key &  k,
const T &  v 
)
inline

Insert (k, v) or overwrite the value if k is mapped.

Parameters
kKey to insert or update.
vValue to associate.
Returns
Pair of (iterator to the entry, true if inserted / false if an existing value was overwritten).
Exceptions
std::bad_allocif a growing reallocation fails.
Note
Complexity: O(log n) on update, O(n) on insertion.

Definition at line 969 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::begin(), Aleph::FlatMap< Key, T, Compare >::insert_at(), k, Aleph::FlatMap< Key, T, Compare >::lower_idx(), Aleph::FlatMap< Key, T, Compare >::match_at(), and Aleph::FlatMap< Key, T, Compare >::vals_.

◆ insert_or_assign() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
std::pair< iterator, bool > Aleph::FlatMap< Key, T, Compare >::insert_or_assign ( Key &&  k,
T &&  v 
)
inline

◆ is_empty()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
bool Aleph::FlatMap< Key, T, Compare >::is_empty ( ) const
inlinenoexcept

Return true if the map holds no entries. O(1).

Definition at line 700 of file tpl_flat_map.H.

References Aleph::MemArray< T >::is_empty(), and Aleph::FlatMap< Key, T, Compare >::keys_.

◆ keeping_invariants()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
template<class Op >
void Aleph::FlatMap< Key, T, Compare >::keeping_invariants ( Op &&  op)
inlineprivate

◆ keys()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
DynList< Key > Aleph::FlatMap< Key, T, Compare >::keys ( ) const
inline

Copy all keys, in ascending order, into a DynList.

Returns
List of keys sorted according to Compare.
Exceptions
std::bad_allocif memory allocation fails.
Note
Complexity: O(n) copies.

Definition at line 1103 of file tpl_flat_map.H.

References Aleph::DynList< T >::append(), Aleph::MemArray< T >::get_ptr(), Aleph::FlatMap< Key, T, Compare >::keys_, and Aleph::FlatMap< Key, T, Compare >::size().

◆ keys_data()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const Key * Aleph::FlatMap< Key, T, Compare >::keys_data ( ) const
inlinenoexcept

Pointer to the sorted, contiguous key storage. O(1).

Definition at line 1081 of file tpl_flat_map.H.

References Aleph::MemArray< T >::get_ptr(), and Aleph::FlatMap< Key, T, Compare >::keys_.

◆ lower_bound() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
iterator Aleph::FlatMap< Key, T, Compare >::lower_bound ( const Key &  k)
inline

First entry whose key is not less than k.

Parameters
kKey to bound.
Returns
Iterator to the bound, or end().
Note
Complexity: O(log n).

Definition at line 786 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::begin(), k, and Aleph::FlatMap< Key, T, Compare >::lower_idx().

Referenced by Aleph::FlatMap< Key, T, Compare >::equal_range(), and Aleph::FlatMap< Key, T, Compare >::equal_range().

◆ lower_bound() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const_iterator Aleph::FlatMap< Key, T, Compare >::lower_bound ( const Key &  k) const
inline

See lower_bound(const Key & k).

Definition at line 792 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::begin(), k, and Aleph::FlatMap< Key, T, Compare >::lower_idx().

◆ lower_idx()

◆ make_room()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
void Aleph::FlatMap< Key, T, Compare >::make_room ( const size_t  pos)
inlineprivate

◆ match_at()

◆ nth_key()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const Key & Aleph::FlatMap< Key, T, Compare >::nth_key ( size_t  i) const
inline

Key of the i-th entry in sorted order (checked).

Parameters
iZero-based rank.
Returns
Const reference to the i-th key.
Exceptions
std::out_of_rangeif i >= size().
Note
Complexity: O(1).

Definition at line 890 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::keys_.

Referenced by TEST(), and TEST().

◆ nth_value() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
T & Aleph::FlatMap< Key, T, Compare >::nth_value ( size_t  i)
inline

Value of the i-th entry in sorted order (checked).

Parameters
iZero-based rank.
Returns
Mutable reference to the i-th value.
Exceptions
std::out_of_rangeif i >= size().
Note
Complexity: O(1).

Definition at line 901 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::vals_.

Referenced by TEST(), and TEST().

◆ nth_value() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const T & Aleph::FlatMap< Key, T, Compare >::nth_value ( size_t  i) const
inline

◆ operator!=()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
bool Aleph::FlatMap< Key, T, Compare >::operator!= ( const FlatMap< Key, T, Compare > &  m) const
inline

Inequality: negation of operator==.

Definition at line 1180 of file tpl_flat_map.H.

References m.

◆ operator=() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
FlatMap & Aleph::FlatMap< Key, T, Compare >::operator= ( const FlatMap< Key, T, Compare > &  m)
inline

Copy assignment (requires copyable Key and T).

The copy is built apart and then swapped in, so a failure while copying the keys or the values leaves this map as it was.

Parameters
mMap to copy.
Returns
*this.
Exceptions
std::bad_allocor any exception thrown by copying an entry.

Definition at line 666 of file tpl_flat_map.H.

References m, and Aleph::FlatMap< Key, T, Compare >::swap().

◆ operator=() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
FlatMap & Aleph::FlatMap< Key, T, Compare >::operator= ( FlatMap< Key, T, Compare > &&  )
defaultnoexcept

Move assignment. The source is left valid but unspecified.

◆ operator==()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
bool Aleph::FlatMap< Key, T, Compare >::operator== ( const FlatMap< Key, T, Compare > &  m) const
inline

Equality: same size and pairwise equal keys and values.

Parameters
mMap to compare against.
Returns
true if both maps hold equal entries in the same order.
Note
Complexity: O(n). Requires Key and T equality comparable.

Definition at line 1172 of file tpl_flat_map.H.

References Aleph::and, Aleph::MemArray< T >::get_ptr(), Aleph::FlatMap< Key, T, Compare >::keys_, m, OhashCommon< HashTbl, Key >::size(), Aleph::FlatMap< Key, T, Compare >::size(), and Aleph::FlatMap< Key, T, Compare >::vals_.

◆ operator[]() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
T & Aleph::FlatMap< Key, T, Compare >::operator[] ( const Key &  k)
inline

Access the value mapped to k, inserting a default if absent.

Parameters
kKey to search for or insert.
Returns
Mutable reference to the mapped value.
Exceptions
std::bad_allocif a growing reallocation fails.
Note
Complexity: O(log n) on hit, O(n) on insertion.

Definition at line 856 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::insert_at(), k, Aleph::FlatMap< Key, T, Compare >::lower_idx(), Aleph::FlatMap< Key, T, Compare >::match_at(), and Aleph::FlatMap< Key, T, Compare >::vals_.

◆ operator[]() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
T & Aleph::FlatMap< Key, T, Compare >::operator[] ( Key &&  k)
inline

Access the value mapped to k, inserting a default if absent.

Same as the const-reference overload, but moves k on insertion.

Parameters
kKey to search for or insert (moved on insertion).
Returns
Mutable reference to the mapped value.
Exceptions
std::bad_allocif a growing reallocation fails.

Definition at line 873 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::insert_at(), k, Aleph::FlatMap< Key, T, Compare >::lower_idx(), Aleph::FlatMap< Key, T, Compare >::match_at(), and Aleph::FlatMap< Key, T, Compare >::vals_.

◆ relocate()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
template<class U >
static void Aleph::FlatMap< Key, T, Compare >::relocate ( MemArray< U > &  arr,
MemArray< U > &  fresh 
)
inlinestaticprivate

◆ remove_at()

◆ reserve()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
void Aleph::FlatMap< Key, T, Compare >::reserve ( size_t  cap)
inline

Reserve capacity for at least cap entries.

Parameters
capDesired capacity (rounded up to a power of two).
Exceptions
std::bad_allocif memory allocation fails; the map keeps its entries.
std::overflow_errorif cap cannot be rounded up to a power of two; the map keeps its entries.
Anyexception thrown by moving an entry; the map is then left empty (see the class notes on exception safety).

Definition at line 720 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::grow_to().

◆ size()

◆ sort_and_unique()

◆ swap()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
void Aleph::FlatMap< Key, T, Compare >::swap ( FlatMap< Key, T, Compare > &  m)
inlinenoexcept

◆ traverse() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
template<class Operation >
bool Aleph::FlatMap< Key, T, Compare >::traverse ( Operation  operation)
inline

Traverse entries in key order while operation returns true.

Aleph-style bounded traversal: operation receives (const Key &, T &) in ascending key order; returning false stops the walk.

Template Parameters
OperationCallable bool(const Key &, T &).
Parameters
operationOperation to apply.
Returns
true if all entries were visited, false if stopped early.

Definition at line 1136 of file tpl_flat_map.H.

References Aleph::MemArray< T >::get_ptr(), Aleph::FlatMap< Key, T, Compare >::keys_, m, Aleph::FlatMap< Key, T, Compare >::size(), and Aleph::FlatMap< Key, T, Compare >::vals_.

Referenced by TEST().

◆ traverse() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
template<class Operation >
bool Aleph::FlatMap< Key, T, Compare >::traverse ( Operation  operation) const
inline

Const traversal in key order while operation returns true.

Template Parameters
OperationCallable bool(const Key &, const T &).
Parameters
operationOperation to apply.
Returns
true if all entries were visited, false if stopped early.

Definition at line 1154 of file tpl_flat_map.H.

References Aleph::MemArray< T >::get_ptr(), Aleph::FlatMap< Key, T, Compare >::keys_, m, Aleph::FlatMap< Key, T, Compare >::size(), and Aleph::FlatMap< Key, T, Compare >::vals_.

◆ upper_bound() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
iterator Aleph::FlatMap< Key, T, Compare >::upper_bound ( const Key &  k)
inline

First entry whose key is greater than k.

Parameters
kKey to bound.
Returns
Iterator to the bound, or end().
Note
Complexity: O(log n).

Definition at line 802 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::begin(), k, and Aleph::FlatMap< Key, T, Compare >::upper_idx().

Referenced by Aleph::FlatMap< Key, T, Compare >::equal_range(), and Aleph::FlatMap< Key, T, Compare >::equal_range().

◆ upper_bound() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const_iterator Aleph::FlatMap< Key, T, Compare >::upper_bound ( const Key &  k) const
inline

See upper_bound(const Key & k).

Definition at line 808 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::begin(), k, and Aleph::FlatMap< Key, T, Compare >::upper_idx().

◆ upper_idx()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
size_t Aleph::FlatMap< Key, T, Compare >::upper_idx ( const Key &  k) const
inlineprivate

◆ values()

template<typename Key , typename T , class Compare = Aleph::less<Key>>
DynList< T > Aleph::FlatMap< Key, T, Compare >::values ( ) const
inline

Copy all values, in ascending key order, into a DynList.

Returns
List of values ordered by their keys.
Exceptions
std::bad_allocif memory allocation fails.
Note
Complexity: O(n) copies.

Definition at line 1117 of file tpl_flat_map.H.

References Aleph::DynList< T >::append(), Aleph::FlatMap< Key, T, Compare >::size(), and Aleph::FlatMap< Key, T, Compare >::vals_.

◆ values_data() [1/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
const T * Aleph::FlatMap< Key, T, Compare >::values_data ( ) const
inlinenoexcept

See values_data().

Definition at line 1093 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::vals_.

◆ values_data() [2/2]

template<typename Key , typename T , class Compare = Aleph::less<Key>>
T * Aleph::FlatMap< Key, T, Compare >::values_data ( )
inlinenoexcept

Pointer to the contiguous value storage (parallel to keys). O(1).

Definition at line 1087 of file tpl_flat_map.H.

References Aleph::FlatMap< Key, T, Compare >::vals_.

Member Data Documentation

◆ cmp_

◆ keys_

◆ nothrow_relocation

template<typename Key , typename T , class Compare = Aleph::less<Key>>
constexpr bool Aleph::FlatMap< Key, T, Compare >::nothrow_relocation
staticconstexprprivate
Initial value:
=
std::is_nothrow_move_constructible_v<Key> and
std::is_nothrow_move_assignable_v<Key> and
std::is_nothrow_move_constructible_v<T> and
std::is_nothrow_move_assignable_v<T>
and
Check uniqueness with explicit hash + equality functors.

Definition at line 195 of file tpl_flat_map.H.

Referenced by Aleph::FlatMap< Key, T, Compare >::drop_last(), Aleph::FlatMap< Key, T, Compare >::grow_array(), and Aleph::FlatMap< Key, T, Compare >::keeping_invariants().

◆ vals_


The documentation for this class was generated from the following file: