BFLibCPP 0.1
CPP Library
Loading...
Searching...
No Matches
dictionary.hpp
Go to the documentation of this file.
1
6#ifndef DICTIONARY_HPP
7#define DICTIONARY_HPP
8
9#include "rbtree.hpp"
10#include "access.hpp"
11#include "object.hpp"
12
13namespace BF {
14
18template <typename K, typename V, typename S = int>
19class Dictionary : public Object {
20public:
21
25 class Entry : public Object {
26 friend class Dictionary<K,V,S>;
27 public:
35 static int Compare(Entry * a, Entry * b) {
36 return a->runCompare(b);
37 }
38
39 K key() const { return this->_key; }
40 V value() const { return this->_value; }
41
42 private:
43
44 Entry(K k, V v, Dictionary<K,V,S> * dictRef) : Object() {
45 this->_dictRef = dictRef;
46
47 // See how the dict owner wants to retain key and values
48 if (this->_dictRef->_keyValueRetain) {
49 this->_dictRef->_keyValueRetain(&k, &v);
50 }
51
52 this->_key = k;
53 this->_value = v;
54 }
55
56 virtual ~Entry() {
57 if (this->_dictRef->_keyValueRelease) {
58 this->_dictRef->_keyValueRelease(&this->_key, &this->_value);
59 }
60 }
61
68 int runCompare(Entry * b) const {
69 return this->_dictRef->runCompare(this->key(), b->key());
70 }
71
72 K _key;
73 V _value;
74
78 const Dictionary<K,V,S> * _dictRef;
79 };
80
81 friend class Entry;
82
84 this->_keyCompare = 0;
85 this->_keyValueRelease = 0;
86 this->_keyValueRetain = 0;
87
88 // Establish how the tree will compare the entries
89 this->_tree.setCompareCallback(Entry::Compare);
90 }
91
93 int (* keyCompareCallback) (K a1, K a2),
94 void (* retainCallback) (K * k, V * v),
95 void (* releaseCallback) (K * k, V * v)) : Dictionary() {
96 this->_keyCompare = keyCompareCallback;
97 this->_keyValueRelease = releaseCallback;
98 this->_keyValueRetain = retainCallback;
99 }
100
101 virtual ~Dictionary() {
102 this->_keyCompare = 0;
103 this->_keyValueRelease = 0;
104 this->_keyValueRetain = 0;
105 }
106
110 S size() const {
111 return this->_tree.count();
112 }
113
121 void setKeyCompareCallback(int (* cb) (K a1, K a2)) {
122 this->_keyCompare = cb;
123 }
124
128 void setEntryReleaseCallback(void (* cb) (K * k, V * v)) {
129 this->_keyValueRelease = cb;
130 }
131
136 void setEntryRetainCallback(void (* cb) (K * k, V * v)) {
137 this->_keyValueRetain = cb;
138 }
139
143 V valueForKey(K key) {
144 int error = 0;
145 Entry * obj = this->getEntryForKey(key, this->_tree.root(), &error);
146
147 if (!error && obj) {
148 return obj->value();
149 }
150
151 return 0;
152 }
153
157 int setValueForKey(K key, V value) {
158 Entry * ent = new Entry(key, value, this);
159 return this->_tree.insert(ent);
160 }
161
165 int removeValueForKey(K key) {
166 int error = 0;
167 const typename RBTree<Entry *>::RBNode * n = this->getEntryNodeForKey(key, this->_tree.root(), &error);
168
169 if (n && !error) {
170 return this->_tree.deleteNode(n);
171 } else {
172 return error == 0 ? 15 : error;
173 }
174 }
175
179 void print() {
180 this->print(this->_tree.root());
181 }
182
184
186 return this->_tree.createIterator(itr);
187 }
188private:
189
193 void print(const typename RBTree<Entry *>::RBNode * node) {
194 if (node) {
195 if (!node->isNull()) {
196 this->print(node->left());
197
198 Entry * entry = node->object();
199 if (entry) {
200 std::cout << entry->key() << " : " << entry->value() << std::endl;
201 }
202
203 this->print(node->right());
204 }
205 }
206 }
207
214 Entry * getEntryForKey(K key, const typename RBTree<Entry *>::RBNode * node, int * err) {
215 int error = 0;
216 const typename RBTree<Entry *>::RBNode * n = this->getEntryNodeForKey(key, node, &error);
217
218 if (n && !error) {
219 return n->object();
220 } else {
221 if (err) *err = error;
222 return NULL;
223 }
224 }
225
229 const typename RBTree<Entry *>::RBNode * getEntryNodeForKey(K key, const typename RBTree<Entry *>::RBNode * node, int * err) {
230 // If the pointer is null then we will throw an error
231 if (node == NULL) {
232 if (err) *err = 13;
233 return NULL;
234 // if we are at a null node, return null
235 } else if (node->isNull()) return NULL;
236
237 Entry * obj = NULL;
238
239 // If the object is null then we are going to return null with error
240 if ((obj = node->object()) == NULL) {
241 if (err) *err = 14;
242 return NULL;
243 }
244
245 int comp = this->runCompare(key, obj->key());
246 if (comp == -1) return this->getEntryNodeForKey(key, node->left(), err);
247 else if (comp == 1) return this->getEntryNodeForKey(key, node->right(), err);
248 else return node;
249 }
250
251 int runCompare(K a1, K a2) const {
252 if (this->_keyCompare) return this->_keyCompare(a1, a2);
253 else {
254 if (a1 < a2) return -1;
255 else if (a1 > a2) return 1;
256 else return 0;
257 }
258 }
259
263 RBTree<Entry *, S> _tree;
264
272 int (* _keyCompare) (K a1, K a2);
273
279 void (* _keyValueRelease) (K * key, V * value);
280
286 void (* _keyValueRetain) (K * key, V * value);
287};
288
289} // namespace BF
290
291#endif // DICTIONARY_HPP
292
T object() const
Definition bintree.hpp:29
Definition dictionary.hpp:25
K key() const
Definition dictionary.hpp:39
V value() const
Definition dictionary.hpp:40
friend class Dictionary< K, V, S >
Definition dictionary.hpp:26
static int Compare(Entry *a, Entry *b)
Definition dictionary.hpp:35
Definition dictionary.hpp:19
int setValueForKey(K key, V value)
Definition dictionary.hpp:157
V valueForKey(K key)
Definition dictionary.hpp:143
void setEntryReleaseCallback(void(*cb)(K *k, V *v))
Definition dictionary.hpp:128
void print()
Definition dictionary.hpp:179
int createIterator(Iterator **itr)
Definition dictionary.hpp:185
virtual ~Dictionary()
Definition dictionary.hpp:101
int removeValueForKey(K key)
Definition dictionary.hpp:165
friend class Entry
Definition dictionary.hpp:81
Dictionary(int(*keyCompareCallback)(K a1, K a2), void(*retainCallback)(K *k, V *v), void(*releaseCallback)(K *k, V *v))
Definition dictionary.hpp:92
void setKeyCompareCallback(int(*cb)(K a1, K a2))
Definition dictionary.hpp:121
RBTree< Entry *, S >::Iterator Iterator
Definition dictionary.hpp:183
void setEntryRetainCallback(void(*cb)(K *k, V *v))
Definition dictionary.hpp:136
Dictionary()
Definition dictionary.hpp:83
S size() const
Definition dictionary.hpp:110
Definition object.hpp:21
Definition rbtree.hpp:201
Definition rbtree.hpp:38
virtual const RBNode * left() const
Definition rbtree.hpp:52
virtual bool isNull() const
Definition rbtree.hpp:51
virtual const RBNode * right() const
Definition rbtree.hpp:53
Definition array.hpp:18