| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2017 Frederik Heber. All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | *
|
|---|
| 7 | * This file is part of MoleCuilder.
|
|---|
| 8 | *
|
|---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
|---|
| 10 | * it under the terms of the GNU General Public License as published by
|
|---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
|---|
| 12 | * (at your option) any later version.
|
|---|
| 13 | *
|
|---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 17 | * GNU General Public License for more details.
|
|---|
| 18 | *
|
|---|
| 19 | * You should have received a copy of the GNU General Public License
|
|---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | /*
|
|---|
| 24 | * Graph6Reader.cpp
|
|---|
| 25 | *
|
|---|
| 26 | * Created on: Sep 26, 2017
|
|---|
| 27 | * Author: heber
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | // include config.h
|
|---|
| 32 | #ifdef HAVE_CONFIG_H
|
|---|
| 33 | #include <config.h>
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 | //#include "CodePatterns/MemDebug.hpp"
|
|---|
| 37 |
|
|---|
| 38 | #include "CodePatterns/Assert.hpp"
|
|---|
| 39 | #include "CodePatterns/Log.hpp"
|
|---|
| 40 |
|
|---|
| 41 | #include "Graph6Reader.hpp"
|
|---|
| 42 |
|
|---|
| 43 | const int Graph6Reader::packet_size(5);
|
|---|
| 44 |
|
|---|
| 45 | void Graph6Reader::operator()(std::istream &_graph_string)
|
|---|
| 46 | {
|
|---|
| 47 | std::istream_iterator<unsigned char> iter(_graph_string);
|
|---|
| 48 | scan_num_nodes(iter);
|
|---|
| 49 | scan_edges(iter);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | void Graph6Reader::scan_num_nodes(std::istream_iterator<unsigned char> &_it)
|
|---|
| 53 | {
|
|---|
| 54 | //now we're one past the optional header
|
|---|
| 55 | //parse the number of nodes
|
|---|
| 56 | ASSERT(*_it >= 64, "The number of nodes is not properly encoded");
|
|---|
| 57 | if (*_it <126) {
|
|---|
| 58 | //6-bit encoding
|
|---|
| 59 | num_nodes = *_it-63;
|
|---|
| 60 | } else if (*_it++ == 126) {
|
|---|
| 61 | unsigned int packets = 3;
|
|---|
| 62 | if (*_it == 126) {
|
|---|
| 63 | //36-bit encoding
|
|---|
| 64 | packets++;
|
|---|
| 65 | _it++;
|
|---|
| 66 | }
|
|---|
| 67 | for(unsigned int i =0; i<packets*packet_size; ++i) {
|
|---|
| 68 | unsigned char packet = (*_it++) - 63;
|
|---|
| 69 | ASSERT(packet<=(1<<(packet_size+1)),
|
|---|
| 70 | "The input is malformed. "
|
|---|
| 71 | "It contains a non-printable ascii-char in the number of nodes encoding.");
|
|---|
| 72 | num_nodes += packet;
|
|---|
| 73 | num_nodes *= (1<<packet_size);
|
|---|
| 74 | }
|
|---|
| 75 | } else {
|
|---|
| 76 | ASSERT(*_it<=126,
|
|---|
| 77 | "The input is malformed. "
|
|---|
| 78 | "The number of nodes is not correctly encoded, the first byte is >126."
|
|---|
| 79 | );
|
|---|
| 80 | }
|
|---|
| 81 | ++_it;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | void Graph6Reader::next_edge(std::istream_iterator<unsigned char> &_it) {
|
|---|
| 85 | unsigned int bit = 0;
|
|---|
| 86 | while(!bit && !eos) {
|
|---|
| 87 | if (++row==column) {
|
|---|
| 88 | column+=1;
|
|---|
| 89 | row = 0;
|
|---|
| 90 | }
|
|---|
| 91 | if (column>=num_nodes) {
|
|---|
| 92 | eos = true;
|
|---|
| 93 | break;
|
|---|
| 94 | }
|
|---|
| 95 | if (bit_pos<0) {
|
|---|
| 96 | ASSERT(_it != std::istream_iterator<unsigned char>(),
|
|---|
| 97 | "Graph6Reader::next_edge() - less characters than expected in string.");
|
|---|
| 98 | ASSERT((*_it >= 64) && (*_it <= 126),
|
|---|
| 99 | "The input contains a non-printable ascii char in the matrix encoding");
|
|---|
| 100 | cur_byte = (*_it) - 63;
|
|---|
| 101 | ++(_it);
|
|---|
| 102 |
|
|---|
| 103 | bit_pos = packet_size;
|
|---|
| 104 | }
|
|---|
| 105 | bit = cur_byte & (1<<(bit_pos--));
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | void Graph6Reader::scan_edges(std::istream_iterator<unsigned char> &_it)
|
|---|
| 110 | {
|
|---|
| 111 | while(!eos) {
|
|---|
| 112 | next_edge(_it);
|
|---|
| 113 | if (!eos) {
|
|---|
| 114 | LOG(3, "DEBUG: Adding edge bit in (" << column << "," << row << ")");
|
|---|
| 115 | edges.push_back(std::make_pair(column,row));
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|