summaryrefslogtreecommitdiff
path: root/storage/oqgraph/graphcore-graph.h
blob: 4b55068a777e82e8df057b566e0f2fc3246fbb4c (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Copyright (C) 2007-2013 Arjen G Lentz & Antony T Curtis for Open Query

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */

/* ======================================================================
   Open Query Graph Computation Engine, based on a concept by Arjen Lentz
   v3 implementation by Antony Curtis, Arjen Lentz, Andrew McDonnell
   For more information, documentation, support, enhancement engineering,
   see http://openquery.com/graph or contact graph@openquery.com
   ======================================================================
*/

#ifndef oq_graphcore_graph_h_
#define oq_graphcore_graph_h_

#include "oqgraph_shim.h"

#include <boost/graph/two_bit_color_map.hpp>

namespace boost
{
  typedef oqgraph3::graph Graph;

  template<typename IndexMap = identity_property_map>
  struct two_bit_judy_map
  {
    typedef typename property_traits<IndexMap>::key_type key_type;
    typedef two_bit_color_type value_type;
    typedef void reference;
    typedef read_write_property_map_tag category;

    open_query::judy_bitset msb;
    open_query::judy_bitset lsb;
    IndexMap index;

    two_bit_judy_map(const IndexMap& i)
      : index(i)
    { }

    friend two_bit_color_type get(
        const two_bit_judy_map<IndexMap>& pm,
        typename property_traits<IndexMap>::key_type key)
    {
      typename property_traits<IndexMap>::value_type i = get(pm.index, key);
      return two_bit_color_type((2*int(pm.msb.test(i))) | int(pm.lsb.test(i)));
    }

    friend void put(
        two_bit_judy_map<IndexMap>& pm,
        typename property_traits<IndexMap>::key_type key,
        two_bit_color_type value)
    {
      typename property_traits<IndexMap>::value_type i = get(pm.index, key);
      pm.msb.set(i, value & 2);
      pm.lsb.set(i, value & 1);
    }
  };

  template<typename IndexMap>
  inline two_bit_judy_map<IndexMap>
  make_two_bit_judy_map(const IndexMap& index)
  {
    return two_bit_judy_map<IndexMap>(index);
  }


  template <typename Type>
  struct default_lazy_initializer
  {
    template <typename Key>
    Type operator()(const Key&) const { return Type(); }
  };

  template <typename Type>
  struct copy_initializer
  {
    copy_initializer(const Type& value) : _(value) { }
    template <typename Key>
    const Type& operator()(const Key&) const { return _; }
    const Type& _;
  };

  template <typename Type>
  copy_initializer<Type> make_copy_initializer(const Type& value)
  { return copy_initializer<Type>(value); }


  template <typename Type>
  struct value_initializer
  {
    value_initializer(const Type& value) : _(value) { }
    template <typename Key>
    const Type& operator()(const Key&) const { return _; }
    const Type _;
  };

  template <typename Type>
  value_initializer<Type> make_value_initializer(const Type& value)
  { return value_initializer<Type>(value); }


  template <typename Key>
  struct identity_initializer
  {
    const Key& operator()(const Key& _) const { return _; }
  };

  template <class Container, class Generator>
  struct lazy_property_map
  {
    typedef lazy_property_map<Container, Generator> self;
    typedef typename Container::key_type key_type;
    typedef typename Container::value_type::second_type value_type;
    typedef value_type& reference;
    typedef lvalue_property_map_tag category;

    lazy_property_map(Container& m, Generator g= Generator())
      : _m(m)
      , _g(g)
    { }

    reference operator[](const key_type& k) const
    {
      typename Container::iterator found= _m.find(k);
      if (_m.end() == found)
      {
        found= _m.insert(std::make_pair(k, _g(k))).first;
      }
      return found->second;
    }

    void set(const key_type& k, const value_type& v)
    { _m[k] = v; }

    friend reference get(const self& s, const key_type& k)
    {
      return s[k];
    }

    friend void put(self& s, const key_type& k, const value_type& v)
    { s.set(k, v); }

    Container& _m;
    Generator _g;
  };

  template <class Container, class Generator>
  lazy_property_map<Container, Generator>
  make_lazy_property_map(Container& c, Generator g)
  { return lazy_property_map<Container, Generator>(c, g); }

}

#endif