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/linkedlistutilinlines.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 }
18 
19 DICTIONARY_TEMPLATE
20 RUDIMENTS_TEMPLATE_INLINE
21 DICTIONARY_CLASS::~dictionary() {
22  dict.clear();
23 }
24 
25 DICTIONARY_TEMPLATE
26 RUDIMENTS_TEMPLATE_INLINE
27 void DICTIONARY_CLASS::setValue(keytype key, valuetype value) {
29  if (node) {
30  node->getValue()->setValue(value);
31  } else {
32  dict.append(new dictionarynode<keytype,valuetype>(key,value));
33  }
34 }
35 
36 DICTIONARY_TEMPLATE
37 RUDIMENTS_TEMPLATE_INLINE
38 bool DICTIONARY_CLASS::getValue(keytype key, valuetype *value) {
40  if (node) {
41  *value=node->getValue()->getValue();
42  return true;
43  }
44  return false;
45 }
46 
47 DICTIONARY_TEMPLATE
48 RUDIMENTS_TEMPLATE_INLINE
49 valuetype DICTIONARY_CLASS::getValue(keytype key) {
51  if (node) {
52  return node->getValue()->getValue();
53  }
54  return (valuetype)0;
55 }
56 
57 DICTIONARY_TEMPLATE
58 RUDIMENTS_TEMPLATE_INLINE
59 dictionarynode<keytype,valuetype> *DICTIONARY_CLASS::getNode(keytype key) {
61  if (node) {
62  return node->getValue();
63  }
64  return NULL;
65 }
66 
67 DICTIONARY_TEMPLATE
68 RUDIMENTS_TEMPLATE_INLINE
69 bool DICTIONARY_CLASS::remove(keytype key) {
71  if (node) {
72  return dict.remove(node);
73  }
74  return false;
75 }
76 
77 DICTIONARY_TEMPLATE
78 RUDIMENTS_TEMPLATE_INLINE
79 dictionarynode<keytype,valuetype> *DICTIONARY_CLASS::detach(keytype key) {
81  if (node) {
82  dict.detach(node);
84  delete node;
85  return contents;
86  }
87  return NULL;
88 }
89 
90 DICTIONARY_TEMPLATE
91 RUDIMENTS_TEMPLATE_INLINE
93  find(keytype key) {
95  dict.getFirst(); node; node=node->getNext()) {
96  if (!node->getValue()->compare(key)) {
97  return node;
98  }
99  }
100  return NULL;
101 }
102 
103 DICTIONARY_TEMPLATE
104 RUDIMENTS_TEMPLATE_INLINE
105 linkedlist< keytype > *DICTIONARY_CLASS::getKeys() {
108  dict.getFirst(); node; node=node->getNext()) {
109  keys->append(node->getValue()->getKey());
110  }
111  return keys;
112 }
113 
114 DICTIONARY_TEMPLATE
115 RUDIMENTS_TEMPLATE_INLINE
116 linkedlist< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getList() {
117  return &dict;
118 }
119 
120 DICTIONARY_TEMPLATE
121 RUDIMENTS_TEMPLATE_INLINE
122 void DICTIONARY_CLASS::clear() {
124  dict.getFirst(); node; node=node->getNext()) {
125  delete node->getValue();
126  }
127  dict.clear();
128 }
129 
130 DICTIONARY_TEMPLATE
131 RUDIMENTS_TEMPLATE_INLINE
132 void DICTIONARY_CLASS::print() {
134  dict.getFirst(); node; node=node->getNext()) {
135  node->getValue()->print();
136  stdoutput.printf("\n");
137  }
138 }
139 
140 #define DICTIONARYNODE_TEMPLATE \
141  template <class keytype, class valuetype>
142 
143 #define DICTIONARYNODE_CLASS \
144  dictionarynode<keytype,valuetype>
145 
146 DICTIONARYNODE_TEMPLATE
147 RUDIMENTS_TEMPLATE_INLINE
148 DICTIONARYNODE_CLASS::dictionarynode(keytype key, valuetype value) {
149  this->key=key;
150  this->value=value;
151 }
152 
153 DICTIONARYNODE_TEMPLATE
154 RUDIMENTS_TEMPLATE_INLINE
155 DICTIONARYNODE_CLASS::~dictionarynode() {}
156 
157 DICTIONARYNODE_TEMPLATE
158 RUDIMENTS_TEMPLATE_INLINE
159 void DICTIONARYNODE_CLASS::setKey(keytype key) {
160  this->key=key;
161 }
162 
163 DICTIONARYNODE_TEMPLATE
164 RUDIMENTS_TEMPLATE_INLINE
165 void DICTIONARYNODE_CLASS::setValue(valuetype value) {
166  this->value=value;
167 }
168 
169 DICTIONARYNODE_TEMPLATE
170 RUDIMENTS_TEMPLATE_INLINE
171 keytype DICTIONARYNODE_CLASS::getKey() const {
172  return key;
173 }
174 
175 DICTIONARYNODE_TEMPLATE
176 RUDIMENTS_TEMPLATE_INLINE
177 valuetype DICTIONARYNODE_CLASS::getValue() const {
178  return value;
179 }
180 
181 DICTIONARYNODE_TEMPLATE
182 RUDIMENTS_TEMPLATE_INLINE
183 int32_t DICTIONARYNODE_CLASS::compare(keytype testkey) const {
184  return _linkedlistutil_compare(key,testkey);
185 }
186 
187 DICTIONARYNODE_TEMPLATE
188 RUDIMENTS_TEMPLATE_INLINE
189 void DICTIONARYNODE_CLASS::print() const {
190  _linkedlistutil_print(key);
191  stdoutput.printf(":");
192  _linkedlistutil_print(value);
193 }
size_t printf(const char *format,...)
Definition: linkedlist.h:60
valuetype getValue() const
Definition: dictionary.h:12
void append(valuetype value)
Definition: linkedlist.h:11
linkedlistnode< valuetype > * getNext()