Flow 1.0.0
Flow project: Full implementation reference.
Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Related Functions | List of all members
flow::util::Linked_hash_map< Key, Mapped, Hash, Pred > Class Template Reference

An object of this class is a map that combines the lookup speed of a boost::unordered_map<> and ordering and iterator stability capabilities of an std::list<>. More...

#include <linked_hash_map.hpp>

Collaboration diagram for flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >:
[legend]

Public Types

using Value = std::pair< Key const, Mapped >
 Short-hand for key/mapped-value pairs stored in the structure. More...
 
using size_type = std::size_t
 Type for index into array of items, where items are all applicable objects including Values and Keys. More...
 
using difference_type = std::ptrdiff_t
 Type for difference of size_types. More...
 
using Iterator = typename Value_list::iterator
 Type for iterator pointing into a mutable structure of this type. More...
 
using Const_iterator = typename Value_list::const_iterator
 Type for iterator pointing into an immutable structure of this type. More...
 
using Reverse_iterator = typename Value_list::reverse_iterator
 Type for reverse iterator pointing into a mutable structure of this type. More...
 
using Const_reverse_iterator = typename Value_list::const_reverse_iterator
 Type for reverse iterator pointing into an immutable structure of this type. More...
 
using key_type = Key
 For container compliance (hence the irregular capitalization): Key type. More...
 
using mapped_type = Mapped
 For container compliance (hence the irregular capitalization): Mapped type. More...
 
using value_type = Value
 For container compliance (hence the irregular capitalization): Key/Mapped pair type. More...
 
using hasher = Hash
 For container compliance (hence the irregular capitalization): Hash type. More...
 
using key_equal = Pred
 For container compliance (hence the irregular capitalization): Pred type. More...
 
using pointer = Value *
 For container compliance (hence the irregular capitalization): pointer to Key/Mapped pair type. More...
 
using const_pointer = Value const *
 For container compliance (hence the irregular capitalization): pointer to const Key/Mapped pair type. More...
 
using reference = Value &
 For container compliance (hence the irregular capitalization): reference to Key/Mapped pair type. More...
 
using const_reference = Value const &
 For container compliance (hence the irregular capitalization): reference to const Key/Mapped pair type. More...
 
using iterator = Iterator
 For container compliance (hence the irregular capitalization): Iterator type. More...
 
using const_iterator = Const_iterator
 For container compliance (hence the irregular capitalization): Const_iterator type. More...
 

Public Member Functions

 Linked_hash_map (size_type n_buckets=size_type(-1), Hash const &hasher_instance=Hash(), Pred const &key_equal_instance=Pred())
 Constructs empty structure with some basic parameters. More...
 
 Linked_hash_map (std::initializer_list< Value > values, size_type n_buckets=size_type(-1), Hash const &hasher_instance=Hash(), Pred const &key_equal_instance=Pred())
 Constructs structure with some basic parameters, and values initialized from initializer list. More...
 
 Linked_hash_map (Linked_hash_map const &src)
 Constructs object that is a copy of the given source. More...
 
 Linked_hash_map (Linked_hash_map &&src)
 Constructs object by making it equal to the given source, while the given source becomes as-if default-cted. More...
 
Linked_hash_mapoperator= (Linked_hash_map const &src)
 Overwrites this object with a copy of the given source. More...
 
Linked_hash_mapoperator= (Linked_hash_map &&src)
 Overwrites this object making it equal to the given source, while the given source becomes as-if default-cted. More...
 
std::pair< Iterator, boolinsert (Value const &key_and_mapped)
 Attempts to insert the given key/mapped-value pair into the map. More...
 
Iterator find (Key const &key)
 Attempts to find value at the given key in the map. More...
 
Const_iterator find (Key const &key) const
 Attempts to find value at the given key in the map. More...
 
size_type count (Key const &key) const
 Returns the number of times a key is equivalent to the given one is present in the map: either 1 or 0. More...
 
Mapped & operator[] (Key const &key)
 Equivalent to insert(Value(key, Mapped())).first->second (but avoids unnecessarily invoking Mapped()/generally strives for better performance). More...
 
Valuefront ()
 Returns reference to mutable front ("newest") element in the structure; formally equivalent to *(this->newest()). More...
 
Valueback ()
 Returns reference to mutable back ("oldest") element in the structure; formally equivalent to *(--this->past_oldest()). More...
 
Value const & const_front () const
 Returns reference to immutable front ("newest") element in the structure; formally equivalent to *(this->const_newest()). More...
 
Value const & const_back () const
 Returns reference to immutable back ("oldest") element in the structure; formally equivalent to *(--this->const_past_oldest()). More...
 
void touch (Const_iterator const &it)
 Given a valid iterator into the structure, makes the pointed to element "newest" by moving it from wherever it is to be first in the iteration order. More...
 
bool touch (Key const &key)
 Given a key into the structure, makes the corresponding element "newest" by moving it from wherever it is to be first in the iteration order; or does nothing if no such key. More...
 
Iterator erase (Const_iterator const &it)
 Erases the element pointed to by the given valid iterator. More...
 
Iterator erase (Const_iterator const &it_newest, Const_iterator const &it_past_oldest)
 Erases all elements in the range [it_newest, it_past_oldest). More...
 
size_type erase (Key const &key)
 Erases the element with the given key, if it exists. More...
 
void clear ()
 Makes it so that size() == 0. More...
 
void swap (Linked_hash_map &other)
 Swaps the contents of this structure and other. More...
 
Iterator begin ()
 Synonym of newest(). More...
 
Iterator newest ()
 Returns first, a/k/a "newest," element's iterator. More...
 
Iterator end ()
 Synonym of past_oldest(). More...
 
Iterator past_oldest ()
 Returns one past last, a/k/a "oldest," element's iterator. More...
 
Const_iterator cbegin () const
 Synonym of const_newest(). More...
 
Const_iterator begin () const
 Synonym of cbegin(). More...
 
Const_iterator const_newest () const
 Returns first, a/k/a "newest," element's iterator (to immutable element). More...
 
Const_iterator cend () const
 Synonym of const_past_oldest(). More...
 
Const_iterator end () const
 Synonym of cend(). More...
 
Const_iterator const_past_oldest () const
 Returns one past last, a/k/a "oldest," element's iterator (to immutable element). More...
 
Reverse_iterator rbegin ()
 Synonym of oldest(). More...
 
Reverse_iterator oldest ()
 Returns first, a/k/a "oldest," element's reverse iterator. More...
 
Reverse_iterator rend ()
 Synonym of past_newest(). More...
 
Reverse_iterator past_newest ()
 Returns one past last, a/k/a "newest," element's reverse iterator. More...
 
Const_reverse_iterator crbegin () const
 Synonym of const_oldest(). More...
 
Const_reverse_iterator const_oldest () const
 Returns first, a/k/a "oldest," element's reverse iterator (to immutable element). More...
 
Const_reverse_iterator crend () const
 Synonym of const_past_newest(). More...
 
Const_reverse_iterator const_past_newest () const
 Returns one past last, a/k/a "newest," element's reverse iterator (to immutable element). More...
 
bool empty () const
 Returns true if and only if container is empty. More...
 
size_type size () const
 Returns number of elements stored. More...
 
size_type max_size () const
 Returns max number of elements that can be stored. More...
 

Private Types

using Value_list = std::list< Value >
 Short-hand for doubly linked list of (Key, Mapped) pairs. More...
 
using Value_list_iter = Iterator
 Short-hand for iterator into doubly linked list of (Key, Mapped) pairs. More...
 
using Value_list_const_iter = Const_iterator
 Short-hand for const iterator into doubly linked list of (Key, Mapped) pairs. More...
 
using Key_to_value_iter_map = boost::unordered_map< Key, Value_list_iter, Hash, Pred >
 Short-hand for a hash map that maps Key to iterator into doubly linked list of (Key, Mapped) pairs. More...
 

Private Member Functions

Iterator insert_impl (Value const &key_and_mapped)
 Helper that modifies m_value_list and m_keys_into_list_map so that key_and_mapped's copy is inserted into the structure. More...
 

Private Attributes

Value_list m_value_list
 The actual values – which, as in unordered_map<K, M>, are instances of Value = pair<Key, Mapped> – are stored in here, in the order in which user would iterate over them. More...
 
boost::movelib::unique_ptr< Key_to_value_iter_mapm_keys_into_list_map
 Maps each Key K that is in m_value_list to an iterator into m_value_list (note the iterator points to a Value instance, which itself contains a copy of K but also the Mapped value, in which the user likely has keen interest). More...
 

Related Functions

(Note that these are not member functions.)

template<typename Key , typename Mapped , typename Hash , typename Pred >
void swap (Linked_hash_map< Key, Mapped, Hash, Pred > &val1, Linked_hash_map< Key, Mapped, Hash, Pred > &val2)
 Equivalent to val1.swap(val2). More...
 

Detailed Description

template<typename Key, typename Mapped, typename Hash, typename Pred>
class flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >

An object of this class is a map that combines the lookup speed of a boost::unordered_map<> and ordering and iterator stability capabilities of an std::list<>.

The API is generally that of an unordered_map<>. The differences essentially all have to do with iterators. This map introduces a concept of "newness," which determines the iteration order. Moreover, every iterator remains valid except (of course) under erasure of the underlying element. Newness is defined as follows inductively: whenever an element is inserted, it is "newest," thus it is placed at the front of the iterator order. Furthermore, the methods touch() can be used to make any element "newest" (moved to the front of the iterator order). Iterator thus formed orders elements from newest to oldest (hence newest() is begin(), past_oldest() is end()).

Performance expectations: The best way to determine a method's time needs is to imagine what it must do. If it must perform a lookup by key, that is an unordered_map<> lookup resulting in an (at least internal) iterator. If it must insert an element, it is always inserted at the start of a list; and also into an unordered_map<>. If it must erase an element based on an iterator, that element is erased from a list based on that iterator; and also by key from an unordered_map<>. Iteration itself is iteration along a list. But essentially, every operation is either near constant time or constant time. In terms of space needs, this essentially stores the values themselves in a list; and also a copy of each key in an unordered_map<>, which also stores a pointer or list iterator per element.

Thread safety

Same as for unordered_map<>.

Template Parameters
KeyKey type. Same requirements and behavior as unordered_map<> counterpart. Also (is it "in particular"?): Key must be Assignable, which is STL-speak for: If Key x, y are objects of this type, then x = y; is valid and has all the usual semantics. (There are other requirements, but that's the "controversial" one of interest.) In particular, Key cannot be of the form T const – more commonly written as const T (but recall that, say, const char* = char const * really; which is therefore fine here).
MappedThe 2nd (satellite) part of the Value pair type. Same requirements and behavior as unordered_map<> counterpart. Colloquially, in a K->V map, this is V, while formally the values stored are (K, V) pairs.
HashHasher type. Same requirements and behavior as unordered_map<> counterpart. To get a hasher object, one must be able to call: Hash h = Hash(). To then hash a Key, one must be able to call h(key). Typically one will simply define a size_t hash_value(Key) function, which will be activated via the default value for this template parameter. Defaults to boost::hash<Key>.
PredEquality functor type. Same requirements and behavior as unordered_map<> counterpart. Once a functor object Pred e = Pred() is obtained, bool eq = e(a, b) must return whether a equals b, where a and b are keys. Typically operator==() will be used via the default template parameter. Defaults to std::equal_to<Key>.

Definition at line 75 of file linked_hash_map.hpp.

Member Typedef Documentation

◆ Const_iterator

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Const_iterator = typename Value_list::const_iterator

Type for iterator pointing into an immutable structure of this type.

Definition at line 102 of file linked_hash_map.hpp.

◆ const_iterator

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::const_iterator = Const_iterator

For container compliance (hence the irregular capitalization): Const_iterator type.

Definition at line 131 of file linked_hash_map.hpp.

◆ const_pointer

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::const_pointer = Value const *

For container compliance (hence the irregular capitalization): pointer to const Key/Mapped pair type.

Definition at line 123 of file linked_hash_map.hpp.

◆ const_reference

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::const_reference = Value const &

For container compliance (hence the irregular capitalization): reference to const Key/Mapped pair type.

Definition at line 127 of file linked_hash_map.hpp.

◆ Const_reverse_iterator

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Const_reverse_iterator = typename Value_list::const_reverse_iterator

Type for reverse iterator pointing into an immutable structure of this type.

Definition at line 108 of file linked_hash_map.hpp.

◆ difference_type

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::difference_type = std::ptrdiff_t

Type for difference of size_types.

Definition at line 96 of file linked_hash_map.hpp.

◆ hasher

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::hasher = Hash

For container compliance (hence the irregular capitalization): Hash type.

Definition at line 117 of file linked_hash_map.hpp.

◆ Iterator

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Iterator = typename Value_list::iterator

Type for iterator pointing into a mutable structure of this type.

Definition at line 99 of file linked_hash_map.hpp.

◆ iterator

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::iterator = Iterator

For container compliance (hence the irregular capitalization): Iterator type.

Definition at line 129 of file linked_hash_map.hpp.

◆ key_equal

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::key_equal = Pred

For container compliance (hence the irregular capitalization): Pred type.

Definition at line 119 of file linked_hash_map.hpp.

◆ Key_to_value_iter_map

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Key_to_value_iter_map = boost::unordered_map<Key, Value_list_iter, Hash, Pred>
private

Short-hand for a hash map that maps Key to iterator into doubly linked list of (Key, Mapped) pairs.

Definition at line 564 of file linked_hash_map.hpp.

◆ key_type

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::key_type = Key

For container compliance (hence the irregular capitalization): Key type.

Definition at line 111 of file linked_hash_map.hpp.

◆ mapped_type

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::mapped_type = Mapped

For container compliance (hence the irregular capitalization): Mapped type.

Definition at line 113 of file linked_hash_map.hpp.

◆ pointer

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::pointer = Value*

For container compliance (hence the irregular capitalization): pointer to Key/Mapped pair type.

Definition at line 121 of file linked_hash_map.hpp.

◆ reference

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::reference = Value&

For container compliance (hence the irregular capitalization): reference to Key/Mapped pair type.

Definition at line 125 of file linked_hash_map.hpp.

◆ Reverse_iterator

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Reverse_iterator = typename Value_list::reverse_iterator

Type for reverse iterator pointing into a mutable structure of this type.

Definition at line 105 of file linked_hash_map.hpp.

◆ size_type

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::size_type = std::size_t

Type for index into array of items, where items are all applicable objects including Values and Keys.

Definition at line 94 of file linked_hash_map.hpp.

◆ Value

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Value = std::pair<Key const, Mapped>

Short-hand for key/mapped-value pairs stored in the structure.

Definition at line 81 of file linked_hash_map.hpp.

◆ Value_list

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Value_list = std::list<Value>
private

Short-hand for doubly linked list of (Key, Mapped) pairs.

Definition at line 87 of file linked_hash_map.hpp.

◆ Value_list_const_iter

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Value_list_const_iter = Const_iterator
private

Short-hand for const iterator into doubly linked list of (Key, Mapped) pairs.

Definition at line 561 of file linked_hash_map.hpp.

◆ Value_list_iter

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Value_list_iter = Iterator
private

Short-hand for iterator into doubly linked list of (Key, Mapped) pairs.

Definition at line 558 of file linked_hash_map.hpp.

◆ value_type

template<typename Key , typename Mapped , typename Hash , typename Pred >
using flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::value_type = Value

For container compliance (hence the irregular capitalization): Key/Mapped pair type.

Definition at line 115 of file linked_hash_map.hpp.

Constructor & Destructor Documentation

◆ Linked_hash_map() [1/4]

template<typename Key , typename Mapped , typename Hash , typename Pred >
flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Linked_hash_map ( size_type  n_buckets = size_type(-1),
Hash const &  hasher_instance = Hash(),
Pred const &  key_equal_instance = Pred() 
)
explicit

Constructs empty structure with some basic parameters.

Parameters
n_bucketsNumber of buckets for the unordered (hash) table. Special value -1 (default) will cause us to use whatever unordered_map<> would use by default.
hasher_instanceInstance of the hash function type (hasher_instance(Key k) should be size_typed hash of key k).
key_equal_instanceInstance of the equality function type (key_equal_instance(Key k1, Key k2) should return true if and only if k1 equals k2).

Definition at line 622 of file linked_hash_map.hpp.

◆ Linked_hash_map() [2/4]

template<typename Key , typename Mapped , typename Hash , typename Pred >
flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Linked_hash_map ( std::initializer_list< Value values,
size_type  n_buckets = size_type(-1),
Hash const &  hasher_instance = Hash(),
Pred const &  key_equal_instance = Pred() 
)
explicit

Constructs structure with some basic parameters, and values initialized from initializer list.

The values are inserted as if insert(v) was called for each pair v in values in reverse order. Since the canonical ordering places the newest (last inserted/touch()ed) element at the front of the ordering, that means that forward iteration through the set (right after this constructor runs) will yield values in the same order as in initializer list values.

Parameters
valuesValues with which to fill the structure after initializing it. Typically you'd provide a series of key/value pairs like this: {{ key1, value1 }, { key2, value2 }, ...}. They will appear in iterated sequence in the same order as they appear in this list.
n_bucketsSee other constructor.
hasher_instanceSee other constructor.
key_equal_instanceSee other constructor.

Definition at line 631 of file linked_hash_map.hpp.

References flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::m_keys_into_list_map, and flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::m_value_list.

◆ Linked_hash_map() [3/4]

template<typename Key , typename Mapped , typename Hash , typename Pred >
flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Linked_hash_map ( Linked_hash_map< Key, Mapped, Hash, Pred > const &  src)

Constructs object that is a copy of the given source.

Equivalent to operator=(src).

Parameters
srcSource object.

Definition at line 661 of file linked_hash_map.hpp.

References flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::operator=().

Here is the call graph for this function:

◆ Linked_hash_map() [4/4]

template<typename Key , typename Mapped , typename Hash , typename Pred >
flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Linked_hash_map ( Linked_hash_map< Key, Mapped, Hash, Pred > &&  src)

Constructs object by making it equal to the given source, while the given source becomes as-if default-cted.

Parameters
srcSource object which is emptied.

Definition at line 668 of file linked_hash_map.hpp.

References flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::operator=().

Here is the call graph for this function:

Member Function Documentation

◆ back()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Value & flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::back

Returns reference to mutable back ("oldest") element in the structure; formally equivalent to *(--this->past_oldest()).

All other comments for front() apply analogously.

Returns
Reference to mutable Mapped value directly inside the data structure; or to undefined location if currently empty().

Definition at line 845 of file linked_hash_map.hpp.

◆ begin() [1/2]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::begin

Synonym of newest().

Returns
See newest().

Definition at line 951 of file linked_hash_map.hpp.

◆ begin() [2/2]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::begin

Synonym of cbegin().

Exists to satisfy the C++11 rangy stuff (which makes for(:) – and other magic – work).

Returns
See cbegin().

Definition at line 986 of file linked_hash_map.hpp.

◆ cbegin()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::cbegin

Synonym of const_newest().

Exists as standard container method (hence the odd formatting).

Returns
See const_newest().

Definition at line 979 of file linked_hash_map.hpp.

◆ cend()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::cend

Synonym of const_past_oldest().

Exists as standard container method (hence the odd formatting).

Returns
See const_past_oldest().

Definition at line 1000 of file linked_hash_map.hpp.

◆ clear()

template<typename Key , typename Mapped , typename Hash , typename Pred >
void flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::clear

Makes it so that size() == 0.

Definition at line 926 of file linked_hash_map.hpp.

◆ const_back()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Value const & flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::const_back

Returns reference to immutable back ("oldest") element in the structure; formally equivalent to *(--this->const_past_oldest()).

All other comments for const_front() apply analogously.

Returns
Reference to immutable Mapped value directly inside the data structure; or to undefined location if currently empty().

Definition at line 861 of file linked_hash_map.hpp.

◆ const_front()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Value const & flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::const_front

Returns reference to immutable front ("newest") element in the structure; formally equivalent to *(this->const_newest()).

OK to call when empty(); but behavior undefined if you attempt to access the result in any way if either empty() when this was called; or if !empty() at that time, but the underlying element is erased at time of access. If not empty() when this was called, then resulting reference continues to be valid as long as the underlying element is not erased; however, in the future the reference (while referring to the same element) may not refer to front ("newest") element any longer. (Informally, most uses would only call front() when !empty(), and would access it immediately and but once. However, I'm listing the corner cases above.)

Returns
Reference to immutable Mapped value directly inside the data structure; or to undefined location if currently empty().

Definition at line 853 of file linked_hash_map.hpp.

◆ const_newest()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::const_newest

Returns first, a/k/a "newest," element's iterator (to immutable element).

Returns
Ditto.

Definition at line 972 of file linked_hash_map.hpp.

◆ const_oldest()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_reverse_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::const_oldest

Returns first, a/k/a "oldest," element's reverse iterator (to immutable element).

Returns
Ditto.

Definition at line 1042 of file linked_hash_map.hpp.

◆ const_past_newest()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_reverse_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::const_past_newest

Returns one past last, a/k/a "newest," element's reverse iterator (to immutable element).

Returns
Ditto.

Definition at line 1056 of file linked_hash_map.hpp.

◆ const_past_oldest()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::const_past_oldest

Returns one past last, a/k/a "oldest," element's iterator (to immutable element).

Returns
Ditto.

Definition at line 993 of file linked_hash_map.hpp.

◆ count()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::size_type flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::count ( Key const &  key) const

Returns the number of times a key is equivalent to the given one is present in the map: either 1 or 0.

Parameters
keyKey whose equal to find.
Returns
0 or 1.

Definition at line 813 of file linked_hash_map.hpp.

◆ crbegin()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_reverse_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::crbegin

Synonym of const_oldest().

Returns
See const_oldest().

Definition at line 1049 of file linked_hash_map.hpp.

◆ crend()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_reverse_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::crend

Synonym of const_past_newest().

Exists as standard container method.

Returns
See const_past_newest().

Definition at line 1063 of file linked_hash_map.hpp.

◆ empty()

template<typename Key , typename Mapped , typename Hash , typename Pred >
bool flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::empty

Returns true if and only if container is empty.

Same performance as of unordered_map<>.

Returns
Ditto.

Definition at line 1076 of file linked_hash_map.hpp.

◆ end() [1/2]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::end

Synonym of past_oldest().

Exists as standard container method.

Returns
See past_oldest().

Definition at line 965 of file linked_hash_map.hpp.

◆ end() [2/2]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::end

Synonym of cend().

Exists to satisfy the C++11 rangy stuff (which makes for(:) – and other magic – work).

Returns
See cend().

Definition at line 1007 of file linked_hash_map.hpp.

◆ erase() [1/3]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::erase ( Const_iterator const &  it)

Erases the element pointed to by the given valid iterator.

Behavior undefined if it is not valid. it becomes invalid.

Parameters
itIterator of element to erase.
Returns
Iterator one position past (i.e., "older") than it, before *it was removed.

Definition at line 889 of file linked_hash_map.hpp.

◆ erase() [2/3]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::erase ( Const_iterator const &  it_newest,
Const_iterator const &  it_past_oldest 
)

Erases all elements in the range [it_newest, it_past_oldest).

Behavior undefined if a given iterator is invalid. it_newest becomes invalid.

Parameters
it_newestIterator of first ("newest") element to erase.
it_past_oldestIterator of one past last ("oldest") element to erase.
Returns
it_past_oldest copy.

Definition at line 897 of file linked_hash_map.hpp.

◆ erase() [3/3]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::size_type flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::erase ( Key const &  key)

Erases the element with the given key, if it exists.

Return value indicates various info of interest about what occurred or did not occur.

Parameters
keyKey such that its equal's (if found) element will be erased.
Returns
Number of elements erased (0 or 1).

Definition at line 910 of file linked_hash_map.hpp.

◆ find() [1/2]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::find ( Key const &  key)

Attempts to find value at the given key in the map.

Key presence is determined according to the Pred template parameter which determines equality of 2 given keys; and via the Hash template parameter that enables efficient hash-based lookup. The returned iterator (if valid) can be used to mutate the elements inside the map.

As long as the value is not removed from the map, the reference will continue to be valid.

Any subsequent writes to the referred to (by returned iterator) area of memory will NOT have the effect of touch(). If you need it, call touch() yourself.

Parameters
keyKey whose equal to find.
Returns
If found, iterator to the key/mapped-value pair with the equivalent key; else this->end().

Definition at line 797 of file linked_hash_map.hpp.

◆ find() [2/2]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Const_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::find ( Key const &  key) const

Attempts to find value at the given key in the map.

Key presence is determined according to the Pred template parameter which determines equality of 2 given keys; and via the Hash template parameter that enables efficient hash-based lookup. The returned iterator (if valid) cannot be used to mutate the elements inside the map.

As long as the value is not removed from the map, the iterator will continue to be valid.

Parameters
keyKey whose equal to find.
Returns
If found, iterator to the key/mapped-value pair with the equivalent key; else this->const_past_oldest().

Definition at line 805 of file linked_hash_map.hpp.

◆ front()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Value & flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::front

Returns reference to mutable front ("newest") element in the structure; formally equivalent to *(this->newest()).

OK to call when empty(); but behavior undefined if you attempt to access the result in any way if either empty() when this was called; or if !empty() at that time, but the underlying element is erased at time of access. If not empty() when this was called, then resulting reference continues to be valid as long as the underlying element is not erased; however, in the future the reference (while referring to the same element) might not refer to front ("newest") element any longer. (Informally, most uses would only call front() when !empty(), and would access it immediately and but once. However, I'm listing the corner cases above.)

Note that if Mapped& x is returned, then although x is mutable, in actuality x.first is const; so only x.second is truly mutable. You must not write to the key (such as via a const_cast<>); doing so will result in undefined behavior.

Returns
Reference to mutable value directly inside the data structure; or to undefined location if currently empty(). Note that only the Mapped part of Value is mutable.

Definition at line 837 of file linked_hash_map.hpp.

◆ insert()

template<typename Key , typename Mapped , typename Hash , typename Pred >
std::pair< typename Linked_hash_map< Key, Mapped, Hash, Pred >::Iterator, bool > flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::insert ( Value const &  key_and_mapped)

Attempts to insert the given key/mapped-value pair into the map.

If the key is already present in the map, does nothing. Return value indicates various info of interest about what occurred or did not occur. Key presence is determined according to the Pred template parameter which determines equality of 2 given keys; and via the Hash template parameter that enables efficient hash-based lookup. If inserted, the new element is considered "newest," as if by touch(). If not inserted, the existing element location is not affected.

Parameters
key_and_mappedThe key/mapped-value pair to attempt to insert. This value is copied, and the copy is inserted.
Returns
A pair whose second element is true if and only if the insertion occurred; and whose first element is an iterator pointing to either the newly inserted element or already present one with a key equal to key_and_mapped.first.

Definition at line 761 of file linked_hash_map.hpp.

Referenced by flow::net_flow::Node::close_connection_immediately().

Here is the caller graph for this function:

◆ insert_impl()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::insert_impl ( Value const &  key_and_mapped)
private

Helper that modifies m_value_list and m_keys_into_list_map so that key_and_mapped's copy is inserted into the structure.

Pre-condition is that key_and_mapped.first is not in the structure (else behavior undefined).

Parameters
key_and_mappedSame as in insert().
Returns
Same as in insert().first.

Definition at line 781 of file linked_hash_map.hpp.

◆ max_size()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::size_type flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::max_size

Returns max number of elements that can be stored.

Same performance as of unordered_map<> + list<>.

Returns
Ditto.

Definition at line 1083 of file linked_hash_map.hpp.

◆ newest()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::newest

Returns first, a/k/a "newest," element's iterator.

Returns
Ditto.

Definition at line 944 of file linked_hash_map.hpp.

◆ oldest()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Reverse_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::oldest

Returns first, a/k/a "oldest," element's reverse iterator.

Returns
Ditto.

Definition at line 1014 of file linked_hash_map.hpp.

◆ operator=() [1/2]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred > & flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::operator= ( Linked_hash_map< Key, Mapped, Hash, Pred > &&  src)

Overwrites this object making it equal to the given source, while the given source becomes as-if default-cted.

Parameters
srcSource object which is emptied (unless it is *this; then no-op).
Returns
*this.

Definition at line 749 of file linked_hash_map.hpp.

References flow::util::swap().

Here is the call graph for this function:

◆ operator=() [2/2]

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred > & flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::operator= ( Linked_hash_map< Key, Mapped, Hash, Pred > const &  src)

Overwrites this object with a copy of the given source.

We become equal to src but independent of it to the max extent possible (if you've got pointers stored in there, for example, the pointers are copied, not the values at those pointers). In addition, the hasher instance and equality predicate are copied from src. Finally, a reasonable attempt is made to also make the internal structure of the hash map to be similar to that of `src.

Parameters
srcSource object.
Returns
*this.

Definition at line 676 of file linked_hash_map.hpp.

References flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::m_keys_into_list_map, and flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::m_value_list.

Referenced by flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Linked_hash_map().

Here is the caller graph for this function:

◆ operator[]()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Mapped & flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::operator[] ( Key const &  key)

Equivalent to insert(Value(key, Mapped())).first->second (but avoids unnecessarily invoking Mapped()/generally strives for better performance).

Less formally, it either finds the value at the given key, or if not found inserts one with a default-constructed value; then returns reference to the in-structure stored Mapped value which can be used to to read and/or modify that value directly.

Note that if Mapped& x is returned, then although x is mutable, in actuality x.first is const; so only x.second is truly mutable. You must not write to the key (such as via a const_cast<>); doing so will result in undefined behavior.

If inserted, the new element is considered "newest," as if by touch(). If not inserted, the existing element location is not affected.

As long as the value is not removed from the map, the reference will continue to be valid.

Any subsequent writes to the referred to area of memory will NOT have the effect of touch(). If you need it, call touch() yourself.

Parameters
keyKey whose equal to find or insert if not found.
Returns
Reference to mutable Mapped value directly inside the data structure.

Definition at line 819 of file linked_hash_map.hpp.

◆ past_newest()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Reverse_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::past_newest

Returns one past last, a/k/a "newest," element's reverse iterator.

Returns
Ditto.

Definition at line 1028 of file linked_hash_map.hpp.

◆ past_oldest()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::past_oldest

Returns one past last, a/k/a "oldest," element's iterator.

Returns
Ditto.

Definition at line 958 of file linked_hash_map.hpp.

◆ rbegin()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Reverse_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::rbegin

Synonym of oldest().

Returns
See oldest().

Definition at line 1021 of file linked_hash_map.hpp.

◆ rend()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::Reverse_iterator flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::rend

Synonym of past_newest().

Exists as standard container method.

Returns
See past_newest().

Definition at line 1035 of file linked_hash_map.hpp.

◆ size()

template<typename Key , typename Mapped , typename Hash , typename Pred >
Linked_hash_map< Key, Mapped, Hash, Pred >::size_type flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::size

Returns number of elements stored.

Same performance as of unordered_map<>.

Returns
Ditto.

Definition at line 1070 of file linked_hash_map.hpp.

Referenced by flow::net_flow::Event_set::ev_type_to_socks_map_sizes_to_str().

Here is the caller graph for this function:

◆ swap()

template<typename Key , typename Mapped , typename Hash , typename Pred >
void flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::swap ( Linked_hash_map< Key, Mapped, Hash, Pred > &  other)

Swaps the contents of this structure and other.

This is a constant-time operation.

Parameters
otherThe other structure.

Definition at line 933 of file linked_hash_map.hpp.

References flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::m_keys_into_list_map, flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::m_value_list, and flow::util::swap().

Referenced by flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::swap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ touch() [1/2]

template<typename Key , typename Mapped , typename Hash , typename Pred >
void flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::touch ( Const_iterator const &  it)

Given a valid iterator into the structure, makes the pointed to element "newest" by moving it from wherever it is to be first in the iteration order.

Behavior undefined if iterator invalid.

The iterator continues to be valid.

Parameters
itIterator to an element of the structure.

Definition at line 868 of file linked_hash_map.hpp.

◆ touch() [2/2]

template<typename Key , typename Mapped , typename Hash , typename Pred >
bool flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::touch ( Key const &  key)

Given a key into the structure, makes the corresponding element "newest" by moving it from wherever it is to be first in the iteration order; or does nothing if no such key.

Return value indicates various info of interest about what occurred or did not occur.

Parameters
keyKey whose equal to find.
Returns
true if the key was found (even if it was already "newest"); false if not found.

Definition at line 874 of file linked_hash_map.hpp.

Friends And Related Function Documentation

◆ swap()

template<typename Key , typename Mapped , typename Hash , typename Pred >
void swap ( Linked_hash_map< Key, Mapped, Hash, Pred > &  val1,
Linked_hash_map< Key, Mapped, Hash, Pred > &  val2 
)
related

Equivalent to val1.swap(val2).

Parameters
val1Object.
val2Object.

Definition at line 1089 of file linked_hash_map.hpp.

References flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::swap().

Here is the call graph for this function:

Member Data Documentation

◆ m_keys_into_list_map

template<typename Key , typename Mapped , typename Hash , typename Pred >
boost::movelib::unique_ptr<Key_to_value_iter_map> flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::m_keys_into_list_map
private

Maps each Key K that is in m_value_list to an iterator into m_value_list (note the iterator points to a Value instance, which itself contains a copy of K but also the Mapped value, in which the user likely has keen interest).

This supplies the one capability m_value_list alone cannot: near-constant-time lookup of a Value or a Mapped by Key (a linear search would be necessary).

The unique_ptr<> wrapper remains constant after setting it to non-null. Why have it at all? Because in at least one constructor we are unable to determine all the constructor arguments by the time the constructor body executes, and we don't want to construct the map until then.

Performance

Anything they'll need to do to this map carries the same performance cost as if they used a straight unordered_map<>, so by definition it is acceptable. The only operation this does not provide is iteration and insertion in the proper order, and that's done through m_value_list instead.

Definition at line 614 of file linked_hash_map.hpp.

Referenced by flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Linked_hash_map(), flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::operator=(), and flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::swap().

◆ m_value_list

template<typename Key , typename Mapped , typename Hash , typename Pred >
Value_list flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::m_value_list
private

The actual values – which, as in unordered_map<K, M>, are instances of Value = pair<Key, Mapped> – are stored in here, in the order in which user would iterate over them.

If Value v is in this list, then no Value v1 == v can be elsewhere in the list. The order is semantically defined to be from "newest" to "oldest." Therefore, any newly inserted value goes at the START of the list. Similarly, any "touched" value is moved to the START of the list (see touch() and other methods that are documented as "touching" the referenced key). This ordering is what a normal unordered_map<K, M> would not supply (it's in the name!) but that we advertise.

Since m_keys_into_list_map stores keys, why store the keys here duplicately? Answer: that way we can expose iterators into m_value_list directly to the user; so that they can take an iterator I and directly access the key and mapped value via I->first and I->second, respectively – as is expected of any map container. This does, however, come at some memory cost.

Todo:
It is probably possible to cut down on the memory cost of storing, for each element, a copy of the Key in m_value_list (in addition to the mandatory one in the lookup table m_keys_into_list_map). Perhaps the key copy would be replaced by an iterator back into m_value_list. A custom iterator class would be necessary to properly dereference this (this is non-trivial given that operator*() would have to return a reference to a pair which is no longer stored anywhere in this hypothetical design). Moreover, iterators exposed to the user would become invalid the same way an unordered_map<> iterator does due to seemingly unrelated changes. Finally, the memory savings would not even exist for Key types roughly the size of a pointer. All in all, not a slam-dunk....

Performance

Moving a value from anywhere to either end of the list is a constant-time operation (assuming the source location's iterator is known). Hence touch() is constant-time. Moreover, touch() does NOT involve a copy of a Value (it only involves assigning, internally, a few linked list pointers). Also note that insertion is similarly constant-time (but does, necessarily, require a Value copy as for any container). Finally, erasure is also constant-time. These are the only operations needed.

Definition at line 597 of file linked_hash_map.hpp.

Referenced by flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::Linked_hash_map(), flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::operator=(), and flow::util::Linked_hash_map< Key, Mapped, Hash, Pred >::swap().


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