Rudiments
dictionaryinlines.h
1 // Copyright (c) 2003 David Muse
2 // See the COPYING file for more information
3 
4 #ifndef EXCLUDE_RUDIMENTS_TEMPLATE_IMPLEMENTATIONS
5 
6 #ifdef RUDIMENTS_HAVE_STDLIB_H
7  #include <stdlib.h>
8 #endif
9 #include <stdio.h>
10 
11 #include <rudiments/private/rudimentsinlines.h>
12 
13 #ifdef RUDIMENTS_NAMESPACE
14 namespace rudiments {
15 #endif
16 
17 #define DICTIONARY_TEMPLATE \
18  template <class keytype, class datatype, \
19  class dictionarynodetype, \
20  class dictionarylistnodetype, \
21  class dictionarylisttype>
22 
23 #define DICTIONARY_CLASS \
24  dictionary<keytype,datatype,dictionarynodetype,\
25  dictionarylistnodetype,dictionarylisttype>
26 
27 DICTIONARY_TEMPLATE
28 RUDIMENTS_TEMPLATE_INLINE
29 DICTIONARY_CLASS::dictionary() {
30 }
31 
32 DICTIONARY_TEMPLATE
33 RUDIMENTS_TEMPLATE_INLINE
34 DICTIONARY_CLASS::~dictionary() {
35  for (dictionarylistnodetype *node=
36  (dictionarylistnodetype *)dict.getFirstNode();
37  node; node=(dictionarylistnodetype *)node->getNext()) {
38  delete node->getData();
39  }
40 }
41 
42 DICTIONARY_TEMPLATE
43 RUDIMENTS_TEMPLATE_INLINE
44 void DICTIONARY_CLASS::setData(keytype key, datatype data) {
45  dictionarylistnodetype *node=findNode(key);
46  if (node) {
47  node->getData()->setData(data);
48  } else {
49  dictionarynodetype *dictnode=new dictionarynodetype();
50  dictnode->setKey(key);
51  dictnode->setData(data);
52  dict.append(dictnode);
53  }
54 }
55 
56 DICTIONARY_TEMPLATE
57 RUDIMENTS_TEMPLATE_INLINE
58 bool DICTIONARY_CLASS::getData(keytype key, datatype *data) {
59  dictionarylistnodetype *node=findNode(key);
60  if (node) {
61  *data=node->getData()->getData();
62  return true;
63  }
64  return false;
65 }
66 
67 DICTIONARY_TEMPLATE
68 RUDIMENTS_TEMPLATE_INLINE
69 bool DICTIONARY_CLASS::removeData(keytype key) {
70  dictionarylistnodetype *node=findNode(key);
71  if (node) {
72  return dict.removeNode(node);
73  }
74  return false;
75 }
76 
77 DICTIONARY_TEMPLATE
78 RUDIMENTS_TEMPLATE_INLINE
79 dictionarylistnodetype *DICTIONARY_CLASS::findNode(keytype key) {
80  for (dictionarylistnodetype *node=
81  (dictionarylistnodetype *)dict.getFirstNode();
82  node; node=(dictionarylistnodetype *)node->getNext()) {
83  if (!node->getData()->compare(key)) {
84  return node;
85  }
86  }
87  return NULL;
88 }
89 
90 DICTIONARY_TEMPLATE
91 RUDIMENTS_TEMPLATE_INLINE
92 dictionarylisttype *DICTIONARY_CLASS::getList() {
93  return &dict;
94 }
95 
96 DICTIONARY_TEMPLATE
97 RUDIMENTS_TEMPLATE_INLINE
98 void DICTIONARY_CLASS::clear() {
99  dict.clear();
100 }
101 
102 DICTIONARY_TEMPLATE
103 RUDIMENTS_TEMPLATE_INLINE
104 void DICTIONARY_CLASS::print() {
105  for (dictionarylistnodetype *node=
106  (dictionarylistnodetype *)dict.getFirstNode();
107  node; node=(dictionarylistnodetype *)node->getNext()) {
108  node->getData()->print();
109  printf("\n");
110  }
111 }
112 
113 
114 
115 template <class datatype>
116 RUDIMENTS_TEMPLATE_INLINE
118 
119 template <class datatype>
120 RUDIMENTS_TEMPLATE_INLINE
122 
123 template <class datatype>
124 RUDIMENTS_TEMPLATE_INLINE
126 
127 template <class datatype>
128 RUDIMENTS_TEMPLATE_INLINE
130 
131 
132 
133 template <class datatype>
134 RUDIMENTS_TEMPLATE_INLINE
136 
137 template <class datatype>
138 RUDIMENTS_TEMPLATE_INLINE
140 
141 template <class datatype>
142 RUDIMENTS_TEMPLATE_INLINE
144 
145 template <class datatype>
146 RUDIMENTS_TEMPLATE_INLINE
148 
149 
150 
151 template <class datatype>
152 RUDIMENTS_TEMPLATE_INLINE
154 
155 template <class datatype>
156 RUDIMENTS_TEMPLATE_INLINE
158 
159 template <class datatype>
160 RUDIMENTS_TEMPLATE_INLINE
162 
163 template <class datatype>
164 RUDIMENTS_TEMPLATE_INLINE
166 
167 #ifdef RUDIMENTS_NAMESPACE
168 }
169 #endif
170 
171 #endif