oqgraph_thunk.h 4.4 KB
Newer Older
1
/* Copyright (C) 2007-2013 Arjen G Lentz & Antony T Curtis for Open Query
Antony T Curtis's avatar
Antony T Curtis committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* ======================================================================
   Open Query Graph Computation Engine, based on a concept by Arjen Lentz
19
   v3 implementation by Antony Curtis, Arjen Lentz, Andrew McDonnell
Antony T Curtis's avatar
Antony T Curtis committed
20
   For more information, documentation, support, enhancement engineering,
21
   see http://openquery.com/graph or contact graph@openquery.com
Antony T Curtis's avatar
Antony T Curtis committed
22 23 24 25 26 27 28 29 30 31
   ======================================================================
*/

#pragma once

#include <list>
#include <queue>
#include <string>
#include <utility>

Antony T Curtis's avatar
Antony T Curtis committed
32 33
#include "graphcore-config.h"

Antony T Curtis's avatar
Antony T Curtis committed
34 35 36 37 38 39 40 41 42 43 44 45 46
#include <boost/intrusive_ptr.hpp>
#include <boost/optional.hpp>
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <boost/pending/queue.hpp>
#include <boost/ptr_container/ptr_deque.hpp>

#include "graphcore-types.h"

namespace oqgraph3
{
  typedef open_query::VertexID vertex_id;
  typedef open_query::EdgeWeight weight_t;
47

Antony T Curtis's avatar
Antony T Curtis committed
48 49 50 51 52
  typedef size_t vertices_size_type;
  typedef size_t edges_size_type;
  typedef size_t degree_size_type;

  struct graph;
53
  struct cursor;
Antony T Curtis's avatar
Antony T Curtis committed
54 55 56

  typedef boost::intrusive_ptr<graph> graph_ptr;

57
  struct cursor_ptr : public boost::intrusive_ptr<cursor>
Antony T Curtis's avatar
Antony T Curtis committed
58
  {
59 60
    cursor_ptr() : boost::intrusive_ptr<cursor>() { }
    cursor_ptr(cursor* pos) : boost::intrusive_ptr<cursor>(pos) { }
Antony T Curtis's avatar
Antony T Curtis committed
61

62 63 64
    operator const std::string&() const;
    bool operator==(const cursor_ptr&) const;
    bool operator!=(const cursor_ptr&) const;
Antony T Curtis's avatar
Antony T Curtis committed
65 66
  };

67
  struct edge_info
Antony T Curtis's avatar
Antony T Curtis committed
68
  {
69 70 71 72 73 74 75 76 77 78 79 80
    cursor_ptr _cursor;

    edge_info() : _cursor(0) { }
    explicit edge_info(const cursor_ptr& pos) : _cursor(pos) { }
    edge_info& operator=(const cursor_ptr& pos) { _cursor= pos; return *this; }

    vertex_id origid() const;
    vertex_id destid() const;
    weight_t weight() const;

    bool operator==(const edge_info&) const;
    bool operator!=(const edge_info&) const;
Antony T Curtis's avatar
Antony T Curtis committed
81 82
  };

83
  struct cursor
Antony T Curtis's avatar
Antony T Curtis committed
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
    mutable int _ref_count;
    graph_ptr _graph;

    int _index;
    unsigned _parts;
    std::string _key;
    std::string _position;

    int _debugid;

    boost::optional<vertex_id> _origid;
    boost::optional<vertex_id> _destid;

    cursor(const graph_ptr& graph);
    cursor(const cursor& src);

    ~cursor();

    operator bool() const
    { return !_position.empty(); }

    operator edge_info() const
    { return edge_info(const_cast<cursor*>(this)); }

    vertex_id get_origid();
    vertex_id get_destid();
    weight_t get_weight();

    int seek_to(
        boost::optional<vertex_id> origid,
        boost::optional<vertex_id> destid);

    int seek_next();
    int seek_prev();

    void save_position();
    int restore_position();
    const std::string& record_position() const;
    void clear_position();
    int clear_position(int rc) { clear_position(); return rc; }

    bool operator==(const cursor& x) const;
    bool operator!=(const cursor& x) const;

    friend void intrusive_ptr_add_ref(cursor* ptr)
    { ++ptr->_ref_count; }
    friend void intrusive_ptr_release(cursor* ptr)
    { if (!--(ptr->_ref_count)) delete ptr; }
Antony T Curtis's avatar
Antony T Curtis committed
133 134 135 136 137
  };

  struct graph
  {
    mutable int _ref_count;
138 139
    cursor* _cursor;
    bool _stale;
Antony T Curtis's avatar
Antony T Curtis committed
140

141 142
    cursor_ptr _rnd_cursor;
    size_t _rnd_pos;
Antony T Curtis's avatar
Antony T Curtis committed
143 144 145 146 147

    ::TABLE* _table;
    ::Field* _source;
    ::Field* _target;
    ::Field* _weight;
148

149 150 151
    ::THD* get_table_thd();
    void set_table_thd(::THD* thd);

Antony T Curtis's avatar
Antony T Curtis committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    graph(
        ::TABLE* table,
        ::Field* source,
        ::Field* target,
        ::Field* weight= 0);
    ~graph();

    edges_size_type num_edges() const;

    friend edges_size_type num_edges(const graph& g)
    { return g.num_edges(); }

    friend void intrusive_ptr_add_ref(graph* ptr)
    { ptr->_ref_count++; }

    friend void intrusive_ptr_release(graph* ptr)
    { ptr->_ref_count--; }
  };

}