Template Class OrderedDict¶
Defined in File ordered_dict.h
Page Contents
Class Documentation¶
-
template<typename Key, typename Value>
class OrderedDict¶ An ordered dictionary implementation, akin to Python’s
OrderedDict
.Public Types
Public Functions
-
explicit OrderedDict(std::string key_description = "Key")¶
Constructs the
OrderedDict
with a short description of the kinds of keys stored in theOrderedDict
.This description is used in error messages thrown by the
OrderedDict
.
-
OrderedDict(const OrderedDict &other)¶
Copy constructs this
OrderedDict
fromother
.
-
OrderedDict &operator=(const OrderedDict &other)¶
Assigns items from
other
to thisOrderedDict
.
-
OrderedDict(OrderedDict &&other) noexcept = default¶
-
OrderedDict &operator=(OrderedDict &&other) noexcept = default¶
-
~OrderedDict() = default¶
-
OrderedDict(std::initializer_list<Item> initializer_list)¶
Constructs a new
OrderedDict
and pre-populates it with the givenItem
s.
-
const std::string &key_description() const noexcept¶
Returns the key description string the
OrderedDict
was constructed with.
-
Item &front()¶
Returns the very first item in the
OrderedDict
and throws an exception if it is empty.
-
const Item &front() const¶
Returns the very first item in the
OrderedDict
and throws an exception if it is empty.
-
Item &back()¶
Returns the very last item in the
OrderedDict
and throws an exception if it is empty.
-
const Item &back() const¶
Returns the very last item in the
OrderedDict
and throws an exception if it is empty.
-
Item &operator[](size_t index)¶
Returns the item at the
index
-th position in theOrderedDict
.Throws an exception if the index is out of bounds.
-
const Item &operator[](size_t index) const¶
Returns the item at the
index
-th position in theOrderedDict
.Throws an exception if the index is out of bounds.
-
Value &operator[](const Key &key)¶
Returns the value associated with the given
key
.Throws an exception if no such key is stored in the
OrderedDict
. Usefind()
for a non-throwing way of accessing a value if it is present.
-
const Value &operator[](const Key &key) const¶
Returns the value associated with the given
key
.Throws an exception if no such key is stored in the
OrderedDict
. Usefind()
for a non-throwing way of accessing a value if it is present.
-
Value *find(const Key &key) noexcept¶
Returns a pointer to the value associated with the given key, or a
nullptr
if no such key is stored in theOrderedDict
.
-
const Value *find(const Key &key) const noexcept¶
Returns a pointer to the value associated with the given key, or a
nullptr
if no such key is stored in theOrderedDict
.
-
bool contains(const Key &key) const noexcept¶
Returns true if the key is present in the
OrderedDict
.
-
Iterator begin()¶
Returns an iterator to the first item in the
OrderedDict
.Iteration is ordered.
-
ConstIterator begin() const¶
Returns an iterator to the first item in the
OrderedDict
.Iteration is ordered.
-
Iterator end()¶
Returns an iterator one past the last item in the
OrderedDict
.
-
ConstIterator end() const¶
Returns an iterator one past the last item in the
OrderedDict
.
-
size_t size() const noexcept¶
Returns the number of items currently stored in the
OrderedDict
.
-
bool is_empty() const noexcept¶
Returns true if the
OrderedDict
contains no elements.
-
void reserve(size_t requested_capacity)¶
Resizes internal storage to fit at least
requested_capacity
items without requiring reallocation.
-
template<typename K, typename V>
Value &insert(K &&key, V &&value)¶ Inserts a new
(key, value)
pair into theOrderedDict
.Throws an exception if the key is already present. If insertion is successful, immediately returns a reference to the inserted value.
-
Value &insert(Key key, Value &&value)¶
Inserts a new
(key, value)
pair into theOrderedDict
.Throws an exception if the key is already present. If insertion is successful, immediately returns a reference to the inserted value.
-
void update(OrderedDict &&other)¶
Inserts all items from
other
into thisOrderedDict
.If any key from
other
is already present in thisOrderedDict
, an exception is thrown.
-
void update(const OrderedDict &other)¶
Inserts all items from
other
into thisOrderedDict
.If any key from
other
is already present in thisOrderedDict
, an exception is thrown.
-
void erase(const Key &key)¶
Removes the item that has
key
from thisOrderedDict
if exists and if it doesn’t an exception is thrown.
-
void clear()¶
Removes all items from this
OrderedDict
.
-
const std::vector<Item> &items() const noexcept¶
Returns the items stored in the
OrderedDict
.
-
::std::vector<Key> keys() const¶
Returns a newly allocated vector and copies all keys from this
OrderedDict
into the vector.
-
::std::vector<Value> values() const¶
Returns a newly allocated vector and copies all values from this
OrderedDict
into the vector.
-
::std::vector<std::pair<Key, Value>> pairs() const¶
Returns a newly allocated vector and copies all keys and values from this
OrderedDict
into a vector ofstd::pair<Key, Value>
.
Friends
-
template<typename K, typename V>
friend bool operator==(const OrderedDict<K, V> &a, const OrderedDict<K, V> &b)¶ Returns true if both dicts contain the same keys and values, in the same order.
-
class Item¶
Public Functions
-
explicit OrderedDict(std::string key_description = "Key")¶