aboutsummaryrefslogtreecommitdiff
path: root/libbutl/multi-index.mxx
blob: d51bdfc6a870c86541f04415063355583bc94b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// file      : libbutl/multi-index.mxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#pragma once
#endif

// C includes.

#ifndef __cpp_lib_modules_ts
#include <utility>    // declval()
#include <functional> // hash
#endif

// Other includes.

#ifdef __cpp_modules_ts
export module butl.multi_index;
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
#endif

#include <libbutl/export.hxx>

LIBBUTL_MODEXPORT 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;}
  };
}

LIBBUTL_MODEXPORT 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);
    }
  };
}