473,598 Members | 3,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hashing with collision resolution

3 New Member
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 9555
Flugeldorph
11 New Member
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
1628
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 _fieldValue;
4
3298
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 interact with a rich 3D environment. Seem to be 90% of the way there! My problems are related to calculations where the result tends to zero (or another defined limit.) Have loads of cases where this kind of interaction occurs but this one
8
4222
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, read, write, and fd if anyone is familiar with them. Any info would be greatly helpful. Thanks, James
8
4569
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 although its unique but because it had certain characters and variable lengths I ended up using MD5 hashing of the name.
1
3896
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: System::Collections::SortedList, System::Collections::Generic::SortedList, using namespace System::Collections; using namespace System::Collections::Generic; Below is a working version of the generic template SortedList, which
11
2144
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 values in two dimensional matrices each time that I encounter a pair of these strings. I would like to keep the code as simple and standard as possible, but at the same time to have a reasonable performance.
24
1949
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 value are in fact identical? 2) Two different files will NEVER have the same hash value? 3) If two files have the same MD5 hash value, they will ALSO have the same
8
5404
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 a function which maps a tuple of 25 integers into 1 integer with good hashing properties. Does anybody know such a thing?
1
1510
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, Division(mod)? If there is what kind of comparison can I do? How about the collision resolution techniques: linear probing, quadratic probing and separate chaining? This is only a simple research. Is there any other way that I can make it better? How?...
0
7991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8395
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8050
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2412
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.