Sane C++ Libraries
C++ Platform Abstraction Libraries
VectorMap.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Containers/Vector.h"
5#include "../Foundation/StrongID.h"
6
7namespace SC
8{
9template <typename Key, typename Value, typename Container>
10struct VectorMap;
11template <typename Key, typename Value>
12struct VectorMapItem;
13} // namespace SC
16
20template <typename Key, typename Value>
22{
23 Key key;
24 Value value;
25};
26
31template <typename Key, typename Value, typename Container = SC::Vector<SC::VectorMapItem<Key, Value>>>
33{
35
36 Container items;
37
39 [[nodiscard]] auto size() const { return items.size(); }
40
42 [[nodiscard]] auto isEmpty() const { return items.isEmpty(); }
43
44 [[nodiscard]] Item* begin() { return items.begin(); }
45 [[nodiscard]] const Item* begin() const { return items.begin(); }
46 [[nodiscard]] Item* end() { return items.end(); }
47 [[nodiscard]] const Item* end() const { return items.end(); }
48
52 template <typename ComparableToKey>
53 [[nodiscard]] bool remove(const ComparableToKey& key)
54 {
55 size_t idx = 0;
56 for (auto& item : items)
57 {
58 if (item.key == key)
59 {
60 return items.removeAt(idx);
61 }
62 ++idx;
63 }
64 return false;
65 }
66
70 [[nodiscard]] bool insertIfNotExists(Item&& item)
71 {
72 if (not contains(item.key))
73 {
74 return items.push_back(forward<Item>(item));
75 }
76 return false;
77 }
78
82 [[nodiscard]] Value* insertOverwrite(Item&& item)
83 {
84 for (auto& it : items)
85 {
86 if (it.key == item.key)
87 {
88 it.value = move(item.value);
89 return &it.value;
90 }
91 }
92 if (items.push_back(forward<Item>(item)))
93 {
94 return &items.back().value;
95 }
96 return nullptr;
97 }
98
103 [[nodiscard]] Key* insertValueUniqueKey(Value&& value)
104 {
105 if (items.push_back({Key::generateUniqueKey(*this), forward<Value>(value)}))
106 {
107 return &items.back().key;
108 }
109 return nullptr;
110 }
111
113 template <typename ComparableToKey>
114 [[nodiscard]] bool contains(const ComparableToKey& key) const
115 {
116 for (auto& item : items)
117 {
118 if (item.key == key)
119 {
120 return true;
121 }
122 }
123 return false;
124 }
125
129 template <typename ComparableToKey>
130 [[nodiscard]] bool contains(const ComparableToKey& key, const Value*& outValue) const
131 {
132 for (auto& item : items)
133 {
134 if (item.key == key)
135 {
136 outValue = &item.value;
137 return true;
138 }
139 }
140 return false;
141 }
142
146 template <typename ComparableToKey>
147 [[nodiscard]] bool contains(const ComparableToKey& key, Value*& outValue)
148 {
149 for (auto& item : items)
150 {
151 if (item.key == key)
152 {
153 outValue = &item.value;
154 return true;
155 }
156 }
157 return false;
158 }
159
162 template <typename ComparableToKey>
163 [[nodiscard]] const Value* get(const ComparableToKey& key) const
164 {
165 for (auto& item : items)
166 {
167 if (item.key == key)
168 {
169 return &item.value;
170 }
171 }
172 return nullptr;
173 }
174
177 template <typename ComparableToKey>
178 [[nodiscard]] Value* get(const ComparableToKey& key)
179 {
180 for (auto& item : items)
181 {
182 if (item.key == key)
183 {
184 return &item.value;
185 }
186 }
187 return nullptr;
188 }
189
192 template <typename ComparableToKey>
193 [[nodiscard]] Value* getOrCreate(const ComparableToKey& key)
194 {
195 for (auto& item : items)
196 {
197 if (item.key == key)
198 {
199 return &item.value;
200 }
201 }
202 if (items.push_back({key, Value()}))
203 {
204 return &items.back().value;
205 }
206 return nullptr;
207 }
208};
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition: Compiler.h:269
A map holding VectorMapItem key-value pairs in an unsorted Vector.
Definition: VectorMap.h:33
auto size() const
Return the number of key-value pairs in the map.
Definition: VectorMap.h:39
Key * insertValueUniqueKey(Value &&value)
Inserts a new value, automatically generating key with Key::generateUniqueKey (works for StrongID for...
Definition: VectorMap.h:103
auto isEmpty() const
Check if the map is empty.
Definition: VectorMap.h:42
bool contains(const ComparableToKey &key, Value *&outValue)
Check if the given key is contained in the map.
Definition: VectorMap.h:147
const Value * get(const ComparableToKey &key) const
Get the Value associated to the given key.
Definition: VectorMap.h:163
bool contains(const ComparableToKey &key) const
Check if the given key is contained in the map.
Definition: VectorMap.h:114
bool insertIfNotExists(Item &&item)
Inserts an item if it doesn't exist already.
Definition: VectorMap.h:70
Value * getOrCreate(const ComparableToKey &key)
Get the value associated to the given key, or creates a new one if needed.
Definition: VectorMap.h:193
Value * get(const ComparableToKey &key)
Get the Value associated to the given key.
Definition: VectorMap.h:178
bool remove(const ComparableToKey &key)
Remove an item with matching key from the Map.
Definition: VectorMap.h:53
Value * insertOverwrite(Item &&item)
Insert an item, overwriting the potentially already existing one.
Definition: VectorMap.h:82
bool contains(const ComparableToKey &key, const Value *&outValue) const
Check if the given key is contained in the map.
Definition: VectorMap.h:130
The single item of VectorMap, holding a Key and Value.
Definition: VectorMap.h:22
Key key
Key item value.
Definition: VectorMap.h:23
Value value
Map item value.
Definition: VectorMap.h:24