datastructures 0.1.0
Loading...
Searching...
No Matches
map.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13#include "_avl.hpp"
14#include "_avl_iterator.hpp"
15
16#include <functional>
17#include <iterator>
18#include <memory>
19#include <string>
20#include <utility>
21
22namespace datastructures {
23
45template <class TKey, class TValue, class TKeyComp = std::less<TKey>,
46 class TAlloc = std::allocator<std::pair<const TKey, TValue>>>
47class map {
48
49 public:
50 // ==== Keys and values types ====
51 using key_type = TKey;
52 using mapped_type = TValue;
53 using value_type = std::pair<const key_type, mapped_type>;
54
56 using const_reference = const value_type &;
57
58 // ==== Comparisons types ====
59
60 using key_compare = TKeyComp;
61
68 protected:
69 friend class map;
70 [[no_unique_address]] key_compare comp;
71
72 public:
74
75 inline bool operator()(const value_type &a,
76 const value_type &b) const noexcept {
77 return comp(a.first, b.first);
78 }
79
80 inline bool operator()(const key_type &a,
81 const value_type &b) const noexcept {
82 return comp(a, b.first);
83 }
84
85 inline bool operator()(const value_type &a,
86 const key_type &b) const noexcept {
87 return comp(a.first, b);
88 }
89
90 inline bool operator()(const key_type &a,
91 const key_type &b) const noexcept {
92 return comp(a, b);
93 }
94 };
95
96 // ==== Allocator and memory types ====
97 using allocator_type = TAlloc;
98 using alloc_traits = std::allocator_traits<allocator_type>;
99 using size_type = typename alloc_traits::size_type;
100 using difference_type = typename alloc_traits::difference_type;
101
102 using pointer = typename alloc_traits::pointer;
103 using const_pointer = typename alloc_traits::const_pointer;
104
105 private:
106 // ==== The Tree Type ====
107
108 using tree_type =
110
111 public:
112 // ==== Iterator types ====
115
116 using reverse_iterator = std::reverse_iterator<iterator>;
117 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
118
119 private:
120 [[no_unique_address]] key_compare comparator;
121 [[no_unique_address]] value_compare value_comparator;
122
123 tree_type tree;
124
125 public:
126 // ==== Constructors & Assigments ====
127 map();
128
129 explicit map(const key_compare &comparator,
130 const allocator_type &allocator = allocator_type{});
131
132 explicit map(const allocator_type &allocator);
133
134 map(const map &other);
135 map &operator=(const map &other);
136
137 map(map &&other) noexcept;
138 map &operator=(map &&other) noexcept;
139
140 ~map() noexcept;
141
142 // ==== Observers & Allocator getter ====
143
146
149
151 [[nodiscard]] value_compare value_comp() const noexcept;
152
153 // ==== Access Functions ====
154
161
169
177
186
194
202
207
212
223
235
246
258
270
282
283 // ==== Iterators ====
284
289
294
299
304
309
314
319
324
330
335
340
345
346 // ==== Size Queries ====
347
351 [[nodiscard]] bool empty() const noexcept;
352
357
362
363 // ==== Update Operations ====
364
372 void clear() noexcept;
373
385 std::pair<iterator, bool> insert(const value_type &item);
386
399 std::pair<iterator, bool> insert(value_type &&item);
400
414 TMapped &&value);
415
429
443 template <class... Args>
445
460 template <class... Args>
462
472
482
491
497 void swap(map &other) noexcept;
498
499 // ==== Debug Functions ====
500
502 std::string to_dot(bool print_invis) const;
503};
504
505} // namespace datastructures
506
507// implementation
508#include "map.ipp"
Declarations of the ethereal AVL tree.
Declarations and implementation of the ethereal AVL tree iterator.
The AVL tree iterator type.
Definition _avl_iterator.hpp:34
A map type using an AVL tree as the underlying structure.
Definition map.hpp:47
iterator erase(iterator pos) noexcept
Erases an item.
Definition map.ipp:389
iterator upper_bound(const key_type &key) noexcept
Returns an iterator to the first item which is greater that the given key.
Definition map.ipp:175
iterator find(const key_type &key) noexcept
Finds an item with the given key.
Definition map.ipp:132
int get_height() const noexcept
Returns the height of the underlying tree.
Definition map.ipp:291
typename alloc_traits::const_pointer const_pointer
Definition map.hpp:103
reverse_iterator rbegin() noexcept
Returns a reverse iterator to the biggest item.
Definition map.ipp:243
iterator begin() noexcept
Returns an iterator to the smallest item.
Definition map.ipp:207
key_compare key_comp() const noexcept
Returns the function that compares keys.
Definition map.ipp:76
const_iterator cbegin() const noexcept
Returns explicitly a constant iterator to the smallest item.
Definition map.ipp:219
void clear() noexcept
Erases the whole tree.
Definition map.ipp:298
mapped_type & at(const key_type &key)
Access specified item.
Definition map.ipp:90
~map() noexcept
Definition map.ipp:62
std::allocator_traits< allocator_type > alloc_traits
Definition map.hpp:98
detail::avl_iterator< tree_type, false > iterator
Definition map.hpp:113
value_type & reference
Definition map.hpp:55
void swap(map &other) noexcept
Swaps the content.
Definition map.ipp:418
const_reverse_iterator crbegin() const noexcept
Returns explicitly a constant reverse iterator to the biggest item.
Definition map.ipp:255
map & operator=(const map &other)
Definition map.ipp:39
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition map.hpp:117
std::pair< iterator, bool > insert_or_assign(const key_type &key, TMapped &&value)
Inserts an item or assigns the value.
std::pair< const key_type, mapped_type > value_type
Definition map.hpp:53
iterator end() noexcept
Returns an iterator to the end.
Definition map.ipp:225
typename alloc_traits::size_type size_type
Definition map.hpp:99
std::pair< iterator, bool > try_emplace(const key_type &key, Args &&...args)
Construct an item in-place.
map()
Definition map.ipp:20
typename alloc_traits::difference_type difference_type
Definition map.hpp:100
const value_type & const_reference
Definition map.hpp:56
typename alloc_traits::pointer pointer
Definition map.hpp:102
TKeyComp key_compare
Definition map.hpp:60
TAlloc allocator_type
Definition map.hpp:97
std::string to_dot(bool print_invis) const
Returns the dot representation of the tree.
Definition map.ipp:428
std::reverse_iterator< iterator > reverse_iterator
Definition map.hpp:116
allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition map.ipp:70
const_iterator cend() const noexcept
Returns explicitly a constant iterator to the end.
Definition map.ipp:237
value_compare value_comp() const noexcept
Returns the function that compares keys in the key, value pairs.
Definition map.ipp:82
TKey key_type
Definition map.hpp:51
size_type size() const noexcept
Returns the current number of items.
Definition map.ipp:286
std::pair< iterator, bool > insert(const value_type &item)
Inserts an item.
Definition map.ipp:304
const_reverse_iterator crend() const noexcept
Returns explicitly a constant reverse iterator to the end.
Definition map.ipp:273
bool contains(const key_type &key) const noexcept
Checks whether there exists an item with given key in the tree.
Definition map.ipp:145
TValue mapped_type
Definition map.hpp:52
bool empty() const noexcept
Checks whether the map contains any values.
Definition map.ipp:280
iterator lower_bound(const key_type &key) noexcept
Returns an iterator to the first item which is not less than the given key.
Definition map.ipp:160
size_type count(const key_type &key) const noexcept
Returns the number of items with the given key (0 or 1).
Definition map.ipp:153
detail::avl_iterator< tree_type, true > const_iterator
Definition map.hpp:114
std::pair< iterator, iterator > equal_range(const key_type &key) noexcept
Returns a range containing all the items with the given key.
Definition map.ipp:191
reverse_iterator rend() noexcept
Returns a reverse iterator to the end.
Definition map.ipp:261
Declarations of the persistent map.
Definition _avl.hpp:14
value_compare(key_compare c)
Definition map.hpp:73
bool operator()(const key_type &a, const key_type &b) const noexcept
Definition map.hpp:90
key_compare comp
Definition map.hpp:70
bool operator()(const value_type &a, const value_type &b) const noexcept
Definition map.hpp:75
bool operator()(const value_type &a, const key_type &b) const noexcept
Definition map.hpp:85
bool operator()(const key_type &a, const value_type &b) const noexcept
Definition map.hpp:80