Program Listing for File GraphToolBinaryWriter.hpp

Return to documentation for file (include/networkit/io/GraphToolBinaryWriter.hpp)

/*
 * GraphToolBinaryWriter.hpp
 *
 *  Created on: 02.12.14
 *      Author: Maximilian Vogel
 */

#ifndef NETWORKIT_IO_GRAPH_TOOL_BINARY_WRITER_HPP_
#define NETWORKIT_IO_GRAPH_TOOL_BINARY_WRITER_HPP_

#include <networkit/io/GraphWriter.hpp>

namespace NetworKit {

class GraphToolBinaryWriter final : public GraphWriter {

public:
    GraphToolBinaryWriter(bool littleEndianness = true);

    void write(const Graph &G, const std::string &path) override;

private:
    bool littleEndianness;

    void writeAdjacencies(std::ofstream &file, const Graph &G);

    uint8_t getAdjacencyWidth(uint64_t n);

    void writeComment(std::ofstream &file);

    void writeHeader(std::ofstream &file);

    template <typename Type>
    void writeType(std::ofstream &file, int width, Type val) {
        // DEBUG("writing ",val, "with width ", width, " to file");
        uint8_t *bytes = new uint8_t[width];
        if (this->littleEndianness) {
            for (int i = 0; i < width; ++i) {
                bytes[i] = (val >> (i * 8)) & 0xFF;
            }
        } else {
            for (int i = 0; i < width; ++i) {
                bytes[i] = (val >> ((width - 1 - i) * 8)) & 0xFF;
            }
        }
        file.write((char *)bytes, width);
        delete[] bytes;
    }
};

} /* namespace NetworKit */
#endif // NETWORKIT_IO_GRAPH_TOOL_BINARY_WRITER_HPP_