aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-06-18 12:32:55 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-06-18 12:32:55 +0200
commit5a79b446475c9643346024a83bb14c2ba9c55dbd (patch)
treee5b2822e799eb146f9a8af55481c37b9c0f06c82
parentdc65f9612e9feea1732572e8188d900495349059 (diff)
Move map-key from build2 to libbutl, rename multi-index
-rw-r--r--butl/multi-index59
-rw-r--r--butl/prefix-map4
2 files changed, 61 insertions, 2 deletions
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 <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 // BUTL_MULTI_INDEX
diff --git a/butl/prefix-map b/butl/prefix-map
index 188d18f..80264d0 100644
--- a/butl/prefix-map
+++ b/butl/prefix-map
@@ -7,8 +7,8 @@
#include <map>
#include <string>
-#include <utility> // move
-#include <algorithm> // min
+#include <utility> // move()
+#include <algorithm> // min()
namespace butl
{