Cashing in a tiny portion of PHP's HashMap

Source: Internet
Author: User
Tags strcmp
Implement a minimal portion of PHP HashMap
Modified again, realized the 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 a storage buckets array}hashtable;/** * * This is an algorithm for calculating hash value **/int time33 (char* arkey,int arlength) {int h = 0;int I;for (i=0;is    ize = 1; (*ht)->mask = (*HT)->size-1; (*ht)->elementsnum = 1; (*ht)->arbuckets = (bucket**) malloc (sizeof (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 ( Buckets));(*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;//because I don't define a plistnext pointer, I can only do this rehash. 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;} /** * * Expands the size of HashTable by 1 time Times **/int _hash_resize (hashtable* ht) {if (HT! = NULL) {ht->size = Ht->size << 1;ht->ma SK = 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));p rintf ("resize:%i\r\n", ht->size); _hash_rehash (HT); return 1;} return 0;} /** * * Add elements to HashTable **/int _hash_add_or_update (hashtable* ht,char* arkey,int arlength,void* pData) {int h = time33 (Arke y,arlength); int index = h & ht->mask; bucket* p = ht->arbuckets[index]; while (P!=null) {if (strcmp (Arkey,p->key) ==0) {//The update operation should be performed here free (p->pdata);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;} IntPUT (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 (&AMP;HT); PUT (HT, "1", "Mengjun"); PUT (HT, "2", "AAAAA"); PUT (HT, "3", "FFF"); PUT (HT, "" "," Eee "); PUT (HT, "+", "ddd");p rintf ("%s\r\n", (char*) Get (HT, "1"));p rintf ("%s\r\n", (char*) Get (HT, "2"));p rintf ("%s\r\n", ( char*) GET (HT, "3"));p rintf ("%s\r\n", (char*) Get (HT, ""));p rintf ("%s\r\n", (char*) Get (HT, ")");p rintf (" Hashtable a total of elements%i a \ r \ n ", ht->elementsnum); return 0;}
       
      
     
    
   
  
 
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.