23#include <boost/unordered_map.hpp>
24#include <boost/move/unique_ptr.hpp>
74template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
81 using Value = std::pair<Key const, Mapped>;
99 using Iterator =
typename Value_list::iterator;
148 Hash
const & hasher_instance = Hash(),
149 Pred
const & key_equal_instance = Pred());
172 Hash
const & hasher_instance = Hash(),
173 Pred
const & key_equal_instance = Pred());
621template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
623 hasher const & hasher_instance,
630template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
633 hasher const & hasher_instance,
638 using boost::unordered_map;
644 unordered_map<Key, Mapped, Hash, Pred> dummy;
645 n_buckets = dummy.bucket_count();
656 (*m_keys_into_list_map)[value_list_it->first] = value_list_it;
660template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
667template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
674template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
717 using Mutable_key_value = pair<Key, Mapped>;
718 using Mutable_key_value_list = list<Mutable_key_value>;
720 Mutable_key_value_list*
const dst_list_ptr
721 =
reinterpret_cast<Mutable_key_value_list*
>(&m_value_list);
722 const Mutable_key_value_list*
const src_list_ptr
723 =
reinterpret_cast<const Mutable_key_value_list*
>(&src.
m_value_list);
725 *dst_list_ptr = *src_list_ptr;
737 for (
Value_list_iter value_list_it = m_value_list.begin(); value_list_it != m_value_list.end();
741 (*m_keys_into_list_map)[value_list_it->first] = value_list_it;
747template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
754 swap(std::move(src));
759template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
760std::pair<typename Linked_hash_map<Key, Mapped, Hash, Pred>::Iterator,
bool>
765 Key
const & key = key_and_mapped.first;
768 typename Key_to_value_iter_map::iterator
const map_it = m_keys_into_list_map->find(key);
769 if (map_it != m_keys_into_list_map->end())
772 return pair<Iterator, bool>(map_it->second,
false);
776 return pair<Iterator, bool>(insert_impl(key_and_mapped),
true);
779template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
785 m_value_list.push_front(key_and_mapped);
788 Iterator const new_elem_it = m_value_list.begin();
790 (*m_keys_into_list_map)[key_and_mapped.first] = new_elem_it;
795template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
799 typename Key_to_value_iter_map::iterator
const map_it = m_keys_into_list_map->find(key);
800 return (map_it == m_keys_into_list_map->end()) ? m_value_list.end() : map_it->second;
803template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
807 typename Key_to_value_iter_map::const_iterator
const map_it = m_keys_into_list_map->find(key);
808 return (map_it == m_keys_into_list_map->cend()) ? m_value_list.cend() : map_it->second;
811template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
815 return m_keys_into_list_map->count(key);
818template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
824 typename Key_to_value_iter_map::iterator
const map_it = m_keys_into_list_map->find(key);
825 if (map_it != m_keys_into_list_map->end())
828 return map_it->second->second;
832 return insert_impl(
Value(key, Mapped()))->second;
835template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
843template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
848 return *(--past_oldest());
851template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
856 return *(const_newest());
859template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
864 return *(--const_past_oldest());
867template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
870 m_value_list.splice(m_value_list.begin(), m_value_list, it);
873template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
887template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
891 m_keys_into_list_map->erase(m_keys_into_list_map->find(it->first));
892 return m_value_list.erase(it);
895template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
902 m_keys_into_list_map->erase(it->first);
905 return m_value_list.erase(it_newest, it_past_oldest);
908template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
912 typename Key_to_value_iter_map::iterator
const map_it = m_keys_into_list_map->find(key);
913 if (map_it == m_keys_into_list_map->end())
919 m_value_list.erase(map_it->second);
920 m_keys_into_list_map->erase(map_it);
925template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
928 m_keys_into_list_map->clear();
929 m_value_list.clear();
932template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
942template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
946 return m_value_list.begin();
949template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
956template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
960 return m_value_list.end();
963template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
967 return past_oldest();
970template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
974 return m_value_list.cbegin();
977template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
981 return const_newest();
984template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
988 return const_newest();
991template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
995 return m_value_list.cend();
998template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1002 return const_past_oldest();
1005template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1009 return const_past_oldest();
1012template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1016 return m_value_list.rbegin();
1019template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1026template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1030 return m_value_list.rend();
1033template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1037 return past_newest();
1040template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1044 return m_value_list.crbegin();
1047template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1051 return const_oldest();
1054template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1058 return m_value_list.crend();
1061template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1065 return const_past_newest();
1068template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1072 return m_keys_into_list_map->size();
1075template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1078 return m_keys_into_list_map->empty();
1081template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
1085 return std::min(m_keys_into_list_map->max_size(), m_value_list.max_size());
1088template<
typename Key,
typename Mapped,
typename Hash,
typename Pred>
An object of this class is a map that combines the lookup speed of a boost::unordered_map<> and order...
boost::movelib::unique_ptr< Key_to_value_iter_map > m_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...
std::ptrdiff_t difference_type
Type for difference of size_types.
Const_iterator end() const
Synonym of cend().
Iterator erase(Const_iterator const &it)
Erases the element pointed to by the given valid iterator.
typename Value_list::const_reverse_iterator Const_reverse_iterator
Type for reverse iterator pointing into an immutable structure of this type.
Value & front()
Returns reference to mutable front ("newest") element in the structure; formally equivalent to *(this...
typename Value_list::reverse_iterator Reverse_iterator
Type for reverse iterator pointing into a mutable structure of this type.
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...
Iterator iterator
For container compliance (hence the irregular capitalization): Iterator type.
typename Value_list::const_iterator Const_iterator
Type for iterator pointing into an immutable structure of this type.
Iterator Value_list_iter
Short-hand for iterator into doubly linked list of (Key, Mapped) pairs.
size_type max_size() const
Returns max number of elements that can be stored.
Const_iterator const_iterator
For container compliance (hence the irregular capitalization): Const_iterator type.
Linked_hash_map(Linked_hash_map const &src)
Constructs object that is a copy of the given source.
typename Value_list::iterator Iterator
Type for iterator pointing into a mutable structure of this type.
size_type erase(Key const &key)
Erases the element with the given key, if it exists.
Value_list m_value_list
The actual values – which, as in unordered_map<K, M>, are instances of Value = pair<Key,...
Const_iterator cbegin() const
Synonym of const_newest().
Const_iterator cend() const
Synonym of const_past_oldest().
Iterator newest()
Returns first, a/k/a "newest," element's iterator.
Const_iterator const_past_oldest() const
Returns one past last, a/k/a "oldest," element's iterator (to immutable element).
Mapped & operator[](Key const &key)
Equivalent to insert(Value(key, Mapped())).first->second (but avoids unnecessarily invoking Mapped()/...
Value const * const_pointer
For container compliance (hence the irregular capitalization): pointer to const Key/Mapped pair type.
Pred key_equal
For container compliance (hence the irregular capitalization): Pred type.
Value & back()
Returns reference to mutable back ("oldest") element in the structure; formally equivalent to *(--thi...
Reverse_iterator rbegin()
Synonym of oldest().
bool touch(Key const &key)
Given a key into the structure, makes the corresponding element "newest" by moving it from wherever i...
Value & reference
For container compliance (hence the irregular capitalization): reference to Key/Mapped pair type.
Value const & const_front() const
Returns reference to immutable front ("newest") element in the structure; formally equivalent to *(th...
Const_iterator const_newest() const
Returns first, a/k/a "newest," element's iterator (to immutable element).
Reverse_iterator rend()
Synonym of past_newest().
Value const & const_reference
For container compliance (hence the irregular capitalization): reference to const Key/Mapped pair typ...
Iterator end()
Synonym of past_oldest().
Reverse_iterator past_newest()
Returns one past last, a/k/a "newest," element's reverse iterator.
void touch(Const_iterator const &it)
Given a valid iterator into the structure, makes the pointed to element "newest" by moving it from wh...
void swap(Linked_hash_map &other)
Swaps the contents of this structure and other.
Value * pointer
For container compliance (hence the irregular capitalization): pointer to Key/Mapped pair type.
Const_reverse_iterator crend() const
Synonym of const_past_newest().
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.
Linked_hash_map & operator=(Linked_hash_map &&src)
Overwrites this object making it equal to the given source, while the given source becomes as-if defa...
Iterator find(Key const &key)
Attempts to find value at the given key in the map.
std::pair< Iterator, bool > insert(Value const &key_and_mapped)
Attempts to insert the given key/mapped-value pair into the map.
Iterator past_oldest()
Returns one past last, a/k/a "oldest," element's iterator.
std::size_t size_type
Type for index into array of items, where items are all applicable objects including Values and Keys.
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.
size_type size() const
Returns number of elements stored.
Const_iterator find(Key const &key) const
Attempts to find value at the given key in the map.
Iterator begin()
Synonym of newest().
Linked_hash_map(Linked_hash_map &&src)
Constructs object by making it equal to the given source, while the given source becomes as-if defaul...
Key key_type
For container compliance (hence the irregular capitalization): Key type.
boost::unordered_map< Key, Value_list_iter, Hash, Pred > Key_to_value_iter_map
Short-hand for a hash map that maps Key to iterator into doubly linked list of (Key,...
Linked_hash_map & operator=(Linked_hash_map const &src)
Overwrites this object with a copy of the given source.
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 ...
void clear()
Makes it so that size() == 0.
Mapped mapped_type
For container compliance (hence the irregular capitalization): Mapped type.
Const_iterator Value_list_const_iter
Short-hand for const iterator into doubly linked list of (Key, Mapped) pairs.
std::list< Value > Value_list
Short-hand for doubly linked list of (Key, Mapped) pairs.
Hash hasher
For container compliance (hence the irregular capitalization): Hash type.
Const_reverse_iterator const_oldest() const
Returns first, a/k/a "oldest," element's reverse iterator (to immutable element).
Const_reverse_iterator const_past_newest() const
Returns one past last, a/k/a "newest," element's reverse iterator (to immutable element).
Const_iterator begin() const
Synonym of cbegin().
Const_reverse_iterator crbegin() const
Synonym of const_oldest().
Value const & const_back() const
Returns reference to immutable back ("oldest") element in the structure; formally equivalent to *(--t...
Value value_type
For container compliance (hence the irregular capitalization): Key/Mapped pair type.
Reverse_iterator oldest()
Returns first, a/k/a "oldest," element's reverse iterator.
Iterator erase(Const_iterator const &it_newest, Const_iterator const &it_past_oldest)
Erases all elements in the range [it_newest, it_past_oldest).
std::pair< Key const, Mapped > Value
Short-hand for key/mapped-value pairs stored in the structure.
bool empty() const
Returns true if and only if container is empty.
Flow module containing miscellaneous general-use facilities that don't fit into any other Flow module...
void swap(Basic_blob< Allocator, S_SHARING_ALLOWED > &blob1, Basic_blob< Allocator, S_SHARING_ALLOWED > &blob2, log::Logger *logger_ptr)
Equivalent to blob1.swap(blob2).
void swap(Linked_hash_map< Key, Mapped, Hash, Pred > &val1, Linked_hash_map< Key, Mapped, Hash, Pred > &val2)
Equivalent to val1.swap(val2).