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 
7 #define DICTIONARY_TEMPLATE \
8  template <class keytype, class valuetype>
9 
10 #define DICTIONARY_CLASS \
11  dictionary<keytype,valuetype>
12 
13 DICTIONARY_TEMPLATE
14 inline
15 DICTIONARY_CLASS::dictionary() {
16  trackinsertionorder=true;
17 }
18 
19 DICTIONARY_TEMPLATE
20 inline
21 DICTIONARY_CLASS::~dictionary() {
22  clear();
23 }
24 
25 DICTIONARY_TEMPLATE
26 inline
27 bool DICTIONARY_CLASS::setTrackInsertionOrder(bool trackinsertionorder) {
28  if (!tree.getLength()) {
29  this->trackinsertionorder=trackinsertionorder;
30  return true;
31  }
32  return false;
33 }
34 
35 DICTIONARY_TEMPLATE
36 inline
37 bool DICTIONARY_CLASS::getTrackInsertionOrder() {
38  return trackinsertionorder;
39 }
40 
41 DICTIONARY_TEMPLATE
42 inline
43 void DICTIONARY_CLASS::setValue(keytype key, valuetype value) {
44  dictionarynode<keytype,valuetype> *dnode=getNode(key);
45  if (dnode) {
46  dnode->setValue(value);
47  } else {
48  dnode=new dictionarynode<keytype,valuetype>(key,value);
49  tree.insert(dnode);
50  if (trackinsertionorder) {
51  list.append(dnode);
52  }
53  }
54 }
55 
56 DICTIONARY_TEMPLATE
57 inline
58 void DICTIONARY_CLASS::setValues(keytype *keys, valuetype *values) {
59  keytype *key=keys;
60  valuetype *value=values;
61  while (*key) {
62  setValue(*key,*value);
63  key++;
64  value++;
65  }
66 }
67 
68 DICTIONARY_TEMPLATE
69 inline
70 void DICTIONARY_CLASS::setValues(keytype const *keys, valuetype const *values) {
71  if (keys && values) {
72  keytype const *key=keys;
73  valuetype const *value=values;
74  while (*key) {
75  setValue(*key,*value);
76  key++;
77  value++;
78  }
79  }
80 }
81 
82 DICTIONARY_TEMPLATE
83 inline
84 void DICTIONARY_CLASS::setValues(keytype *keys, valuetype *values,
85  uint64_t count) {
86  if (keys && values) {
87  keytype *key=keys;
88  valuetype *value=values;
89  for (uint64_t i=0; i<count; i++) {
90  setValue(*key,*value);
91  key++;
92  value++;
93  }
94  }
95 }
96 
97 DICTIONARY_TEMPLATE
98 inline
99 void DICTIONARY_CLASS::setValues(keytype const *keys, valuetype const *values,
100  uint64_t count) {
101  if (keys && values) {
102  keytype const *key=keys;
103  valuetype const *value=values;
104  for (uint64_t i=0; i<count; i++) {
105  setValue(*key,*value);
106  key++;
107  value++;
108  }
109  }
110 }
111 
112 DICTIONARY_TEMPLATE
113 inline
114 void DICTIONARY_CLASS::setValues(dictionary<keytype,valuetype> *dict) {
115  if (dict) {
117  *node=dict->getList()->getFirst();
118  node; node=node->getNext()) {
119  setValue(node->getValue()->getKey(),
120  node->getValue()->getValue());
121  }
122  }
123 }
124 
125 DICTIONARY_TEMPLATE
126 inline
127 bool DICTIONARY_CLASS::getValue(keytype key, valuetype *value) {
128  dictionarynode<keytype,valuetype> *dnode=getNode(key);
129  if (dnode) {
130  *value=dnode->getValue();
131  return true;
132  }
133  return false;
134 }
135 
136 DICTIONARY_TEMPLATE
137 inline
138 valuetype DICTIONARY_CLASS::getValue(keytype key) {
139  valuetype value;
140  if (getValue(key,&value)) {
141  return value;
142  }
143  return (valuetype)0;
144 }
145 
146 DICTIONARY_TEMPLATE
147 inline
148 dictionarynode<keytype,valuetype> *DICTIONARY_CLASS::getNode(keytype key) {
150  if (tnode) {
151  return tnode->getValue();
152  }
153  return NULL;
154 }
155 
156 DICTIONARY_TEMPLATE
157 inline
158 bool DICTIONARY_CLASS::remove(keytype key) {
160  if (tnode) {
161  if (trackinsertionorder) {
162  list.remove(tnode->getValue());
163  }
164  delete tnode->getValue();
165  return tree.remove(tnode);
166  }
167  return false;
168 }
169 
170 DICTIONARY_TEMPLATE
171 inline
172 bool DICTIONARY_CLASS::removeAndDelete(keytype key) {
174  if (tnode) {
175  if (trackinsertionorder) {
176  list.remove(tnode->getValue());
177  }
178  delete tnode->getValue()->getKey();
179  delete tnode->getValue()->getValue();
180  delete tnode->getValue();
181  return tree.remove(tnode);
182  }
183  return false;
184 }
185 
186 DICTIONARY_TEMPLATE
187 inline
188 bool DICTIONARY_CLASS::removeAndArrayDelete(keytype key) {
190  if (tnode) {
191  if (trackinsertionorder) {
192  list.remove(tnode->getValue());
193  }
194  delete[] tnode->getValue()->getKey();
195  delete[] tnode->getValue()->getValue();
196  delete tnode->getValue();
197  return tree.remove(tnode);
198  }
199  return false;
200 }
201 
202 DICTIONARY_TEMPLATE
203 inline
204 bool DICTIONARY_CLASS::removeAndDeleteKey(keytype key) {
206  if (tnode) {
207  if (trackinsertionorder) {
208  list.remove(tnode->getValue());
209  }
210  delete tnode->getValue()->getKey();
211  delete tnode->getValue();
212  return tree.remove(tnode);
213  }
214  return false;
215 }
216 
217 DICTIONARY_TEMPLATE
218 inline
219 bool DICTIONARY_CLASS::removeAndArrayDeleteKey(keytype key) {
221  if (tnode) {
222  if (trackinsertionorder) {
223  list.remove(tnode->getValue());
224  }
225  delete[] tnode->getValue()->getKey();
226  delete tnode->getValue();
227  return tree.remove(tnode);
228  }
229  return false;
230 }
231 
232 DICTIONARY_TEMPLATE
233 inline
234 bool DICTIONARY_CLASS::removeAndDeleteValue(keytype key) {
236  if (tnode) {
237  if (trackinsertionorder) {
238  list.remove(tnode->getValue());
239  }
240  delete tnode->getValue()->getValue();
241  delete tnode->getValue();
242  return tree.remove(tnode);
243  }
244  return false;
245 }
246 
247 DICTIONARY_TEMPLATE
248 inline
249 bool DICTIONARY_CLASS::removeAndArrayDeleteValue(keytype key) {
251  if (tnode) {
252  if (trackinsertionorder) {
253  list.remove(tnode->getValue());
254  }
255  delete[] tnode->getValue()->getValue();
256  delete tnode->getValue();
257  return tree.remove(tnode);
258  }
259  return false;
260 }
261 
262 DICTIONARY_TEMPLATE
263 inline
264 bool DICTIONARY_CLASS::removeAndDeleteKeyAndArrayDeleteValue(keytype key) {
266  if (tnode) {
267  if (trackinsertionorder) {
268  list.remove(tnode->getValue());
269  }
270  delete tnode->getValue()->getKey();
271  delete[] tnode->getValue()->getValue();
272  delete tnode->getValue();
273  return tree.remove(tnode);
274  }
275  return false;
276 }
277 
278 DICTIONARY_TEMPLATE
279 inline
280 bool DICTIONARY_CLASS::removeAndArrayDeleteKeyAndDeleteValue(keytype key) {
282  if (tnode) {
283  if (trackinsertionorder) {
284  list.remove(tnode->getValue());
285  }
286  delete[] tnode->getValue()->getKey();
287  delete tnode->getValue()->getValue();
288  delete tnode->getValue();
289  return tree.remove(tnode);
290  }
291  return false;
292 }
293 
294 DICTIONARY_TEMPLATE
295 inline
296 bool DICTIONARY_CLASS::remove(dictionarynode<keytype,valuetype> *node) {
298  *tnode=tree.find(node);
299  if (tnode) {
300  if (trackinsertionorder) {
301  list.remove(tnode->getValue());
302  }
303  delete tnode->getValue();
304  return tree.remove(tnode);
305  }
306  return false;
307 }
308 
309 DICTIONARY_TEMPLATE
310 inline
311 bool DICTIONARY_CLASS::removeAndDelete(
314  *tnode=tree.find(node);
315  if (tnode) {
316  if (trackinsertionorder) {
317  list.remove(tnode->getValue());
318  }
319  delete tnode->getValue()->getKey();
320  delete tnode->getValue()->getValue();
321  delete tnode->getValue();
322  return tree.remove(tnode);
323  }
324  return false;
325 }
326 
327 DICTIONARY_TEMPLATE
328 inline
329 bool DICTIONARY_CLASS::removeAndArrayDelete(
332  *tnode=tree.find(node);
333  if (tnode) {
334  if (trackinsertionorder) {
335  list.remove(tnode->getValue());
336  }
337  delete[] tnode->getValue()->getKey();
338  delete[] tnode->getValue()->getValue();
339  delete tnode->getValue();
340  return tree.remove(tnode);
341  }
342  return false;
343 }
344 
345 DICTIONARY_TEMPLATE
346 inline
347 bool DICTIONARY_CLASS::removeAndDeleteKey(
350  *tnode=tree.find(node);
351  if (tnode) {
352  if (trackinsertionorder) {
353  list.remove(tnode->getValue());
354  }
355  delete tnode->getValue()->getKey();
356  delete tnode->getValue();
357  return tree.remove(tnode);
358  }
359  return false;
360 }
361 
362 DICTIONARY_TEMPLATE
363 inline
364 bool DICTIONARY_CLASS::removeAndArrayDeleteKey(
367  *tnode=tree.find(node);
368  if (tnode) {
369  if (trackinsertionorder) {
370  list.remove(tnode->getValue());
371  }
372  delete[] tnode->getValue()->getKey();
373  delete tnode->getValue();
374  return tree.remove(tnode);
375  }
376  return false;
377 }
378 
379 DICTIONARY_TEMPLATE
380 inline
381 bool DICTIONARY_CLASS::removeAndDeleteValue(
384  *tnode=tree.find(node);
385  if (tnode) {
386  if (trackinsertionorder) {
387  list.remove(tnode->getValue());
388  }
389  delete tnode->getValue()->getValue();
390  delete tnode->getValue();
391  return tree.remove(tnode);
392  }
393  return false;
394 }
395 
396 DICTIONARY_TEMPLATE
397 inline
398 bool DICTIONARY_CLASS::removeAndArrayDeleteValue(
401  *tnode=tree.find(node);
402  if (tnode) {
403  if (trackinsertionorder) {
404  list.remove(tnode->getValue());
405  }
406  delete[] tnode->getValue()->getValue();
407  delete tnode->getValue();
408  return tree.remove(tnode);
409  }
410  return false;
411 }
412 
413 DICTIONARY_TEMPLATE
414 inline
415 bool DICTIONARY_CLASS::removeAndDeleteKeyAndArrayDeleteValue(
418  *tnode=tree.find(node);
419  if (tnode) {
420  if (trackinsertionorder) {
421  list.remove(tnode->getValue());
422  }
423  delete tnode->getValue()->getKey();
424  delete[] tnode->getValue()->getValue();
425  delete tnode->getValue();
426  return tree.remove(tnode);
427  }
428  return false;
429 }
430 
431 DICTIONARY_TEMPLATE
432 inline
433 bool DICTIONARY_CLASS::removeAndArrayDeleteKeyAndDeleteValue(
436  *tnode=tree.find(node);
437  if (tnode) {
438  if (trackinsertionorder) {
439  list.remove(tnode->getValue());
440  }
441  delete[] tnode->getValue()->getKey();
442  delete tnode->getValue()->getValue();
443  delete tnode->getValue();
444  return tree.remove(tnode);
445  }
446  return false;
447 }
448 
449 DICTIONARY_TEMPLATE
450 inline
451 void DICTIONARY_CLASS::clear() {
453  list.getFirst(); node; node=node->getNext()) {
454  delete node->getValue();
455  }
456  tree.clear();
457  list.clear();
458 }
459 
460 DICTIONARY_TEMPLATE
461 inline
462 void DICTIONARY_CLASS::clearAndDelete() {
464  list.getFirst(); node; node=node->getNext()) {
465  delete node->getValue()->getKey();
466  delete node->getValue()->getValue();
467  delete node->getValue();
468  }
469  tree.clear();
470  list.clear();
471 }
472 
473 DICTIONARY_TEMPLATE
474 inline
475 void DICTIONARY_CLASS::clearAndArrayDelete() {
477  list.getFirst(); node; node=node->getNext()) {
478  delete[] node->getValue()->getKey();
479  delete[] node->getValue()->getValue();
480  delete node->getValue();
481  }
482  tree.clear();
483  list.clear();
484 }
485 
486 DICTIONARY_TEMPLATE
487 inline
488 void DICTIONARY_CLASS::clearAndDeleteKeys() {
490  list.getFirst(); node; node=node->getNext()) {
491  delete node->getValue()->getKey();
492  delete node->getValue();
493  }
494  tree.clear();
495  list.clear();
496 }
497 
498 DICTIONARY_TEMPLATE
499 inline
500 void DICTIONARY_CLASS::clearAndArrayDeleteKeys() {
502  list.getFirst(); node; node=node->getNext()) {
503  delete[] node->getValue()->getKey();
504  delete node->getValue();
505  }
506  tree.clear();
507  list.clear();
508 }
509 
510 DICTIONARY_TEMPLATE
511 inline
512 void DICTIONARY_CLASS::clearAndDeleteValues() {
514  list.getFirst(); node; node=node->getNext()) {
515  delete node->getValue()->getValue();
516  delete node->getValue();
517  }
518  tree.clear();
519  list.clear();
520 }
521 
522 DICTIONARY_TEMPLATE
523 inline
524 void DICTIONARY_CLASS::clearAndArrayDeleteValues() {
526  list.getFirst(); node; node=node->getNext()) {
527  delete[] node->getValue()->getValue();
528  delete node->getValue();
529  }
530  tree.clear();
531  list.clear();
532 }
533 
534 DICTIONARY_TEMPLATE
535 inline
536 void DICTIONARY_CLASS::clearAndDeleteKeysAndArrayDeleteValues() {
538  list.getFirst(); node; node=node->getNext()) {
539  delete node->getValue()->getKey();
540  delete[] node->getValue()->getValue();
541  delete node->getValue();
542  }
543  tree.clear();
544  list.clear();
545 }
546 
547 DICTIONARY_TEMPLATE
548 inline
549 void DICTIONARY_CLASS::clearAndArrayDeleteKeysAndDeleteValues() {
551  list.getFirst(); node; node=node->getNext()) {
552  delete[] node->getValue()->getKey();
553  delete node->getValue()->getValue();
554  delete node->getValue();
555  }
556  tree.clear();
557  list.clear();
558 }
559 
560 DICTIONARY_TEMPLATE
561 inline
562 linkedlist<keytype> *DICTIONARY_CLASS::getKeys() {
565  *node=getList()->getFirst(); node; node=node->getNext()) {
566  keys->append(node->getValue()->getKey());
567  }
568  return keys;
569 }
570 
571 DICTIONARY_TEMPLATE
572 inline
573 avltree< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getTree() {
574  return &tree;
575 }
576 
577 DICTIONARY_TEMPLATE
578 inline
579 linkedlist< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getList() {
580  if (!trackinsertionorder) {
581  list.clear();
583  *node=tree.getFirst(); node; node=node->getNext()) {
584  list.append(node->getValue());
585  }
586  }
587  return &list;
588 }
589 
590 DICTIONARY_TEMPLATE
591 inline
592 void DICTIONARY_CLASS::print() {
594  list.getFirst(); node; node=node->getNext()) {
595  node->getValue()->print();
596  stdoutput.printf("\n");
597  }
598 }
599 
600 DICTIONARY_TEMPLATE
601 inline
603  find(keytype key) {
604  dictionarynode<keytype,valuetype> fnode(key,(valuetype)0);
605  return tree.find(&fnode);
606 }
607 
608 #define DICTIONARYNODE_TEMPLATE \
609  template <class keytype, class valuetype>
610 
611 #define DICTIONARYNODE_CLASS \
612  dictionarynode<keytype,valuetype>
613 
614 DICTIONARYNODE_TEMPLATE
615 inline
616 DICTIONARYNODE_CLASS::dictionarynode(keytype key, valuetype value) {
617  this->key=key;
618  this->value=value;
619 }
620 
621 DICTIONARYNODE_TEMPLATE
622 inline
623 DICTIONARYNODE_CLASS::~dictionarynode() {}
624 
625 DICTIONARYNODE_TEMPLATE
626 inline
627 void DICTIONARYNODE_CLASS::setKey(keytype key) {
628  this->key=key;
629 }
630 
631 DICTIONARYNODE_TEMPLATE
632 inline
633 void DICTIONARYNODE_CLASS::setValue(valuetype value) {
634  this->value=value;
635 }
636 
637 DICTIONARYNODE_TEMPLATE
638 inline
639 keytype DICTIONARYNODE_CLASS::getKey() const {
640  return key;
641 }
642 
643 DICTIONARYNODE_TEMPLATE
644 inline
645 valuetype DICTIONARYNODE_CLASS::getValue() const {
646  return value;
647 }
648 
649 DICTIONARYNODE_TEMPLATE
650 inline
651 int32_t DICTIONARYNODE_CLASS::compare(keytype testkey) const {
652  return node_compare(key,testkey);
653 }
654 
655 DICTIONARYNODE_TEMPLATE
656 inline
657 int32_t DICTIONARYNODE_CLASS::compare(
658  dictionarynode<keytype,valuetype> *testnode) const {
659  return node_compare(key,testnode->key);
660 }
661 
662 DICTIONARYNODE_TEMPLATE
663 inline
664 void DICTIONARYNODE_CLASS::print() const {
665  node_print(key);
666  stdoutput.printf(":");
667  node_print(value);
668 }
669 
670 
671 
672 DICTIONARYNODE_TEMPLATE
673 inline
674 int32_t node_compare(
677  return node_compare(value1->getKey(),value2->getKey());
678 }
679 
680 DICTIONARYNODE_TEMPLATE
681 inline
682 void node_compare(dictionarynode<keytype,valuetype> *value) {
683  node_print(value);
684 }
valuetype getValue() const
Definition: avltree.h:77
size_t printf(const char *format,...)
void setValue(valuetype value)
Definition: linkedlist.h:60
Definition: dictionary.h:12
Definition: dictionary.h:63
linkedlist< dictionarynode< keytype, valuetype > * > * getList()
void append(valuetype value)
Definition: linkedlist.h:11
valuetype getValue() const
keytype getKey() const
Definition: avltree.h:11