how to have user defined hash for unordered_map ?

Discussion in 'C++' started by abir, Jul 11, 2008.

  1. abir

    abir Guest

    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
     
    abir, Jul 11, 2008
    #1
    1. Advertising

  2. abir

    Guest

    On Jul 11, 1:05 am, abir <> 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).
     
    , Jul 11, 2008
    #2
    1. Advertising

  3. abir

    James Kanze Guest

    On Jul 11, 7:05 am, abir <> 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:
    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
     
    James Kanze, Jul 11, 2008
    #3
  4. abir

    abir Guest

    On Jul 11, 8:17 pm, James Kanze <> wrote:
    > On Jul 11, 7:05 am, abir <> 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:
    > 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
     
    abir, Jul 11, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  2. Paulo Matos

    Template problem with unordered_map

    Paulo Matos, Aug 3, 2006, in forum: C++
    Replies:
    4
    Views:
    456
    Paulo Matos
    Aug 3, 2006
  3. Rares Vernica

    error with tr1 unordered_map iterator

    Rares Vernica, Feb 24, 2007, in forum: C++
    Replies:
    6
    Views:
    1,808
  4. abir
    Replies:
    6
    Views:
    863
    W Karas
    Jun 26, 2008
  5. rp
    Replies:
    1
    Views:
    539
    red floyd
    Nov 10, 2011
Loading...

Share This Page