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 template<class t_key, class t_value, class t_hash = std::hash<t_key>>
47 class Dictionary : public MAP_BASE<t_key, t_value, t_hash>
48 {
49 public:
51 : MAP_BASE<t_key, t_value, t_hash>()
52 {}
53 [[nodiscard]] bool hasKey(const t_key& key) const
54 {
55 return MAP_BASE<t_key, t_value, t_hash>::find(key) != MAP_BASE<t_key, t_value, t_hash>::end();
56 }
57 void add(const t_key& key, const t_value& value)
58 {
59 MAP_BASE<t_key, t_value, t_hash>::try_emplace(key, value);
60 }
61
62 void add(const t_key& key)
63 {
64 MAP_BASE<t_key, t_value, t_hash>::try_emplace(key);
65 }
66 void add(t_key&& key)
67 {
68 MAP_BASE<t_key, t_value>::try_emplace(std::move(key));
69 }
70 [[nodiscard]] const t_value& get(const t_key& key) const
71 {
72 return MAP_BASE<t_key, t_value, t_hash>::at(key);
73 }
74 [[nodiscard]] t_value& get(const t_key& key)
75 {
76 lib_assert(hasKey(key), "Tried to get from Dictionary, without having key");
77 return MAP_BASE<t_key, t_value, t_hash>::at(key);
78 }
79 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>>
81 {
83 for (const auto& kv : (*this))
84 {
85 keys.add(kv.first);
86 }
87 return keys;
88 }
89 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>>
91 {
93 for (const auto& kv : (*this))
94 {
95 values.add(kv.second);
96 }
97 return values;
98 }
99 [[nodiscard]] uint04 indexOf(const t_key& key) const
100 {
101 uint04 i = 0;
102 for (const auto& location : *this)
103 {
104 if (location.first == key)
105 return i;
106 i++;
107 }
109 }
110 void removeIndex(uint04 index)
111 {
112 erase(std::next(MAP_BASE<t_key, t_value, t_hash>::begin(), index)->first);
113 }
114 [[nodiscard]] const t_value& getFromIndex(uint04 index) const
115 {
116 return std::next(MAP_BASE<t_key, t_value, t_hash>::begin(), index)->second;
117 }
118 /*[[nodiscard]] t_value& getFromIndex(uint04 index)
119 {
120#if NDEVR_USE_ROBIN_MAP
121 return std::next(MAP_BASE<t_key, t_value>::begin(), index)->second;
122#else
123 return std::next(MAP_BASE<t_key, t_value>::begin(), index)->second;
124#endif
125 }*/
126 void set(const t_key& key, const t_value& value)
127 {
128 MAP_BASE<t_key, t_value, t_hash>::at(key) = value;
129 }
130
131 [[nodiscard]] const t_value& operator[](const t_key& key) const
132 {
133 lib_assert(hasKey(key), "Tried to get from Dictionary, without having key");
134 return MAP_BASE<t_key, t_value, t_hash>::at(key);
135 }
136 [[nodiscard]] t_value& operator[](const t_key& key)
137 {
138 return MAP_BASE<t_key, t_value, t_hash>::operator[](key);
139 }
140 [[nodiscard]] uint04 size() const
141 {
142 return cast<uint04>(MAP_BASE<t_key, t_value, t_hash>::size());
143 }
144 };
145}
146
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
Definition Pointer.hpp:297
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
Definition Dictionary.h:48
void add(const t_key &key)
Definition Dictionary.h:62
const t_value & getFromIndex(uint04 index) const
Definition Dictionary.h:114
const t_value & get(const t_key &key) const
Definition Dictionary.h:70
uint04 indexOf(const t_key &key) const
Definition Dictionary.h:99
Buffer< t_key, t_index_type, t_memory_allocator, t_memory_manager > keys() const
Definition Dictionary.h:80
void add(const t_key &key, const t_value &value)
Definition Dictionary.h:57
bool hasKey(const t_key &key) const
Definition Dictionary.h:53
Dictionary()
Definition Dictionary.h:50
void removeIndex(uint04 index)
Definition Dictionary.h:110
t_value & operator[](const t_key &key)
Definition Dictionary.h:136
uint04 size() const
Definition Dictionary.h:140
const t_value & operator[](const t_key &key) const
Definition Dictionary.h:131
void set(const t_key &key, const t_value &value)
Definition Dictionary.h:126
Buffer< t_value, t_index_type, t_memory_allocator, t_memory_manager > values() const
Definition Dictionary.h:90
t_value & get(const t_key &key)
Definition Dictionary.h:74
void add(t_key &&key)
Definition Dictionary.h:66
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:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
Definition BaseValues.hpp:272