Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

insert() function of a HashTable.......

Posted on 2003-12-06
1
Medium Priority
?
298 Views
Last Modified: 2010-04-02
To C++ Experts,
 
     I am still struggling to understand the logic of a hashtable. This is a continued question of :
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20817844.html

The implementation of the insert() function looks like :
---------------------------------------------------------------------
template <class K, class T, class H>
bool HashTable<K,T,H>::insert(K key, T val)
{   const K ZERO_K = K() ;
    int k0 = hash(key), k = k0 ;  // line01 .... Q1
    while ( _[k].first != key && _[k].first != ZERO_K)
      k=(k+1)%_.size() ;
    if (_[k].first == key) return false ;
    if (size()+1 < LOAD*_.size()) _[k] = Pair(key,val) ;
    else
    {  rebuild() ;
       insert(key,val) ;
    }
    return true ;
}
------------------------------------------------------
 So here is my question :
Q1. in line01: k0 and k could be values larger than 1, and "_" is a vector of pair in the HashTable. Then it seems to me that : the vector "_" does not start from 0 ? (I mean _[0], v[1] ... could be empty). It starts at certain k dependes on the pair we inserted and the first several elements of "_" could be empty ???

Q2. if the above statement is true, it troubles me a lot ! If the first several elements of "_" is empty, what does _.size() mean ? Then how do we know the later implementation such as "size()+1 < LOAD*_.size()" is correct ?

Thanks very much !!!

meow.
0
Comment
Question by:meow00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
bcladd earned 600 total points
ID: 9889461
(1) You don't show the hash() function but it returns a number modulo the table size (_.size()). Modular arithmetic works with values on the range 0 - (_.size()-1), exactly the range of indices in a C++ array (or vector).
The point of a hash function is to "randomize" the key values to avoid clustering. So, if there is one item in your hash there is no guarantee that it is in _[0]. Your hash table is using open address hashing where if two keys do hash to the same location (which is bound to happen by the pigeon hole principle), the collision is resolved by walking the table the the +1 direction (modular arithmetic again) until an opening is found. Notice that the load factor makes sure that there is at least one free slot so the loop must terminate

(2) A hash table's size is the number of SLOTs in the hash table. So _.size() is initialized (remembering from another of your posts) to 109? That means there is room for 109 pairs and when there are too many in the table rebuild is called to expand _ (and _.size()). You would keep track of the size() of the hash table (the number of entries inserted into it) and _.size() separately (as your implementation seems to do).

Hope this helps, -bcl
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question