473,586 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to have user defined hash for unordered_map ?

Hi,
I want a user defined key for tr1 unordered_map.

My classes are,
template<typena me T>
struct work{
int count;
work(int count) : count(count){}
};
template<typena me W>
class worker{
public:
typedef worker<Wself_ty pe;
worker(W& w,int pos) : w_(&w),pos_(pos ){}
bool operator== (const self_type& other) const {
assert(w_ == other.w_);
return pos_ == other.pos_;
}
private:
W* w_;
int pos_;
friend std::size_t hash(const self_type& self){
return self.pos_ + w_->count;
}
};

and want to have worker class as key to map.
so calling is,
typedef work<intWORK;
typedef worker<WORKWORK ER;
WORK w(2);
WORKER w1(w,1);
WORKER w2(w,2);
WORKER w3(w,3);
unordered_map<W ORKER,intm;
m.insert(std::m ake_pair(w1,5)) ;

i have == op for worker.and either declaring a hash_value or hash
function is not working.
how can i do it?

thanks
abir
Jul 11 '08 #1
5 10076
On 2008-07-11 01:05:57 -0400, abir <ab*******@gmai l.comsaid:
Hi,
I want a user defined key for tr1 unordered_map.

My classes are,
template<typena me T>
struct work{
int count;
work(int count) : count(count){}
};
template<typena me W>
class worker{
public:
typedef worker<Wself_ty pe;
worker(W& w,int pos) : w_(&w),pos_(pos ){}
bool operator== (const self_type& other) const {
assert(w_ == other.w_);
return pos_ == other.pos_;
}
private:
W* w_;
int pos_;
friend std::size_t hash(const self_type& self){
return self.pos_ + w_->count;
}
};

and want to have worker class as key to map.
so calling is,
typedef work<intWORK;
typedef worker<WORKWORK ER;
WORK w(2);
WORKER w1(w,1);
WORKER w2(w,2);
WORKER w3(w,3);
unordered_map<W ORKER,intm;
m.insert(std::m ake_pair(w1,5)) ;

i have == op for worker.and either declaring a hash_value or hash
function is not working.
how can i do it?
unordered_map takes five template arguments, three of which have defaults:

template <class Key, class T,
class Hash = hash<Key>, class Pred = std::equal_to<K ey>,
class Alloc = std::allocator< std::pair<const Key, T>>>
class unordered_map;

To provide your own hash object, use its type as the third argument. To
provide your own equality predicate, use its type as the fourth
argument. To provide your own allocator, use its type as the fifth
argument.

With just those changes, you'll get default-constructed versions of
your hash type, equality type, and allocator type. If that's not
appropriate, unordered_map has constructors that take objects of those
types.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 11 '08 #2
On Jul 11, 7:05 am, abir <abirba...@gmai l.comwrote:
I want a user defined key for tr1 unordered_map.
Pete Becker has given the reply to your question, but...
template<typena me W>
class worker{
public:
typedef worker<Wself_ty pe;
worker(W& w,int pos) : w_(&w),pos_(pos ){}
bool operator== (const self_type& other) const {
assert(w_ == other.w_);
Do you mean this? If so, the simplest way of ensuring it is to
make w_ static.
return pos_ == other.pos_;
}
private:
W* w_;
int pos_;
friend std::size_t hash(const self_type& self){
return self.pos_ + w_->count;
}
};
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 11 '08 #3
On Jul 11, 8:17*pm, James Kanze <james.ka...@gm ail.comwrote:
On Jul 11, 7:05 am, abir <abirba...@gmai l.comwrote:
* I want a user defined key for tr1 unordered_map.

Pete Becker has given the reply to your question, but...
Previously i used unordered_map from boost, which automatically takes
hash_value friend to compute hash, so i thought it is in the tr1
standard.
Now this works for tr1,
unordered_map<W ORKER,int,boost ::hash<WORKER>m ;
but for boost, this even works.
unordered_map<W ORKER,intm;
with,
friend std::size_t hash_value(cons t self_type& self){
return self.pos() + self.w().count;
}
I am still not sure why i cant just have a overloaded hash or some
other function, and unordered_map automatically finds it like boost.
Why i have to specify hash function for my class while i don't need
for int, float ot string!
There was some problem of const correctness in previous post, actually
pos() & w() have to be const member function.
>
template<typena me W>
class worker{
public:
* * typedef worker<Wself_ty pe;
* * worker(W& w,int pos) : w_(&w),pos_(pos ){}
* * bool operator== (const self_type& other) const {
* * * * assert(w_ == other.w_);

Do you mean this? *If so, the simplest way of ensuring it is to
make w_ static.
* * * * return pos_ == other.pos_;
* * }
Yes i mean this.
w_ can't be static, as two worker can do different work.
Two worker doing different work are not comparable and so are not
allowed to form hash key for same container.
private:
* * W* w_;
* * int pos_;
* * friend std::size_t hash(const self_type& self){
* * * * return self.pos_ + w_->count;
* * }
};

--
James Kanze (GABI Software) * * * * * * email:james.ka. ..@gmail.com
Conseils en informatique orientée objet/
* * * * * * * * * *Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Thanks
abir
Jul 11 '08 #4
On 2008-07-11 14:48:47 -0400, abir <ab*******@gmai l.comsaid:
Why i have to specify hash function for my class while i don't need
for int, float ot string!
Because the tr1 library (as well as the C++0x library) provides hash
objects for int, float, and string. It doesn't know anything about your
type, so can't provide a hash object.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 11 '08 #5
On 2008-07-11 16:42:11 -0400, Pete Becker <pe**@versatile coding.comsaid:
On 2008-07-11 14:48:47 -0400, abir <ab*******@gmai l.comsaid:
>Why i have to specify hash function for my class while i don't need
for int, float ot string!

Because the tr1 library (as well as the C++0x library) provides hash
objects for int, float, and string. It doesn't know anything about your
type, so can't provide a hash object.
Whoops, spoke too quickly. You can put your hash<MyClassin namespace
std, and the unordered_map template will use it without you having to
say so.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 11 '08 #6

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

Similar topics

2
5612
by: Liang | last post by:
Hi, I use "defined $r_libs->{$name}" to check first if a key exists in a hash table. But Perl gives a warning WHENEVER the key exists: "Use of uninitialized value". Would u please help to check the script, and let me know the reason? Thanks in advance. Liang
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...
26
9597
by: JGH | last post by:
How can I check if a key is defined in an associative array? var users = new array(); users = "Joe Blow"; users = "John Doe"; users = "Jane Doe"; function isUser (userID) { if (?????)
7
62273
by: Diane | last post by:
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!
4
4899
by: yuyang08 | last post by:
Hello, everyone, I am wondering what is the hash function that is used in hash_map/hash_set. Can I replace it with my own hash function? Any comments on this? Thanks! -Andy
4
1723
by: filox | last post by:
so, i'm using the hash_map that comes with MS visual studio, and it seems to spend an awful lot of memory. what i basiclly have is something like this: hash_map<char*, inta; char* b = new char; int i; //some code that initializes b and i a = i; i do that for a huge amount of data and end up with 470 MB of memory for the table. now, if i...
7
4264
by: monomaniac21 | last post by:
hi i have a php site which allows users to save a cookie on their computer which stores their user id details and allows them to auto- login. i'm wondering whether this is safe, is it possible for a malicious user to find that cookie and change its value and therefore auto-login as someone else? and if so how can this be prevented?
6
8591
by: Rares Vernica | last post by:
Hi, I am using tr1/unordered_map in my code. My code compiled ok on g++ (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu1). Now I need to compile my code on g++ (GCC) 4.0.3 (Ubuntu 4.0.3-1ubuntu5). I get a messy error. Here are what I try to do: typedef unordered_map<string, unsignedStringHash;
2
8122
by: marek.vondrak | last post by:
Hi, I am wondering if there are any functional differences between SGI's hash_map and tr1's unordered_map. Can these two containers be interchanged? What would it take to switch from hash_map to unordered_map? Thank you. -Marek
0
7908
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...
0
8336
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...
1
7950
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...
0
8212
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...
1
5710
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...
0
5389
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...
0
3835
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...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
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

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.