18template <
typename K,
typename V,
typename S =
int>
36 return a->runCompare(b);
39 K
key()
const {
return this->_key; }
40 V
value()
const {
return this->_value; }
45 this->_dictRef = dictRef;
48 if (this->_dictRef->_keyValueRetain) {
49 this->_dictRef->_keyValueRetain(&k, &v);
57 if (this->_dictRef->_keyValueRelease) {
58 this->_dictRef->_keyValueRelease(&this->_key, &this->_value);
68 int runCompare(
Entry * b)
const {
69 return this->_dictRef->runCompare(this->
key(), b->key());
84 this->_keyCompare = 0;
85 this->_keyValueRelease = 0;
86 this->_keyValueRetain = 0;
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;
102 this->_keyCompare = 0;
103 this->_keyValueRelease = 0;
104 this->_keyValueRetain = 0;
111 return this->_tree.count();
122 this->_keyCompare = cb;
129 this->_keyValueRelease = cb;
137 this->_keyValueRetain = cb;
145 Entry * obj = this->getEntryForKey(key, this->_tree.root(), &error);
159 return this->_tree.insert(ent);
170 return this->_tree.deleteNode(n);
172 return error == 0 ? 15 : error;
180 this->
print(this->_tree.root());
186 return this->_tree.createIterator(itr);
200 std::cout << entry->key() <<
" : " << entry->value() << std::endl;
214 Entry * getEntryForKey(K key,
const typename RBTree<Entry *>::RBNode * node,
int * err) {
216 const typename RBTree<Entry *>::RBNode * n = this->getEntryNodeForKey(key, node, &error);
221 if (err) *err = error;
229 const typename RBTree<Entry *>::RBNode * getEntryNodeForKey(K key,
const typename RBTree<Entry *>::RBNode * node,
int * err) {
235 }
else if (node->isNull())
return NULL;
240 if ((obj = node->object()) == NULL) {
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);
251 int runCompare(K a1, K a2)
const {
252 if (this->_keyCompare)
return this->_keyCompare(a1, a2);
254 if (a1 < a2)
return -1;
255 else if (a1 > a2)
return 1;
263 RBTree<Entry *, S> _tree;
272 int (* _keyCompare) (K a1, K a2);
279 void (* _keyValueRelease) (K * key, V * value);
286 void (* _keyValueRetain) (K * key, V * value);
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 rbtree.hpp:201
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