473,396 Members | 1,813 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,396 software developers and data experts.

Good Hash Implementation?

Hello all,

I need to use a hash table for quick lookup... What is the best way to
do it?

My current solution uses stl::map, where I essentially do the following
(not compilable code):

class MyObj;
struct MyObjList {
MyObj *object;
MyObjList *next;
}
stl::map<int, MyObjList*> objMap;
objMap[createHash(&Obj)]->obj = &Obj; // Storage
objMap[createHash(lookup_info)]; // lookup

Where createHash generates a non-unique integer, and I properly use the
list structure, to create a list of the objects with the same hash key.

While this method has _drastically_ reduced my lookup time, from a
standard scan (Comparison is expensive on this object), it feels clunky,
and there must be a better way to do this, aside from writing it
completely from scratch (STL MAP is probably better than I can do from
scratch, without a lot of work, I would immagine).

Any general ideas?

Thanks,
Brian

Jul 22 '05 #1
2 1638
In article <3f********@10.10.0.241>, Brian Genisio wrote:
Hello all,

I need to use a hash table for quick lookup... What is the best way to
do it?

My current solution uses stl::map, where I essentially do the following
(not compilable code):

class MyObj;
struct MyObjList {
MyObj *object;
MyObjList *next;
}
stl::map<int, MyObjList*> objMap;
objMap[createHash(&Obj)]->obj = &Obj; // Storage
objMap[createHash(lookup_info)]; // lookup

Where createHash generates a non-unique integer, and I properly use the
list structure, to create a list of the objects with the same hash key.

While this method has _drastically_ reduced my lookup time, from a
standard scan (Comparison is expensive on this object), it feels clunky,
and there must be a better way to do this, aside from writing it
completely from scratch (STL MAP is probably better than I can do from
scratch, without a lot of work, I would immagine).

Any general ideas?


The only thing you've gained from this hoop-jumping exercise is that your
table lookups now only involve integer comparisons. But you could also
achieve the same result like this:

#include <set>

struct MyObj {};

int createHash(MyObj* );

struct MyObjAndInt {
MyObj *object;
int hashcode;
MyObjAndInt(MyObj* x) : object(x), hashcode(createHash(x)) {}
};

struct cmpObjAndInt {
bool operator() (const MyObjAndInt& left, const MyObjAndInt& right)
{
return left.hashcode < right.hashcode;
}
};
std::multiset< MyObjAndInt, cmpObjAndInt > s;

If you want a real hash, there are several available that ship with various
versions of STL.

One way to implement a *real* hashtable from standard containers that is
quite promising (IMO) is that you store all the nodes in a linked list, and
then for the hashtable entries, you use list iterators. Among other things,
this gives you (unordered) bidirectional iteration.

sketch of this:

template <typename T, typename HashFunction>
class HashTable {
std::list<T> thelist;
// It might help to have a way to initialise entries of v to a "NULL" value,
// but I don't see a clean way to do this
std::vector< list<T>::iterator > v;
HashFunction f;

// operations are for the most part a combination of vector and list member functions
Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #2
"Brian Genisio" <Br**********@yahoo.com> wrote in message
news:3f********@10.10.0.241
Hello all,

I need to use a hash table for quick lookup... What is the best way to
do it?

My current solution uses stl::map, where I essentially do the
following (not compilable code):

class MyObj;
struct MyObjList {
MyObj *object;
MyObjList *next;
}
stl::map<int, MyObjList*> objMap;
objMap[createHash(&Obj)]->obj = &Obj; // Storage
objMap[createHash(lookup_info)]; // lookup

Where createHash generates a non-unique integer, and I properly use
the list structure, to create a list of the objects with the same
hash key.

While this method has _drastically_ reduced my lookup time, from a
standard scan (Comparison is expensive on this object), it feels
clunky, and there must be a better way to do this, aside from writing
it completely from scratch (STL MAP is probably better than I can do
from scratch, without a lot of work, I would immagine).

Any general ideas?

Thanks,
Brian

Like Donovan says, a lot of implementations of the C++ standard library
supply one as an extension. In particular, the implementation from
Dinkumware that ships with VC++ has hash table implementations.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3

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

Similar topics

31
by: Alexis Nikichine | last post by:
Hello, It is common knowledge that arrays can be used as hashtables: var color = ; color = 0xFF0000; color = 0x0000FF;
8
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...
4
by: James Kanze | last post by:
On Jul 16, 10:53 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote: It depends. You might like to have a look at my "Hashing.hh" header (in the code at kanze.james.neuf.fr/code-en.html---the...
1
by: Mirco Wahab | last post by:
Alf P. Steinbach wrote: No, there isn't any (afaik). You can look it up here: http://www.boost.org/doc/libs/1_35_0/doc/html/boost_tr1/unsupported.html#boost_tr1.unsupported.unordered_map ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.