Rudiments
dictionaryinlines.h
1// Copyright (c) 1999-2018 David Muse
2// See the COPYING file for more information
3
4#include <rudiments/stdio.h>
5#include <rudiments/private/nodeinlines.h>
6
7template <class keytype, class valuetype>
8class dictionarypair : public object {
9 public:
11
12 void setKey(keytype key);
13 void setValue(valuetype value);
14 keytype getKey();
15 keytype &getKeyRef();
16 valuetype getValue();
17 valuetype &getValueRef();
18
19 private:
20 keytype key;
21 valuetype value;
22};
23
24template <class keytype, class valuetype>
26 public:
27 int32_t compare(object *value1, object *value2);
28};
29
30template <class keytype, class valuetype>
31inline
34 trackinsertionorder(true),
35 keylist(NULL) {
36
38 tree.setComparator(comp);
39
40#ifdef DARWIN_GCC_2952_HACKS
41 return;
42
43 // Various methods here use linkedlist and linkedlistnode, but if the
44 // calling app doens't happen to call one of them, then they get
45 // declared but the code that defines them never gets pulled in.
46 // On Darwin platforms, when using gcc 2.95.2 (and possibly other
47 // versions) this results in various undefined symbols. Adding some
48 // calls here, after the return causes the code to be included and the
49 // symbols to be defined. It's called here, after the return so that
50 // it never actually gets executed. The old compiler doesn't complain
51 // about that.
52 keylist=getKeys();
54#endif
55}
56
57template <class keytype, class valuetype>
58inline
62 keylist(NULL) {
63
65 clone(&a);
66
67#ifdef DARWIN_GCC_2952_HACKS
68 return;
69
70 // see note above
71 keylist=getKeys();
73#endif
74}
75
76template <class keytype, class valuetype>
77inline
81 keylist(NULL) {
82
84 clone(&a);
85
86#ifdef DARWIN_GCC_2952_HACKS
87 return;
88
89 // see note above
90 keylist=getKeys();
92#endif
93}
94
95template <class keytype, class valuetype>
96inline
106
107template <class keytype, class valuetype>
108inline
118
119template <class keytype, class valuetype>
120inline
122
123 trackinsertionorder=a->getTrackInsertionOrder();
124
125 // comp should already exist at this point
126 tree.setComparator(comp);
127
128 // We can't just set tree=a->tree and list=a->list because
129 // we don't have a node_duplicate_value() for a
130 // dictionarypair. We can't just implement one because it
131 // would need to know whether to duplicate the key and value
132 // individually.
133 //
134 // So, we'll manually build both by running through a.tree and
135 // adding its key/value pairs to ourself.
136 bool managekeys=this->getManageKeys();
137 bool managearraykeys=this->getManageArrayKeys();
138 bool managevalues=this->getManageValues();
139 bool managearrayvalues=this->getManageArrayValues();
141 *node=a->tree.getFirst(); node; node=node->getNext()) {
142 setValue(node_duplicate_value(
143 &(node->getValue()->getKeyRef()),
144 managekeys,managearraykeys),
145 node_duplicate_value(
146 &(node->getValue()->getValueRef()),
147 managevalues,managearrayvalues));
148 }
149
150 // if a's keylist was already built then go ahead and build ours too
151 if (a->keylist) {
152 getKeys();
153 }
154}
155
156template <class keytype, class valuetype>
157inline
160
161 trackinsertionorder=a->getTrackInsertionOrder();
162
163 // comp should already exist at this point
164 tree.setComparator(comp);
165
166 bool managekeys=this->getManageKeys();
167 bool managearraykeys=this->getManageArrayKeys();
168 bool managevalues=this->getManageValues();
169 bool managearrayvalues=this->getManageArrayValues();
170 for (listnode<keytype> *node=a->getKeys()->getFirst();
171 node; node=node->getNext()) {
172 keytype key=node->getValue();
173 valuetype value=a->getValue(key);
174 setValue(node_duplicate_value(&key,
175 managekeys,managearraykeys),
176 node_duplicate_value(&value,
177 managevalues,managearrayvalues));
178 }
179
180 // a's keylist may have been built as a result of the getKeys() call
181 // above, so go ahead and build ours too
182 getKeys();
183}
184
185template <class keytype, class valuetype>
186inline
188 clear();
189 delete comp;
190}
191
192template <class keytype, class valuetype>
193inline
195 bool trackinsertionorder) {
196 if (!tree.getCount()) {
197 this->trackinsertionorder=trackinsertionorder;
198 return true;
199 }
200 return false;
201}
202
203template <class keytype, class valuetype>
204inline
206 return trackinsertionorder;
207}
208
209template <class keytype, class valuetype>
210inline
213 if (dnode) {
214 dnode->setValue(value);
215 } else {
217 tree.insert(dnode);
218 if (trackinsertionorder) {
219 list.append(dnode);
220 }
221 }
222}
223
224template <class keytype, class valuetype>
225inline
227 valuetype *values) {
228 keytype *key=keys;
229 valuetype *value=values;
230 while (*key) {
231 setValue(*key,*value);
232 key++;
233 value++;
234 }
235}
236
237template <class keytype, class valuetype>
238inline
240 valuetype const *values) {
241 if (keys && values) {
242 keytype const *key=keys;
243 valuetype const *value=values;
244 while (*key) {
245 setValue(*key,*value);
246 key++;
247 value++;
248 }
249 }
250}
251
252template <class keytype, class valuetype>
253inline
255 valuetype *values,
256 uint64_t count) {
257 if (keys && values) {
258 keytype *key=keys;
259 valuetype *value=values;
260 for (uint64_t i=0; i<count; i++) {
261 setValue(*key,*value);
262 key++;
263 value++;
264 }
265 }
266}
267
268template <class keytype, class valuetype>
269inline
271 valuetype const *values,
272 uint64_t count) {
273 if (keys && values) {
274 keytype const *key=keys;
275 valuetype const *value=values;
276 for (uint64_t i=0; i<count; i++) {
277 setValue(*key,*value);
278 key++;
279 value++;
280 }
281 }
282}
283
284template <class keytype, class valuetype>
285inline
288 if (dict) {
289 if (dict->trackinsertionorder) {
291 *node=dict->list.getFirst();
292 node; node=node->getNext()) {
293 setValue(node->getValue()->getKey(),
294 node->getValue()->getValue());
295 }
296 } else {
298 *node=dict->tree.getFirst();
299 node; node=node->getNext()) {
300 setValue(node->getValue()->getKey(),
301 node->getValue()->getValue());
302 }
303 }
304 }
305}
306
307template <class keytype, class valuetype>
308inline
310 valuetype *value) {
312 if (dnode) {
313 *value=dnode->getValue();
314 return true;
315 }
316 return false;
317}
318
319template <class keytype, class valuetype>
320inline
322 valuetype value;
323 if (getValue(key,&value)) {
324 return value;
325 }
326 return (valuetype)0;
327}
328
329template <class keytype, class valuetype>
330inline
333 if (dnode) {
334 *k=dnode->getKey();
335 return true;
336 }
337 return false;
338}
339
340template <class keytype, class valuetype>
341inline
343 keytype k;
344 if (getKey(key,&k)) {
345 return k;
346 }
347 return (keytype)0;
348}
349
350template <class keytype, class valuetype>
351inline
355 if (tnode) {
356 return tnode->getValue();
357 }
358 return NULL;
359}
360
361template <class keytype, class valuetype>
362inline
364 delete keylist;
365 keylist=new linkedlist<keytype>();
366 if (trackinsertionorder) {
368 *node=list.getFirst(); node; node=node->getNext()) {
369 keylist->append(node->getValue()->getKey());
370 }
371 } else {
373 *node=tree.getFirst(); node; node=node->getNext()) {
374 keylist->append(node->getValue()->getKey());
375 }
376 }
377 return keylist;
378}
379
380template <class keytype, class valuetype>
381inline
383 return tree.getCount();
384}
385
386template <class keytype, class valuetype>
387inline
390 if (tnode) {
391 if (trackinsertionorder) {
392 list.remove(tnode->getValue());
393 }
394 node_delete_value(&(tnode->getValue()->getKeyRef()),
395 this->getManageKeys(),
396 this->getManageArrayKeys());
397 node_delete_value(&(tnode->getValue()->getValueRef()),
398 this->getManageValues(),
399 this->getManageArrayValues());
400 delete tnode->getValue();
401 return tree.remove(tnode);
402 }
403 return false;
404}
405
406template <class keytype, class valuetype>
407inline
409 bool managekeys=this->getManageKeys();
410 bool managearraykeys=this->getManageArrayKeys();
411 bool managevalues=this->getManageValues();
412 bool managearrayvalues=this->getManageArrayValues();
414 tree.getFirst(); node; node=node->getNext()) {
415 node_delete_value(&(node->getValue()->getKeyRef()),
416 managekeys,managearraykeys);
417 node_delete_value(&(node->getValue()->getValueRef()),
418 managevalues,managearrayvalues);
419 delete node->getValue();
420 }
421 tree.clear();
422 list.clear();
423 delete keylist;
424 keylist=NULL;
425 return true;
426}
427
428template <class keytype, class valuetype>
429inline
433 return tree.find(&fnode);
434}
435
436template <class keytype, class valuetype>
437inline
439 valuetype value) :
440 object(),
441 key(key),
442 value(value) {
443}
444
445template <class keytype, class valuetype>
446inline
448 this->key=key;
449}
450
451template <class keytype, class valuetype>
452inline
454 this->value=value;
455}
456
457template <class keytype, class valuetype>
458inline
460 return key;
461}
462
463template <class keytype, class valuetype>
464inline
466 return key;
467}
468
469template <class keytype, class valuetype>
470inline
472 return value;
473}
474
475template <class keytype, class valuetype>
476inline
478 return value;
479}
480
481template <class keytype, class valuetype>
482inline
Definition avltree.h:11
avltreenode(valuetype value)
Definition avltreeinlines.h:555
treenode< valuetype > * getNext()
Definition avltreeinlines.h:671
valuetype getValue()
Definition avltreeinlines.h:578
void setValue(valuetype value)
Definition avltreeinlines.h:572
collection & operator=(collection &c)
Definition collectioninlines.h:30
Definition comparator.h:11
virtual int32_t compare(char *value1, char *value2)
Definition comparatorinlines.h:61
Definition dictionary.h:12
dictionary< keytype, valuetype > & operator=(dictionary< keytype, valuetype > &a)
Definition dictionaryinlines.h:98
bool getTrackInsertionOrder()
Definition dictionaryinlines.h:205
bool setTrackInsertionOrder(bool trackinsertionorder)
Definition dictionaryinlines.h:194
bool clear()
Definition dictionaryinlines.h:408
bool getValue(keytype key, valuetype *value)
Definition dictionaryinlines.h:309
uint64_t getCount()
Definition dictionaryinlines.h:382
linkedlist< keytype > * getKeys()
Definition dictionaryinlines.h:363
bool getKey(keytype key, keytype *k)
Definition dictionaryinlines.h:331
void setValue(keytype key, valuetype value)
Definition dictionaryinlines.h:211
void setValues(keytype *keys, valuetype *values)
Definition dictionaryinlines.h:226
~dictionary()
Definition dictionaryinlines.h:187
bool remove(keytype key)
Definition dictionaryinlines.h:388
dictionary()
Definition dictionaryinlines.h:32
Definition dictionarycollection.h:13
Definition dictionaryinlines.h:8
Definition dictionaryinlines.h:25
int32_t compare(object *value1, object *value2)
Definition dictionaryinlines.h:483
Definition listcollection.h:12
Definition object.h:10
Definition treecollection.h:12