All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GraphToolBinaryWriter.h
Go to the documentation of this file.
1 /*
2  * GraphToolBinaryWriter.h
3  *
4  * Created on: 02.12.14
5  * Author: Maximilian Vogel
6  */
7 
8 #ifndef GRAPHTOOLBINARYWRITER_H_
9 #define GRAPHTOOLBINARYWRITER_H_
10 
11 #include <fstream>
12 #include <iostream>
13 #include <string>
14 #include <unordered_map>
15 
16 
17 #include "GraphWriter.h"
18 
19 namespace NetworKit {
20 
26 
27 public:
28 
30 
36  void write(const Graph& G, const std::string& path);
37 
38 protected:
40 
41 
42 private:
43  void writeAdjacencies(std::ofstream& file, const Graph& G);
44 
45  uint8_t getAdjacencyWidth(uint64_t n);
46 
47  void writeComment(std::ofstream& file);
48 
49  void writeHeader(std::ofstream& file);
50 
51  template<typename Type>
52  void writeType(std::ofstream& file, int width, Type val) {
53  //DEBUG("writing ",val, "with width ", width, " to file");
54  uint8_t* bytes = new uint8_t[width];
55  if (this->littleEndianness) {
56  for (int i = 0; i < width;++i) {
57  bytes[i] = (val >> (i*8)) & 0xFF;
58  }
59  } else {
60  for (int i = 0; i < width; ++i) {
61  bytes[i] = (val >> ((width-1-i)*8)) & 0xFF;
62  }
63  }
64  file.write((char*)bytes,width);
65  delete[] bytes;
66  };
67 
68 };
69 
70 } /* namespace NetworKit */
71 #endif /* GRAPHTOOLBINARYWRITER_H_ */
void write(const Graph &G, const std::string &path)
Given the path of an input file, read the graph contained.
Definition: GraphToolBinaryWriter.cpp:18
bool littleEndianness
Definition: GraphToolBinaryWriter.h:39
Abstract base class for graph writers.
Definition: GraphWriter.h:21
GraphToolBinaryWriter(bool littleEndianness=true)
Definition: GraphToolBinaryWriter.cpp:16
Writes graphs to files in the binary format defined by graph-tool[1].
Definition: GraphToolBinaryWriter.h:25
A graph (with optional weights) and parallel iterator methods.
Definition: Graph.h:79