473,396 Members | 1,836 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.

how to have user defined hash for unordered_map ?

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

My classes are,
template<typename T>
struct work{
int count;
work(int count) : count(count){}
};
template<typename W>
class worker{
public:
typedef worker<Wself_type;
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<WORKWORKER;
WORK w(2);
WORKER w1(w,1);
WORKER w2(w,2);
WORKER w3(w,3);
unordered_map<WORKER,intm;
m.insert(std::make_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 10048
On 2008-07-11 01:05:57 -0400, abir <ab*******@gmail.comsaid:
Hi,
I want a user defined key for tr1 unordered_map.

My classes are,
template<typename T>
struct work{
int count;
work(int count) : count(count){}
};
template<typename W>
class worker{
public:
typedef worker<Wself_type;
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<WORKWORKER;
WORK w(2);
WORKER w1(w,1);
WORKER w2(w,2);
WORKER w3(w,3);
unordered_map<WORKER,intm;
m.insert(std::make_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<Key>,
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...@gmail.comwrote:
I want a user defined key for tr1 unordered_map.
Pete Becker has given the reply to your question, but...
template<typename W>
class worker{
public:
typedef worker<Wself_type;
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 objektorientierter Datenverarbeitung
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...@gmail.comwrote:
On Jul 11, 7:05 am, abir <abirba...@gmail.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<WORKER,int,boost::hash<WORKER>m;
but for boost, this even works.
unordered_map<WORKER,intm;
with,
friend std::size_t hash_value(const 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<typename W>
class worker{
public:
* * typedef worker<Wself_type;
* * 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 objektorientierter Datenverarbeitung
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*******@gmail.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**@versatilecoding.comsaid:
On 2008-07-11 14:48:47 -0400, abir <ab*******@gmail.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
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...
7
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...
26
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
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
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
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;...
7
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...
6
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...
2
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...
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: 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?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.