FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
utils.h
1 #ifndef __UTILS_H__
2 #define __UTILS_H__
3 
4 /*
5  * Copyright 2014 Open Connectome Project (http://openconnecto.me)
6  * Written by Da Zheng (zhengda1936@gmail.com)
7  *
8  * This file is part of FlashGraph.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include <stdlib.h>
24 
25 #include <vector>
26 #include <memory>
27 
28 #include "graph_file_header.h"
29 #include "FG_basic_types.h"
30 
31 namespace fg
32 {
33 
34 class in_mem_graph;
35 class vertex_index;
36 class vertex_index;
37 class in_mem_vertex;
38 class ext_mem_undirected_vertex;
39 class vertex_index_construct;
40 
41 namespace utils
42 {
43 
47 enum {
48  DEFAULT_TYPE,
49  EDGE_COUNT,
50  EDGE_TIMESTAMP,
51 };
52 
53 class serial_subgraph;
54 
55 /*
56  * This class serializes a graph and stores it in contiguous memory.
57  */
58 class serial_graph
59 {
60  size_t num_edges;
61  size_t num_vertices;
62  size_t num_non_empty;
63  std::shared_ptr<vertex_index_construct> index;
64  size_t edge_data_size;
65 public:
66  typedef std::shared_ptr<serial_graph> ptr;
67 
68  serial_graph(std::shared_ptr<vertex_index_construct> index,
69  size_t edge_data_size) {
70  num_edges = 0;
71  num_vertices = 0;
72  num_non_empty = 0;
73  this->index = index;
74  this->edge_data_size = edge_data_size;
75  }
76 
77  virtual ~serial_graph();
78 
79  virtual void add_vertex(const in_mem_vertex &v);
80 
81  // This needs to be redefined for undirected graphs.
82  virtual size_t get_num_edges() const {
83  return num_edges;
84  }
85 
86  size_t get_num_vertices() const {
87  return num_vertices;
88  }
89 
90  bool has_edge_data() const {
91  return edge_data_size > 0;
92  }
93 
94  size_t get_edge_data_size() const {
95  return edge_data_size;
96  }
97 
98  size_t get_num_non_empty_vertices() const {
99  return num_non_empty;
100  }
101 
102  vertex_index_construct &get_index() {
103  return *index;
104  }
105 
106  std::shared_ptr<vertex_index> dump_index(bool compressed) const;
107 
108  virtual void finalize_graph_file() {
109  }
110 
111  virtual graph_type get_graph_type() const = 0;
112  virtual void add_vertices(const serial_subgraph &subg) = 0;
113 
114  virtual std::shared_ptr<in_mem_graph> dump_graph(
115  const std::string &graph_name) = 0;
116 };
117 
118 /*
119  * The interface defines a graph represented by edges.
120  */
121 class edge_graph
122 {
123  size_t edge_data_size;
124 public:
125  typedef std::shared_ptr<edge_graph> ptr;
126 
127  edge_graph(size_t edge_data_size) {
128  this->edge_data_size = edge_data_size;
129  }
130 
131  virtual ~edge_graph() {
132  }
133 
134  virtual void sort_edges() = 0;
135  virtual void check_vertices(
136  const std::vector<ext_mem_undirected_vertex *> &vertices,
137  bool in_part, std::vector<off_t> &edge_offs) const = 0;
138  virtual size_t get_num_edges() const = 0;
139 
140  bool has_edge_data() const {
141  return edge_data_size > 0;
142  }
143 
144  size_t get_edge_data_size() const {
145  return edge_data_size;
146  }
147 };
148 
149 /*
150  * This interface serializes a graph into a single piece of memory.
151  */
152 class mem_serial_graph: public serial_graph
153 {
154 protected:
155  mem_serial_graph(std::shared_ptr<vertex_index_construct> index,
156  size_t edge_data_size): serial_graph(index, edge_data_size) {
157  }
158 public:
159  typedef std::shared_ptr<mem_serial_graph> ptr;
160  static ptr create(bool directed, size_t edge_data_size);
161  virtual void add_empty_vertex(vertex_id_t id) = 0;
162 };
163 
164 }
165 
166 }
167 
168 #endif