NDEVR
API Documentation
Dictionary.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Dictionary
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/BaseValues.h>
35#include <NDEVR/LibAssert.h>
36#include <NDEVR/Buffer.h>
37#define NDEVR_USE_ROBIN_MAP 1
38#if NDEVR_USE_ROBIN_MAP
39 #include "tsl/robin_map.h"
40 #define MAP_BASE tsl::robin_map
41#else
42 #include <unordered_map>
43 #define MAP_BASE MAP_BASE
44#endif
45namespace NDEVR
46{
52 {};
53
62 template<class t_key, class t_value, class t_hash = std::hash<t_key>>
63 class Dictionary : public MAP_BASE<t_key, t_value, t_hash>, public DictionaryBase
64 {
65 public:
70 : MAP_BASE<t_key, t_value, t_hash>()
71 {}
72
77 template<class t_key_type>
78 [[nodiscard]] bool hasKey(const t_key_type& key) const
79 {
80 return MAP_BASE<t_key, t_value, t_hash>::find(key) != MAP_BASE<t_key, t_value, t_hash>::end();
81 }
82
87 void add(const t_key& key, const t_value& value)
88 {
89 MAP_BASE<t_key, t_value, t_hash>::try_emplace(key, value);
90 }
91
96 template<class t_key_type>
97 void add(const t_key_type& key, const t_value& value)
98 {
99 MAP_BASE<t_key, t_value, t_hash>::try_emplace(t_key(key), value);
100 }
101
106 void add(const t_key& key)
107 {
108 MAP_BASE<t_key, t_value, t_hash>::try_emplace(key);
109 }
110
114 void add(t_key&& key)
115 {
116 MAP_BASE<t_key, t_value>::try_emplace(std::move(key));
117 }
118
123 template<class t_key_type>
124 [[nodiscard]] const t_value& get(const t_key_type& key) const
125 {
126 return MAP_BASE<t_key, t_value, t_hash>::at(key);
127 }
128
133 template<class t_key_type>
134 [[nodiscard]] t_value& get(const t_key_type& key)
135 {
136 lib_assert(hasKey(key), "Tried to get from Dictionary, without having key");
137 return MAP_BASE<t_key, t_value, t_hash>::at(key);
138 }
139
143 template<class t_index_type = uint04, class t_memory_manager = BufferAllocator<t_key, DetermineAlignment<sizeof(t_key)>(), ObjectInfo<t_key>::Primitive, t_index_type, false>>
145 {
147 for (const auto& kv : (*this))
148 {
149 keys.add(kv.first);
150 }
151 return keys;
152 }
153
157 template<class t_index_type = uint04, class t_memory_manager = BufferAllocator<t_value, DetermineAlignment<sizeof(t_value)>(), ObjectInfo<t_value>::Primitive, t_index_type, false>>
159 {
161 for (const auto& kv : (*this))
162 {
163 values.add(kv.second);
164 }
165 return values;
166 }
167
172 [[nodiscard]] uint04 indexOf(const t_key& key) const
173 {
174 uint04 i = 0;
175 for (const auto& location : *this)
176 {
177 if (location.first == key)
178 return i;
179 i++;
180 }
181 return Constant<uint04>::Invalid;
182 }
183
187 void removeIndex(uint04 index)
188 {
189 erase(std::next(MAP_BASE<t_key, t_value, t_hash>::begin(), index)->first);
190 }
191
196 [[nodiscard]] decltype(auto) getFromIndex(uint04 index) const
197 {
198 return std::next(MAP_BASE<t_key, t_value, t_hash>::begin(), index);
199 }
200
204 [[nodiscard]] decltype(auto) first() const
205 {
206 return MAP_BASE<t_key, t_value, t_hash>::begin();
207 }
208
213 template<class t_key_type>
214 void set(const t_key_type& key, const t_value& value)
215 {
216 MAP_BASE<t_key, t_value, t_hash>::at(key) = value;
217 }
218
223 template<class t_key_type>
224 [[nodiscard]] const t_value& operator[](const t_key_type& key) const
225 {
226 lib_assert(hasKey(key), "Tried to get from Dictionary, without having key");
227 return MAP_BASE<t_key, t_value, t_hash>::at(key);
228 }
229
234 template<class t_key_type>
235 [[nodiscard]] t_value& operator[](const t_key_type& key)
236 {
237 return MAP_BASE<t_key, t_value, t_hash>::operator[](key);
238 }
239
243 [[nodiscard]] uint04 size() const
244 {
245 return cast<uint04>(MAP_BASE<t_key, t_value, t_hash>::size());
246 }
247 };
248}
249
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
A dummy base class for all Dictionary templates Used to more easily track inheritance for the templat...
Definition Dictionary.h:52
bool hasKey(const t_key_type &key) const
Checks whether the dictionary contains the given key.
Definition Dictionary.h:78
Dictionary()
Constructs an empty Dictionary.
Definition Dictionary.h:69
decltype(auto) getFromIndex(uint04 index) const
Retrieves an iterator to the entry at the given iteration index.
Definition Dictionary.h:196
t_value & operator[](const t_key_type &key)
Subscript operator that retrieves or inserts the value associated with the given key.
Definition Dictionary.h:235
void add(const t_key_type &key, const t_value &value)
Inserts a key-value pair using a convertible key type, if the key does not already exist.
Definition Dictionary.h:97
void add(t_key &&key)
Inserts a key with a default-constructed value using move semantics, if the key does not already exis...
Definition Dictionary.h:114
void removeIndex(uint04 index)
Removes the entry at the given iteration index.
Definition Dictionary.h:187
void add(const t_key &key)
Inserts a key with a default-constructed value if the key does not already exist.
Definition Dictionary.h:106
uint04 indexOf(const t_key &key) const
Finds the iteration index of the given key within the dictionary.
Definition Dictionary.h:172
void set(const t_key_type &key, const t_value &value)
Sets the value for an existing key, replacing the previous value.
Definition Dictionary.h:214
Buffer< t_key, t_memory_manager > keys() const
Collects all keys in the dictionary into a Buffer.
Definition Dictionary.h:144
t_value & get(const t_key_type &key)
Retrieves a mutable reference to the value associated with the given key.
Definition Dictionary.h:134
void add(const t_key &key, const t_value &value)
Inserts a key-value pair if the key does not already exist.
Definition Dictionary.h:87
Buffer< t_value, t_memory_manager > values() const
Collects all values in the dictionary into a Buffer.
Definition Dictionary.h:158
const t_value & operator[](const t_key_type &key) const
Const subscript operator that retrieves the value associated with the given key.
Definition Dictionary.h:224
const t_value & get(const t_key_type &key) const
Retrieves a const reference to the value associated with the given key.
Definition Dictionary.h:124
uint04 size() const
Returns the number of entries in the dictionary.
Definition Dictionary.h:243
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408
Information about the object.
Definition ObjectInfo.h:55