All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GraphToolBinaryReader.h
Go to the documentation of this file.
1 /*
2  * GraphToolBinaryReader.h
3  *
4  * Created on: 02.12.14
5  * Author: Maximilian Vogel
6  */
7 
8 #ifndef GRAPHTOOLBINARYREADER_H_
9 #define GRAPHTOOLBINARYREADER_H_
10 
11 #include <fstream>
12 #include <iostream>
13 #include <string>
14 #include <unordered_map>
15 
16 
17 #include "GraphReader.h"
18 
19 namespace NetworKit {
20 
26 
27 public:
28 
29  GraphToolBinaryReader() = default; //nullary constructor for Python shell
30 
36  Graph read(const std::string& path);
37 
38 protected:
40 
41 private:
42  void addOutNeighbours(std::ifstream& file, uint64_t numNodes, Graph& G);
43 
44  std::vector<std::vector<uint64_t>> getOutNeighbours(std::ifstream& file, uint64_t numNodes);
45 
46  uint8_t getAdjacencyWidth(uint64_t n);
47 
48  uint64_t getNumNodes(std::ifstream& file);
49 
50  bool getDirected(std::ifstream& file);
51 
52  std::string readComment(std::ifstream& file);
53 
54  bool checkHeader(std::ifstream& file);
55 
56  template<typename Type>
57  Type readType(std::ifstream& file, int width) {
58  Type val = 0;
59  uint8_t* bytes = new uint8_t[width];
60  file.read((char*)bytes,width);
61  if (this->littleEndianness) {
62  for (int i = 0; i < width;++i) {
63  val |= ((Type)bytes[i] << (i*8));
64  }
65  } else {
66  for (int i = 0; i < width; ++i) {
67  val |= ((Type)bytes[i] << (width-1-i)*8);
68  }
69  }
70  delete[] bytes;
71  return val;
72  };
73 
74 };
75 
76 } /* namespace NetworKit */
77 #endif /* GRAPHTOOLBINARYREADER_H_ */
Graph read(const std::string &path)
Given the path of an input file, read the graph contained.
Definition: GraphToolBinaryReader.cpp:18
Abstract base class for graph readers.
Definition: GraphReader.h:21
bool littleEndianness
Definition: GraphToolBinaryReader.h:39
A graph (with optional weights) and parallel iterator methods.
Definition: Graph.h:79
Reads graphs from files in the binary format defined by graph-tool[1].
Definition: GraphToolBinaryReader.h:25