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 <iterator>
|
---|
39 |
|
---|
40 | #include "CodePatterns/Assert.hpp"
|
---|
41 |
|
---|
42 | #include "Graph6Reader.hpp"
|
---|
43 |
|
---|
44 | const int Graph6Reader::packet_size(5);
|
---|
45 |
|
---|
46 | void Graph6Reader::operator()(std::istream &_graph_string)
|
---|
47 | {
|
---|
48 | std::istream_iterator<unsigned char> iter(_graph_string);
|
---|
49 | scan_num_nodes(iter);
|
---|
50 | scan_edges(iter);
|
---|
51 | }
|
---|
52 |
|
---|
53 | void Graph6Reader::scan_num_nodes(std::istream_iterator<unsigned char> &_it)
|
---|
54 | {
|
---|
55 | int packets;
|
---|
56 | int num_nodes;
|
---|
57 | //now we're one past the optional header
|
---|
58 | //parse the number of nodes
|
---|
59 | ASSERT(*_it >= 63, "The number of nodes is not properly encoded");
|
---|
60 | if (*_it <126) {
|
---|
61 | //6-bit encoding
|
---|
62 | num_nodes = *_it-63;
|
---|
63 | } else if (*_it++ == 126) {
|
---|
64 | unsigned int packets = 3;
|
---|
65 | if (*_it == 126) {
|
---|
66 | //36-bit encoding
|
---|
67 | packets++;
|
---|
68 | _it++;
|
---|
69 | }
|
---|
70 | for(unsigned int i =0; i<packets*packet_size; ++i) {
|
---|
71 | unsigned char packet = (*_it++) - 63;
|
---|
72 | ASSERT(packet<=(1<<(packet_size+1)),
|
---|
73 | "The input is malformed. "
|
---|
74 | "It contains a non-printable ascii-char in the number of nodes encoding.");
|
---|
75 | num_nodes += packet;
|
---|
76 | num_nodes *= (1<<packet_size);
|
---|
77 | }
|
---|
78 | } else {
|
---|
79 | ASSERT(*_it<=126,
|
---|
80 | "The input is malformed. "
|
---|
81 | "The number of nodes is not correctly encoded, the first byte is >126."
|
---|
82 | );
|
---|
83 | }
|
---|
84 | ++_it;
|
---|
85 | }
|
---|
86 |
|
---|
87 | void Graph6Reader::next_edge(std::istream_iterator<unsigned char> &_it) {
|
---|
88 | unsigned int bit = 0;
|
---|
89 | while(!bit && !eos) {
|
---|
90 | if (++row==column) {
|
---|
91 | column+=1;
|
---|
92 | row = 0;
|
---|
93 | }
|
---|
94 | if (column>=num_nodes) {
|
---|
95 | eos = true;
|
---|
96 | break;
|
---|
97 | }
|
---|
98 | if (bit_pos<0) {
|
---|
99 | ASSERT((*_it >= 63) && (*_it <= 126),
|
---|
100 | "The input contains a non-printable ascii char in the matrix encoding");
|
---|
101 | cur_byte = (*_it) - 63;
|
---|
102 | ++(_it);
|
---|
103 |
|
---|
104 | bit_pos = packet_size;
|
---|
105 | }
|
---|
106 | bit = cur_byte & (1<<(bit_pos--));
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | void Graph6Reader::scan_edges(std::istream_iterator<unsigned char> &_it)
|
---|
111 | {
|
---|
112 | while(!eos) {
|
---|
113 | next_edge(_it);
|
---|
114 | edges.push_back(std::make_pair(column,row));
|
---|
115 | }
|
---|
116 | }
|
---|