aboutsummaryrefslogtreecommitdiff
path: root/cutl/container/map-iterator.hxx
blob: a1467704032a4efab5d261ce9fca9d7249638f6e (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
// file      : cutl/container/map-iterator.hxx
// copyright : Copyright (c) 2009-2017 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef CUTL_CONTAINER_MAP_ITERATOR_HXX
#define CUTL_CONTAINER_MAP_ITERATOR_HXX

namespace cutl
{
  namespace container
  {
    // Map iterator adapter that can be used to implement multi-index
    // containers, as discussed in the following post:
    //
    // http://www.codesynthesis.com/~boris/blog/2012/09/11/emulating-boost-multi-index-with-std-containers/
    //
    template <typename M>
    struct map_iterator: M::iterator
    {
      typedef typename M::iterator base_iterator;
      typedef typename M::value_type::second_type value_type;
      typedef value_type* pointer;
      typedef value_type& reference;

      map_iterator () {}
      map_iterator (base_iterator i): base_iterator (i) {}

      reference
      operator* () const
      {
        return base_iterator::operator* ().second;
      }

      pointer
      operator-> () const
      {
        return &base_iterator::operator-> ()->second;
      }
    };

    template <typename M>
    struct map_const_iterator: M::const_iterator
    {
      typedef typename M::iterator base_iterator;
      typedef typename M::const_iterator base_const_iterator;
      typedef const typename M::value_type::second_type value_type;
      typedef value_type* pointer;
      typedef value_type& reference;

      map_const_iterator () {}
      map_const_iterator (base_iterator i): base_const_iterator (i) {}
      map_const_iterator (base_const_iterator i): base_const_iterator (i) {}

      reference
      operator* () const
      {
        return base_const_iterator::operator* ().second;
      }

      pointer
      operator-> () const
      {
        return &base_const_iterator::operator-> ()->second;
      }
    };
  }
}

#endif // CUTL_CONTAINER_MAP_ITERATOR_HXX