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 bool DICTIONARY_CLASS::getValue(keytype key, valuetype *value) {
60  dictionarynode<keytype,valuetype> *dnode=getNode(key);
61  if (dnode) {
62  *value=dnode->getValue();
63  return true;
64  }
65  return false;
66 }
67 
68 DICTIONARY_TEMPLATE
69 RUDIMENTS_TEMPLATE_INLINE
70 valuetype DICTIONARY_CLASS::getValue(keytype key) {
71  valuetype value;
72  if (getValue(key,&value)) {
73  return value;
74  }
75  return (valuetype)0;
76 }
77 
78 DICTIONARY_TEMPLATE
79 RUDIMENTS_TEMPLATE_INLINE
80 dictionarynode<keytype,valuetype> *DICTIONARY_CLASS::getNode(keytype key) {
82  if (tnode) {
83  return tnode->getValue();
84  }
85  return NULL;
86 }
87 
88 DICTIONARY_TEMPLATE
89 RUDIMENTS_TEMPLATE_INLINE
90 bool DICTIONARY_CLASS::remove(keytype key) {
92  if (tnode) {
93  if (trackinsertionorder) {
94  list.remove(tnode->getValue());
95  }
96  return tree.remove(tnode);
97  }
98  return false;
99 }
100 
101 DICTIONARY_TEMPLATE
102 RUDIMENTS_TEMPLATE_INLINE
103 bool DICTIONARY_CLASS::remove(dictionarynode<keytype,valuetype> *node) {
105  *tnode=tree.find(node);
106  if (tnode) {
107  if (trackinsertionorder) {
108  list.remove(tnode->getValue());
109  }
110  return tree.remove(tnode);
111  }
112  return false;
113 }
114 
115 DICTIONARY_TEMPLATE
116 RUDIMENTS_TEMPLATE_INLINE
117 void DICTIONARY_CLASS::clear() {
119  list.getFirst(); node; node=node->getNext()) {
120  delete node->getValue();
121  }
122  tree.clear();
123  list.clear();
124 }
125 
126 DICTIONARY_TEMPLATE
127 RUDIMENTS_TEMPLATE_INLINE
128 linkedlist<keytype> *DICTIONARY_CLASS::getKeys() {
131  *node=getList()->getFirst(); node; node=node->getNext()) {
132  keys->append(node->getValue()->getKey());
133  }
134  return keys;
135 }
136 
137 DICTIONARY_TEMPLATE
138 RUDIMENTS_TEMPLATE_INLINE
139 avltree< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getTree() {
140  return &tree;
141 }
142 
143 DICTIONARY_TEMPLATE
144 RUDIMENTS_TEMPLATE_INLINE
145 linkedlist< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getList() {
146  if (!trackinsertionorder) {
147  list.clear();
149  *node=tree.getFirst(); node; node=node->getNext()) {
150  list.append(node->getValue());
151  }
152  }
153  return &list;
154 }
155 
156 DICTIONARY_TEMPLATE
157 RUDIMENTS_TEMPLATE_INLINE
158 void DICTIONARY_CLASS::print() {
160  list.getFirst(); node; node=node->getNext()) {
161  node->getValue()->print();
162  stdoutput.printf("\n");
163  }
164 }
165 
166 DICTIONARY_TEMPLATE
167 RUDIMENTS_TEMPLATE_INLINE
169  find(keytype key) {
170  dictionarynode<keytype,valuetype> fnode(key,(valuetype)0);
171  return tree.find(&fnode);
172 }
173 
174 #define DICTIONARYNODE_TEMPLATE \
175  template <class keytype, class valuetype>
176 
177 #define DICTIONARYNODE_CLASS \
178  dictionarynode<keytype,valuetype>
179 
180 DICTIONARYNODE_TEMPLATE
181 RUDIMENTS_TEMPLATE_INLINE
182 DICTIONARYNODE_CLASS::dictionarynode(keytype key, valuetype value) {
183  this->key=key;
184  this->value=value;
185 }
186 
187 DICTIONARYNODE_TEMPLATE
188 RUDIMENTS_TEMPLATE_INLINE
189 DICTIONARYNODE_CLASS::~dictionarynode() {}
190 
191 DICTIONARYNODE_TEMPLATE
192 RUDIMENTS_TEMPLATE_INLINE
193 void DICTIONARYNODE_CLASS::setKey(keytype key) {
194  this->key=key;
195 }
196 
197 DICTIONARYNODE_TEMPLATE
198 RUDIMENTS_TEMPLATE_INLINE
199 void DICTIONARYNODE_CLASS::setValue(valuetype value) {
200  this->value=value;
201 }
202 
203 DICTIONARYNODE_TEMPLATE
204 RUDIMENTS_TEMPLATE_INLINE
205 keytype DICTIONARYNODE_CLASS::getKey() const {
206  return key;
207 }
208 
209 DICTIONARYNODE_TEMPLATE
210 RUDIMENTS_TEMPLATE_INLINE
211 valuetype DICTIONARYNODE_CLASS::getValue() const {
212  return value;
213 }
214 
215 DICTIONARYNODE_TEMPLATE
216 RUDIMENTS_TEMPLATE_INLINE
217 int32_t DICTIONARYNODE_CLASS::compare(keytype testkey) const {
218  return node_compare(key,testkey);
219 }
220 
221 DICTIONARYNODE_TEMPLATE
222 RUDIMENTS_TEMPLATE_INLINE
223 int32_t DICTIONARYNODE_CLASS::compare(
224  dictionarynode<keytype,valuetype> *testnode) const {
225  return node_compare(key,testnode->key);
226 }
227 
228 DICTIONARYNODE_TEMPLATE
229 RUDIMENTS_TEMPLATE_INLINE
230 void DICTIONARYNODE_CLASS::print() const {
231  node_print(key);
232  stdoutput.printf(":");
233  node_print(value);
234 }
235 
236 
237 
238 DICTIONARYNODE_TEMPLATE
239 RUDIMENTS_TEMPLATE_INLINE
240 int32_t node_compare(
243  return node_compare(value1->getKey(),value2->getKey());
244 }
245 
246 DICTIONARYNODE_TEMPLATE
247 RUDIMENTS_TEMPLATE_INLINE
248 void node_compare(dictionarynode<keytype,valuetype> *value) {
249  node_print(value);
250 }
Definition: avltree.h:77
size_t printf(const char *format,...)
void setValue(valuetype value)
Definition: linkedlist.h:60
valuetype getValue() const
Definition: dictionary.h:12
void append(valuetype value)
Definition: linkedlist.h:11
keytype getKey() const
Definition: avltree.h:11
valuetype getValue() const