Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Chapter 29. Boost.Unordered

Daniel James

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

Table of Contents

Introduction
The Data Structure
Equality Predicates and Hash Functions
Comparison with Associative Containers
Implementation Rationale
Change Log
Reference
Header <boost/unordered_set.hpp>
Header <boost/unordered_map.hpp>
Bibliography

For accessing data based on key lookup, the C++ standard library offers std::set, std::map, std::multiset and std::multimap. These are generally implemented using balanced binary trees so that lookup time has logarithmic complexity. That is generally okay, but in many cases a hash table can perform better, as accessing data has constant complexity, on average. The worst case complexity is linear, but that occurs rarely and with some care, can be avoided.

Also, the existing containers require a 'less than' comparison object to order their elements. For some data types this is impossible to implement or isn't practical. In contrast, a hash table only needs an equality function and a hash function for the key.

With this in mind, the C++ Standard Library Technical Report introduced the unordered associative containers, which are implemented using hash tables, and they have now been added to the Working Draft of the C++ Standard.

This library supplies an almost complete implementation of the specification in the Working Draft of the C++ Standard.

unordered_set and unordered_multiset are defined in the header <boost/unordered_set.hpp>

namespace boost {
    template <
        class Key,
        class Hash = boost::hash<Key>, 
        class Pred = std::equal_to<Key>, 
        class Alloc = std::allocator<Key> > 
    class unordered_set;

    template<
        class Key,
        class Hash = boost::hash<Key>, 
        class Pred = std::equal_to<Key>, 
        class Alloc = std::allocator<Key> > 
    class unordered_multiset;
}

unordered_map and unordered_multimap are defined in the header <boost/unordered_map.hpp>

namespace boost {
    template <
        class Key, class Mapped,
        class Hash = boost::hash<Key>,
        class Pred = std::equal_to<Key>,
        class Alloc = std::allocator<Key> >
    class unordered_map;

    template<
        class Key, class Mapped,
        class Hash = boost::hash<Key>,
        class Pred = std::equal_to<Key>,
        class Alloc = std::allocator<Key> >
    class unordered_multimap;
}

When using Boost.TR1, these classes are included from <unordered_set> and <unordered_map>, with the classes added to the std::tr1 namespace.

The containers are used in a similar manner to the normal associative containers:

typedef boost::unordered_map<std::string, int> map;
map x;
x["one"] = 1;
x["two"] = 2;
x["three"] = 3;

assert(x.at("one") == 1);
assert(x.find("missing") == x.end());

But since the elements aren't ordered, the output of:

BOOST_FOREACH(map::value_type i, x) {
    std::cout<<i.first<<","<<i.second<<"\n";
}

can be in any order. For example, it might be:

two,2
one,1
three,3

To store an object in an unordered associative container requires both an key equality function and a hash function. The default function objects in the standard containers support a few basic types including integer types, floating point types, pointer types, and the standard strings. Since Boost.Unordered uses boost::hash it also supports some other types, including standard containers. To use any types not supported by these methods you have to extend Boost.Hash to support the type or use your own custom equality predicates and hash functions. See the Equality Predicates and Hash Functions section for more details.

There are other differences, which are listed in the Comparison with Associative Containers section.

Last revised: July 05, 2011 at 15:28:11 GMT


PrevUpHomeNext