sead
Loading...
Searching...
No Matches
seadStrTreeMap.h
Go to the documentation of this file.
1#ifndef SEAD_STR_TREE_MAP_H_
2#define SEAD_STR_TREE_MAP_H_
3
4#include <basis/seadAssert.h>
5#include <container/seadTreeMapImpl.h>
6#include <container/seadFreeList.h>
7#include <prim/seadDelegate.h>
8#include <prim/seadSafeString.h>
9
10namespace sead {
11
12class Heap;
13
14template <s32 KeyStrN, typename Value>
16{
17public:
18 class Node : public TreeMapNode<SafeString>
19 {
20 public:
21 Node(const SafeString& akey, const Value& avalue, StrTreeMap<KeyStrN, Value>* map)
22 : mValue(avalue)
23 , mMap(map)
24 {
25 BufferedSafeString key(mKeyStr, KeyStrN + 1);
26 key.copy(akey);
27 mKey_ = key;
28 }
29
31 {
33 }
34
36 {
37 return mKey_;
38 }
39
40 Value& value()
41 {
42 return mValue;
43 }
44
45 private:
46 Value mValue;
47 StrTreeMap<KeyStrN, Value>* mMap;
48 char mKeyStr[KeyStrN + 1];
49 };
50
51public:
52 template <typename T> // T = {*}Delegate2<..., SafeString&, Value&>
54 {
55 public:
56 ForEachConstContext(const T& afun)
57 : fun(afun)
58 {
59 }
60
62 {
63 Node* node = static_cast<Node*>(n);
64 fun(node->key(), node->value());
65 }
66
67 private:
68 const T& fun;
69 };
70
71public:
73 : mSize(0)
74 , mNodeMax(0)
75 {
76 }
77
78 void setBuffer(s32 node_max, void* buf)
79 {
80 if (node_max > 0)
81 {
82 if (buf != nullptr)
83 {
84 mNodeMax = node_max;
85 mFreeList.init(buf, sizeof(Node), node_max);
86 }
87 else
88 {
89 SEAD_ASSERT_MSG(false, "buf is null");
90 }
91 }
92 else
93 {
94 SEAD_ASSERT_MSG(false, "node_max[%d] must be larger than zero", node_max);
95 }
96 }
97
98 void allocBuffer(s32 node_max, Heap* heap, s32 alignment = 4)
99 {
100 SEAD_ASSERT(mFreeList.work() == nullptr);
101 if (node_max > 0)
102 {
103 void* buf = new (heap, alignment) u8[node_max * sizeof(Node)]; // NewArray<u8>(node_max * sizeof(Node), heap, alignment)
104 setBuffer(node_max, buf);
105 }
106 else
107 {
108 SEAD_ASSERT_MSG(false, "node_max[%d] must be larger than zero", node_max);
109 }
110 }
111
113 {
114 if (isBufferReady())
115 {
116 clear();
117 delete[] static_cast<u8*>(mFreeList.work()); // DeleteArray<u8>(mFreeList.work())
118 mNodeMax = 0;
120 }
121 }
122
123 bool isBufferReady() const
124 {
125 return mFreeList.work() != nullptr;
126 }
127
128 bool isEmpty() const
129 {
130 return mSize == 0;
131 }
132
133 bool isFull() const
134 {
135 return mSize >= mNodeMax;
136 }
137
138 s32 size() const
139 {
140 return mSize;
141 }
142
143 s32 maxSize() const
144 {
145 return mNodeMax;
146 }
147
148 Value* find(const SafeString& key) const
149 {
150 Node* node = static_cast<Node*>(TreeMapImpl<SafeString>::find(key));
151 if (node != nullptr)
152 return &node->value();
153
154 return nullptr;
155 }
156
157 bool contains(const SafeString& key) const
158 {
159 return find(key) != nullptr;
160 }
161
162 void insert(const SafeString& key, const Value& value)
163 {
164 if (!isFull())
165 {
166 Node* node = new (mFreeList.get()) Node(key, value, this);
167 mSize++;
168 TreeMapImpl<SafeString>::insert(node);
169 }
170 else
171 {
172 Value* p_value = find(key);
173 if (p_value != nullptr)
174 new (p_value) Value(value); // ???
175 else
176 {
177 SEAD_ASSERT_MSG(false, "map is full.");
178 }
179 }
180 }
181
182 void clear()
183 {
184 TreeMapImpl<SafeString>::forEach(
185 DelegateCreator<
186 StrTreeMap<KeyStrN, Value>,
187 TreeMapNode<SafeString>*
188 >(
189 this,
190 &StrTreeMap<KeyStrN, Value>::eraseNodeForClear
191 )
192 );
193 mSize = 0;
194 mRoot = nullptr;
195 mFreeList.init(mFreeList.work(), sizeof(Node), mNodeMax);
196 }
197
198 void eraseNode(Node* node)
199 {
200 mFreeList.put(node);
201 mSize--;
202 }
203
205 {
206 static_cast<Node*>(n)->value(); // ... ?
207 }
208
209 template <typename T> // T = {*}Delegate2<..., SafeString&, Value&>
210 void forEach(const T& fun) const
211 {
212 ForEachConstContext<T> context(fun);
213
214 TreeMapImpl<SafeString>::forEach(
215 DelegateCreator<
216 ForEachConstContext<T>,
217 TreeMapNode<SafeString>*
218 >(
219 &context,
220 &ForEachConstContext<T>::call
221 )
222 );
223 }
224
225protected:
229};
230
231template <s32 KeyStrN, typename Value, s32 N>
232class FixedStrTreeMap : public StrTreeMap<KeyStrN, Value>
233{
234public:
236 : StrTreeMap<KeyStrN, Value>()
237 {
238 StrTreeMap<KeyStrN, Value>::setBuffer(N, mWork);
239 }
240
241protected:
242 void setBuffer(s32 node_max, void* buf);
243 void allocBuffer(s32 node_max, Heap* heap, s32 alignment = 4);
245
246protected:
247 u8 mWork[N*sizeof(typename StrTreeMap<KeyStrN, Value>::Node)];
248};
249
250} // namespace sead
251
252#endif // SEAD_STR_TREE_MAP_H_
Definition seadStrTreeMap.h:233
void setBuffer(s32 node_max, void *buf)
void allocBuffer(s32 node_max, Heap *heap, s32 alignment=4)
u8 mWork[N *sizeof(typename StrTreeMap< KeyStrN, Value >::Node)]
Definition seadStrTreeMap.h:247
FixedStrTreeMap()
Definition seadStrTreeMap.h:235
Definition seadFreeList.h:10
void put(void *ptr)
Definition seadFreeList.h:54
void * work() const
Definition seadFreeList.h:52
void * get()
Definition seadFreeList.h:36
void cleanup()
Definition seadFreeList.h:46
Definition seadHeap.h:23
Definition seadStrTreeMap.h:54
const T & fun
Definition seadStrTreeMap.h:68
ForEachConstContext(const T &afun)
Definition seadStrTreeMap.h:56
void call(TreeMapNode< SafeString > *n)
Definition seadStrTreeMap.h:61
Definition seadStrTreeMap.h:19
Node(const SafeString &akey, const Value &avalue, StrTreeMap< KeyStrN, Value > *map)
Definition seadStrTreeMap.h:21
Value mValue
Definition seadStrTreeMap.h:46
char mKeyStr[KeyStrN+1]
Definition seadStrTreeMap.h:48
StrTreeMap< KeyStrN, Value > * mMap
Definition seadStrTreeMap.h:47
Value & value()
Definition seadStrTreeMap.h:40
void erase_() override
Definition seadStrTreeMap.h:30
SafeString & key()
Definition seadStrTreeMap.h:35
Definition seadStrTreeMap.h:16
void freeBuffer()
Definition seadStrTreeMap.h:112
void insert(const SafeString &key, const Value &value)
Definition seadStrTreeMap.h:162
s32 size() const
Definition seadStrTreeMap.h:138
void setBuffer(s32 node_max, void *buf)
Definition seadStrTreeMap.h:78
Value * find(const SafeString &key) const
Definition seadStrTreeMap.h:148
s32 mNodeMax
Definition seadStrTreeMap.h:228
bool contains(const SafeString &key) const
Definition seadStrTreeMap.h:157
void forEach(const T &fun) const
Definition seadStrTreeMap.h:210
FreeList mFreeList
Definition seadStrTreeMap.h:226
void eraseNode(Node *node)
Definition seadStrTreeMap.h:198
StrTreeMap()
Definition seadStrTreeMap.h:72
bool isFull() const
Definition seadStrTreeMap.h:133
s32 maxSize() const
Definition seadStrTreeMap.h:143
bool isEmpty() const
Definition seadStrTreeMap.h:128
s32 mSize
Definition seadStrTreeMap.h:227
void clear()
Definition seadStrTreeMap.h:182
bool isBufferReady() const
Definition seadStrTreeMap.h:123
void eraseNodeForClear(TreeMapNode< SafeString > *n)
Definition seadStrTreeMap.h:204
void allocBuffer(s32 node_max, Heap *heap, s32 alignment=4)
Definition seadStrTreeMap.h:98
Definition seadTreeMapImpl.h:10
Definition seadAssert.h:44
SafeStringBase< char > SafeString
Definition seadSafeString.h:409
BufferedSafeStringBase< char > BufferedSafeString
Definition seadSafeString.h:411
#define SEAD_ASSERT(condition)
Definition seadAssert.h:24
#define SEAD_ASSERT_MSG(condition, format,...)
Definition seadAssert.h:33