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/rudimentsinlines.h>
6 #include <rudiments/private/nodeinlines.h>
7 
8 #define DICTIONARY_TEMPLATE \
9  template <class keytype, class valuetype>
10 
11 #define DICTIONARY_CLASS \
12  dictionary<keytype,valuetype>
13 
14 DICTIONARY_TEMPLATE
15 RUDIMENTS_TEMPLATE_INLINE
16 DICTIONARY_CLASS::dictionary() {
17  trackinsertionorder=true;
18 }
19 
20 DICTIONARY_TEMPLATE
21 RUDIMENTS_TEMPLATE_INLINE
22 DICTIONARY_CLASS::~dictionary() {
23  clear();
24 }
25 
26 DICTIONARY_TEMPLATE
27 RUDIMENTS_TEMPLATE_INLINE
28 bool DICTIONARY_CLASS::setTrackInsertionOrder(bool trackinsertionorder) {
29  if (!tree.getLength()) {
30  this->trackinsertionorder=trackinsertionorder;
31  return true;
32  }
33  return false;
34 }
35 
36 DICTIONARY_TEMPLATE
37 RUDIMENTS_TEMPLATE_INLINE
38 bool DICTIONARY_CLASS::getTrackInsertionOrder() {
39  return trackinsertionorder;
40 }
41 
42 DICTIONARY_TEMPLATE
43 RUDIMENTS_TEMPLATE_INLINE
44 void DICTIONARY_CLASS::setValue(keytype key, valuetype value) {
45  dictionarynode<keytype,valuetype> *dnode=getNode(key);
46  if (dnode) {
47  dnode->setValue(value);
48  } else {
49  dnode=new dictionarynode<keytype,valuetype>(key,value);
50  tree.insert(dnode);
51  if (trackinsertionorder) {
52  list.append(dnode);
53  }
54  }
55 }
56 
57 DICTIONARY_TEMPLATE
58 RUDIMENTS_TEMPLATE_INLINE
59 void DICTIONARY_CLASS::setValues(keytype *keys, valuetype *values) {
60  keytype *key=keys;
61  valuetype *value=values;
62  while (*key) {
63  setValue(*key,*value);
64  key++;
65  value++;
66  }
67 }
68 
69 DICTIONARY_TEMPLATE
70 RUDIMENTS_TEMPLATE_INLINE
71 void DICTIONARY_CLASS::setValues(keytype const *keys, valuetype const *values) {
72  if (keys && values) {
73  keytype const *key=keys;
74  valuetype const *value=values;
75  while (*key) {
76  setValue(*key,*value);
77  key++;
78  value++;
79  }
80  }
81 }
82 
83 DICTIONARY_TEMPLATE
84 RUDIMENTS_TEMPLATE_INLINE
85 void DICTIONARY_CLASS::setValues(keytype *keys, valuetype *values,
86  uint64_t count) {
87  if (keys && values) {
88  keytype *key=keys;
89  valuetype *value=values;
90  for (uint64_t i=0; i<count; i++) {
91  setValue(*key,*value);
92  key++;
93  value++;
94  }
95  }
96 }
97 
98 DICTIONARY_TEMPLATE
99 RUDIMENTS_TEMPLATE_INLINE
100 void DICTIONARY_CLASS::setValues(keytype const *keys, valuetype const *values,
101  uint64_t count) {
102  if (keys && values) {
103  keytype const *key=keys;
104  valuetype const *value=values;
105  for (uint64_t i=0; i<count; i++) {
106  setValue(*key,*value);
107  key++;
108  value++;
109  }
110  }
111 }
112 
113 DICTIONARY_TEMPLATE
114 RUDIMENTS_TEMPLATE_INLINE
115 void DICTIONARY_CLASS::setValues(dictionary<keytype,valuetype> *dict) {
116  if (dict) {
118  *node=dict->getList()->getFirst();
119  node; node=node->getNext()) {
120  setValue(node->getValue()->getKey(),
121  node->getValue()->getValue());
122  }
123  }
124 }
125 
126 DICTIONARY_TEMPLATE
127 RUDIMENTS_TEMPLATE_INLINE
128 bool DICTIONARY_CLASS::getValue(keytype key, valuetype *value) {
129  dictionarynode<keytype,valuetype> *dnode=getNode(key);
130  if (dnode) {
131  *value=dnode->getValue();
132  return true;
133  }
134  return false;
135 }
136 
137 DICTIONARY_TEMPLATE
138 RUDIMENTS_TEMPLATE_INLINE
139 valuetype DICTIONARY_CLASS::getValue(keytype key) {
140  valuetype value;
141  if (getValue(key,&value)) {
142  return value;
143  }
144  return (valuetype)0;
145 }
146 
147 DICTIONARY_TEMPLATE
148 RUDIMENTS_TEMPLATE_INLINE
149 dictionarynode<keytype,valuetype> *DICTIONARY_CLASS::getNode(keytype key) {
151  if (tnode) {
152  return tnode->getValue();
153  }
154  return NULL;
155 }
156 
157 DICTIONARY_TEMPLATE
158 RUDIMENTS_TEMPLATE_INLINE
159 bool DICTIONARY_CLASS::remove(keytype key) {
161  if (tnode) {
162  if (trackinsertionorder) {
163  list.remove(tnode->getValue());
164  }
165  return tree.remove(tnode);
166  }
167  return false;
168 }
169 
170 DICTIONARY_TEMPLATE
171 RUDIMENTS_TEMPLATE_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  return tree.remove(tnode);
180  }
181  return false;
182 }
183 
184 DICTIONARY_TEMPLATE
185 RUDIMENTS_TEMPLATE_INLINE
186 void DICTIONARY_CLASS::clear() {
188  list.getFirst(); node; node=node->getNext()) {
189  delete node->getValue();
190  }
191  tree.clear();
192  list.clear();
193 }
194 
195 DICTIONARY_TEMPLATE
196 RUDIMENTS_TEMPLATE_INLINE
197 linkedlist<keytype> *DICTIONARY_CLASS::getKeys() {
200  *node=getList()->getFirst(); node; node=node->getNext()) {
201  keys->append(node->getValue()->getKey());
202  }
203  return keys;
204 }
205 
206 DICTIONARY_TEMPLATE
207 RUDIMENTS_TEMPLATE_INLINE
208 avltree< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getTree() {
209  return &tree;
210 }
211 
212 DICTIONARY_TEMPLATE
213 RUDIMENTS_TEMPLATE_INLINE
214 linkedlist< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getList() {
215  if (!trackinsertionorder) {
216  list.clear();
218  *node=tree.getFirst(); node; node=node->getNext()) {
219  list.append(node->getValue());
220  }
221  }
222  return &list;
223 }
224 
225 DICTIONARY_TEMPLATE
226 RUDIMENTS_TEMPLATE_INLINE
227 void DICTIONARY_CLASS::print() {
229  list.getFirst(); node; node=node->getNext()) {
230  node->getValue()->print();
231  stdoutput.printf("\n");
232  }
233 }
234 
235 DICTIONARY_TEMPLATE
236 RUDIMENTS_TEMPLATE_INLINE
238  find(keytype key) {
239  dictionarynode<keytype,valuetype> fnode(key,(valuetype)0);
240  return tree.find(&fnode);
241 }
242 
243 #define DICTIONARYNODE_TEMPLATE \
244  template <class keytype, class valuetype>
245 
246 #define DICTIONARYNODE_CLASS \
247  dictionarynode<keytype,valuetype>
248 
249 DICTIONARYNODE_TEMPLATE
250 RUDIMENTS_TEMPLATE_INLINE
251 DICTIONARYNODE_CLASS::dictionarynode(keytype key, valuetype value) {
252  this->key=key;
253  this->value=value;
254 }
255 
256 DICTIONARYNODE_TEMPLATE
257 RUDIMENTS_TEMPLATE_INLINE
258 DICTIONARYNODE_CLASS::~dictionarynode() {}
259 
260 DICTIONARYNODE_TEMPLATE
261 RUDIMENTS_TEMPLATE_INLINE
262 void DICTIONARYNODE_CLASS::setKey(keytype key) {
263  this->key=key;
264 }
265 
266 DICTIONARYNODE_TEMPLATE
267 RUDIMENTS_TEMPLATE_INLINE
268 void DICTIONARYNODE_CLASS::setValue(valuetype value) {
269  this->value=value;
270 }
271 
272 DICTIONARYNODE_TEMPLATE
273 RUDIMENTS_TEMPLATE_INLINE
274 keytype DICTIONARYNODE_CLASS::getKey() const {
275  return key;
276 }
277 
278 DICTIONARYNODE_TEMPLATE
279 RUDIMENTS_TEMPLATE_INLINE
280 valuetype DICTIONARYNODE_CLASS::getValue() const {
281  return value;
282 }
283 
284 DICTIONARYNODE_TEMPLATE
285 RUDIMENTS_TEMPLATE_INLINE
286 int32_t DICTIONARYNODE_CLASS::compare(keytype testkey) const {
287  return node_compare(key,testkey);
288 }
289 
290 DICTIONARYNODE_TEMPLATE
291 RUDIMENTS_TEMPLATE_INLINE
292 int32_t DICTIONARYNODE_CLASS::compare(
293  dictionarynode<keytype,valuetype> *testnode) const {
294  return node_compare(key,testnode->key);
295 }
296 
297 DICTIONARYNODE_TEMPLATE
298 RUDIMENTS_TEMPLATE_INLINE
299 void DICTIONARYNODE_CLASS::print() const {
300  node_print(key);
301  stdoutput.printf(":");
302  node_print(value);
303 }
304 
305 
306 
307 DICTIONARYNODE_TEMPLATE
308 RUDIMENTS_TEMPLATE_INLINE
309 int32_t node_compare(
312  return node_compare(value1->getKey(),value2->getKey());
313 }
314 
315 DICTIONARYNODE_TEMPLATE
316 RUDIMENTS_TEMPLATE_INLINE
317 void node_compare(dictionarynode<keytype,valuetype> *value) {
318  node_print(value);
319 }
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