Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to have user defined hash for unordered_map ?

Reply
Thread Tools

how to have user defined hash for unordered_map ?

 
 
abir
Guest
Posts: n/a
 
      07-11-2008
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<W> self_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<int> WORK;
typedef worker<WORK> WORKER;
WORK w(2);
WORKER w1(w,1);
WORKER w2(w,2);
WORKER w3(w,3);
unordered_map<WORKER,int> m;
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
 
Reply With Quote
 
 
 
 
huili80@gmail.com
Guest
Posts: n/a
 
      07-11-2008
On Jul 11, 1:05*am, abir <(E-Mail Removed)> wrote:
> 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<W> self_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<int> WORK;
> typedef worker<WORK> WORKER;
> WORK w(2);
> WORKER w1(w,1);
> WORKER w2(w,2);
> WORKER w3(w,3);
> unordered_map<WORKER,int> m;
> 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


template class unordered_map has more than two template parameters. I
believe the 3rd and 4th would be for hash function and for equalty
compare (not necessarily in that order).
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      07-11-2008
On Jul 11, 7:05 am, abir <(E-Mail Removed)> wrote:
> 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<W> self_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:(E-Mail Removed)
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
 
Reply With Quote
 
abir
Guest
Posts: n/a
 
      07-11-2008
On Jul 11, 8:17*pm, James Kanze <(E-Mail Removed)> wrote:
> On Jul 11, 7:05 am, abir <(E-Mail Removed)> wrote:
>
> > * 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,int> m;
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<W> self_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:(E-Mail Removed)
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Template problem with unordered_map Paulo Matos C++ 4 08-03-2006 05:10 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
Searching an example for a defined hash value of a nonexisting hash key Ralf Baerwaldt Perl Misc 1 07-20-2004 03:05 PM



Advertisments