jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
graph_type.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011,2012 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #ifndef JUBATUS_CORE_GRAPH_GRAPH_TYPE_HPP_
18 #define JUBATUS_CORE_GRAPH_GRAPH_TYPE_HPP_
19 
20 #include <map>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include <msgpack.hpp>
26 #include "jubatus/util/data/unordered_map.h"
27 
28 #include "../common/exception.hpp"
29 #include "../common/type.hpp"
30 #include "../common/unordered_map.hpp"
31 
32 namespace jubatus {
33 namespace core {
34 namespace graph {
35 
36 typedef uint64_t node_id_t;
37 typedef uint64_t edge_id_t;
38 
39 typedef std::map<std::string, std::string> property;
40 
43 };
44 
46  double score;
47  uint64_t out_degree_num;
48 
49  MSGPACK_DEFINE(score, out_degree_num);
50 };
51 
52 typedef jubatus::util::data::unordered_map<node_id_t, eigen_vector_info>
54 
55 struct node_info {
56  std::map<std::string, std::string> property;
57  std::vector<edge_id_t> in_edges;
58  std::vector<edge_id_t> out_edges;
59 
60  MSGPACK_DEFINE(property, in_edges, out_edges);
61 };
62 
63 struct edge_info {
64  property p;
65  node_id_t src;
66  node_id_t tgt;
67 
68  MSGPACK_DEFINE(p, src, tgt);
69 };
70 
71 struct preset_query {
72  // all AND conditions
73  std::vector<std::pair<std::string, std::string> > edge_query;
74  std::vector<std::pair<std::string, std::string> > node_query;
75 
76  bool operator==(const preset_query& r) const {
77  return edge_query == r.edge_query && node_query == r.node_query;
78  }
79 
80  MSGPACK_DEFINE(edge_query, node_query);
81 };
82 
83 typedef jubatus::util::data::unordered_map<
84  node_id_t, std::pair<uint64_t, node_id_t> > spt_edges;
85 
87  node_id_t landmark;
88  spt_edges from_root;
89  spt_edges to_root;
90 
91  MSGPACK_DEFINE(landmark, from_root, to_root);
92 };
93 
94 typedef std::vector<shortest_path_tree> spt_diff;
95 
96 typedef jubatus::util::data::unordered_map<preset_query, eigen_vector_diff>
98 
99 typedef jubatus::util::data::unordered_map<preset_query, spt_diff>
101 
103  public:
104  explicit graph_exception(const std::string& what)
105  : runtime_error(what) {
106  }
107 };
108 
110  public:
111  explicit unknown_graph(const std::string& name)
112  : graph_exception(name) {
113  }
114 };
115 
117  public:
118  explicit local_node_exists(node_id_t id)
119  : graph_exception(__func__),
120  id_(id) {
121  }
122 
123  node_id_t id_;
124 };
125 
127  public:
128  explicit global_node_exists(node_id_t id)
129  : graph_exception(__func__),
130  id_(id) {
131  }
132 
133  node_id_t id_;
134 };
135 
136 class edge_exists : public graph_exception {
137  public:
138  explicit edge_exists(edge_id_t id)
139  : graph_exception(__func__),
140  id_(id) {
141  }
142 
143  edge_id_t id_;
144 };
145 
146 class unknown_id : public graph_exception {
147  public:
148  explicit unknown_id(const std::string& type, uint64_t id)
149  : graph_exception(type),
150  id_(id) {
151  }
152 
153  uint64_t id_;
154 };
155 
157  public:
159  : graph_exception(__func__),
160  t_(t) {
161  }
162 
164 };
165 
167  public:
168  explicit unknown_query(const preset_query& q)
169  : graph_exception(__func__),
170  q_(q) {
171  }
172 
173  virtual ~unknown_query() throw() {
174  } // mmm...
175 
177 };
178 
179 } // namespace graph
180 } // namespace core
181 
182 namespace util {
183 namespace data {
184 
185 template<> struct hash<jubatus::core::graph::preset_query> {
187  return update(p.node_query, update(p.edge_query, 14695981039346656037LLU));
188  }
189 
190  private:
191  static uint64_t update(
192  const std::vector<std::pair<std::string, std::string> >& q,
193  uint64_t h) {
194  for (size_t i = 0; i < q.size(); ++i) {
195  h = update(q[i].first, h);
196  h = update(q[i].second, h);
197  }
198  return h;
199  }
200 
201  static uint64_t update(const std::string& s, uint64_t h) {
202  for (size_t i = 0; i < s.size(); ++i) {
203  h *= 1099511628211LLU;
204  h ^= s[i];
205  }
206  return h * 1099511628211LLU;
207  }
208 };
209 
210 } // namespace data
211 } // namespace util
212 } // namespace jubatus
213 
214 #endif // JUBATUS_CORE_GRAPH_GRAPH_TYPE_HPP_
jubatus::util::data::unordered_map< node_id_t, eigen_vector_info > eigen_vector_diff
Definition: graph_type.hpp:53
std::vector< std::pair< std::string, std::string > > edge_query
Definition: graph_type.hpp:73
jubatus::util::data::unordered_map< preset_query, eigen_vector_diff > eigen_vector_query_diff
Definition: graph_type.hpp:97
std::vector< std::pair< std::string, std::string > > node_query
Definition: graph_type.hpp:74
MSGPACK_DEFINE(landmark, from_root, to_root)
std::vector< edge_id_t > in_edges
Definition: graph_type.hpp:57
jubatus::util::data::unordered_map< preset_query, spt_diff > spt_query_diff
Definition: graph_type.hpp:100
unknown_query(const preset_query &q)
Definition: graph_type.hpp:168
uint64_t operator()(const jubatus::core::graph::preset_query &p) const
Definition: graph_type.hpp:186
static uint64_t update(const std::string &s, uint64_t h)
Definition: graph_type.hpp:201
unknown_id(const std::string &type, uint64_t id)
Definition: graph_type.hpp:148
std::vector< shortest_path_tree > spt_diff
Definition: graph_type.hpp:94
std::map< std::string, std::string > property
Definition: graph_type.hpp:39
graph_exception(const std::string &what)
Definition: graph_type.hpp:104
unknown_graph(const std::string &name)
Definition: graph_type.hpp:111
std::vector< edge_id_t > out_edges
Definition: graph_type.hpp:58
MSGPACK_DEFINE(edge_query, node_query)
std::map< std::string, std::string > property
Definition: graph_type.hpp:56
MSGPACK_DEFINE(score, out_degree_num)
jubatus::util::data::unordered_map< node_id_t, std::pair< uint64_t, node_id_t > > spt_edges
Definition: graph_type.hpp:84
MSGPACK_DEFINE(property, in_edges, out_edges)
static uint64_t update(const std::vector< std::pair< std::string, std::string > > &q, uint64_t h)
Definition: graph_type.hpp:191
bool operator==(const preset_query &r) const
Definition: graph_type.hpp:76