version 1.1, 2011/06/08 07:09:12
|
version 1.1.1.2, 2012/02/17 12:50:03
|
Line 1
|
Line 1
|
/* |
/* |
* LibAxl: Another XML library | * Libaxl: Another XML library |
* Copyright (C) 2006 Advanced Software Production Line, S.L. |
* Copyright (C) 2006 Advanced Software Production Line, S.L. |
* |
* |
* This program is free software; you can redistribute it and/or |
* This program is free software; you can redistribute it and/or |
Line 333 axlHash * axl_hash_new_full (axlHashFunc
|
Line 333 axlHash * axl_hash_new_full (axlHashFunc
|
axlHash * result; |
axlHash * result; |
|
|
result = axl_new (axlHash, 1); |
result = axl_new (axlHash, 1); |
|
/* check allocated node */ |
|
if (result == NULL) |
|
return NULL; |
|
|
result->factory = axl_factory_create (sizeof (axlHashNode)); |
result->factory = axl_factory_create (sizeof (axlHashNode)); |
|
/* check allocated node */ |
|
if (result->factory == NULL) { |
|
/* release allocated node */ |
|
axl_free (result); |
|
return NULL; |
|
} |
|
|
result->hash = hash; |
result->hash = hash; |
result->equal = equal; |
result->equal = equal; |
result->step = step; |
result->step = step; |
Line 380 void axl_hash_insert (axlHash * hash,
|
Line 391 void axl_hash_insert (axlHash * hash,
|
*/ |
*/ |
#define __axl_hash_create_node(factory, node, key, key_destroy, data, data_destroy)\ |
#define __axl_hash_create_node(factory, node, key, key_destroy, data, data_destroy)\ |
node = axl_factory_get (factory);\ |
node = axl_factory_get (factory);\ |
node->key = key;\ | if (node != NULL) { \ |
node->key_destroy = key_destroy;\ | node->key = key; \ |
node->data = data;\ | node->key_destroy = key_destroy; \ |
node->data_destroy = data_destroy; | node->data = data; \ |
| node->data_destroy = data_destroy; \ |
| } |
|
|
/** |
/** |
* @internal Function that performs the hash insertion for the |
* @internal Function that performs the hash insertion for the |
Line 426 void axl_hash_insert_full (axlHash * hash
|
Line 439 void axl_hash_insert_full (axlHash * hash
|
axlPointer data, |
axlPointer data, |
axlDestroyFunc data_destroy) |
axlDestroyFunc data_destroy) |
{ |
{ |
int pos = 0; | int pos = 0; |
axlHashNode * node = NULL; | axlHashNode * node = NULL; |
axlHashNode * aux = NULL; | axlHashNode * aux = NULL; |
int iterator = 0; | int iterator = 0; |
| axlHashNode ** temp; |
|
|
/* check incoming data */ |
/* check incoming data */ |
axl_return_if_fail (hash); |
axl_return_if_fail (hash); |
Line 439 void axl_hash_insert_full (axlHash * hash
|
Line 453 void axl_hash_insert_full (axlHash * hash
|
if (hash->hash_size == 0) { |
if (hash->hash_size == 0) { |
/* allocate memory for the hash */ |
/* allocate memory for the hash */ |
hash->table = axl_new (axlHashNode *, hash->step); |
hash->table = axl_new (axlHashNode *, hash->step); |
|
/* check allocated value */ |
|
if (hash->table == NULL) |
|
return; |
hash->hash_size = hash->step; |
hash->hash_size = hash->step; |
|
|
/* create the node to store */ |
/* create the node to store */ |
__axl_hash_create_node (hash->factory, node, key, key_destroy, data, data_destroy); |
__axl_hash_create_node (hash->factory, node, key, key_destroy, data, data_destroy); |
|
/* check allocated value and cleanup on failure */ |
|
if (node == NULL) { |
|
hash->hash_size = 0; |
|
axl_free (hash->table); |
|
hash->table = NULL; |
|
return; |
|
} /* end if */ |
|
|
/* insert the node into the hash */ |
/* insert the node into the hash */ |
__axl_hash_insert_node (pos, hash, key, node, axl_true); |
__axl_hash_insert_node (pos, hash, key, node, axl_true); |
Line 500 void axl_hash_insert_full (axlHash * hash
|
Line 524 void axl_hash_insert_full (axlHash * hash
|
/* now we have in node a complete list of items |
/* now we have in node a complete list of items |
* stored, allocate more memory and rehash */ |
* stored, allocate more memory and rehash */ |
hash->hash_size += hash->step; |
hash->hash_size += hash->step; |
|
temp = hash->table; |
hash->table = realloc (hash->table, sizeof (axlHashNode *) * (hash->hash_size)); |
hash->table = realloc (hash->table, sizeof (axlHashNode *) * (hash->hash_size)); |
|
|
|
/* check realloc operation */ |
|
if (hash->table == NULL) { |
|
/* alloc failed, restore and cleanup */ |
|
hash->table = temp; |
|
hash->hash_size -= hash->step; |
|
return; |
|
} /* end if */ |
|
|
/* clear the table */ |
/* clear the table */ |
memset (hash->table, 0, sizeof (axlHashNode*) * (hash->hash_size)); |
memset (hash->table, 0, sizeof (axlHashNode*) * (hash->hash_size)); |
|
|
Line 526 void axl_hash_insert_full (axlHash * hash
|
Line 559 void axl_hash_insert_full (axlHash * hash
|
if (node == NULL) { |
if (node == NULL) { |
/* create a new node */ |
/* create a new node */ |
__axl_hash_create_node (hash->factory, node, key, key_destroy, data, data_destroy); |
__axl_hash_create_node (hash->factory, node, key, key_destroy, data, data_destroy); |
|
/* check allocated value and cleanup on failure */ |
|
if (node == NULL) { |
|
return; |
|
} /* end if */ |
|
|
/* insert the node into the hash as usual */ |
/* insert the node into the hash as usual */ |
__axl_hash_insert_node (pos, hash, key, node, axl_true); |
__axl_hash_insert_node (pos, hash, key, node, axl_true); |
Line 585 axl_bool __axl_hash_remove_common (ax
|
Line 622 axl_bool __axl_hash_remove_common (ax
|
hash->table [pos] = node->next; |
hash->table [pos] = node->next; |
|
|
remove_element: |
remove_element: |
|
|
|
/* decreases elements found */ |
|
hash->items--; |
|
|
/* key destruction is defined */ |
/* key destruction is defined */ |
if (node->key_destroy != NULL && remove) |
if (node->key_destroy != NULL && remove) |
node->key_destroy (node->key); |
node->key_destroy (node->key); |
Line 593 axl_bool __axl_hash_remove_common (ax
|
Line 634 axl_bool __axl_hash_remove_common (ax
|
if (node->data_destroy != NULL && remove) |
if (node->data_destroy != NULL && remove) |
node->data_destroy (node->data); |
node->data_destroy (node->data); |
|
|
/* decreases elements found */ |
|
hash->items--; |
|
|
|
/* delete the node */ |
/* delete the node */ |
axl_factory_release_spare (hash->factory, node); |
axl_factory_release_spare (hash->factory, node); |
/* axl_free (node); */ |
/* axl_free (node); */ |
Line 1209 void axl_hash_free (axlHash * hash)
|
Line 1247 void axl_hash_free (axlHash * hash)
|
{ |
{ |
int iterator = 0; |
int iterator = 0; |
axlHashNode * node; |
axlHashNode * node; |
axlHashNode * aux; |
|
|
|
/* do not perform any operation if a null reference is |
/* do not perform any operation if a null reference is |
* received */ |
* received */ |
Line 1233 void axl_hash_free (axlHash * hash)
|
Line 1270 void axl_hash_free (axlHash * hash)
|
node->data_destroy (node->data); |
node->data_destroy (node->data); |
|
|
/* check data */ |
/* check data */ |
aux = node; |
|
node = node->next; |
node = node->next; |
|
|
/* while more nodes */ |
/* while more nodes */ |
Line 1403 axlHashCursor * axl_hash_cursor_new (axlHash * ha
|
Line 1439 axlHashCursor * axl_hash_cursor_new (axlHash * ha
|
|
|
/* create the cursor */ |
/* create the cursor */ |
cursor = axl_new (axlHashCursor, 1); |
cursor = axl_new (axlHashCursor, 1); |
|
/* check allocated value */ |
|
if (cursor == NULL) |
|
return NULL; |
|
|
/* initial configuration */ |
/* initial configuration */ |
cursor->hash = hash; |
cursor->hash = hash; |