From 75d03492c0a42031a987969a6eb20dd2121f54d1 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 13 Mar 2015 14:39:46 +0200 Subject: Rename set_key to map_key; feels more correct --- build/map-key | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 build/map-key (limited to 'build/map-key') diff --git a/build/map-key b/build/map-key new file mode 100644 index 0000000..43cdefe --- /dev/null +++ b/build/map-key @@ -0,0 +1,59 @@ +// file : build/map-key -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef BUILD_MAP_KEY +#define BUILD_MAP_KEY + +#include // declval() +#include // hash + +namespace build +{ + // Google the "Emulating Boost.MultiIndex with Standard Containers" blog + // post for deatils. + // + + 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() (build::map_key x) const + noexcept (noexcept (declval> () (*x.p))) + { + return hash::operator() (*x.p); + } + }; +} + +#endif // BUILD_MAP_KEY -- cgit v1.1