473,594 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hash Table Implementation in C++

Hi everybody. Does anyone have any sample hash table implementation for
separate chaining? I'm trying to implement it myself, but I'm stuck.
Thanks!

Apr 3 '06 #1
7 62277
Diane wrote:
Hi everybody. Does anyone have any sample hash table implementation for
separate chaining? I'm trying to implement it myself, but I'm stuck.
Thanks!


Why don't you show us what you currently have?
Apr 4 '06 #2
Please find below my code. I need an array of lists which are of type
Info. My problem is that I;m not quite sure if I should use an pointer
to a list ( see private member variable of class HashTable) or not. And
if i must use pointers, how should i modify my code? Thanks!

#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;

template <class Type>
struct Info
{
Info(const string& k, const Type& myData)
{
key = k;
data = myData;
occupied=false;
hasBeenOccupied = false;
}

const string& getKey() const
{ return key; }

// Overloaded operators
bool operator==(cons t Info& rhs) const
{ return getKey() == rhs.getKey(); }
bool operator!=(cons t Info& rhs) const
{ return !(*this == rhs); }

string key;
Type data;
bool occupied;
bool hasBeenOccupied ;
};

template <class Info>
class HashTable
{
public:
// constructor and destructor
HashTable(int currentSize);
~HashTable();
// other functions
bool put(const Info& x); // enter data into table
void remove(const Info& x); // delete from table
bool isIn(const Info& x) const; // is key in table
bool isEmpty() const; // is table empty

int getTableSize() { return tableSize; }

// hash functions
int myhash(const Info& x, int tableSize) const;

private:
vector<std::lis t <Info> > myLists; // the array of lists
int tableSize;
int numberInTable; // number of items in table
};

// Class Implementation

template <class Info>
HashTable<Info> ::HashTable(int currentSize)
{
numberInTable = 0;
tableSize = currentSize;

// myLists = new vector<std::lis t<Info> > [currentSize];

for (int i = 0; i < tableSize; i++)
{
myLists[i].clear();
}
}

template <class Info>
HashTable<Info> ::~HashTable()
{}

// global main hash function
int hash(const string& key, int tableSize)
{
int index = 0;

for (int j = 0; j < key.length(); j++)
{
index += key[j];
}

return index % tableSize;
}

// my hash function takes an Info object as argument
template <class Info>
int HashTable<Info> ::myhash(const Info& x, int tableSize) const
{
int index = hash(x.key, tableSize);

index %= myLists.size();

if (index < 0)
{
index += myLists.size();
}

return index;
}
// insert data into hash table
template <class Info>
bool HashTable<Info> ::put(const Info& x)
{
int index = myhash(x, tableSize);

list<Info> & thelist = myLists[index];
if( find(thelist.be gin(), thelist.end(), x) != thelist.end() )
{
return false;
}

thelist.push_ba ck(x);
return true;

}

Apr 4 '06 #3
Why don't you use STL's hash_set or hash_map?

Regarding your question, if you use pointers to the list you'll save
few bytes, but you'll have to implement your own copy constructor,
assignment operator and destructor of the HashTable.
BTW, you don't need to "clear" the lists in the constructor, the lists
will start clean.

Yuval.

Apr 4 '06 #4
yu*****@gmail.c om wrote :
Why don't you use STL's hash_set or hash_map?


Those are nonèstandard SGI extensions.

The standard one is unordered_map, but it is in TR1.
It's already available in namespace std::tr1 of some compilers.
Apr 4 '06 #5

"loufoque" <lo******@remov e.gmail.com> wrote in message
news:44******** **************@ news.free.fr...
yu*****@gmail.c om wrote :
Why don't you use STL's hash_set or hash_map?


Those are nonhstandard SGI extensions.


(What's SGI? Silicon Graphics, Inc.? Standard Graphical Interface?)

-Howard
Apr 4 '06 #6
>> yu*****@gmail.c om wrote :
Why don't you use STL's hash_set or hash_map?
"loufoque" <lo******@remov e.gmail.com> wrote in message
news:44******** **************@ news.free.fr...
Those are nonhstandard SGI extensions.

Howard <al*****@hotmai l.com> wrote: (What's SGI? Silicon Graphics, Inc.? Standard Graphical Interface?)


SGI is Silicon Graphics, Inc.

http://www.sgi.com/tech/stl/

--
Marcus Kwok
Apr 4 '06 #7
On 3 Apr 2006 16:06:56 -0700, "Diane" <fr*********@ho tmail.com> wrote:
Hi everybody. Does anyone have any sample hash table implementation for
separate chaining? I'm trying to implement it myself, but I'm stuck.
Thanks!


Try http://www.koders.com/

Good luck!
Roland Pibinger
Apr 4 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4235
by: Pelo GANDO | last post by:
Hi everybody ! I am a beginner in C++. I am looking for a (simple if it's possible) source code in C++ about hash table... Thank you ! Pelo
7
22325
by: Matthias Käppler | last post by:
Hi, I need to store objects of a FileInfo class containing information about a specific file. I can uniquely identify these files by their serial number. I thought it would be a good idea to use a hash map so I can access the file information in constant time, without having to iterate over a (possibly very large) list of files. As far as I know, std::map does not hash its entries (I guess it takes O(nlogn) time to find an entry)....
31
4502
by: Alexis Nikichine | last post by:
Hello, It is common knowledge that arrays can be used as hashtables: var color = ; color = 0xFF0000; color = 0x0000FF;
5
2012
by: R. Rajesh Jeba Anbiah | last post by:
I could see that it is possible to have hash array using objects like var hash = {"a" : "1", "b" : "2"}; Couldn't still findout how to declare hash array in Array. var arr = new Array("a" : "1", "b" : "2"); doesn't work. Any hints? TIA -- <?php echo 'Just another PHP saint'; ?> Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
2
2516
by: Ravi | last post by:
Hi, I am working on a winform app. I need to use an object which can store some information(key/value pairs) and also can be acessed by multiple threads(read/write). From what I heard Hash table is best suited for it. My question is Why hash table. why can't we use an array. I thought hash table uses more memory than array. Correct me if am wrong.
21
3189
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
11
2669
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
139
14099
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
8
2765
by: Jim Cobban | last post by:
I am writing a program in which I need a hash table implementation of a Map. The version of g++ available for Windo$e does not yet include the TR1 support for this. It just has the original SGI implementation. However the SGI implementation is so old that it predates the STL, so it does not, among other things, include hash support for std::basic_string. So I tried implementing the hash map from Stroustrup 3rd edition. At the least I...
0
7946
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
7877
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8374
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
8009
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,...
0
6661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
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
3867
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1482
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1216
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.