目录

项目名称

源代码

hashtable.h、obj_hashtable.h

hashtable.c、obj_hashtable.c

demos


项目名称

OpenAirInterface

源代码

hashtable.h、obj_hashtable.h

/*******************************************************************************OpenAirInterface Copyright(c) 1999 - 2014 EurecomOpenAirInterface is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.OpenAirInterface is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with OpenAirInterface.The full GNU General Public License is included in this distribution in the file called "COPYING". If not, see <http://www.gnu.org/licenses/>.Contact InformationOpenAirInterface Admin: openair_admin@eurecom.frOpenAirInterface Tech : openair_tech@eurecom.frOpenAirInterface Dev  : openair4g-devel@eurecom.frAddress      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE*******************************************************************************//* 哈希表 Rongtao(2019.09.24) */#ifndef _UTILS_COLLECTION_HASH_TABLE_H_
#define _UTILS_COLLECTION_HASH_TABLE_H_
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>typedef size_t   hash_size_t;
typedef uint64_t hash_key_t;#define HASHTABLE_NOT_A_KEY_VALUE ((uint64_t)-1)typedef enum hashtable_return_code_e {HASH_TABLE_OK                      = 0,HASH_TABLE_INSERT_OVERWRITTEN_DATA = 1,HASH_TABLE_KEY_NOT_EXISTS          = 2,HASH_TABLE_KEY_ALREADY_EXISTS      = 3,HASH_TABLE_BAD_PARAMETER_HASHTABLE = 4,HASH_TABLE_SYSTEM_ERROR            = 5,HASH_TABLE_CODE_MAX
} hashtable_rc_t;typedef struct hash_node_s {hash_key_t          key;void               *data;struct hash_node_s *next;
} hash_node_t;typedef struct hash_table_s {hash_size_t         size;hash_size_t         num_elements;struct hash_node_s **nodes;hash_size_t       (*hashfunc)(const hash_key_t);void              (*freefunc)(void*);
} hash_table_t;char*           hashtable_rc_code2string(hashtable_rc_t rcP);
void            hash_free_int_func(void* memoryP);
hash_table_t   *hashtable_create (const hash_size_t   size, hash_size_t (*hashfunc)(const hash_key_t ), void (*freefunc)(void*));
hashtable_rc_t  hashtable_destroy(hash_table_t * const hashtbl);
hashtable_rc_t  hashtable_is_key_exists (const hash_table_t * const hashtbl, const uint64_t key);
hashtable_rc_t  hashtable_apply_funct_on_elements (hash_table_t * const hashtblP, void funct(hash_key_t keyP, void* dataP, void* parameterP), void* parameterP);
hashtable_rc_t  hashtable_dump_content (const hash_table_t * const hashtblP, char * const buffer_pP, int * const remaining_bytes_in_buffer_pP );
hashtable_rc_t  hashtable_insert (hash_table_t * const hashtbl, const hash_key_t key, void *data);
hashtable_rc_t  hashtable_remove (hash_table_t * const hashtbl, const hash_key_t key);
hashtable_rc_t  hashtable_get    (const hash_table_t * const hashtbl, const hash_key_t key, void **dataP);
hashtable_rc_t  hashtable_resize (hash_table_t * const hashtbl, const hash_size_t size);#endif //_UTILS_COLLECTION_HASH_TABLE_H_/*******************************************************************************OpenAirInterface Copyright(c) 1999 - 2014 EurecomOpenAirInterface is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.OpenAirInterface is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with OpenAirInterface.The full GNU General Public License is included in this distribution in the file called "COPYING". If not, see <http://www.gnu.org/licenses/>.Contact InformationOpenAirInterface Admin: openair_admin@eurecom.frOpenAirInterface Tech : openair_tech@eurecom.frOpenAirInterface Dev  : openair4g-devel@eurecom.frAddress      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE*******************************************************************************/#ifndef _UTILS_COLLECTION_OBJ_HASH_TABLE_H_
#define _UTILS_COLLECTION_OBJ_HASH_TABLE_H_
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>//#include "collection/hashtable/hashtable.h"
#include "types/hash/hashtab.h"typedef struct obj_hash_node_s {int                 key_size;void               *key;void               *data;struct obj_hash_node_s *next;
} obj_hash_node_t;typedef struct obj_hash_table_s {hash_size_t         size;hash_size_t         num_elements;struct obj_hash_node_s **nodes;hash_size_t       (*hashfunc)(const void*, int);void              (*freekeyfunc)(void*);void              (*freedatafunc)(void*);
} obj_hash_table_t;obj_hash_table_t   *obj_hashtable_create  (hash_size_t   size, hash_size_t (*hashfunc)(const void*, int ), void (*freekeyfunc)(void*), void (*freedatafunc)(void*));
hashtable_rc_t      obj_hashtable_destroy (obj_hash_table_t *hashtblP);
hashtable_rc_t      obj_hashtable_is_key_exists (obj_hash_table_t *hashtblP, void* keyP, int key_sizeP);
hashtable_rc_t      obj_hashtable_insert  (obj_hash_table_t *hashtblP,       void* keyP, int key_sizeP, void *dataP);
hashtable_rc_t      obj_hashtable_remove  (obj_hash_table_t *hashtblP, const void* keyP, int key_sizeP);
hashtable_rc_t      obj_hashtable_get     (obj_hash_table_t *hashtblP, const void* keyP, int key_sizeP, void ** dataP);
hashtable_rc_t      obj_hashtable_get_keys(obj_hash_table_t *hashtblP, void ** keysP, unsigned int *sizeP);
hashtable_rc_t      obj_hashtable_resize  (obj_hash_table_t *hashtblP, hash_size_t sizeP);#endif //_UTILS_COLLECTION_OBJ_HASH_TABLE_H_

hashtable.c、obj_hashtable.c

/*******************************************************************************OpenAirInterface Copyright(c) 1999 - 2014 EurecomOpenAirInterface is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.OpenAirInterface is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with OpenAirInterface.The full GNU General Public License is included in this distribution in the file called "COPYING". If not, see <http://www.gnu.org/licenses/>.Contact InformationOpenAirInterface Admin: openair_admin@eurecom.frOpenAirInterface Tech : openair_tech@eurecom.frOpenAirInterface Dev  : openair4g-devel@eurecom.frAddress      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE*******************************************************************************/#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
//#include "hashtable.h"
//#include "assertions.h"
#include "types/hash/hashtab_rt.h"//-------------------------------------------------------------------------------------------------------------------------------
char* hashtable_rc_code2string(hashtable_rc_t rcP)
//-------------------------------------------------------------------------------------------------------------------------------
{switch (rcP) {case HASH_TABLE_OK:                      return "HASH_TABLE_OK";break;case HASH_TABLE_INSERT_OVERWRITTEN_DATA: return "HASH_TABLE_INSERT_OVERWRITTEN_DATA";break;case HASH_TABLE_KEY_NOT_EXISTS:          return "HASH_TABLE_KEY_NOT_EXISTS";break;case HASH_TABLE_KEY_ALREADY_EXISTS:      return "HASH_TABLE_KEY_ALREADY_EXISTS";break;case HASH_TABLE_BAD_PARAMETER_HASHTABLE: return "HASH_TABLE_BAD_PARAMETER_HASHTABLE";break;default:                                 return "UNKNOWN hashtable_rc_t";}
}
//-------------------------------------------------------------------------------------------------------------------------------
/** free int function* hash_free_int_func() is used when this hashtable is used to store int values as data (pointer = value).*/void hash_free_int_func(void* memoryP){}//-------------------------------------------------------------------------------------------------------------------------------
/** Default hash function* def_hashfunc() is the default used by hashtable_create() when the user didn't specify one.* This is a simple/naive hash function which adds the key's ASCII char values. It will probably generate lots of collisions on large hash tables.*/static hash_size_t def_hashfunc(const uint64_t keyP)
{return (hash_size_t)keyP;
}//-------------------------------------------------------------------------------------------------------------------------------
/** Initialisation* hashtable_create() sets up the initial structure of the hash table. The user specified size will be allocated and initialized to NULL.* The user can also specify a hash function. If the hashfunc argument is NULL, a default hash function is used.* If an error occurred, NULL is returned. All other values in the returned hash_table_t pointer should be released with hashtable_destroy().*/
hash_table_t *hashtable_create(const hash_size_t sizeP, hash_size_t (*hashfuncP)(const hash_key_t ), void (*freefuncP)(void*))
{hash_table_t *hashtbl = NULL;if(!(hashtbl=malloc(sizeof(hash_table_t)))) {return NULL;}if(!(hashtbl->nodes=calloc(sizeP, sizeof(hash_node_t*)))) {free(hashtbl);return NULL;}hashtbl->size=sizeP;if(hashfuncP) hashtbl->hashfunc=hashfuncP;else hashtbl->hashfunc=def_hashfunc;if(freefuncP) hashtbl->freefunc=freefuncP;else hashtbl->freefunc=free;return hashtbl;
}
//-------------------------------------------------------------------------------------------------------------------------------
/** Cleanup* The hashtable_destroy() walks through the linked lists for each possible hash value, and releases the elements. It also releases the nodes array and the hash_table_t.*/
hashtable_rc_t hashtable_destroy(hash_table_t * const hashtblP)
{hash_size_t n;hash_node_t *node, *oldnode;if (hashtblP == NULL) {rt_assert(0);return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}for(n=0; n<hashtblP->size; ++n) {node=hashtblP->nodes[n];while(node) {oldnode=node;node=node->next;if (oldnode->data) {hashtblP->freefunc(oldnode->data);}free(oldnode);}}free(hashtblP->nodes);free(hashtblP);return HASH_TABLE_OK;
}
//-------------------------------------------------------------------------------------------------------------------------------
hashtable_rc_t hashtable_is_key_exists (const hash_table_t * const hashtblP, const hash_key_t keyP)
//-------------------------------------------------------------------------------------------------------------------------------
{hash_node_t *node = NULL;hash_size_t  hash = 0;if (hashtblP == NULL) {return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}hash=hashtblP->hashfunc(keyP)%hashtblP->size;node=hashtblP->nodes[hash];while(node) {if(node->key == keyP) {return HASH_TABLE_OK;}node=node->next;}return HASH_TABLE_KEY_NOT_EXISTS;
}
//-------------------------------------------------------------------------------------------------------------------------------
hashtable_rc_t hashtable_apply_funct_on_elements (hash_table_t *const hashtblP, void functP(hash_key_t keyP, void* dataP, void* parameterP), void* parameterP)
//-------------------------------------------------------------------------------------------------------------------------------
{hash_node_t  *node         = NULL;unsigned int  i            = 0;unsigned int  num_elements = 0;if (hashtblP == NULL) {return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}while ((num_elements < hashtblP->num_elements) && (i < hashtblP->size)) {if (hashtblP->nodes[i] != NULL) {node=hashtblP->nodes[i];while(node) {num_elements += 1;functP(node->key, node->data, parameterP);node=node->next;}}i += 1;}return HASH_TABLE_OK;
}
//-------------------------------------------------------------------------------------------------------------------------------
hashtable_rc_t hashtable_dump_content (const hash_table_t * const hashtblP, char * const buffer_pP, int * const remaining_bytes_in_buffer_pP )
//-------------------------------------------------------------------------------------------------------------------------------
{hash_node_t  *node         = NULL;unsigned int  i            = 0;unsigned int  _unused num_elements = 0;if (hashtblP == NULL) {*remaining_bytes_in_buffer_pP = snprintf(buffer_pP,*remaining_bytes_in_buffer_pP,"HASH_TABLE_BAD_PARAMETER_HASHTABLE");return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}while ((i < hashtblP->size) && (*remaining_bytes_in_buffer_pP > 0)) {if (hashtblP->nodes[i] != NULL) {node=hashtblP->nodes[i];while(node) {*remaining_bytes_in_buffer_pP = snprintf(buffer_pP,*remaining_bytes_in_buffer_pP,"Key 0x%"PRIx64" Element %p\n",node->key,node->data);node=node->next;}}i += 1;}return HASH_TABLE_OK;
}//-------------------------------------------------------------------------------------------------------------------------------
/** Adding a new element* To make sure the hash value is not bigger than size, the result of the user provided hash function is used modulo size.*/
hashtable_rc_t hashtable_insert(hash_table_t * const hashtblP, const hash_key_t keyP, void *dataP)
{hash_node_t *node = NULL;hash_size_t  hash = 0;if (hashtblP == NULL) {return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}hash=hashtblP->hashfunc(keyP)%hashtblP->size;node=hashtblP->nodes[hash];while(node) {if(node->key == keyP) {if (node->data) {hashtblP->freefunc(node->data);}node->data=dataP;return HASH_TABLE_INSERT_OVERWRITTEN_DATA;}node=node->next;}if(!(node=malloc(sizeof(hash_node_t)))) return -1;node->key=keyP;node->data=dataP;if (hashtblP->nodes[hash]) {node->next=hashtblP->nodes[hash];} else {node->next = NULL;}hashtblP->nodes[hash]=node;hashtblP->num_elements += 1;return HASH_TABLE_OK;
}
//-------------------------------------------------------------------------------------------------------------------------------
/** To remove an element from the hash table, we just search for it in the linked list for that hash value,* and remove it if it is found. If it was not found, it is an error and -1 is returned.*/
hashtable_rc_t hashtable_remove(hash_table_t * const hashtblP, const hash_key_t keyP)
{hash_node_t *node, *prevnode=NULL;hash_size_t  hash = 0;if (hashtblP == NULL) {return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}hash=hashtblP->hashfunc(keyP)%hashtblP->size;node=hashtblP->nodes[hash];while(node) {if(node->key == keyP) {if(prevnode) prevnode->next=node->next;else hashtblP->nodes[hash]=node->next;if (node->data) {hashtblP->freefunc(node->data);}free(node);hashtblP->num_elements -= 1;return HASH_TABLE_OK;}prevnode=node;node=node->next;}return HASH_TABLE_KEY_NOT_EXISTS;
}
//-------------------------------------------------------------------------------------------------------------------------------
/** Searching for an element is easy. We just search through the linked list for the corresponding hash value.* NULL is returned if we didn't find it.*/
hashtable_rc_t hashtable_get(const hash_table_t * const hashtblP, const hash_key_t keyP, void** dataP)
{hash_node_t *node = NULL;hash_size_t  hash = 0;if (hashtblP == NULL) {*dataP = NULL;return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}hash=hashtblP->hashfunc(keyP)%hashtblP->size;
/*  fprintf(stderr, "hashtable_get() key=%s, hash=%d\n", key, hash);*/node=hashtblP->nodes[hash];while(node) {if(node->key == keyP) {*dataP = node->data;return HASH_TABLE_OK;}node=node->next;}*dataP = NULL;return HASH_TABLE_KEY_NOT_EXISTS;
}
//-------------------------------------------------------------------------------------------------------------------------------
/** Resizing* The number of elements in a hash table is not always known when creating the table.* If the number of elements grows too large, it will seriously reduce the performance of most hash table operations.* If the number of elements are reduced, the hash table will waste memory. That is why we provide a function for resizing the table.* Resizing a hash table is not as easy as a realloc(). All hash values must be recalculated and each element must be inserted into its new position.* We create a temporary hash_table_t object (newtbl) to be used while building the new hashes.* This allows us to reuse hashtable_insert() and hashtable_remove(), when moving the elements to the new table.* After that, we can just free the old table and copy the elements from newtbl to hashtbl.*/hashtable_rc_t hashtable_resize(hash_table_t * const hashtblP, const hash_size_t sizeP)
{hash_table_t       newtbl;hash_size_t        n;hash_node_t       *node,*next;if (hashtblP == NULL) {return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}newtbl.size     = sizeP;newtbl.hashfunc = hashtblP->hashfunc;if(!(newtbl.nodes=calloc(sizeP, sizeof(hash_node_t*)))) return -1;for(n=0; n<hashtblP->size; ++n) {for(node=hashtblP->nodes[n]; node; node=next) {next = node->next;hashtable_insert(&newtbl, node->key, node->data);// Lionel GAUTHIER: BAD CODE TO BE REWRITTENhashtable_remove(hashtblP, node->key);}}free(hashtblP->nodes);hashtblP->size=newtbl.size;hashtblP->nodes=newtbl.nodes;return HASH_TABLE_OK;
}/*******************************************************************************OpenAirInterface Copyright(c) 1999 - 2014 EurecomOpenAirInterface is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.OpenAirInterface is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with OpenAirInterface.The full GNU General Public License is included in this distribution in the file called "COPYING". If not, see <http://www.gnu.org/licenses/>.Contact InformationOpenAirInterface Admin: openair_admin@eurecom.frOpenAirInterface Tech : openair_tech@eurecom.frOpenAirInterface Dev  : openair4g-devel@eurecom.frAddress      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE*******************************************************************************/#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "types/hash/hashtab.h"
//-------------------------------------------------------------------------------------------------------------------------------
/** Default hash function* def_hashfunc() is the default used by hashtable_create() when the user didn't specify one.* This is a simple/naive hash function which adds the key's ASCII char values. It will probably generate lots of collisions on large hash tables.*/static hash_size_t obj_def_hashfunc(const void *keyP, int key_sizeP)
{hash_size_t hash=0;while(key_sizeP) hash^=((unsigned char*)keyP)[key_sizeP --];return hash;
}//-------------------------------------------------------------------------------------------------------------------------------
/** Initialisation* hashtable_create() sets up the initial structure of the hash table. The user specified size will be allocated and initialized to NULL.* The user can also specify a hash function. If the hashfunc argument is NULL, a default hash function is used.* If an error occurred, NULL is returned. All other values in the returned obj_hash_table_t pointer should be released with hashtable_destroy().*/
obj_hash_table_t *obj_hashtable_create(hash_size_t sizeP, hash_size_t (*hashfuncP)(const void*, int ), void (*freekeyfuncP)(void*), void (*freedatafuncP)(void*))
{obj_hash_table_t *hashtbl;if(!(hashtbl=malloc(sizeof(obj_hash_table_t)))) return NULL;if(!(hashtbl->nodes=calloc(sizeP, sizeof(obj_hash_node_t*)))) {free(hashtbl);return NULL;}hashtbl->size=sizeP;if(hashfuncP) hashtbl->hashfunc=hashfuncP;else hashtbl->hashfunc=obj_def_hashfunc;if(freekeyfuncP) hashtbl->freekeyfunc=freekeyfuncP;else hashtbl->freekeyfunc=free;if(freedatafuncP) hashtbl->freedatafunc=freedatafuncP;else hashtbl->freedatafunc=free;return hashtbl;
}
//-------------------------------------------------------------------------------------------------------------------------------
/** Cleanup* The hashtable_destroy() walks through the linked lists for each possible hash value, and releases the elements. It also releases the nodes array and the obj_hash_table_t.*/
hashtable_rc_t obj_hashtable_destroy(obj_hash_table_t *hashtblP)
{hash_size_t n;obj_hash_node_t *node, *oldnode;for(n=0; n<hashtblP->size; ++n) {node=hashtblP->nodes[n];while(node) {oldnode=node;node=node->next;hashtblP->freekeyfunc(oldnode->key);hashtblP->freedatafunc(oldnode->data);free(oldnode);}}free(hashtblP->nodes);free(hashtblP);return HASH_TABLE_OK;
}
//-------------------------------------------------------------------------------------------------------------------------------
hashtable_rc_t obj_hashtable_is_key_exists (obj_hash_table_t *hashtblP, void* keyP, int key_sizeP)
//-------------------------------------------------------------------------------------------------------------------------------
{obj_hash_node_t *node;hash_size_t      hash;if (hashtblP == NULL) {return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}hash=hashtblP->hashfunc(keyP, key_sizeP)%hashtblP->size;node=hashtblP->nodes[hash];while(node) {if(node->key == keyP) {return HASH_TABLE_OK;} else if (node->key_size == key_sizeP) {if (memcmp(node->key, keyP, key_sizeP) == 0) {return HASH_TABLE_OK;}}node=node->next;}return HASH_TABLE_KEY_NOT_EXISTS;
}
//-------------------------------------------------------------------------------------------------------------------------------
/** Adding a new element* To make sure the hash value is not bigger than size, the result of the user provided hash function is used modulo size.*/
hashtable_rc_t obj_hashtable_insert(obj_hash_table_t *hashtblP, void* keyP, int key_sizeP, void *dataP)
{obj_hash_node_t *node;hash_size_t      hash;if (hashtblP == NULL) {return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}hash=hashtblP->hashfunc(keyP, key_sizeP)%hashtblP->size;node=hashtblP->nodes[hash];while(node) {if(node->key == keyP) {if (node->data) {hashtblP->freedatafunc(node->data);}node->data=dataP;// waste of memory here (keyP is lost) we should free it nowreturn HASH_TABLE_INSERT_OVERWRITTEN_DATA;}node=node->next;}if(!(node=malloc(sizeof(obj_hash_node_t)))) return -1;node->key=keyP;node->data=dataP;if (hashtblP->nodes[hash]) {node->next=hashtblP->nodes[hash];} else {node->next = NULL;}hashtblP->nodes[hash]=node;return HASH_TABLE_OK;
}
//-------------------------------------------------------------------------------------------------------------------------------
/** To remove an element from the hash table, we just search for it in the linked list for that hash value,* and remove it if it is found. If it was not found, it is an error and -1 is returned.*/
hashtable_rc_t obj_hashtable_remove(obj_hash_table_t *hashtblP, const void* keyP, int key_sizeP)
{obj_hash_node_t *node, *prevnode=NULL;hash_size_t      hash;if (hashtblP == NULL) {return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}hash=hashtblP->hashfunc(keyP, key_sizeP)%hashtblP->size;node=hashtblP->nodes[hash];while(node) {if ((node->key == keyP) || ((node->key_size == key_sizeP) && (memcmp(node->key, keyP, key_sizeP) == 0))){if(prevnode) {prevnode->next=node->next;} else {hashtblP->nodes[hash]=node->next;}hashtblP->freekeyfunc(node->key);hashtblP->freedatafunc(node->data);free(node);return HASH_TABLE_OK;}prevnode=node;node=node->next;}return HASH_TABLE_KEY_NOT_EXISTS;
}
//-------------------------------------------------------------------------------------------------------------------------------
/** Searching for an element is easy. We just search through the linked list for the corresponding hash value.* NULL is returned if we didn't find it.*/
hashtable_rc_t obj_hashtable_get(obj_hash_table_t *hashtblP, const void* keyP, int key_sizeP, void** dataP)
{obj_hash_node_t *node;hash_size_t      hash;if (hashtblP == NULL) {*dataP = NULL;return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}hash=hashtblP->hashfunc(keyP, key_sizeP)%hashtblP->size;node=hashtblP->nodes[hash];while(node) {if(node->key == keyP) {*dataP = node->data;return HASH_TABLE_OK;} else if (node->key_size == key_sizeP) {if (memcmp(node->key, keyP, key_sizeP) == 0) {*dataP = node->data;return HASH_TABLE_OK;}}node=node->next;}*dataP = NULL;return HASH_TABLE_KEY_NOT_EXISTS;
}
//-------------------------------------------------------------------------------------------------------------------------------
/** Function to return all keys of an object hash table*/
hashtable_rc_t obj_hashtable_get_keys(obj_hash_table_t *hashtblP, void ** keysP, unsigned int *sizeP)
{size_t                 n     = 0;obj_hash_node_t       *node  = NULL;obj_hash_node_t       *next  = NULL;*sizeP = 0;keysP = calloc(hashtblP->num_elements, sizeof(void *));if (keysP) {for(n=0; n<hashtblP->size; ++n) {for(node=hashtblP->nodes[n]; node; node=next) {keysP[*sizeP++] = node->key;next = node->next;}}return HASH_TABLE_OK;}return HASH_TABLE_SYSTEM_ERROR;
}
//-------------------------------------------------------------------------------------------------------------------------------
/** Resizing* The number of elements in a hash table is not always known when creating the table.* If the number of elements grows too large, it will seriously reduce the performance of most hash table operations.* If the number of elements are reduced, the hash table will waste memory. That is why we provide a function for resizing the table.* Resizing a hash table is not as easy as a realloc(). All hash values must be recalculated and each element must be inserted into its new position.* We create a temporary obj_hash_table_t object (newtbl) to be used while building the new hashes.* This allows us to reuse hashtable_insert() and hashtable_remove(), when moving the elements to the new table.* After that, we can just free the old table and copy the elements from newtbl to hashtbl.*/
hashtable_rc_t obj_hashtable_resize(obj_hash_table_t *hashtblP, hash_size_t sizeP)
{obj_hash_table_t       newtbl;hash_size_t        n;obj_hash_node_t       *node,*next;if (hashtblP == NULL) {return HASH_TABLE_BAD_PARAMETER_HASHTABLE;}newtbl.size     = sizeP;newtbl.hashfunc = hashtblP->hashfunc;if(!(newtbl.nodes=calloc(sizeP, sizeof(obj_hash_node_t*)))) return HASH_TABLE_SYSTEM_ERROR;for(n=0; n<hashtblP->size; ++n) {for(node=hashtblP->nodes[n]; node; node=next) {next = node->next;obj_hashtable_insert(&newtbl, node->key, node->key_size, node->data);obj_hashtable_remove(hashtblP, node->key, node->key_size);}}free(hashtblP->nodes);hashtblP->size=newtbl.size;hashtblP->nodes=newtbl.nodes;return HASH_TABLE_OK;
}

demos


static void _unused demo2_openair4Gs_hashtable()
{struct ht_struct {int id;};struct ht_struct *n1 = malloc(sizeof(struct ht_struct)); //hashtable_insert must use malloc insert a noden1->id = 10;hash_table_t *ht = hashtable_create(26, NULL, NULL);hashtable_rc_t _unused ret = hashtable_insert(ht, n1->id, n1);ret = hashtable_is_key_exists (ht, n1->id);log_debug("hashtable_is_key_exists ret = %d\n", ret);struct ht_struct *n2 = NULL;hashtable_get(ht, n1->id, (void**)&n2);log_debug("n2.id = %d\n", n2->id);//free(n2); //forbidden free hashtable_destroy(ht);}static void _unused demo3_openair4Gs_obj_hashtable()
{struct ht_struct {int id;};struct ht_struct *n1 = malloc(sizeof(struct ht_struct));//hashtable_insert must use malloc insert a noden1->id = 10;int *key = malloc(sizeof(int)); //key must malloc again, else will double free abort*key = n1->id;obj_hash_table_t *ht = obj_hashtable_create(26, NULL, NULL, NULL);hashtable_rc_t _unused ret = obj_hashtable_insert(ht, key, sizeof(int), n1);ret = obj_hashtable_is_key_exists (ht, key, sizeof(int));log_debug("hashtable_is_key_exists ret = %d\n", ret);struct ht_struct *n2 = NULL;obj_hashtable_get(ht, key, sizeof(int),(void**)&n2);log_debug("n2.id = %d\n", n2->id);obj_hashtable_destroy(ht);}/* demostration */
_api int rt_hashtab_rt_test()
{//demo1_myhashtab();demo2_openair4Gs_hashtable();demo3_openair4Gs_obj_hashtable();return ret_success;
}#endif //RT_TEST/*MMMMMMMMMMMMMMMMMMMMMMMMMM     .MMMMMMMMMHMMMMMMMMMMMMMMMMMMMMMM  MMMMMMMMMMMMMM  MMMMMMMMMMMMMM  MMMMMMMMMMMMMM:oMMMMMMMMMMMMMM.MMMMMMMMMMMMMMo           MMMMMMMMMMMMMMM M
MMMMMMMMMMMMMMMMMMMMMMMMMMM      MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.  oMMMMMMMMMMMMMMM.MMMMMMMMMMMMMMMMMMMMMMMMMMMMM  MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMoMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM:                     HMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM                  .         MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM              M       MMMMMM.MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM          M   MMMMMMMMMMMM.      MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM       M MMMMMMMMMMMMMM    MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM    .MMMMMMMMMMMMMMMM  MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.MMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMM.MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMHMMMMMMMMMMMMMMMMMMMMM.MMMMMMMMM.MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MMM.oMMMMMMM..MMMMMMMMM:MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MM..MMMMMMM...MMMMMMM. MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM ..MMMMMM...MMMMMM ..MMMMMMMMMMMMMMMMMMMMMMMMMM:M.MMM.M.. MMMMM M..MMMMM...MMMMMMMMMMMMMMMMMM  MMMMMMM. .M..MM.M...MMMMMM..MMMMM.. MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM .MMMM..M....M.....:MMM .MMMMMM..MMMMMMM...MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.M.. ...M......MM.MMMMM.......MHM.M  .MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM..MM. . MMM.....MMMMMM.M.....M ..MM..M MMMMMMMMMMMMMMMMMMM.MMMMMHMM. ..MMMM. MMM............o..... . .MMMMMMMMMMMMMMMMMM. M... .........................M..:.MMMMMMMMMMMMoMMM............ .................M.M.MMMMMMMMM.....MM........................ . MMMMMMM.....M.....................o.MM.MMMMMMMM.M........................M.. ...MMMMMMMMMMMMMo:....MMM..............MMM..oMMMMMMMM...MMM.............MMMMMMM.............:MMMMMMMMM..... MMM.....MM M.............................MooM.................MM  MoMMMMMoooMMMoooM......................MoooooooH..oMMMHooooMoM.....................MMooooooM........MoooooooMoooM......... o........MoooooooM............Mooooooooooo.......M.........Moooooooo:..............MMooMoooooooooM...M........:Mooooooooooo:..............MM..oooooooooooo .........Mooooooooooooooo..............MM...Mooo:oooooooo.M....ooooooooooooooooooo..M...........M...oooooMoooooooM..Mooooooooooooo:oooooooM.M...........M.M...ooooooMoo:ooooMoooooooooooooHoooooooooH:M. ...........:M..MoooooooMoooooooooooooooooo:ooooooMooooMoM..............MM..ooooooooooMooooooooooooooHoooooooMooHooooM...............M...ooooooooooooooooooo:MooooooooooooooMoMoooM................M...oooooooooooooooooooooooooooooooooooooMooMM................M...MooooooooooooooooooooooooooooooooooooooooMo ...................MooooooooooooooooooooooooooooooooooooooooM M................MM...ooooooooooooooooooooooooooooooooooooooooM   ................M...MoooooooooooooooooooooooooooooooooooooooMM   .:....................MooooooooooooooooooooooooooooooooooooMoo       .............MM...... ooooooooooooooooooooooooooooooooooooM       M..............MM........MooooMMM MM MM  MMMMMMMMMooooooooM         M...............M.........HM     M:  MM :MMMMMM          M           M...............M..........M     M   MoM M                           M................MM.........:M  MoH  M M M MooooHoooMM.   M             M...............MM..........Moooo MMooM    oooooMooooooooM              M..............HM.........MooooM  Mooo  : ooooooMooooMoooM              M........ . .o.MH..  .....ooooo   oooo  M MooooooooooooooM               M... MMMMMMMMMMMMMMMMMMMMMooooM M oooo  .  ooooooMooooooooM              .MMMMMMMMMMMMMMMMMMMMMMMMMooooH : ooooH    oooooooooooooooo               MMMMMMMMMMMMMMMMMMMMMMMMMoooo    ooooM    Moooooooooooooooo              .MMMMMMMMMMMMMMMMMMMMMMMMMoooo    ooooM    MooooooooooooooooM              MMMMMMMMMMMMMMMMMMMMMMMMMoooM    ooooM     ooooooooooooooooo               MMMMMMMMMMM:MMMMMMMMMMMoooM   MooooM     oooooooooooMoooooo               MH............ ......Mooo.   MooooM     oooooooooooooooooo              M............MM.M......oooo    MooooM     Moooooooooooooooooo:           .........M.....M.M.....Moooo    MooooM      ooooooooooooooooooM            .M...................MooooH    MooooM      oooooooooMoooooooooo          M..o...M..o....M.o....HMooooM    MooooH      MooooooooMooooooooooM          .:M...M.......MM..M.....MoooM    :oooo:    .MooooooooHooMoooooooooM         M M... ..oM.MM...M.:.Mooo. MMMMooooo   oooooooooooMoooooooooooooM          ....M. MM:M..o.Moooooooooooooo MooooooooooooooMooooooooooooM          .MoMooooooooooooooMooooooooooooMoMooooooooooooooMooooooooooooooo:oooooooooooooooooooooooooooooooooooooooooooooMooooooooooMooooooooooooooooooooooooooooooooooMoooooooooooMooooooooooooooooHoooMooooooooooooooMoooooooooooooooooooooooooooMoMMooMoooooooooooooo.ooooooooooooooooooooooooooo:oMMoooooooooooooooooooooooooooooooooooooooooooooooMMoooMooooooooooooooMooooooooooooooooooooooooooooo.MoooMooooooooooooooMoooooooooooooooooooooooooMooooMMooooooooooooooooooMoooooooooooooooooooooooooMoooooMMooooMoooooooooooooMoooooooooooooooooooooooooMoHooooMooooooMooooooooooooooooooooooooooooooooooooooooMoMoooMMooooooooooooooooooooMooooooooooooooooooooooooooMoooooH:MoooooooMooooooooooooMoooooooooooooooooooooooooooooHoooMMooooooooMoooooooooooMoooooooooooooooooooooooooMoooMooooMMoooooooooooooooooooooooooooooooooooooooooooooo.oooMoooooMoooooooooooooooooooooooooooooooooooooooooooooMoooooooooMMooooooooooooooooooooMoooooooooooooooooooooooooooooooooMMooooooooooooooooooooMHooooooooooooooooooooMoooo:oooooMMooooooooooooooooooMoMHoooooooooooooooooooooooMoooooMMoooooooooooooooMMooo MMooooooooooooooooooooooooooMMMMoooooooooooooMooooo  oooooooooooooooooooooMoooooMooMMoooooooooMoooMMoM  ooooHooooooooooooooooMooooMMooooMooooooMooooMoooM  MoooooMoooooooooooooMoooooooooooMMooooooooMooooM  MoooooooooMooooooooooooooMHooooooMoooooooMooooM    HoooooooHooMooooooooooooooooMoooooooooHoooM         MoooooooooMoooooooooMHooooooooooooHM             MooooooooMMoooooooMMMMMMMMMMMMMMM                Moooooo:MooooHMMMMMMMMM: ...                  MMMMMMMMMMMMMMM............M                  MMMMMMMMM ....M.MM..........                  M.............MM ..............MM                 M..............MMMMM............MMMM                 ..MMMMMMMM ....MMMMMMMMMMMMMMMMMMMMMMMMM               MMMMMMMMMMMMM...M.MMMMMMMMMMMMMMMMMMMMMMMMMM               MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM                MMMMMMMMMMMMMMMMMMM:MMMMMMMMMMMMMMMMMMH                     MMMMMMMMMMMMMMMMMMMBy EBEN Jérôme                        MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMHMMMMMM*/

OpenAirInterface中的哈希表hashtable实现相关推荐

  1. 在C#中应用哈希表(Hashtable)

    一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其 ...

  2. C#中哈希表(HashTable)的用法详解

    1.  哈希表(HashTable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似keyvalue的键值对, ...

  3. PHP内核中的哈希表结构

    https://github.com/HonestQiao/tipi/commit/17ca680289e490763a6a402f79afa2a13802bb36 下载:https://github ...

  4. 哈希表(hashtable)的javascript简单实现

    javascript中没有像c#,java那样的哈希表(hashtable)的实现.在js中,object属性的实现就是hash表,因此只要在object上封装点方法,简单的使用obejct管理属性的 ...

  5. c ++中哈希表如何访问_C / C ++中的哈希表–完整的实现

    c ++中哈希表如何访问 A Hash Table in C/C++ (Associative array) is a data structure that maps keys to values. ...

  6. 哈希表 matlab实现,MATLAB中的哈希表

    MATLAB中的哈希表 MATLAB是否支持散列表? 一些背景 我正在研究Matlab中的一个问题,需要图像的尺度空间表示. 为了做到这一点,我创build了一个二维高斯滤波器,在一定范围内为方差si ...

  7. 哈希表 Hashtable c# 1613537346

    哈希表 Hashtable c# 1613537346 使用命名空间 using System.Collections; 实例化得到对象 Hashtable 对象 = new Hashtable(); ...

  8. 哈希表 HashTable

    (1)哈希表底层存储结构也是线性表 (2)哈希表的核心在于哈希函数,哈希函数用于获取index值,决定了将元素放在哪个位置 (3)hash表的增删查时间复杂度都是O(1) 可以根据hash函数直接定位 ...

  9. 一文看懂哈希表并学会使用C++ STL 中的哈希表

        最近在刷题以及做编程练习的作业时经常会用到哈希表,碰到一些想用的函数时每次都看别人的博客,现结合别人的博客对哈希表做个总结. 本篇博客的主要内容如下 1. 哈希表的定义 2. 如何使用STL库 ...

最新文章

  1. gets函数的不安性详解
  2. ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问
  3. Matlab | Matlab从入门到放弃(7)——struct结构体
  4. 动画讲解C语言的指针,从未如此简单
  5. linux+守护进程+php,【转载】Linux 守护进程的编程方法
  6. Linux之强大的selinux
  7. 一文读懂802.1x协议,随便秒杀面试官
  8. linux中用shell获取昨天、明天或多天前的日期
  9. InvalidClassException
  10. Caffe学习系列(13):数据可视化环境(python接口)配置 jupyter notebook
  11. 小学steam计算机课程案例,STEAM课程典型案例——桥世界
  12. 无法安装驱动程序 此计算机上不存在英特尔适配器,无法安装驱动程序。此计算机不存在英特尔(R)适配器...
  13. 常见经典音频运放(一般作前级用)
  14. 码农到架构师视频学习笔记
  15. python如何编写爬虫程序_python编写网络爬虫程序
  16. 支付宝社交风波以道歉收场,微信小程序会有不同结局吗?
  17. 广州市长温国辉:用“加减乘除法”发展民营经济
  18. 【历史上的今天】9 月 2 日:互联网的“诞生日”;三星逐步跌落神坛;世界上第一个ATM自动取款机
  19. 如何批量将 bmp 格式图片转换为 png 格式
  20. 几个FFmpeg 视频参数 fps、tbr、tbn、tbc

热门文章

  1. Java性能调优小技巧
  2. vue中的 $children 和 $parent
  3. Linux分区之parted命令
  4. ios---NSNotificationCenter传值
  5. [BZOJ 3260] 跳
  6. UVa 1347 旅行
  7. SQL转换全角和半角函数
  8. Android代码混淆
  9. 超人气光棍节!现在时间虽然不是2011年11月11日11点11分11秒11毫秒11微秒11纳秒11皮秒11飞秒11阿秒11渺秒11......
  10. JavaScript对象学习笔记