| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2021 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 | * Graph6Writer.cpp
|
|---|
| 25 | *
|
|---|
| 26 | * Created on: Apr 2, 2021
|
|---|
| 27 | * Author: heber
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | // include config.h
|
|---|
| 32 | #ifdef HAVE_CONFIG_H
|
|---|
| 33 | #include <config.h>
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 | #include "Graph6Writer.hpp"
|
|---|
| 37 |
|
|---|
| 38 | #include "CodePatterns/Assert.hpp"
|
|---|
| 39 | #include "CodePatterns/Log.hpp"
|
|---|
| 40 |
|
|---|
| 41 | #include <cassert>
|
|---|
| 42 | #include <cmath>
|
|---|
| 43 | #include <iostream>
|
|---|
| 44 |
|
|---|
| 45 | #include "Atom/atom.hpp"
|
|---|
| 46 | #include "Element/element.hpp"
|
|---|
| 47 |
|
|---|
| 48 | //#include "CodePatterns/MemDebug.hpp"
|
|---|
| 49 |
|
|---|
| 50 | Graph6Writer::Graph6Writer(const std::vector<const atom *> atoms):
|
|---|
| 51 | _atoms(atoms)
|
|---|
| 52 | {}
|
|---|
| 53 |
|
|---|
| 54 | void Graph6Writer::write_n(std::ostream& out) {
|
|---|
| 55 | const unsigned long n = _atoms.size();
|
|---|
| 56 |
|
|---|
| 57 | if (n<62) {
|
|---|
| 58 | out << ((unsigned char)(n+63));
|
|---|
| 59 | return;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | out << ((unsigned char)126);
|
|---|
| 63 | int num_bytes = 2;
|
|---|
| 64 | if (n> 258047) {
|
|---|
| 65 | out << ((unsigned char)126);
|
|---|
| 66 | num_bytes = 3;
|
|---|
| 67 | }
|
|---|
| 68 | for(int value=num_bytes; value>=0; value--) {
|
|---|
| 69 | unsigned char c = 0;
|
|---|
| 70 | int n_pos = 6*(value+1)-1;
|
|---|
| 71 | for(int c_pos=5; c_pos>=0; n_pos--, c_pos--) {
|
|---|
| 72 | c += (n & (1<<n_pos))>>((int)n_pos/6);
|
|---|
| 73 | }
|
|---|
| 74 | out << (c+63);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /* Given an iterator over the adjacency matrix in the order (0,1),(0,2),(1,2),(0,3),(1,3),(2,3),...,(n-1,n)
|
|---|
| 80 | this writes a graph6 representation to out. */
|
|---|
| 81 | void Graph6Writer::write_graph6(std::ostream& out) {
|
|---|
| 82 | write_n(out);
|
|---|
| 83 |
|
|---|
| 84 | const unsigned long n = _atoms.size();
|
|---|
| 85 |
|
|---|
| 86 | unsigned char value = 0;
|
|---|
| 87 | int byte_pos = 5;
|
|---|
| 88 | unsigned int bytes_written = 0;
|
|---|
| 89 | for (size_t j=0; j<n; ++j)
|
|---|
| 90 | for (size_t i=0; i<j; ++i) {
|
|---|
| 91 | // std::cout << "\t\n" << (int)value << " " << byte_pos << std::endl;
|
|---|
| 92 |
|
|---|
| 93 | unsigned int bit = _atoms[i]->IsBondedTo(_atoms[j]);
|
|---|
| 94 | LOG(2, "DEBUG: (" << i << "," << j << ") = " << bit << "," << value << " | " << bit << " << " << byte_pos << " = " << (unsigned int)value << " | " << (bit << byte_pos));
|
|---|
| 95 | value = value | (bit << byte_pos--);
|
|---|
| 96 | if (byte_pos < 0) {
|
|---|
| 97 | LOG(2, "DEBUG: Writing byte " << value << " into range [" << (unsigned char)63 << "," << (unsigned char)126 << "]");
|
|---|
| 98 | ASSERT( (value+63) <= 126,
|
|---|
| 99 | "Graph6Writer::write_graph6() - char to write is outside "+toString((unsigned char)63)
|
|---|
| 100 | +" and "+toString((unsigned char)126));
|
|---|
| 101 | out << (unsigned char)(value+63);
|
|---|
| 102 | bytes_written++;
|
|---|
| 103 | value = 0;
|
|---|
| 104 | byte_pos = 5;
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | if (byte_pos!=5) {
|
|---|
| 108 | ASSERT( (value+63) <= 126,
|
|---|
| 109 | "Graph6Writer::write_graph6() - char to write is outside "+toString((unsigned char)63)
|
|---|
| 110 | +" and "+toString((unsigned char)126));
|
|---|
| 111 | LOG(2, "DEBUG: Writing byte " << value << " into range [" << (unsigned char)63 << "," << (unsigned char)126 << "]");
|
|---|
| 112 | out << (unsigned char)(value+63);
|
|---|
| 113 | bytes_written++;
|
|---|
| 114 | value=0;
|
|---|
| 115 | }
|
|---|
| 116 | ASSERT( value==0,
|
|---|
| 117 | "Graph6Writer::write_graph6() - byte is not null, i.e. chars left to write?");
|
|---|
| 118 | ASSERT( bytes_written == (int)ceil(n*(n-1)/12.0f),
|
|---|
| 119 | "Graph6Writer::write_graph6() - unexpected number of bytes written");
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | void Graph6Writer::write_elementlist(std::ostream& out) {
|
|---|
| 123 | for (std::vector<const atom *>::const_iterator iter = _atoms.begin();
|
|---|
| 124 | iter != _atoms.end(); ++iter) {
|
|---|
| 125 | if (iter != _atoms.begin())
|
|---|
| 126 | out << ' ';
|
|---|
| 127 | out << (*iter)->getElement().getSymbol();
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|