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

Are there any problems with this?

Hello all,

I have implemented a very simple generic hash table library. For those that
are so inclined, I would like to request a critical review and comments. Is
anything wrong? Could anything be done better?

Thanks!
Dave
// File hash_table.h

#ifndef HASH_TABLE_INCLUDED
#define HASH_TABLE_INCLUDED

#include <list>
#include <vector>

namespace dct
{
// HASH_FUNC is a policy class which must have the following public
method:
// int hash(const ELEMENT_TYPE &element) const;
//
// This method must return an integer hash value for element.

template <typename ELEMENT_TYPE, int BUCKETS, typename HASH_FUNC>
class hash_table_t: public HASH_FUNC
{
public:
hash_table_t(): buckets(BUCKETS) {}

void clear();
void delete_element(const ELEMENT_TYPE &element);
bool element_exists(const ELEMENT_TYPE &element) const;
void insert_element(const ELEMENT_TYPE &element);

private:
typedef std::list<ELEMENT_TYPE> bucket_t;
typedef std::vector<bucket_t> buckets_t;

buckets_t buckets;
};
}

#include "hash_table.inl"

#endif

// File hash_table.inl

#include <algorithm>

namespace dct
{
//
************************************************** ************************
template <typename ELEMENT_TYPE, int BUCKETS, typename HASH_FUNC>
void hash_table_t<
ELEMENT_TYPE,
BUCKETS,
HASH_FUNC
::clear() {
for (typename buckets_t::size_type i = 0; i != buckets.size(); ++i)
buckets[i].clear();
} // hash_table_t::clear

//
************************************************** ************************
template <typename ELEMENT_TYPE, int BUCKETS, typename HASH_FUNC>
void hash_table_t<
ELEMENT_TYPE,
BUCKETS,
HASH_FUNC::delete_element(const ELEMENT_TYPE &element) {
typename buckets_t::size_type
bucket_idx(static_cast<typename buckets_t::size_type>(
HASH_FUNC::hash(element) %
BUCKETS)
);

bucket_t &bucket(buckets[bucket_idx]);

bucket.remove(element);
} // hash_table_t::delete_element

//
************************************************** ************************
template <typename ELEMENT_TYPE, int BUCKETS, typename HASH_FUNC>
bool hash_table_t<
ELEMENT_TYPE,
BUCKETS,
HASH_FUNC::element_exists(const ELEMENT_TYPE &element) const {
typename buckets_t::size_type
bucket_idx(static_cast<typename buckets_t::size_type>(
HASH_FUNC::hash(element) %
BUCKETS)
);

const bucket_t &bucket(buckets[bucket_idx]);

return find(bucket.begin(), bucket.end(), element) != bucket.end();
} // hash_table_t::element_exists

//
************************************************** ************************
template <typename ELEMENT_TYPE, int BUCKETS, typename HASH_FUNC>
void hash_table_t<
ELEMENT_TYPE,
BUCKETS,
HASH_FUNC::insert_element(const ELEMENT_TYPE &element)

{
typename buckets_t::size_type
bucket_idx(static_cast<typename buckets_t::size_type>(
HASH_FUNC::hash(element) %
BUCKETS)
);

bucket_t &bucket(buckets[bucket_idx]);

// Consciously allow an element to be inserted multiple times. This
is
// for the sake of efficiency. This operation becomes O(1) and,
assuming
// that recently inserted elements are referenced the most, lookups
will
// be quicker too since recent elements are close to the front of
their
// bucket.
bucket.push_front(element);
} // hash_table_t::insert_element
}
Jul 22 '05 #1
1 1324

"Dave" <be***********@yahoo.com> wrote in message
news:10*************@news.supernews.com...
Hello all,

I have implemented a very simple generic hash table library. For those that are so inclined, I would like to request a critical review and comments. Is anything wrong? Could anything be done better?

Thanks!
Dave


<code snipped>

How about adding an Iterator to it? I have made my own hash table recently
and found it useful to add the iterator. Also, I know this wouldnt be
portable, but remember to keep the hashtable threadsafe.
Allan
Jul 22 '05 #2

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

Similar topics

0
by: Andrew | last post by:
Hi guys, Has anyone else had trouble with the latest releases of PHP in IIS (using the ISAPI module)? I updated our copies of PHP recently and very soon IIS became very unstable with the...
0
by: Jerome Lefebvre | last post by:
Hello, Hope this will interest a few. I been working with a friend on the problems given out during the "International Collegiate Programming Contest" (ICPC) http://icpc.baylor.edu/icpc/ ....
11
by: Dan Rubin | last post by:
HI everyone, lurking for a long time here, since I can usually solve my own problems, but here is one I'm stumped by. I've got a valid XHTML 1.0 Transitional layout, and all the CSS is valid as...
14
by: Jim Hubbard | last post by:
Are you up to speed on the difficulties in using the 1.1 .Net framework? Not if you are unaware of the 1,596 issues listed at KBAlertz (http://www.kbalertz.com/technology_3.aspx). If you are...
1
by: 3f | last post by:
Hello; We have made a web application that people can download from our web site and installed on: Windows XP Windows 2000 Professional Windows 2003 Server Windows 2000 Server
1
by: manish | last post by:
Hi, I am a fresher in the programming field i.e although I have done programming at the basic level but at professional level I am very new and I am facing many problems. These probllems are...
5
by: Corky | last post by:
This works: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS INNER JOIN PROBLEMS ON PROBLEM_OBJECTS.PROBLEM_ID = PROBLEMS.PROBLEM_ID WHERE INTEGER(DAYS(CURRENT DATE) -...
2
by: Ellen Graves | last post by:
I am having a lot of problems with DB2 8.3.1 on RH Linux AS2.1. Installing and running stored procedures is problematic. Stored procedures I have used for years on V7 on WinNT are now failing...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
0
by: Sergistm | last post by:
Hello World, :D I have a problem that it is making me crazy, I hope you can help me. I'm trying to execute a .exe file with the Procces.Start, and there is no problem when the file is on my...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...

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.