Sane C++ Libraries: VectorMap.h Source File (original) (raw)

1

2

3#pragma once

4#include "../Containers/StrongID.h"

5#include "../Containers/Vector.h"

6

7namespace SC

8{

9template <typename Key, typename Value, typename Container>

10struct VectorMap;

11template <typename Key, typename Value>

12struct VectorMapItem;

13}

16

20template <typename Key, typename Value>

26

33template <typename Key, typename Value, typename Container = SC::Vector<SC::VectorMapItem<Key, Value>>>

35{

37

38 Container items;

39

41 [[nodiscard]] auto size() const { return items.size(); }

42

44 [[nodiscard]] auto isEmpty() const { return items.isEmpty(); }

45

46 [[nodiscard]] Item* begin() { return items.begin(); }

47 [[nodiscard]] const Item* begin() const { return items.begin(); }

48 [[nodiscard]] Item* end() { return items.end(); }

49 [[nodiscard]] const Item* end() const { return items.end(); }

50

54 template

55 [[nodiscard]] bool remove(const ComparableToKey& key)

56 {

57 size_t idx = 0;

58 for (auto& item : items)

59 {

60 if (item.key == key)

61 {

62 return items.removeAt(idx);

63 }

64 ++idx;

65 }

66 return false;

67 }

68

73 {

75 {

77 }

78 return false;

79 }

80

85 {

86 for (auto& it : items)

87 {

88 if (it.key == item.key)

89 {

90 it.value = move(item.value);

91 return &it.value;

92 }

93 }

95 {

96 return &items.back().value;

97 }

98 return nullptr;

99 }

100

106 {

107 if (items.push_back({Key::generateUniqueKey(*this), forward(value)}))

108 {

109 return &items.back().key;

110 }

111 return nullptr;

112 }

113

115 template

116 [[nodiscard]] bool contains(const ComparableToKey& key) const

117 {

118 for (auto& item : items)

119 {

120 if (item.key == key)

121 {

122 return true;

123 }

124 }

125 return false;

126 }

127

131 template

132 [[nodiscard]] bool contains(const ComparableToKey& key, const Value*& outValue) const

133 {

134 for (auto& item : items)

135 {

136 if (item.key == key)

137 {

138 outValue = &item.value;

139 return true;

140 }

141 }

142 return false;

143 }

144

148 template

149 [[nodiscard]] bool contains(const ComparableToKey& key, Value*& outValue)

150 {

151 for (auto& item : items)

152 {

153 if (item.key == key)

154 {

155 outValue = &item.value;

156 return true;

157 }

158 }

159 return false;

160 }

161

164 template

165 [[nodiscard]] const Value* get(const ComparableToKey& key) const

166 {

167 for (auto& item : items)

168 {

169 if (item.key == key)

170 {

171 return &item.value;

172 }

173 }

174 return nullptr;

175 }

176

179 template

180 [[nodiscard]] Value* get(const ComparableToKey& key)

181 {

182 for (auto& item : items)

183 {

184 if (item.key == key)

185 {

186 return &item.value;

187 }

188 }

189 return nullptr;

190 }

191

194 template

195 [[nodiscard]] Value* getOrCreate(const ComparableToKey& key)

196 {

197 for (auto& item : items)

198 {

199 if (item.key == key)

200 {

201 return &item.value;

202 }

203 }

204 if (items.push_back({key, Value()}))

205 {

206 return &items.back().value;

207 }

208 return nullptr;

209 }

210};

constexpr T && move(T &value)

Converts an lvalue to an rvalue reference.

Definition Compiler.h:264

constexpr T && forward(typename TypeTraits::RemoveReference< T >::type &value)

Forwards an lvalue or an rvalue as an rvalue reference.

Definition Compiler.h:267

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

A map holding VectorMapItem key-value pairs in an unsorted Vector.

Definition VectorMap.h:35

auto size() const

Return the number of key-value pairs in the map.

Definition VectorMap.h:41

Key * insertValueUniqueKey(Value &&value)

Inserts a new value, automatically generating key with Key::generateUniqueKey (works for StrongID for...

Definition VectorMap.h:105

auto isEmpty() const

Check if the map is empty.

Definition VectorMap.h:44

bool contains(const ComparableToKey &key, Value *&outValue)

Check if the given key is contained in the map.

Definition VectorMap.h:149

const Value * get(const ComparableToKey &key) const

Get the Value associated to the given key.

Definition VectorMap.h:165

bool contains(const ComparableToKey &key) const

Check if the given key is contained in the map.

Definition VectorMap.h:116

bool insertIfNotExists(Item &&item)

Inserts an item if it doesn't exist already.

Definition VectorMap.h:72

Value * getOrCreate(const ComparableToKey &key)

Get the value associated to the given key, or creates a new one if needed.

Definition VectorMap.h:195

Value * get(const ComparableToKey &key)

Get the Value associated to the given key.

Definition VectorMap.h:180

bool remove(const ComparableToKey &key)

Remove an item with matching key from the Map.

Definition VectorMap.h:55

Value * insertOverwrite(Item &&item)

Insert an item, overwriting the potentially already existing one.

Definition VectorMap.h:84

bool contains(const ComparableToKey &key, const Value *&outValue) const

Check if the given key is contained in the map.

Definition VectorMap.h:132