FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
graph_file_header.h
1 #ifndef __GRAPH_FILE_HEADER_H__
2 #define __GRAPH_FILE_HEADER_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 #include <string.h>
25 
26 #include "common.h"
27 #include "parameters.h"
28 
29 namespace fg
30 {
31 
32 const int64_t MAGIC_NUMBER = 0x123456789ABCDEFL;
33 const int CURR_VERSION = 4;
34 
35 enum graph_type {
36  DIRECTED,
37  UNDIRECTED,
38  TS_DIRECTED,
39  TS_UNDIRECTED,
40 };
41 
42 struct graph_header_struct
43 {
44  int64_t magic_number;
45  int version_number;
46  graph_type type;
47  size_t num_vertices;
48  size_t num_edges;
49  int edge_data_size;
50  // This is only used for time-series graphs.
51  int max_num_timestamps;
52 };
53 
59 {
60 public:
61  static const int HEADER_SIZE = 4096;
62 private:
63  union {
64  struct graph_header_struct data;
65  char page[HEADER_SIZE];
66  } h;
67 public:
68  static int get_header_size() {
69  return sizeof(graph_header);
70  }
71 
72  static void init(struct graph_header_struct &data) {
73  data.magic_number = MAGIC_NUMBER;
74  data.version_number = CURR_VERSION;
75  data.type = DIRECTED;
76  data.num_vertices = 0;
77  data.num_edges = 0;
78  data.edge_data_size = 0;
79  data.max_num_timestamps = 0;
80  }
81 
82  graph_header() {
83  assert(sizeof(*this) == HEADER_SIZE);
84  memset(this, 0, sizeof(*this));
85  init(h.data);
86  }
87 
88  graph_header(graph_type type, size_t num_vertices, size_t num_edges,
89  int edge_data_size, int max_num_timestamps = 0) {
90  assert(sizeof(*this) == HEADER_SIZE);
91  memset(this, 0, sizeof(*this));
92  h.data.magic_number = MAGIC_NUMBER;
93  h.data.version_number = CURR_VERSION;
94  h.data.type = type;
95  h.data.num_vertices = num_vertices;
96  h.data.num_edges = num_edges;
97  h.data.edge_data_size = edge_data_size;
98  h.data.max_num_timestamps = max_num_timestamps;
99  }
100 
101  bool is_graph_file() const {
102  return h.data.magic_number == MAGIC_NUMBER;
103  }
104 
105  bool is_right_version() const {
106  return h.data.version_number == CURR_VERSION;
107  }
108 
109  bool is_directed_graph() const {
110  return h.data.type == graph_type::DIRECTED
111  || h.data.type == graph_type::TS_DIRECTED;
112  }
113 
114  graph_type get_graph_type() const {
115  return h.data.type;
116  }
117 
118  size_t get_num_vertices() const {
119  return h.data.num_vertices;
120  }
121 
122  size_t get_num_edges() const {
123  return h.data.num_edges;
124  }
125 
126  bool has_edge_data() const {
127  return h.data.edge_data_size > 0;
128  }
129 
130  int get_edge_data_size() const {
131  return h.data.edge_data_size;
132  }
133 
134  int get_max_num_timestamps() const {
135  return h.data.max_num_timestamps;
136  }
137 
138  void verify() const {
139  if (!is_graph_file()) {
140  fprintf(stderr, "wrong magic number: %ld\n", h.data.magic_number);
141  }
142  if (!is_right_version()) {
143  fprintf(stderr, "wrong version number: %d\n", h.data.version_number);
144  }
145  assert(is_graph_file());
146  assert(is_right_version());
147  }
148 };
149 
150 }
151 
152 #endif
Definition: graph_file_header.h:58