32 #define IGNORE_IN_CLASSLIST
34 #define MapNode CMapNode<K, T>
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
67 CMap(int32_t size=41, int32_t reserved=128,
bool tracable=
true)
77 hash_array=(CMapNode<K, T>**) calloc(size,
sizeof(CMapNode<K, T>*));
79 for (int32_t i=0; i<size; i++)
87 PTHREAD_LOCK_INIT(&lock);
95 PTHREAD_LOCK_DESTROY(&lock);
101 virtual const char*
get_name()
const {
return "Map"; }
109 int32_t
add(
const K& key,
const T& data)
111 int32_t index=hash(key);
112 if (chain_search(index, key)==NULL)
117 int32_t added_index=insert_key(index, key, data);
120 PTHREAD_UNLOCK(&lock);
136 int32_t index=hash(key);
137 if (chain_search(index, key)!=NULL)
147 void remove(
const K& key)
149 int32_t index=hash(key);
150 CMapNode<K, T>* result=chain_search(index, key);
157 delete_key(index, result);
160 PTHREAD_UNLOCK(&lock);
172 int32_t index=hash(key);
173 CMapNode<K ,T>* result=chain_search(index, key);
176 return result->index;
189 int32_t index=hash(key);
190 CMapNode<K, T>* result=chain_search(index, key);
196 int32_t added_index=
add(key, T());
210 int32_t index=hash(key);
211 CMapNode<K, T>* result=chain_search(index, key);
224 PTHREAD_UNLOCK(&lock);
256 if (result!=NULL && !is_free(result))
292 sizeof(CMapNode<K, T>*));
304 add(node->key, node->data);
314 int32_t hash(
const K& key)
320 bool is_free(CMapNode<K, T>*
node)
322 if (node->free==
true)
329 CMapNode<K, T>* chain_search(int32_t index,
const K& key)
341 if (current->key==key)
344 current=current->right;
346 }
while (current!=NULL);
353 int32_t insert_key(int32_t index,
const K& key,
const T& data)
364 new_node=(CMapNode<K, T>*) calloc(1,
sizeof(CMapNode<K, T>));
366 new (&new_node->key) K();
367 new (&new_node->data) T();
377 ASSERT(is_free(new_node));
383 new_node->index=new_index;
384 new_node->free=
false;
388 new_node->right=NULL;
407 void delete_key(int32_t index, CMapNode<K, T>* node)
414 if (node->right!=NULL)
415 node->right->left = node->left;
417 if (node->left!=NULL)
418 node->left->right = node->right;