473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Hashing with collision resolution

Helu gurus!!! I have a code below about hashing method with collision resolution...My problem is how to use the collsion resolution again if the hash index though has already a value. Please kinda help me with this..I want to determine also if the table now is full so as to stop user from entering a key.
size of the table=19,
Thanx a lot..GodBless
________________________________________________
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4. //function prototypes
  5. int input(int);
  6. int process(int);
  7. void output(int,int);
  8. int collision(int,int);
  9.  
  10. //global variable
  11. int arr[19]={0};
  12.  
  13. //start of main program
  14.  main()
  15. {
  16. int key,hash_key,hash_index,m=0;
  17. cout<<"Hash method: FOLD BOUNDARY";
  18. cout<<"\nCollision resolution method: KEY OFFSET";
  19. cout<<"\nEnter 6 digit numbers.";
  20. cout<<"\nEnter 0 as sentinel.";
  21. while(m<19){
  22.  
  23.  hash_key=input(key);
  24.  if (hash_key==0) { return 0;}
  25.  else
  26.    {     hash_index=process(hash_key);
  27.       output(hash_key,hash_index); }
  28.  
  29.   }  m++;
  30. cout<<"\nEntered numbers execeeded!"; //end of loop
  31. }//end of main program
  32.  
  33. //start of input function
  34. int input(int x){
  35. cout<<"\n\nEnter key:";
  36. cin>>x;
  37. return x;}
  38. //end of input function
  39.  
  40. //start of process function
  41. int process(int x){
  42.   int a,b,c,d,e,f;
  43.     a=x/100000;
  44.     b=x%100000/10000;
  45.       int v=(b*10)+a;
  46.  
  47.    c=x%10000/1000;
  48.      d=x%1000/100;
  49.      int q=(c*10)+d;
  50.  
  51.     e=x%10;
  52.      f=x%100/10;
  53.      int w=(e*10)+f ;
  54.  
  55.      return (w+v+q)%19+1;
  56. }  //end of process function
  57.  
  58. //start of output function
  59. void output(int key,int adres){
  60. int b,z,index1=0,index2=0,index3=0,index4=0,u;
  61. bool wer;
  62. cout<<"\n\tList\tKey\n";
  63.  
  64. while (arr[adres-1]!=0)
  65.     { index1=collision(key,adres);
  66.        arr[index1-1]=key;break;
  67.  
  68. if (arr[index1-1]!=0)
  69.  { index2=collision(key,index1);
  70.    arr[index2-1]=key;
  71.  
  72. if(arr[index2-1]!=0)
  73.      { index3=collision(key,index2);
  74.        arr[index3-1]=key;
  75.    }
  76.  }
  77.  
  78.    if (arr[adres-1]==0)
  79.    { arr[adres-1]=key; }
  80.  
  81. for(u=0;u<19;u++){
  82.       z=arr[u];
  83.       cout<<"\t"<<(u+1)<<".\t"<<z<<endl; }
  84. }
  85.  
  86.     //end of output function
  87.  
  88. //start of collision resolution method
  89. int collision(int key,int hashed){
  90. int offset,adrs;
  91. offset=(key/19;
  92. adrs=((offset + hashed) % 19 )+1;
  93. return adrs;
  94. }
  95. //end of collision resolution method
Sep 8 '08 #1
1 9532
May,

The following is rather lengthy, but is a complete system which contains a hashing algorithm that I cranked out in the past hour. Some code was borrowed from some previous software that I had written. At any rate, the code is written in C, as a console program, not in C++ but you should be able to use it directly from within C++.

This shows a method of hashing where you never have to worry about running out of space, and don't have to concern yourself with collisions as they will all be gracefully addressed.

What this does, is take a text file as input, parses out all ot the words (including non words, as I didn't spend the time to remove them), then adds the words to a hash table. If the word already exists in the table, only the count is incremented.

The hashing algorithm is taken directly from the Aho book on compiler design. It works extremely well with strings, but dosen't handle numbers without conversion to text. This is a small price to pay, because it is extremely fast as far as operation is concerned. I used to be able to process one million phone calls per minute on an HP9000 for a major phone company.

Once the file has been added to the hash table, you are allowed to enter words, which will be looked up in the hash table and displayed on the console, along with the number of times that it appears in the document.

The code will be in 8 small files. the only two that you will really need to look at are Hash.c, LoadHashTable.c and TryItOut.c the rest are part of the complete package.

If you have any questions at all, please ask.

Module name HashExample.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef __HashExample__h
  2. #define __HashExample__h
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7.  
  8. #define MASK                0xf0000000
  9. #define SHIFT               24
  10. #define HashTableNumItems   5003    // Needs to be a prime number for
  11.                                     // best distribution. At least 10% of
  12.                                     // estimated data size.
  13.  
  14. #define FileBuffSz          4096
  15. #define Buff80              80
  16. #define Buff256             256
  17.  
  18. #ifndef bool
  19. #define bool                int
  20. #endif
  21.  
  22. #ifndef True
  23. #define True                1
  24. #define False               0
  25. #endif
  26.  
  27. typedef struct _wordused
  28. {
  29.    struct _wordused *Next;
  30.    char *WordUsed;
  31.    long NumberOfTimesUsed;
  32. } wordused;
  33.  
  34. typedef struct
  35. {
  36.    long                     SysInfoSz;
  37.    FILE                     *InFile;
  38.    FILE                     *Trace;
  39.    char                     *InFileName;
  40.    char                     *InBuff;
  41.    wordused                 *HashTable[HashTableNumItems];
  42.    int                      argc;
  43.    char                     **argv;
  44.    long                     TotalNumberOfWords;
  45. } sysinfo;
  46.  
  47. // Function prototypes
  48. void main(int argc, char** argv);
  49. void Initialize(sysinfo *SysInfo);
  50. void TryItOut(sysinfo *SysInfo);
  51. void FreeData(sysinfo *SysInfo);
  52. void LoadHashTable(sysinfo *SysInfo);
  53. unsigned int Hash(char *String, long TabLen);
  54. void SysAbort(sysinfo *SysInfo, char *String, int NumArgs, ...);
  55. #endif
Module Name: HashExample.c
Expand|Select|Wrap|Line Numbers
  1. /*
  2. *
  3. *  Example of Hashing which never runs out of space, and
  4. *  handles collisions gracefully and painlessly
  5. *
  6. *  Usage for this Example HashExample -f FileName
  7. 8      where filename = the name of a local text file
  8. *              or remote file (with full path)
  9. *
  10. *       What does it do? It will parse all of the words
  11. *         In the input file, counting the number of
  12. *         occurences of each word. After the input file
  13. *         has been parsed, the user can query on a word
  14. *         and the count will be returned.
  15. *
  16. *       Plese note that the hashing algorithm used expects
  17. *         a string input, there fore integers, floats, etc.
  18. *         must be converted to string before hashing. The
  19. *         algorithm does, however, give extremely good
  20. *         distribution, thus making it ideal for very large
  21. *         amounts of entries. I used it while employed in the
  22. *         telecommunications industry for looking up data in
  23. *         indexed flat files (created using the hash) of over
  24. *         4 million records. The original algorithm comes from
  25. *         Compilers: Principles, Techniques, and Tools ...
  26. *         By: Alfred V. Aho - fondly known as the Dragon book.
  27. *
  28. *        Enjoy !
  29. *
  30. */
  31. #include "HashExample.h"
  32.  
  33. void main(int argc, char** argv)
  34. {
  35.    sysinfo *SysInfo = NULL;
  36.  
  37.    SysInfo = (sysinfo *) calloc(1, sizeof(sysinfo));
  38.    SysInfo->argc = argc;
  39.    SysInfo->argv = argv;
  40.  
  41.    Initialize(SysInfo);
  42.    LoadHashTable(SysInfo);
  43.    TryItOut(SysInfo);
  44.    FreeData(SysInfo);
  45.    free(SysInfo);
  46. }
  47.  
Module Name: Initialize.c
Expand|Select|Wrap|Line Numbers
  1. #include "HashExample.h"
  2.  
  3. void Initialize(sysinfo *SysInfo)
  4. {
  5.    char        *ArgPtr       = NULL;
  6.    char        Argid         = 0;
  7.    int         i             = 0;
  8.  
  9.    SysInfo->SysInfoSz = sizeof(sysinfo);
  10.    for(i = 1; i < SysInfo->argc; i++)
  11.    {
  12.       if(SysInfo->argv[i][0] == '-')
  13.       {
  14.          Argid = SysInfo->argv[i][1];
  15.          if(!SysInfo->argv[i][2])
  16.          {
  17.             i++;
  18.             ArgPtr = SysInfo->argv[i];
  19.          }
  20.          else
  21.             ArgPtr = (char *)&SysInfo->argv[i][2];
  22.          switch (Argid)
  23.          {
  24.             case 'f':
  25.                SysInfo->InFileName = ArgPtr;
  26.                break;
  27.             default:
  28.                SysAbort(SysInfo, "%c is not a valid option", 1, Argid);
  29.          }
  30.       }
  31.    }
  32.  
  33.    if(SysInfo->InFileName == NULL)
  34.       SysAbort(SysInfo, "Please Specify Input File Name with -f option", 0);
  35.  
  36.    SysInfo->InBuff = (char *)calloc(1, FileBuffSz);
  37.    SysInfo->TotalNumberOfWords = 0;
  38. }
  39.  
Module Name: SysAbort.c
Expand|Select|Wrap|Line Numbers
  1. #include "HashExample.h"
  2.  
  3. void SysAbort(sysinfo *SysInfo, char *String, int NumArgs, ...)
  4. {
  5.    va_list       ap;
  6.    va_start(ap, NumArgs);
  7.  
  8.    vfprintf(stderr, String, ap);
  9.  
  10.    va_end(ap);
  11.  
  12.    // Check if n\memory needs to be cleaned up
  13.    FreeData(SysInfo);
  14.  
  15.    free(SysInfo);
  16.    exit(1);
  17. }
Module Name: FreeData.c
Expand|Select|Wrap|Line Numbers
  1. #include "HashExample.h"
  2.  
  3. void FreeData(sysinfo *SysInfo)
  4. {
  5.    int i = 0;
  6.    wordused *CurrentNode = NULL;
  7.    wordused *NextNode    = NULL;
  8.  
  9.    fclose(SysInfo->InFile);
  10.  
  11.    for(i = 0; i < HashTableNumItems; i++)
  12.    {
  13.       CurrentNode = SysInfo->HashTable[i];
  14.       if(CurrentNode)
  15.       {
  16.          while(CurrentNode)
  17.          {
  18.             NextNode = CurrentNode->Next;
  19.             free(CurrentNode->WordUsed);
  20.             free(CurrentNode);
  21.             CurrentNode = NextNode;
  22.          }
  23.       }
  24.    }
  25.  
  26.    free(SysInfo->InBuff);
  27. }
  28.  
Module Name: Hash.c
Expand|Select|Wrap|Line Numbers
  1. #include "HashExample.h"
  2.  
  3. unsigned int Hash(char *String, long TabLen)
  4. {
  5.    unsigned long HashCode = 0;
  6.    unsigned long g = 0;
  7.    char *p;
  8.  
  9.    if(String) {
  10.       for(p = String; *p; p++) {
  11.          HashCode = (HashCode << 4) + *p;
  12.          if(g = HashCode & MASK) {
  13.             HashCode ^= g >> SHIFT;
  14.             HashCode ^= g;
  15.          }
  16.       }
  17.    }
  18.    return(HashCode % TabLen);
  19. }
  20.  
Module Name: LoadHashTable.c
Expand|Select|Wrap|Line Numbers
  1. #include "HashExample.h"
  2.  
  3. void LoadHashTable(sysinfo *SysInfo)
  4. {
  5.    int           i            = 0;
  6.    int           NodeSz       = sizeof(wordused);
  7.    unsigned long HashCode     = 0L;
  8.    wordused      *CurrentNode = NULL;
  9.    int           IdxSz        = sizeof(wordused);
  10.    char          *Word        = NULL;
  11.    bool          FoundItem    = False;
  12.  
  13.    for(i = 0; i < HashTableNumItems; i++)
  14.    {
  15.       SysInfo->HashTable[i] = NULL;
  16.    }
  17.  
  18.    SysInfo->InFile = fopen(SysInfo->InFileName, "r");
  19.    if(SysInfo->InFile == NULL)
  20.    {
  21.       fprintf(stderr, "Unable to open InFile", SysInfo->InFileName);
  22.    }
  23.    else
  24.    {
  25.       while(True)
  26.       {
  27.          fread(SysInfo->InBuff, IdxSz, 1, SysInfo->InFile);
  28.          if(feof(SysInfo->InFile))
  29.             break;
  30.          Word = strtok(SysInfo->InBuff, " \n\t");
  31.          while(Word)
  32.          {
  33.             FoundItem = False;
  34.             HashCode = Hash(Word, HashTableNumItems);
  35.             CurrentNode = SysInfo->HashTable[HashCode];
  36.             CurrentNode = SysInfo->HashTable[HashCode];
  37.             if(CurrentNode)
  38.             {
  39.                while(CurrentNode)
  40.                {
  41.                   if(CurrentNode->WordUsed &&
  42.                      (!(strcmp(CurrentNode->WordUsed, Word))))
  43.                   {
  44.                      CurrentNode->NumberOfTimesUsed += 1;
  45.                      FoundItem = True;
  46.                      break;
  47.                   }
  48.                   else if(CurrentNode->Next == NULL)
  49.                   {
  50.                      CurrentNode->Next = (wordused *)calloc(1, NodeSz);
  51.                      break;
  52.                   }
  53.                   else
  54.                      CurrentNode = CurrentNode->Next;
  55.                }
  56.             }
  57.             if(!FoundItem)
  58.             {
  59.                SysInfo->TotalNumberOfWords += 1;
  60.                if(!CurrentNode)
  61.                {
  62.                   CurrentNode = SysInfo->HashTable[HashCode] =
  63.                      (wordused *)calloc(1, NodeSz);
  64.                }
  65.                CurrentNode->WordUsed = (char *)calloc(1, (strlen(Word)+1));
  66.                strcpy(CurrentNode->WordUsed, Word);
  67.                CurrentNode->NumberOfTimesUsed = 1;
  68.             }
  69.             Word = strtok(NULL, " \n\t");
  70.          }
  71.       }
  72.    }
  73. }
Module Name: TryItOut.c
Expand|Select|Wrap|Line Numbers
  1. #include "HashExample.h"
  2.  
  3. void TryItOut(sysinfo *SysInfo)
  4. {
  5.    wordused *CurrentNode   = NULL;
  6.    unsigned long HashCode     = 0L;
  7.    char     *Aword         = (char *)calloc(1, Buff256);
  8.    bool     WordFound      = False;
  9.  
  10.    fprintf(stdout, "The document contained %ld words\n",
  11.       SysInfo->TotalNumberOfWords);
  12.    while(True)
  13.    {
  14.       gets(Aword);
  15.       if(!(strcmp(Aword, "GoodByeYall")))
  16.          break;
  17.       HashCode = Hash(Aword, HashTableNumItems);
  18.       CurrentNode = SysInfo->HashTable[HashCode];      WordFound = False;
  19.       while(CurrentNode)
  20.       {
  21.          if(!strcmp(CurrentNode->WordUsed, Aword))
  22.          {
  23.             WordFound = True;
  24.             break;
  25.          }
  26.          CurrentNode = CurrentNode->Next;
  27.       }
  28.  
  29.       if(WordFound)
  30.          fprintf(stdout, "%s was used in the document %d times\n",
  31.             Aword, CurrentNode->NumberOfTimesUsed);
  32.       else
  33.          fprintf(stdout, "%s was not used in the document\n");
  34.    }
  35. }
  36.  
Ti exit the program, type 'GoodByeYall' case sensitive.

Enjoy,

Larry
Oct 2 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Farouche | last post by:
Hi I would like some suggestions on how to, effectively compute a Hash Value for a "Collection" of simple Field Objects: internal class Field { private string _fieldName; private object...
4
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
8
by: Foodbank | last post by:
Hi, Has anyone ever hashed a text file to disk before? I'm trying to convert an in-memory hash to disk hash and I can't find any relevant information to help me. Also, I'd like to use lseek,...
8
by: Maya | last post by:
Hello all, I'm using MD5 hashing in my application to give unique values to huge list of items my application receives, originally every item's name was difficult to use as an id for this item...
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
11
by: January Weiner | last post by:
Hello, I need to use a hashing function for relatively short strings (roughly 16 characters or less). The data that needs to be accessed via hash is just a simple int. I just need to look up...
24
by: Johnny Jörgensen | last post by:
I'm wondering (and hoping that somebody will be able to answer this): If I calculate the hash value of files (either MD5 or SHA1), can I then be sure that: 1) Two files with the same hash...
8
by: Helmut Jarausch | last post by:
Hi, I need to hash arrays of integers (from the hash module). So, I have to derive from array and supply a __hash__ method. But how to hash an array (of fixed length, say 25)? What I need is...
1
by: dansongarcia | last post by:
I am currently doing an investigation in this topic. Please give some feedback that can help me in my investigation. Is there any way to compare hash functions namely: Extraction, Folding,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.