diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2017-05-01 16:08:43 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2017-05-01 16:59:24 +0300 |
commit | 61377c582e0f2675baa5f5e6e30a35d1a4164b33 (patch) | |
tree | 11cdca992834d7f7f197f72856712fbcb3020e3d /libbutl/multi-index.hxx | |
parent | 442c1a6790e52baa0c081f310d4d9e9b6f1ff638 (diff) |
Add hxx extension for headers and lib prefix for library dir
Diffstat (limited to 'libbutl/multi-index.hxx')
-rw-r--r-- | libbutl/multi-index.hxx | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/libbutl/multi-index.hxx b/libbutl/multi-index.hxx new file mode 100644 index 0000000..448e1f7 --- /dev/null +++ b/libbutl/multi-index.hxx @@ -0,0 +1,59 @@ +// file : libbutl/multi-index.hxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef LIBBUTL_MULTI_INDEX_HXX +#define LIBBUTL_MULTI_INDEX_HXX + +#include <utility> // declval() +#include <functional> // hash + +namespace butl +{ + // Google the "Emulating Boost.MultiIndex with Standard Containers" blog + // post for details. + // + + template <typename T> + struct map_key + { + mutable const T* p; + + map_key (const T* v = 0): p (v) {} + bool operator< (const map_key& x) const {return *p < *x.p;} + bool operator== (const map_key& x) const {return *p == *x.p;} + }; + + template <typename I> + struct map_iterator_adapter: I + { + typedef const typename I::value_type::second_type value_type; + typedef value_type* pointer; + typedef value_type& reference; + + map_iterator_adapter () {} + map_iterator_adapter (I i): I (i) {} + + map_iterator_adapter& + operator= (I i) {static_cast<I&> (*this) = i; return *this;} + + reference operator* () const {return I::operator* ().second;} + pointer operator-> () const {return &I::operator-> ()->second;} + }; +} + +namespace std +{ + template <typename T> + struct hash<butl::map_key<T>>: hash<T> + { + size_t + operator() (butl::map_key<T> x) const + noexcept (noexcept (declval<hash<T>> () (*x.p))) + { + return hash<T>::operator() (*x.p); + } + }; +} + +#endif // LIBBUTL_MULTI_INDEX_HXX |