From 55bdde560a947e35108ee22203e5fd3293b7c215 Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Wed, 19 Jan 2022 13:56:50 -0800 Subject: [PATCH] Convert intro to AsciiDoc --- doc/unordered/intro.adoc | 114 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/doc/unordered/intro.adoc b/doc/unordered/intro.adoc index 73481810..c82021fe 100644 --- a/doc/unordered/intro.adoc +++ b/doc/unordered/intro.adoc @@ -1,2 +1,116 @@ [#intro] = Introduction + +:idprefix: intro_ +:cpp: C++ + +For accessing data based on key lookup, the {cpp} 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 +link:https://en.wikipedia.org/wiki/Hash_table[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, unordered associative containers were added to the {cpp} +standard. This is an implementation of the containers described in {cpp}11, +with some <> in +order to work with non-{cpp}11 compilers and libraries. + +`unordered_set` and `unordered_multiset` are defined in the header +`` +[source,c++] +---- +namespace boost { + template < + class Key, + class Hash = boost::hash, + class Pred = std::equal_to, + class Alloc = std::allocator > + class unordered_set; + + template< + class Key, + class Hash = boost::hash, + class Pred = std::equal_to, + class Alloc = std::allocator > + class unordered_multiset; +} +---- + +`unordered_map` and `unordered_multimap` are defined in the header +`` + +[source,c++] +---- +namespace boost { + template < + class Key, class Mapped, + class Hash = boost::hash, + class Pred = std::equal_to, + class Alloc = std::allocator > > + class unordered_map; + + template< + class Key, class Mapped, + class Hash = boost::hash, + class Pred = std::equal_to, + class Alloc = std::allocator > > + class unordered_multimap; +} +---- + +When using Boost.TR1, these classes are included from `` and +``, with the classes added to the `std::tr1` namespace. + +The containers are used in a similar manner to the normal associative +containers: + +[source,cpp] +---- +typedef boost::unordered_map 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: + +[source,c++] +---- +BOOST_FOREACH(map::value_type i, x) { + std::cout<> section +for more details. + +There are other differences, which are listed in the +<> section.