| 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-64;
 | 
|---|
| 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++) - 64;
 | 
|---|
| 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 |   int cur_byte = 0;
 | 
|---|
| 87 |   while(!bit && !eos) {
 | 
|---|
| 88 |     if (++row==column) {
 | 
|---|
| 89 |       column+=1;
 | 
|---|
| 90 |       row = 0;
 | 
|---|
| 91 |     }
 | 
|---|
| 92 |     if (column>=num_nodes) {
 | 
|---|
| 93 |       eos = true;
 | 
|---|
| 94 |       break;
 | 
|---|
| 95 |     }
 | 
|---|
| 96 |     if (bit_pos<0) {
 | 
|---|
| 97 |       ASSERT((*_it >= 63) && (*_it <= 126),
 | 
|---|
| 98 |           "The input contains a non-printable ascii char in the matrix encoding");
 | 
|---|
| 99 |       cur_byte = (*_it) - 63;
 | 
|---|
| 100 |       ++(_it);
 | 
|---|
| 101 | 
 | 
|---|
| 102 |       bit_pos = packet_size;
 | 
|---|
| 103 |     }
 | 
|---|
| 104 |     bit = cur_byte & (1<<(bit_pos--));
 | 
|---|
| 105 |   }
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | void Graph6Reader::scan_edges(std::istream_iterator<unsigned char> &_it)
 | 
|---|
| 109 | {
 | 
|---|
| 110 |   while(!eos) {
 | 
|---|
| 111 |     next_edge(_it);
 | 
|---|
| 112 |     if (!eos) {
 | 
|---|
| 113 |       LOG(3, "DEBUG: Adding edge bit in (" << column << "," << row << ")");
 | 
|---|
| 114 |       edges.push_back(std::make_pair(column,row));
 | 
|---|
| 115 |     }
 | 
|---|
| 116 |   }
 | 
|---|
| 117 | }
 | 
|---|