0

I've tried to implement hash table using vector. My table size will be defined in the constructor, for example lets say table size is 31, to create hash table I do followings:

vector<string> entires; // it is filled with entries that I'll put into hash table; 
vector<string> hashtable;
hashtable.resize(31);
for(int i=0;i<entries.size();i++){
    int index=hashFunction(entries[i]);
    // now I need to know whether I've already put an entry into hashtable[index] or not 
}

Is there anyone to help me how could I do that ?

8
  • Is this your real code? I can spot at least 2 mistakes (a missing closing parenthesis and you misspelled entries) Commented Jan 22, 2014 at 18:44
  • @Borgleader nope I've just write some part of it for simplication. sorry for typos Commented Jan 22, 2014 at 18:44
  • @TheGost Check if hashtable[index].empty()? I don't understand how you plan on implementing a hash table with a vector though. What will you do for 2 distinct entries that hash to the same index? Commented Jan 22, 2014 at 18:46
  • linear or quadratic probing ? I did not show that part of the code ,please just help me what I asked @Praetorian Commented Jan 22, 2014 at 18:48
  • Erm, isn't that what the first sentence in my comment is doing? Commented Jan 22, 2014 at 18:50

3 Answers 3

1

Each cell in your hashtable comes with a bit of extra packaging.

If your hash allows deletions you need a state such that a cell can be marked as "deleted". This enables your search to continue looking even if it encounters this cell which has no actual value in it.

So a cell can have 3 states, occupied, empty and deleted.

You might also wish to store the hash-value in the cell. This is useful when you come to resize the table as you don't need to rehash all the entries.

In addition it can be an optimal first-comparison because comparing two numbers is likely to be quicker than comparing two objects.

These are considerations if this is an exercise, or if you find that std::unordered_map / std::unordered_set is not adequate for your purpose or if those are not available to you.

For practical purpose, at least try using those first.

Sign up to request clarification or add additional context in comments.

Comments

0

It is possible to have several items for the same hash value

You just need to define your hash-table like this:

vector<vector<string>> hashtable;
hashtable.resize(32); //0-31

for(int i=0;i<entries.size();i++){
    int index=hashFunction(entries[i]);
    hashtable[index].push_back(entries[i]);
}

2 Comments

no, if there is an entry I'll use linear probing collision resolution strategy so there cannot be more than one entry at the same place
so it is look like you need to use default value as empty value (if empty string is not good for this)
0

the simple implementation of hash table uses vector of pointers to actual entries:

    class hash_map {
    public:
    iterator find(const key_type& key);
    //...
    private:
    struct Entry {  // representation
      key_type key;
      mepped_type val;
      Entry* next;  // hash overflow link
    };

    vector<Entry> v;  // the actual entries
    vector<Entry*> b; // the hash table, pointers into v
    };

to find a value operator uses a hash function to find an index in the hash table for the key:

mapped_type& hash_map::operator[](const key_type& k) {
  size_type i = hash(k)%b.size();  // hash
  for (Entry* p=b[i];p;p=p->next)  // search among entries hashed to i
    if (eq(k,p->key)) {  //  found
      if (p->erased) {  // re-insert
        p->erased=false;
        no_of_erased--;
        return p->val=default_value;
     }
   // not found, resize if needed
   return operator[](k);
   v.push_back(Entry(k,default_value,b[i]));  // add Entry
   b[i]=&v.back();  // point to new element

   return b[i]->val;   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.