Thread: hashing

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    hashing

    can you tell me the usage of hashing what is hashing

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Hashing is when you take some bit of data and turn it into an integer suited for an array index :-)
    Code:
    #include <iostream>
    
    using namespace std;
    
    const size_t H_SIZE = 100;
    
    size_t myhash(char *string)
    {
      size_t hval;
    
      for (hval = 0; *string != '\0'; string++)
      {
        hval = *string + 31 * hval;
      }
    
      return hval % H_SIZE;
    }
    
    int main()
    {
      cout<< (unsigned long)myhash("Have fun!") <<endl;
    }
    *Cela*

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    heres a great hash generating algo for those who might be interested

    Code:
    getHash(int& hash, const char* key)
    {    
      unsigned long h = 0;
      unsigned long g;
      unsigned char ch;
      while (*key != 0) {
        ch = (unsigned char) *key++;
        h = (h << 4) + ch;
        if ((g = h & 0xf0000000L) != 0)
          h = (h ^ (g >> 24)) ^ g;
      }
      hash = (int)h%CAPACITY;         //CAPACITY is the size of your hash table
     
    }
    i stored a full english dictionary using this hash generating funtion and collision time was neglegable. =)

    (btw: the algorithm is credited to some crazy math guy but i cant remember his name)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Linked List Using Hashing and Stack
    By m0ntana in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 07:11 AM
  2. Hashing, indexing and phonetic normalization for approximate str matching...
    By biterman in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-21-2006, 09:42 AM
  3. Replies: 8
    Last Post: 09-11-2006, 11:26 AM
  4. Doubt about Hashing
    By louis_mine in forum C Programming
    Replies: 1
    Last Post: 11-11-2005, 12:00 AM
  5. Double Hashing
    By carrja99 in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2003, 08:36 AM