473,327 Members | 2,055 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,327 software developers and data experts.

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==(const myHashKey& key) const = 0 ;
virtual size_t Size() const = 0 ;
};
class IHashable
{
virtual const myHashKey* GetKey() const = 0 ;
virtual bool IsKeyEqual(const 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 1235
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_key, val));
}

ValueType& get(const IHashable& key)
{
size_t h_key = key.getHash();
std::list<std::pair<size_t, ValueType::iterator 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
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>...
10
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. #...
3
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...
4
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
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...
3
by: Pierre Barbier de Reuille | last post by:
Hi, after reading the article " The Standard Librarian : Defining Iterators and Const Iterators" from Matt Austern:...
7
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...
0
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...
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.