Boost logo

Boost :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2007-05-22 16:29:37


----- Mensaje original -----
De: Péter Szilágyi <peterke_at_[hidden]>
Fecha: Martes, Mayo 22, 2007 9:39 pm
Asunto: Re: [boost] Unordered Map/Set in VC++/Boost?
Para: boost_at_[hidden]
[...]
> I don't really want to add additional libraries, that aren't
> needed. I'd
> rather solve it with the already available tools.

Boost.MultiIndex provides hashed indices, so you can
emulate an unordered_set with a single-index
multi_index_container. For instance, a TR1-compatible
unordered set of ints would be defined as:

  #include <boost/multi_index_container.hpp>
  #include <boost/multi_index/hashed_index.hpp>
  #include <boost/multi_index/identity.hpp>

  typedef boost::multi_index_container<
    int,
    boost::multi_index::indexed_by<
      boost::multi_index::hashed_unique<
        boost::multi_index::identity<int>
>
>
> int_unordered_set;

An emulation of an unordered map is a little more tricky
and the result won't be TR1-compatible, but might be close
enough for your needs, take for instance an unordered_map
of ints to std::strings:

  #include <boost/multi_index_container.hpp>
  #include <boost/multi_index/hashed_index.hpp>
  #include <boost/multi_index/member.hpp>

  struct istr_pair
  {
    istr_pair(int i,const std::string& str):
      first(i),second(str){}

    int first;
    mutable std::string second;
  };

  typedef boost::multi_index_container<
    istr_pair,
    boost::multi_index::indexed_by<
      boost::multi_index::hashed_unique<
        boost::multi_index::member<
          istr_pair,int,&istr_pair::first
>
>
>
> istr_unordered_map;

(Note that the second member of istr_pair is mutable
so that we can freely modify it in elements of
istr_unordered_map, which are treated as const, that's
why we can use std::pair instead.)

Best,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk