how to have user defined hash for unordered_map ?

A

abir

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
 
H

huili80

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).
 
J

James Kanze

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.
 
A

abir

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.
Do you mean this?  If so, the simplest way of ensuring it is to
make w_ static.
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.
--
James Kanze (GABI Software)             email:[email protected]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top