Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
VectorMap.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Containers/StrongID.h"
5#include "../Containers/Vector.h"
6#include "ContainersExport.h"
7
8namespace SC
9{
10template <typename Key, typename Value, typename Container>
11struct VectorMap;
12template <typename Key, typename Value>
13struct VectorMapItem;
14} // namespace SC
17
21template <typename Key, typename Value>
23{
24 Key key;
25 Value value;
26};
27
34template <typename Key, typename Value, typename Container = SC::Vector<SC::VectorMapItem<Key, Value>>>
36{
38
39 Container items;
40
42 [[nodiscard]] auto size() const { return items.size(); }
43
45 [[nodiscard]] auto isEmpty() const { return items.isEmpty(); }
46
47 [[nodiscard]] Item* begin() { return items.begin(); }
48 [[nodiscard]] const Item* begin() const { return items.begin(); }
49 [[nodiscard]] Item* end() { return items.end(); }
50 [[nodiscard]] const Item* end() const { return items.end(); }
51
55 template <typename ComparableToKey>
56 [[nodiscard]] bool remove(const ComparableToKey& key)
57 {
58 size_t idx = 0;
59 for (auto& item : items)
60 {
61 if (item.key == key)
62 {
63 return items.removeAt(idx);
64 }
65 ++idx;
66 }
67 return false;
68 }
69
73 [[nodiscard]] bool insertIfNotExists(Item&& item)
74 {
75 if (not contains(item.key))
76 {
77 return items.push_back(forward<Item>(item));
78 }
79 return false;
80 }
81
85 [[nodiscard]] Value* insertOverwrite(Item&& item)
86 {
87 for (auto& it : items)
88 {
89 if (it.key == item.key)
90 {
91 it.value = move(item.value);
92 return &it.value;
93 }
94 }
95 if (items.push_back(forward<Item>(item)))
96 {
97 return &items.back().value;
98 }
99 return nullptr;
100 }
101
106 [[nodiscard]] Key* insertValueUniqueKey(Value&& value)
107 {
108 if (items.push_back({Key::generateUniqueKey(*this), forward<Value>(value)}))
109 {
110 return &items.back().key;
111 }
112 return nullptr;
113 }
114
116 template <typename ComparableToKey>
117 [[nodiscard]] bool contains(const ComparableToKey& key) const
118 {
119 for (auto& item : items)
120 {
121 if (item.key == key)
122 {
123 return true;
124 }
125 }
126 return false;
127 }
128
132 template <typename ComparableToKey>
133 [[nodiscard]] bool contains(const ComparableToKey& key, const Value*& outValue) const
134 {
135 for (auto& item : items)
136 {
137 if (item.key == key)
138 {
139 outValue = &item.value;
140 return true;
141 }
142 }
143 return false;
144 }
145
149 template <typename ComparableToKey>
150 [[nodiscard]] bool contains(const ComparableToKey& key, Value*& outValue)
151 {
152 for (auto& item : items)
153 {
154 if (item.key == key)
155 {
156 outValue = &item.value;
157 return true;
158 }
159 }
160 return false;
161 }
162
165 template <typename ComparableToKey>
166 [[nodiscard]] const Value* get(const ComparableToKey& key) const
167 {
168 for (auto& item : items)
169 {
170 if (item.key == key)
171 {
172 return &item.value;
173 }
174 }
175 return nullptr;
176 }
177
180 template <typename ComparableToKey>
181 [[nodiscard]] Value* get(const ComparableToKey& key)
182 {
183 for (auto& item : items)
184 {
185 if (item.key == key)
186 {
187 return &item.value;
188 }
189 }
190 return nullptr;
191 }
192
195 template <typename ComparableToKey>
196 [[nodiscard]] Value* getOrCreate(const ComparableToKey& key)
197 {
198 for (auto& item : items)
199 {
200 if (item.key == key)
201 {
202 return &item.value;
203 }
204 }
205 if (items.push_back({key, Value()}))
206 {
207 return &items.back().value;
208 }
209 return nullptr;
210 }
211};
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition Compiler.h:273
constexpr T && forward(typename TypeTraits::RemoveReference< T >::type &value)
Forwards an lvalue or an rvalue as an rvalue reference.
Definition Compiler.h:276
The single item of VectorMap, holding a Key and Value.
Definition VectorMap.h:23
Key key
Key item value.
Definition VectorMap.h:24
Value value
Map item value.
Definition VectorMap.h:25
A map holding VectorMapItem key-value pairs in an unsorted Vector.
Definition VectorMap.h:36
auto size() const
Return the number of key-value pairs in the map.
Definition VectorMap.h:42
Key * insertValueUniqueKey(Value &&value)
Inserts a new value, automatically generating key with Key::generateUniqueKey (works for StrongID for...
Definition VectorMap.h:106
auto isEmpty() const
Check if the map is empty.
Definition VectorMap.h:45
bool contains(const ComparableToKey &key, Value *&outValue)
Check if the given key is contained in the map.
Definition VectorMap.h:150
const Value * get(const ComparableToKey &key) const
Get the Value associated to the given key.
Definition VectorMap.h:166
bool contains(const ComparableToKey &key) const
Check if the given key is contained in the map.
Definition VectorMap.h:117
bool insertIfNotExists(Item &&item)
Inserts an item if it doesn't exist already.
Definition VectorMap.h:73
Value * getOrCreate(const ComparableToKey &key)
Get the value associated to the given key, or creates a new one if needed.
Definition VectorMap.h:196
Value * get(const ComparableToKey &key)
Get the Value associated to the given key.
Definition VectorMap.h:181
bool remove(const ComparableToKey &key)
Remove an item with matching key from the Map.
Definition VectorMap.h:56
Value * insertOverwrite(Item &&item)
Insert an item, overwriting the potentially already existing one.
Definition VectorMap.h:85
bool contains(const ComparableToKey &key, const Value *&outValue) const
Check if the given key is contained in the map.
Definition VectorMap.h:133