From 5a79b446475c9643346024a83bb14c2ba9c55dbd Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 18 Jun 2015 12:32:55 +0200 Subject: Move map-key from build2 to libbutl, rename multi-index --- butl/multi-index | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 butl/multi-index (limited to 'butl/multi-index') diff --git a/butl/multi-index b/butl/multi-index new file mode 100644 index 0000000..b8b7720 --- /dev/null +++ b/butl/multi-index @@ -0,0 +1,59 @@ +// file : butl/multi-index -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BUTL_MULTI_INDEX +#define BUTL_MULTI_INDEX + +#include // declval() +#include // hash + +namespace butl +{ + // Google the "Emulating Boost.MultiIndex with Standard Containers" blog + // post for details. + // + + template + 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 + 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 (*this) = i; return *this;} + + reference operator* () const {return I::operator* ().second;} + pointer operator-> () const {return &I::operator-> ()->second;} + }; +} + +namespace std +{ + template + struct hash>: hash + { + size_t + operator() (butl::map_key x) const + noexcept (noexcept (declval> () (*x.p))) + { + return hash::operator() (*x.p); + } + }; +} + +#endif // BUTL_MULTI_INDEX -- cgit v1.1