Rudiments
dictionaryinlines.h
1 // Copyright (c) 2003 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::remove(dictionarynode<keytype,valuetype> *node) {
174  *tnode=tree.find(node);
175  if (tnode) {
176  if (trackinsertionorder) {
177  list.remove(tnode->getValue());
178  }
179  delete tnode->getValue();
180  return tree.remove(tnode);
181  }
182  return false;
183 }
184 
185 DICTIONARY_TEMPLATE
186 inline
187 void DICTIONARY_CLASS::clear() {
189  list.getFirst(); node; node=node->getNext()) {
190  delete node->getValue();
191  }
192  tree.clear();
193  list.clear();
194 }
195 
196 DICTIONARY_TEMPLATE
197 inline
198 void DICTIONARY_CLASS::clearAndDelete() {
200  list.getFirst(); node; node=node->getNext()) {
201  delete node->getValue()->getKey();
202  delete node->getValue()->getValue();
203  delete node->getValue();
204  }
205  tree.clear();
206  list.clear();
207 }
208 
209 DICTIONARY_TEMPLATE
210 inline
211 void DICTIONARY_CLASS::clearAndArrayDelete() {
213  list.getFirst(); node; node=node->getNext()) {
214  delete[] node->getValue()->getKey();
215  delete[] node->getValue()->getValue();
216  delete node->getValue();
217  }
218  tree.clear();
219  list.clear();
220 }
221 
222 DICTIONARY_TEMPLATE
223 inline
224 void DICTIONARY_CLASS::clearAndDeleteKeys() {
226  list.getFirst(); node; node=node->getNext()) {
227  delete node->getValue()->getKey();
228  delete node->getValue();
229  }
230  tree.clear();
231  list.clear();
232 }
233 
234 DICTIONARY_TEMPLATE
235 inline
236 void DICTIONARY_CLASS::clearAndArrayDeleteKeys() {
238  list.getFirst(); node; node=node->getNext()) {
239  delete[] node->getValue()->getKey();
240  delete node->getValue();
241  }
242  tree.clear();
243  list.clear();
244 }
245 
246 DICTIONARY_TEMPLATE
247 inline
248 void DICTIONARY_CLASS::clearAndDeleteValues() {
250  list.getFirst(); node; node=node->getNext()) {
251  delete node->getValue()->getValue();
252  delete node->getValue();
253  }
254  tree.clear();
255  list.clear();
256 }
257 
258 DICTIONARY_TEMPLATE
259 inline
260 void DICTIONARY_CLASS::clearAndArrayDeleteValues() {
262  list.getFirst(); node; node=node->getNext()) {
263  delete[] node->getValue()->getValue();
264  delete node->getValue();
265  }
266  tree.clear();
267  list.clear();
268 }
269 
270 DICTIONARY_TEMPLATE
271 inline
272 void DICTIONARY_CLASS::clearAndDeleteKeysAndArrayDeleteValues() {
274  list.getFirst(); node; node=node->getNext()) {
275  delete node->getValue()->getKey();
276  delete[] node->getValue()->getValue();
277  delete node->getValue();
278  }
279  tree.clear();
280  list.clear();
281 }
282 
283 DICTIONARY_TEMPLATE
284 inline
285 void DICTIONARY_CLASS::clearAndArrayDeleteKeysAndDeleteValues() {
287  list.getFirst(); node; node=node->getNext()) {
288  delete[] node->getValue()->getKey();
289  delete node->getValue()->getValue();
290  delete node->getValue();
291  }
292  tree.clear();
293  list.clear();
294 }
295 
296 DICTIONARY_TEMPLATE
297 inline
298 linkedlist<keytype> *DICTIONARY_CLASS::getKeys() {
301  *node=getList()->getFirst(); node; node=node->getNext()) {
302  keys->append(node->getValue()->getKey());
303  }
304  return keys;
305 }
306 
307 DICTIONARY_TEMPLATE
308 inline
309 avltree< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getTree() {
310  return &tree;
311 }
312 
313 DICTIONARY_TEMPLATE
314 inline
315 linkedlist< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getList() {
316  if (!trackinsertionorder) {
317  list.clear();
319  *node=tree.getFirst(); node; node=node->getNext()) {
320  list.append(node->getValue());
321  }
322  }
323  return &list;
324 }
325 
326 DICTIONARY_TEMPLATE
327 inline
328 void DICTIONARY_CLASS::print() {
330  list.getFirst(); node; node=node->getNext()) {
331  node->getValue()->print();
332  stdoutput.printf("\n");
333  }
334 }
335 
336 DICTIONARY_TEMPLATE
337 inline
339  find(keytype key) {
340  dictionarynode<keytype,valuetype> fnode(key,(valuetype)0);
341  return tree.find(&fnode);
342 }
343 
344 #define DICTIONARYNODE_TEMPLATE \
345  template <class keytype, class valuetype>
346 
347 #define DICTIONARYNODE_CLASS \
348  dictionarynode<keytype,valuetype>
349 
350 DICTIONARYNODE_TEMPLATE
351 inline
352 DICTIONARYNODE_CLASS::dictionarynode(keytype key, valuetype value) {
353  this->key=key;
354  this->value=value;
355 }
356 
357 DICTIONARYNODE_TEMPLATE
358 inline
359 DICTIONARYNODE_CLASS::~dictionarynode() {}
360 
361 DICTIONARYNODE_TEMPLATE
362 inline
363 void DICTIONARYNODE_CLASS::setKey(keytype key) {
364  this->key=key;
365 }
366 
367 DICTIONARYNODE_TEMPLATE
368 inline
369 void DICTIONARYNODE_CLASS::setValue(valuetype value) {
370  this->value=value;
371 }
372 
373 DICTIONARYNODE_TEMPLATE
374 inline
375 keytype DICTIONARYNODE_CLASS::getKey() const {
376  return key;
377 }
378 
379 DICTIONARYNODE_TEMPLATE
380 inline
381 valuetype DICTIONARYNODE_CLASS::getValue() const {
382  return value;
383 }
384 
385 DICTIONARYNODE_TEMPLATE
386 inline
387 int32_t DICTIONARYNODE_CLASS::compare(keytype testkey) const {
388  return node_compare(key,testkey);
389 }
390 
391 DICTIONARYNODE_TEMPLATE
392 inline
393 int32_t DICTIONARYNODE_CLASS::compare(
394  dictionarynode<keytype,valuetype> *testnode) const {
395  return node_compare(key,testnode->key);
396 }
397 
398 DICTIONARYNODE_TEMPLATE
399 inline
400 void DICTIONARYNODE_CLASS::print() const {
401  node_print(key);
402  stdoutput.printf(":");
403  node_print(value);
404 }
405 
406 
407 
408 DICTIONARYNODE_TEMPLATE
409 inline
410 int32_t node_compare(
413  return node_compare(value1->getKey(),value2->getKey());
414 }
415 
416 DICTIONARYNODE_TEMPLATE
417 inline
418 void node_compare(dictionarynode<keytype,valuetype> *value) {
419  node_print(value);
420 }
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