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  *node=findNode(key);
30  if (node) {
31  node->getValue()->setValue(value);
32  } else {
33  dict.append(new dictionarynode<keytype,valuetype>(key,value));
34  }
35 }
36 
37 DICTIONARY_TEMPLATE
38 RUDIMENTS_TEMPLATE_INLINE
39 bool DICTIONARY_CLASS::getValue(keytype key, valuetype *value) {
41  *node=findNode(key);
42  if (node) {
43  *value=node->getValue()->getValue();
44  return true;
45  }
46  return false;
47 }
48 
49 DICTIONARY_TEMPLATE
50 RUDIMENTS_TEMPLATE_INLINE
51 bool DICTIONARY_CLASS::removeValue(keytype key) {
53  *node=findNode(key);
54  if (node) {
55  return dict.removeNode(node);
56  }
57  return false;
58 }
59 
60 DICTIONARY_TEMPLATE
61 RUDIMENTS_TEMPLATE_INLINE
63  findNode(keytype key) {
65  dict.getFirstNode(); node; node=node->getNext()) {
66  if (!node->getValue()->compare(key)) {
67  return node;
68  }
69  }
70  return NULL;
71 }
72 
73 DICTIONARY_TEMPLATE
74 RUDIMENTS_TEMPLATE_INLINE
75 linkedlist< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getList() {
76  return &dict;
77 }
78 
79 DICTIONARY_TEMPLATE
80 RUDIMENTS_TEMPLATE_INLINE
81 void DICTIONARY_CLASS::clear() {
83  dict.getFirstNode(); node; node=node->getNext()) {
84  delete node->getValue();
85  }
86  dict.clear();
87 }
88 
89 DICTIONARY_TEMPLATE
90 RUDIMENTS_TEMPLATE_INLINE
91 void DICTIONARY_CLASS::print() {
93  dict.getFirstNode(); node; node=node->getNext()) {
94  node->getValue()->print();
95  stdoutput.printf("\n");
96  }
97 }
98 
99 #define DICTIONARYNODE_TEMPLATE \
100  template <class keytype, class valuetype>
101 
102 #define DICTIONARYNODE_CLASS \
103  dictionarynode<keytype,valuetype>
104 
105 DICTIONARYNODE_TEMPLATE
106 RUDIMENTS_TEMPLATE_INLINE
107 DICTIONARYNODE_CLASS::dictionarynode(keytype key, valuetype value) {
108  this->key=key;
109  this->value=value;
110 }
111 
112 DICTIONARYNODE_TEMPLATE
113 RUDIMENTS_TEMPLATE_INLINE
114 DICTIONARYNODE_CLASS::~dictionarynode() {}
115 
116 DICTIONARYNODE_TEMPLATE
117 RUDIMENTS_TEMPLATE_INLINE
118 void DICTIONARYNODE_CLASS::setKey(keytype key) {
119  this->key=key;
120 }
121 
122 DICTIONARYNODE_TEMPLATE
123 RUDIMENTS_TEMPLATE_INLINE
124 void DICTIONARYNODE_CLASS::setValue(valuetype value) {
125  this->value=value;
126 }
127 
128 DICTIONARYNODE_TEMPLATE
129 RUDIMENTS_TEMPLATE_INLINE
130 keytype DICTIONARYNODE_CLASS::getKey() const {
131  return key;
132 }
133 
134 DICTIONARYNODE_TEMPLATE
135 RUDIMENTS_TEMPLATE_INLINE
136 valuetype DICTIONARYNODE_CLASS::getValue() const {
137  return value;
138 }
139 
140 DICTIONARYNODE_TEMPLATE
141 RUDIMENTS_TEMPLATE_INLINE
142 int32_t DICTIONARYNODE_CLASS::compare(keytype testkey) const {
143  return _linkedlistutil_compare(key,testkey);
144 }
145 
146 DICTIONARYNODE_TEMPLATE
147 RUDIMENTS_TEMPLATE_INLINE
148 void DICTIONARYNODE_CLASS::print() const {
149  _linkedlistutil_print(key);
150  stdoutput.printf(":");
151  _linkedlistutil_print(value);
152 }