|
datastructures 0.1.0
|
A persistent map type using an AVL tree as the underlying structure. More...
#include <persistent_map.hpp>
Classes | |
| struct | value_compare |
Public Types | |
| using | key_type = TKey |
| using | mapped_type = TValue |
| using | value_type = std::pair< const key_type, const mapped_type > |
| using | reference = value_type & |
| using | const_reference = const value_type & |
| using | key_compare = TKeyComp |
| using | allocator_type = TAlloc |
| using | alloc_traits = std::allocator_traits< allocator_type > |
| using | size_type = typename alloc_traits::size_type |
| using | difference_type = typename alloc_traits::difference_type |
| using | pointer = typename alloc_traits::pointer |
| using | const_pointer = typename alloc_traits::const_pointer |
| using | version_type = typename tree_type::version_type |
Public Member Functions | |
| persistent_map () | |
| persistent_map (const key_compare &comparator, const allocator_type &allocator=allocator_type{}) | |
| persistent_map (const allocator_type &allocator) | |
| persistent_map (const persistent_map &other) | |
| persistent_map & | operator= (const persistent_map &other) |
| persistent_map (persistent_map &&other) noexcept | |
| persistent_map & | operator= (persistent_map &&other) noexcept |
| ~persistent_map () noexcept | |
| allocator_type | get_allocator () const noexcept |
| Returns the associated allocator. | |
| key_compare | key_comp () const noexcept |
| Returns the function that compares keys. | |
| value_compare | value_comp () const noexcept |
| Returns the function that compares keys in the key, value pairs. | |
| std::vector< const mapped_type * > | list_in_order (version_type version) const noexcept |
| Returns a vector of values sorted by keys. | |
| const mapped_type * | find (const key_type &key, version_type version) const noexcept |
| Access specified item. | |
| bool | contains (const key_type &key, version_type version) const noexcept |
| Checks whether there exists an item with given key in the tree. | |
| bool | empty (version_type version) const noexcept |
| Checks whether the map contains any values in a given version. | |
| size_type | size (version_type version) const noexcept |
| Returns the current number of items in the given version. | |
| int | get_height (version_type version) const noexcept |
| Returns the height of the underlying tree in the given version. | |
| void | clear () noexcept |
| Erases the whole tree with all its versions. | |
| std::pair< bool, version_type > | insert (const value_type &item, version_type version) |
| Inserts an item. | |
| std::pair< bool, version_type > | insert (value_type &&item, version_type version) |
| Inserts an item. | |
| template<class TMapped > | |
| std::pair< bool, version_type > | insert_or_assign (const key_type &key, TMapped &&value, version_type version) |
| Inserts an item or assigns the value. | |
| template<class TMapped > | |
| std::pair< bool, version_type > | insert_or_assign (key_type &&key, TMapped &&value, version_type version) |
| Inserts an item or assigns the value. | |
| template<class... Args> | |
| std::pair< bool, version_type > | try_emplace (const key_type &key, version_type version, Args &&...args) |
| Construct an item in-place. | |
| template<class... Args> | |
| std::pair< bool, version_type > | try_emplace (key_type &&key, version_type version, Args &&...args) |
| Construct an item in-place. | |
| std::pair< bool, version_type > | erase (const key_type &key, version_type version) noexcept |
| Erases an item with the given key. | |
| void | swap (persistent_map &other) noexcept |
| Swaps the content. | |
| version_type | get_current_version () const noexcept |
| Returns the current verison. | |
| version_type | get_preceding_version (version_type version) const |
| Returns the preceding version to the given version. | |
| template<class TMapped > | |
| std::pair< bool, typename persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type > | insert_or_assign (const key_type &key, TMapped &&value, version_type version) |
| template<class TMapped > | |
| std::pair< bool, typename persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type > | insert_or_assign (key_type &&key, TMapped &&value, version_type version) |
| template<class... Args> | |
| std::pair< bool, typename persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type > | try_emplace (const key_type &key, version_type version, Args &&...args) |
| template<class... Args> | |
| std::pair< bool, typename persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type > | try_emplace (key_type &&key, version_type version, Args &&...args) |
A persistent map type using an AVL tree as the underlying structure.
This map type provides immutable updates while preserving access to all previously created versions. Each successful modifying operation creates a new version instead of changing the existing one.
The implementation uses structural sharing, so unchanged parts of the AVL tree are shared between versions and only the path affected by an update is copied.
When the equivalence relation derived from TKeyComp has an equivalence class with more than one element, then the first key to be inserted stays there until deleted. In other words, keys are immutable and aren't changed even by assignments.
Every version has a unique version ID. Version IDs are assigned sequentially starting from 0, while the initial empty version has the special ID -1. New versions can be based on any existing version, therefore the versions form a tree rather than a simple linear sequence.
References to values belonging to an existing version remain valid as long as the map object itself remains alive and the version is not deleted by .clear_all().
| TKey | Represents the keys in the map, it must support std::less comparisons or you must provide a custom one. |
| TValue | Represents the stored values, can be anything. |
| TKeyComp | The comparison struct, it must contain operator() which takes two keys as parameters and creates a weak ordering on them. |
| TAlloc | The allocator that can allocate the key, value pairs, must comply with the standard allocators, so that std::allocator_traits works on it. Internally it is rebound to allocate the nodes themselves. |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::alloc_traits = std::allocator_traits<allocator_type> |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::allocator_type = TAlloc |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::const_pointer = typename alloc_traits::const_pointer |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::const_reference = const value_type & |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::difference_type = typename alloc_traits::difference_type |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::key_compare = TKeyComp |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::key_type = TKey |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::mapped_type = TValue |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::pointer = typename alloc_traits::pointer |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::reference = value_type & |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::size_type = typename alloc_traits::size_type |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::value_type = std::pair<const key_type, const mapped_type> |
| using datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type = typename tree_type::version_type |
| datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::persistent_map | ( | ) |
|
explicit |
|
explicit |
| datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::persistent_map | ( | const persistent_map< TKey, TValue, TKeyComp, TAlloc > & | other | ) |
|
noexcept |
|
noexcept |
|
noexcept |
Erases the whole tree with all its versions.
Erases every node without allocating any more space and thus cannot throw an exception.
|
noexcept |
Checks whether there exists an item with given key in the tree.
|
noexcept |
Checks whether the map contains any values in a given version.
|
noexcept |
Erases an item with the given key.
Does nothing when the key does not exist, the version does not update.
|
noexcept |
Access specified item.
Returns a constant pointer to the value associated with the key, when such value does not exist returns a nullptr. The value cannot be changed this way, because all updates are immutable, if you want to update the value, use insert_or_assign with the same key and version.
|
noexcept |
Returns the associated allocator.
|
inlinenoexcept |
Returns the current verison.
|
noexcept |
Returns the height of the underlying tree in the given version.
|
inline |
Returns the preceding version to the given version.
| std::pair< bool, typename persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type > datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::insert | ( | const value_type & | item, |
| version_type | version | ||
| ) |
Inserts an item.
Tries to insert an item into the tree, when there already is an item with the given key does nothing. Returns true if and only if the insertion was performed. Copies the key, value pair.
| std::pair< bool, typename persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type > datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::insert | ( | value_type && | item, |
| version_type | version | ||
| ) |
Inserts an item.
Tries to insert an item into the tree, when there already is an item with the given key does nothing. Returns true if and only if the insertion was performed. Moves the key, value pair, if the insertion does not happen the values data are not invalidated.
| std::pair< bool, version_type > datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::insert_or_assign | ( | const key_type & | key, |
| TMapped && | value, | ||
| version_type | version | ||
| ) |
Inserts an item or assigns the value.
If a key equivalent to key already exists, assigns std::forward<M>(value) to the value belonging to key. Otherwise inserts a new value, constructing it from value_type(key, std::forward<M>(value)).
true if the item was inserted, false if it was updated. And the newest version id in this branch.| std::pair< bool, typename persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type > datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::insert_or_assign | ( | const key_type & | key, |
| TMapped && | value, | ||
| version_type | version | ||
| ) |
| std::pair< bool, version_type > datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::insert_or_assign | ( | key_type && | key, |
| TMapped && | value, | ||
| version_type | version | ||
| ) |
Inserts an item or assigns the value.
If a key equivalent to key already exists, assigns std::forward<M>(value) to the value belonging to key and the keys data are not invalidated. Otherwise inserts a new value, constructing it from value_type(std::move(key), std::forward<M>(value)).
true if the item was inserted, false if it was updated. And the newest version id in this branch.| std::pair< bool, typename persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type > datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::insert_or_assign | ( | key_type && | key, |
| TMapped && | value, | ||
| version_type | version | ||
| ) |
|
noexcept |
Returns the function that compares keys.
|
noexcept |
Returns a vector of values sorted by keys.
The values are returned as const pointers since they are immutable.
For an empty tree or invalid version returns an empty vector.
| persistent_map< TKey, TValue, TKeyComp, TAlloc > & datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::operator= | ( | const persistent_map< TKey, TValue, TKeyComp, TAlloc > & | other | ) |
|
noexcept |
|
noexcept |
Returns the current number of items in the given version.
|
noexcept |
Swaps the content.
| other | The map to swap the content with. |
| std::pair< bool, version_type > datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::try_emplace | ( | const key_type & | key, |
| version_type | version, | ||
| Args &&... | args | ||
| ) |
Construct an item in-place.
If there is no item with the given key, inserts a new item into the tree constructed in-place with the given args, otherwise does nothing.
The arguments are forwarded using std::forward<Args>(args)... into the constructor to retain their original types.
| std::pair< bool, typename persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type > datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::try_emplace | ( | const key_type & | key, |
| version_type | version, | ||
| Args &&... | args | ||
| ) |
| std::pair< bool, version_type > datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::try_emplace | ( | key_type && | key, |
| version_type | version, | ||
| Args &&... | args | ||
| ) |
Construct an item in-place.
If there is no item with the given key, inserts a new item into the tree constructed in-place with the given args, otherwise does nothing.
The arguments are forwarded using std::forward<Args>(args)... into the constructor to retain their original types. The key is also moved in this overload.
| std::pair< bool, typename persistent_map< TKey, TValue, TKeyComp, TAlloc >::version_type > datastructures::persistent_map< TKey, TValue, TKeyComp, TAlloc >::try_emplace | ( | key_type && | key, |
| version_type | version, | ||
| Args &&... | args | ||
| ) |
|
noexcept |
Returns the function that compares keys in the key, value pairs.