API Documentation
Loading...
Searching...
No Matches
Dictionary.h
Go to the documentation of this file.
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 <NDEVR/BaseValues.h>
34#include <NDEVR/LibAssert.h>
35#include <NDEVR/Buffer.h>
36#define NDEVR_USE_ROBIN_MAP 1
37#if NDEVR_USE_ROBIN_MAP
38 #include "tsl/robin_map.h"
39 #define MAP_BASE tsl::robin_map
40#else
41 #include <unordered_map>
42 #define MAP_BASE MAP_BASE
43#endif
44namespace NDEVR
45{
46 /**--------------------------------------------------------------------------------------------------
47 \brief A dummy base class for all Dictionary templates
48 Used to more easily track inheritance for the templated Dictionary class
49 **/
51 {};
52 /**--------------------------------------------------------------------------------------------------
53 \brief A hash-based key-value store, useful for quick associative lookups.
54 Key features include:
55 - Uses tsl robin library as backing logic and hashmap. Proven to be significantly faster in
56 almost all metrics over std::unordered_set on modern architecture
57 - Easy functions for converting to other NDEVR containers
58 **/
59 template<class t_key, class t_value, class t_hash = std::hash<t_key>>
60 class Dictionary : public MAP_BASE<t_key, t_value, t_hash>, public DictionaryBase
61 {
62 public:
64 : MAP_BASE<t_key, t_value, t_hash>()
65 {}
66 [[nodiscard]] bool hasKey(const t_key& key) const
67 {
68 return MAP_BASE<t_key, t_value, t_hash>::find(key) != MAP_BASE<t_key, t_value, t_hash>::end();
69 }
70 void add(const t_key& key, const t_value& value)
71 {
72 MAP_BASE<t_key, t_value, t_hash>::try_emplace(key, value);
73 }
74
75 void add(const t_key& key)
76 {
77 MAP_BASE<t_key, t_value, t_hash>::try_emplace(key);
78 }
79 void add(t_key&& key)
80 {
81 MAP_BASE<t_key, t_value>::try_emplace(std::move(key));
82 }
83 [[nodiscard]] const t_value& get(const t_key& key) const
84 {
85 return MAP_BASE<t_key, t_value, t_hash>::at(key);
86 }
87 [[nodiscard]] t_value& get(const t_key& key)
88 {
89 lib_assert(hasKey(key), "Tried to get from Dictionary, without having key");
90 return MAP_BASE<t_key, t_value, t_hash>::at(key);
91 }
92 template<class t_index_type = uint04, class t_memory_allocator = ObjectAllocator<ObjectInfo<t_key>::Primitive, t_index_type>, class t_memory_manager = BufferAllocator<t_key, t_index_type, false>>
94 {
96 for (const auto& kv : (*this))
97 {
98 keys.add(kv.first);
99 }
100 return keys;
101 }
102 template<class t_index_type = uint04, class t_memory_allocator = ObjectAllocator<ObjectInfo<t_value>::Primitive, t_index_type>, class t_memory_manager = BufferAllocator<t_value, t_index_type, false>>
104 {
106 for (const auto& kv : (*this))
107 {
108 values.add(kv.second);
109 }
110 return values;
111 }
112 [[nodiscard]] uint04 indexOf(const t_key& key) const
113 {
114 uint04 i = 0;
115 for (const auto& location : *this)
116 {
117 if (location.first == key)
118 return i;
119 i++;
120 }
122 }
123 void removeIndex(uint04 index)
124 {
125 erase(std::next(MAP_BASE<t_key, t_value, t_hash>::begin(), index)->first);
126 }
127 [[nodiscard]] const t_value& getFromIndex(uint04 index) const
128 {
129 return std::next(MAP_BASE<t_key, t_value, t_hash>::begin(), index)->second;
130 }
131 void set(const t_key& key, const t_value& value)
132 {
133 MAP_BASE<t_key, t_value, t_hash>::at(key) = value;
134 }
135 [[nodiscard]] const t_value& operator[](const t_key& key) const
136 {
137 lib_assert(hasKey(key), "Tried to get from Dictionary, without having key");
138 return MAP_BASE<t_key, t_value, t_hash>::at(key);
139 }
140 [[nodiscard]] t_value& operator[](const t_key& key)
141 {
142 return MAP_BASE<t_key, t_value, t_hash>::operator[](key);
143 }
144 [[nodiscard]] uint04 size() const
145 {
146 return cast<uint04>(MAP_BASE<t_key, t_value, t_hash>::size());
147 }
148 };
149}
150
#define MAP_BASE
Definition Dictionary.h:39
#define lib_assert(expression, message)
Definition LibAssert.h:61
Specific logic for reserving memory for a Buffer. When managed, and more memory is needed memory is r...
Definition Pointer.hpp:311
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:56
A dummy base class for all Dictionary templates Used to more easily track inheritance for the templat...
Definition Dictionary.h:51
A hash-based key-value store, useful for quick associative lookups. Key features include:
Definition Dictionary.h:61
void add(const t_key &key)
Definition Dictionary.h:75
const t_value & getFromIndex(uint04 index) const
Definition Dictionary.h:127
const t_value & get(const t_key &key) const
Definition Dictionary.h:83
uint04 indexOf(const t_key &key) const
Definition Dictionary.h:112
Buffer< t_key, t_index_type, t_memory_allocator, t_memory_manager > keys() const
Definition Dictionary.h:93
void add(const t_key &key, const t_value &value)
Definition Dictionary.h:70
bool hasKey(const t_key &key) const
Definition Dictionary.h:66
Dictionary()
Definition Dictionary.h:63
void removeIndex(uint04 index)
Definition Dictionary.h:123
t_value & operator[](const t_key &key)
Definition Dictionary.h:140
uint04 size() const
Definition Dictionary.h:144
const t_value & operator[](const t_key &key) const
Definition Dictionary.h:135
void set(const t_key &key, const t_value &value)
Definition Dictionary.h:131
Buffer< t_value, t_index_type, t_memory_allocator, t_memory_manager > values() const
Definition Dictionary.h:103
t_value & get(const t_key &key)
Definition Dictionary.h:87
void add(t_key &&key)
Definition Dictionary.h:79
Definition ACIColor.h:37
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233