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 bool DICTIONARY_CLASS::remove(keytype key) {
51  if (node) {
52  return dict.remove(node);
53  }
54  return false;
55 }
56 
57 DICTIONARY_TEMPLATE
58 RUDIMENTS_TEMPLATE_INLINE
60  find(keytype key) {
62  dict.getFirst(); node; node=node->getNext()) {
63  if (!node->getValue()->compare(key)) {
64  return node;
65  }
66  }
67  return NULL;
68 }
69 
70 DICTIONARY_TEMPLATE
71 RUDIMENTS_TEMPLATE_INLINE
72 linkedlist< dictionarynode<keytype,valuetype> *> *DICTIONARY_CLASS::getList() {
73  return &dict;
74 }
75 
76 DICTIONARY_TEMPLATE
77 RUDIMENTS_TEMPLATE_INLINE
78 void DICTIONARY_CLASS::clear() {
80  dict.getFirst(); node; node=node->getNext()) {
81  delete node->getValue();
82  }
83  dict.clear();
84 }
85 
86 DICTIONARY_TEMPLATE
87 RUDIMENTS_TEMPLATE_INLINE
88 void DICTIONARY_CLASS::print() {
90  dict.getFirst(); node; node=node->getNext()) {
91  node->getValue()->print();
92  stdoutput.printf("\n");
93  }
94 }
95 
96 #define DICTIONARYNODE_TEMPLATE \
97  template <class keytype, class valuetype>
98 
99 #define DICTIONARYNODE_CLASS \
100  dictionarynode<keytype,valuetype>
101 
102 DICTIONARYNODE_TEMPLATE
103 RUDIMENTS_TEMPLATE_INLINE
104 DICTIONARYNODE_CLASS::dictionarynode(keytype key, valuetype value) {
105  this->key=key;
106  this->value=value;
107 }
108 
109 DICTIONARYNODE_TEMPLATE
110 RUDIMENTS_TEMPLATE_INLINE
111 DICTIONARYNODE_CLASS::~dictionarynode() {}
112 
113 DICTIONARYNODE_TEMPLATE
114 RUDIMENTS_TEMPLATE_INLINE
115 void DICTIONARYNODE_CLASS::setKey(keytype key) {
116  this->key=key;
117 }
118 
119 DICTIONARYNODE_TEMPLATE
120 RUDIMENTS_TEMPLATE_INLINE
121 void DICTIONARYNODE_CLASS::setValue(valuetype value) {
122  this->value=value;
123 }
124 
125 DICTIONARYNODE_TEMPLATE
126 RUDIMENTS_TEMPLATE_INLINE
127 keytype DICTIONARYNODE_CLASS::getKey() const {
128  return key;
129 }
130 
131 DICTIONARYNODE_TEMPLATE
132 RUDIMENTS_TEMPLATE_INLINE
133 valuetype DICTIONARYNODE_CLASS::getValue() const {
134  return value;
135 }
136 
137 DICTIONARYNODE_TEMPLATE
138 RUDIMENTS_TEMPLATE_INLINE
139 int32_t DICTIONARYNODE_CLASS::compare(keytype testkey) const {
140  return _linkedlistutil_compare(key,testkey);
141 }
142 
143 DICTIONARYNODE_TEMPLATE
144 RUDIMENTS_TEMPLATE_INLINE
145 void DICTIONARYNODE_CLASS::print() const {
146  _linkedlistutil_print(key);
147  stdoutput.printf(":");
148  _linkedlistutil_print(value);
149 }
size_t printf(const char *format,...)
Definition: linkedlist.h:60
valuetype getValue() const
Definition: dictionary.h:12
Definition: linkedlist.h:11
linkedlistnode< valuetype > * getNext()