A very small part of php hashmap has been modified to implement resize # include & lt; stdlib. h & gt; # include & lt; stdio. h & gt; # include & lt; string. h & gt; # implementing a HASHMAP of a very small part of PHP in inclu
Modified it to implement resize.
# Include
# Include
# Include
# Include
# Include
Typedef struct bucket {int h; char * key; void * pData; struct bucket * pNext; struct bucket * pLast;} Bucket; typedef struct hashtable {int size; int elementsNum; int mask; Bucket ** arBuckets; // This is an array that stores buckets} HashTable; /***** this is an algorithm used to calculate the HASH value **/int time33 (char * arKey, int arlength) {int h = 0; int I; for (I = 0; isize = 1; (* ht)-> mask = (* ht)-> size-1; (* ht)-> elementsNum = 1; (* ht) -> arBuckets = (Bucket **) malloc (si Zeof (Bucket *) * (* ht)-> size); memset (* ht)-> arBuckets, 0, sizeof (Bucket *) * (* ht) -> size); return 1;} int _ hash_link_bucket_to_bucket_head (Bucket ** newBucket, Bucket ** bucketHead) {if (* bucketHead = NULL) {* bucketHead = * newBucket ;} else {(* newBucket)-> pNext = (* bucketHead)-> pNext; (* newBucket)-> pLast = (* bucketHead); if (* bucketHead) -> pNext! = NULL) {(* bucketHead)-> pNext-> pLast = * newBucket;} (* bucketHead)-> pNext = * newBucket;} return 1 ;} int _ hash_new_bucket (Bucket ** newBucket, int hash, char * arkey, void * pData) {(* newBucket) = (Bucket *) malloc (sizeof (Bucket )); (* newBucket)-> h = hash; (* newBucket)-> key = arkey; (* newBucket)-> pData = pData; (* newBucket)-> pNext = NULL; (* newBucket)-> pLast = NULL; return 1;} int _ hash_rehash (HashTable * ht) {int I = 0 ;// Since I have not defined the pListNext pointer, we can only rehash this way. For (; I
Size; I ++) {if (ht-> arBuckets [I]! = NULL) {int index = ht-> arBuckets [I]-> h & ht-> mask; if (I! = Index) {_ hash_link_bucket_to_bucket_head (& ht-> arBuckets [I], & ht-> arBuckets [index]); ht-> arBuckets [I] = NULL ;}}} return 1;}/***** increase the size of HASHTABLE by 1 times **/int _ hash_resize (HashTable * ht) {if (ht! = NULL) {ht-> size = ht-> size <1; ht-> mask = ht-> size-1; realloc (& ht-> arBuckets, sizeof (Bucket *) * ht-> size); int I; for (I = ht-> size> 1; I
Size; I ++) {ht-> arBuckets [I] = NULL;} // memset (ht-> arBuckets [0], NULL, sizeof (Bucket *) * (ht-> size> 1); printf ("resize: % I \ r \ n", ht-> size); _ hash_rehash (ht ); return 1;} return 0;}/*** add the element to HASHTABLE **/int _ hash_add_or_update (HashTable * ht, char * arKey, int arLength, void * pData) {int h = time33 (arKey, arLength); int index = h & ht-> mask; Bucket * p = ht-> arBuckets [index]; while (p! = NULL) {if (strcmp (arKey, p-> key) = 0) {// here the update Operation free (p-> pData) should be executed ); p-> pData = pData; return 1;} p = p-> pNext;} Bucket * newBucket; _ hash_new_bucket (& newBucket, h, arKey, pData ); _ hash_link_bucket_to_bucket_head (& newBucket, & ht-> arBuckets [index]); ht-> elementsNum ++; if (ht-> elementsNum = ht-> size) {_ hash_resize (ht);} return 0;} void * _ hash_find (HashTable * ht, char * arKey, int arLength) {int h = time33 (arKey, arLength); int Index = h & ht-> mask; Bucket * p = ht-> arBuckets [index]; while (p! = NULL) {if (strcmp (arKey, p-> key) = 0) {return p-> pData;} p = p-> pNext;} return 0 ;} int PUT (HashTable * ht, void * key, void * value) {char * arKey = (char *) key; int len = strlen (arKey); return _ hash_add_or_update (ht, arKey, len, value);} void * GET (HashTable * ht, void * key) {char * arKey = (char *) key; int len = strlen (arKey ); return _ hash_find (ht, arKey, len);} int main () {printf ("% s \ r \ n", "This is an example of hashtable "); hashTable * ht; _ init_hash_table (& ht); PUT (ht, "1", "mengjun"); PUT (ht, "2", "aaaaa "); PUT (ht, "3", "fff"); PUT (ht, "24", "eee"); PUT (ht, "25", "ddd "); printf ("% s \ r \ n", (char *) GET (ht, "1"); printf ("% s \ r \ n", (char *) GET (ht, "2"); printf ("% s \ r \ n", (char *) GET (ht, "3 ")); printf ("% s \ r \ n", (char *) GET (ht, "24"); printf ("% s \ r \ n", (char *) GET (ht, "25"); printf ("HASHTABLE has a total of % I \ r \ n", ht-> elementsNum); return 0 ;}