71#include <initializer_list>
126template <
typename Key,
typename T,
class Compare = Aleph::less<Key>>
139 const size_t mid = lo + ((hi - lo) >> 1);
154 const size_t mid = lo + ((hi - lo) >> 1);
182 for (
size_t i =
keys_.
size() - 1; i > pos; --i)
184 kp[i] = std::move(
kp[i - 1]);
185 vp[i] = std::move(
vp[i - 1]);
196 for (
size_t i = pos; i < last; ++i)
198 kp[i] = std::move(
kp[i + 1]);
199 vp[i] = std::move(
vp[i + 1]);
205 catch (
const std::bad_alloc &)
212 catch (
const std::bad_alloc &)
225 for (
size_t i = 0; i <
m; ++i)
226 tmp.put(std::pair<Key, T>(std::move(
kp[i]), std::move(
vp[i])));
228 std::pair<Key, T> *tp =
tmp.get_ptr();
229 timsort(tp,
m, [
this](
const auto &a,
const auto &b)
231 return cmp_(a.first, b.first);
235 for (
size_t i = 0; i <
m; ++i)
238 kp[
out] = std::move(tp[i].first);
239 vp[
out] = std::move(tp[i].second);
242 const size_t excess =
m -
out;
245 try { (
void)
keys_.
get(excess); }
catch (
const std::bad_alloc &)
247 try { (
void)
vals_.get(excess); }
catch (
const std::bad_alloc &)
271 template <
bool IsConst>
274 using VPtr = std::conditional_t<IsConst, const T *, T *>;
276 const Key *
k_ =
nullptr;
284 std::conditional_t<IsConst, const T &, T &>
second;
340 return {*(
k_ + i), *(
v_ + i)};
486 const std::remove_reference_t<std::iter_reference_t<It>> & p)
488 static_cast<bool>(first != last);
496 for (; first != last; ++first)
498 const auto &p = *first;
511 FlatMap(std::initializer_list<std::pair<Key, T>>
l,
const Compare &
cmp = Compare())
536 std::swap(
cmp_,
m.cmp_);
668 std::pair<const_iterator, const_iterator>
equal_range(
const Key &
k)
const
687 const T &
at(
const Key &
k)
const
707 vals_.get_ptr()[pos] =
T();
726 vals_.get_ptr()[pos] =
T();
770 std::pair<iterator, bool>
insert(
const Key &
k,
const T &v)
774 return {
begin() + pos,
false};
777 vals_.get_ptr()[pos] = v;
778 return {
begin() + pos,
true};
786 return {
begin() + pos,
false};
789 vals_.get_ptr()[pos] = std::move(v);
790 return {
begin() + pos,
true};
798 std::pair<iterator, bool>
insert(
const std::pair<Key, T> &p)
800 return insert(p.first, p.second);
804 std::pair<iterator, bool>
insert(std::pair<Key, T> &&p)
806 return insert(std::move(p.first), std::move(p.second));
823 return {
begin() + pos,
false};
827 vals_.get_ptr()[pos] = v;
828 return {
begin() + pos,
true};
837 vals_(pos) = std::move(v);
838 return {
begin() + pos,
false};
842 vals_.get_ptr()[pos] = std::move(v);
843 return {
begin() + pos,
true};
856 template <
class...
KArgs>
888 return begin() + pos;
938 return vals_.get_ptr();
944 return vals_.get_ptr();
956 for (
size_t i = 0; i <
size(); ++i)
969 const T *p =
vals_.get_ptr();
970 for (
size_t i = 0; i <
size(); ++i)
984 template <
class Operation>
989 const size_t m =
size();
990 for (
size_t i = 0; i <
m; ++i)
1002 template <
class Operation>
1007 const size_t m =
size();
1008 for (
size_t i = 0; i <
m; ++i)
1025 std::equal(
vals_.get_ptr(),
vals_.get_ptr() +
size(),
m.vals_.get_ptr());
1031 return not (*
this ==
m);
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.
Standard functor implementations and comparison objects.
size_t size_t int32_t * out
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Generic filter iterator wrapper.
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 std::pair< Key, T > &p)
Insert a (key, value) pair if the key is not mapped.
FlatMap(size_t cap=MemArray< Key >::Min_Dim)
Construct an empty map.
T & at(const Key &k)
Checked access to the value mapped to k.
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
std::pair< iterator, bool > insert(const Key &k, const T &v)
Insert (k, v) if k is not mapped.
size_t count(const Key &k) const
Count entries with key k (0 or 1).
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
iterator upper_bound(const Key &k)
First entry whose key is greater than k.
const_iterator lower_bound(const Key &k) const
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).
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.
const T & nth_value(size_t i) const
std::pair< iterator, bool > insert(Key &&k, T &&v)
const_iterator end() const noexcept
Const iterator past the entry with the greatest key. O(1).
const_iterator upper_bound(const Key &k) const
void empty() noexcept
Remove all entries (Aleph convention).
bool is_empty() const noexcept
Return true if the map holds no entries. O(1).
T * values_data() noexcept
Pointer to the contiguous value storage (parallel to keys). O(1).
std::pair< iterator, bool > insert(std::pair< Key, T > &&p)
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).
size_t lower_idx(const Key &k) const
iterator lower_bound(const Key &k)
First entry whose key is not less than k.
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< Key, T > Item_Type
Aleph convention: element type.
std::pair< iterator, iterator > equal_range(const Key &k)
Range of entries with key equivalent to k.
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(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)
basic_iterator< true > const_iterator
Const-value iterator.
std::pair< const_iterator, const_iterator > equal_range(const Key &k) const
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).
T & nth_value(size_t i)
Value of the i-th entry in sorted order (checked).
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).
const T & at(const Key &k) const
size_t size() const noexcept
Return the number of stored entries. O(1).
iterator find(const Key &k)
Find the entry with key k.
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.
Simple, scalable and fast dynamic array.
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_ptr() const noexcept
Return the current base of array.
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(const size_t i=1)
Remove i elements from the end.
void swap(MemArray &a) noexcept
Swap in constant time this with a
T & put(const T &item)
Put a copy of item at the end of sequence.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
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.
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
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)
Simple, scalable, contiguous dynamic array.
Comprehensive sorting algorithms and search utilities for Aleph-w.