473,782 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Yet another template class question (corrected)

I am writing a template HashTable class. I have got it working, but I
want to make the code more generic and more robust. The current code
looks something like this:

template <class InnerType, class KeyType, class KeyToSizeT>
class myHash
{
// Impl here
};

Where:

InnerType is the data type being stored
KeyType is the data type of the hash key
KeyToSize is the function that returns the size of the hash key
I am now mandating that InnerType MUST implement interface IHashable:
class myHashKey
{
virtual bool operator==(cons t myHashKey& key) const = 0 ;
virtual size_t Size() const = 0 ;
};
class IHashable
{
virtual const myHashKey* GetKey() const = 0 ;
virtual bool IsKeyEqual(cons t myHashKey* key) const = 0;
virtual size_t KeySize() const = 0 ;
};

With this new policy, I want to be able to simplify my HashTable class to:
template <class InnerType>
class myHash
{
//Ho do I reference KeyType and KeyToSizeT now ?
// Impl here
};

Aug 31 '07 #1
1 1252
On 2007-08-31 15:05, Anonymous wrote:
I am writing a template HashTable class. I have got it working, but I
want to make the code more generic and more robust. The current code
looks something like this:

template <class InnerType, class KeyType, class KeyToSizeT>
class myHash
{
// Impl here
};

Where:

InnerType is the data type being stored
I'd call it ValueType
KeyType is the data type of the hash key
KeyToSize is the function that returns the size of the hash key
I am now mandating that InnerType MUST implement interface IHashable:
I think you've confused things here, it is the KeyType you should hash,
not the value. Unless you plan to first hash the key, and then hash the
value, but that seems to me like either you have a bad hash-function for
the key or a multi-hash-map (i.e. the keys don't have to be unique).

I'm not an expert but I'd do something like this:
template <typename ValueType, size_t TableSize = 100>
class HashTable
{
std::list<std:: pair<size_t, ValueType table[TableSize];

public:
void add(const IHashable& key, const ValueType& val)
{
size_t h_key = key.getHash();
table[h_key % TableSize].push_back(std: :make_pair(h_ke y, val));
}

ValueType& get(const IHashable& key)
{
size_t h_key = key.getHash();
std::list<std:: pair<size_t, ValueType::iter ator it;

for (it = table[h_key % TableSize].begin();
it != table[h_key % TableSize].end();
++it)
{
if (it->first == h_key)
return it->second;
}
}
};

This assumes that your hash-function always produces unique hashes for
unique IHashables, another method can be used to get the correct one,
such as storing a pointer copy of the IHashable (to prevent slicing).

--
Erik Wikström
Aug 31 '07 #2

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

Similar topics

5
2640
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object1, const MyTemplate <T> & object2); template <typename T> class MyTemplate
10
2549
by: ma740988 | last post by:
Part of my confusion here is certainly my ignorance of templates and std::auto_ptr. Two topics, I've perused but need to really _delve_ into. In any event, consider the 'test' source. # include <iostream> # include <memory> class CallbackBase { public: virtual void operator()() const { };
3
2947
by: Microsoft | last post by:
Is there a way of implementing something like the Template Method design pattern, but only dealing with static methods ? First, I realized that I can't have abstract static methods in the base class to be implemented in the derived classes. Second, I have tried to hide those methods of the base class, with the 'new' modifier, bub it doesn´t work. I would appreciate your help ver much. Thank you Juan M. Cervera
4
7239
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
3
2401
by: chriscorbell | last post by:
I'm curious about what appears to be a restriction on using an STL container inside a user-defined template, esp. using an iterator to such a container. It's not clear to me if this is a general template/language restriction, and STL iterator limitation, or if I'm just going about it wrong. I'm declaring a template which uses a std::map to store references to the template type, e.g. template template <typename T>
3
1485
by: Pierre Barbier de Reuille | last post by:
Hi, after reading the article " The Standard Librarian : Defining Iterators and Const Iterators" from Matt Austern: http://www.ddj.com/showArticle.jhtml;jsessionid=41JODBZDWEMBYQSNDLOSKH0CJUNN2JVN?articleID=184401331 I wanted to test using the non-template function friends of template classes. So I devised this code: ===8<======8<======8<======8<======8<======8<======8<===
7
2100
by: AlanJSmith | last post by:
I am converting some C++ projects from 6 to vs2005 in one of the headers i have the code that follows this passage in which I get an error saying int default cant be assumed so i added DBString<nSizeDBString<nSize>:: to the copy assignment same size oporator and this seems to work, if i try the same thing with copy assignment different sizes i get error C3856: 'DBString<nSize>::=': class is not a class template and adding any type between...
0
1198
by: Anonymous | last post by:
I have been trying to write a template class for a shared memory container, over the last few days - but I keep getting numerous compiler errors e.g: Error: syntax error : identifier 'myStaticShmemAllocator<void>' Error: 'AllocationPolicy' : undeclared identifier Here is the truncated code below -
1
2228
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
1
2378
by: Ed | last post by:
Hi, guys, I declare a template method in one template class in one library. Compiling is OK, but link is not OK. Header File is: <code> template <typename P = float> class TESTLIB_API Linear { public:
0
9639
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
10146
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
10080
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
9942
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
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...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.