| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 6 |  * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 7 |  * 
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *   This file is part of MoleCuilder.
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 12 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 13 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 14 |  *    (at your option) any later version.
 | 
|---|
| 15 |  *
 | 
|---|
| 16 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 17 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 18 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 19 |  *    GNU General Public License for more details.
 | 
|---|
| 20 |  *
 | 
|---|
| 21 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 22 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. 
 | 
|---|
| 23 |  */
 | 
|---|
| 24 | 
 | 
|---|
| 25 | /*
 | 
|---|
| 26 |  * HomologyGraph.cpp
 | 
|---|
| 27 |  *
 | 
|---|
| 28 |  *  Created on: Sep 24, 2012
 | 
|---|
| 29 |  *      Author: heber
 | 
|---|
| 30 |  */
 | 
|---|
| 31 | 
 | 
|---|
| 32 | 
 | 
|---|
| 33 | // include config.h
 | 
|---|
| 34 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 35 | #include <config.h>
 | 
|---|
| 36 | #endif
 | 
|---|
| 37 | 
 | 
|---|
| 38 | // include headers that implement a archive in simple text format
 | 
|---|
| 39 | // otherwise BOOST_CLASS_EXPORT_IMPLEMENT has no effect
 | 
|---|
| 40 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
| 41 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
| 42 | 
 | 
|---|
| 43 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "HomologyGraph.hpp"
 | 
|---|
| 46 | 
 | 
|---|
| 47 | #include <iostream>
 | 
|---|
| 48 | 
 | 
|---|
| 49 | HomologyGraph::HomologyGraph(const KeySet &keyset) :
 | 
|---|
| 50 |   nodes(detail::getNodesFromKeySet(keyset)),
 | 
|---|
| 51 |   edges(detail::getEdgesFromKeySet(keyset))
 | 
|---|
| 52 | {}
 | 
|---|
| 53 | 
 | 
|---|
| 54 | HomologyGraph::HomologyGraph(const IndexSet &index) :
 | 
|---|
| 55 |   nodes(detail::getNodesFromIndexSet(index)),
 | 
|---|
| 56 |   edges(detail::getEdgesFromIndexSet(index))
 | 
|---|
| 57 | {}
 | 
|---|
| 58 | 
 | 
|---|
| 59 | HomologyGraph::HomologyGraph(const AtomIdSet::atomIdSet &index) :
 | 
|---|
| 60 |   nodes(detail::getNodesFromAtomIds(index)),
 | 
|---|
| 61 |   edges(detail::getEdgesFromAtomIds(index))
 | 
|---|
| 62 | {}
 | 
|---|
| 63 | 
 | 
|---|
| 64 | bool HomologyGraph::operator<(const HomologyGraph &graph) const
 | 
|---|
| 65 | {
 | 
|---|
| 66 |   if (nodes < graph.nodes) {
 | 
|---|
| 67 |     return true;
 | 
|---|
| 68 |   } else if (nodes > graph.nodes) {
 | 
|---|
| 69 |     return false;
 | 
|---|
| 70 |   } else {
 | 
|---|
| 71 |     if (edges < graph.edges)
 | 
|---|
| 72 |       return true;
 | 
|---|
| 73 |     else
 | 
|---|
| 74 |       return false;
 | 
|---|
| 75 |   }
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | bool HomologyGraph::operator>(const HomologyGraph &graph) const
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   if (nodes > graph.nodes) {
 | 
|---|
| 81 |     return true;
 | 
|---|
| 82 |   } else if (nodes < graph.nodes) {
 | 
|---|
| 83 |     return false;
 | 
|---|
| 84 |   } else {
 | 
|---|
| 85 |     if (edges > graph.edges)
 | 
|---|
| 86 |       return true;
 | 
|---|
| 87 |     else
 | 
|---|
| 88 |       return false;
 | 
|---|
| 89 |   }
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | bool HomologyGraph::operator==(const HomologyGraph &graph) const
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   if (nodes != graph.nodes) {
 | 
|---|
| 95 |     return false;
 | 
|---|
| 96 |   } else {
 | 
|---|
| 97 |     return (edges == graph.edges);
 | 
|---|
| 98 |   }
 | 
|---|
| 99 | }
 | 
|---|
| 100 | 
 | 
|---|
| 101 | HomologyGraph& HomologyGraph::operator=(const HomologyGraph &graph)
 | 
|---|
| 102 | {
 | 
|---|
| 103 |   // self-assignment check
 | 
|---|
| 104 |   if (this != &graph) {
 | 
|---|
| 105 |     const_cast<nodes_t &>(nodes) = graph.nodes;
 | 
|---|
| 106 |     const_cast<edges_t &>(edges) = graph.edges;
 | 
|---|
| 107 |   }
 | 
|---|
| 108 |   return *this;
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | bool HomologyGraph::hasTimesAtomicNumber(const size_t _number, const size_t _times) const
 | 
|---|
| 112 | {
 | 
|---|
| 113 |   size_t count = 0;
 | 
|---|
| 114 |   for (nodes_t::const_iterator iter = nodes.begin(); iter != nodes.end(); ++iter) {
 | 
|---|
| 115 |     if ((iter->first).getAtomicNumber() == _number)
 | 
|---|
| 116 |       count += iter->second;
 | 
|---|
| 117 |   }
 | 
|---|
| 118 |   return (count == _times);
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | bool HomologyGraph::hasGreaterEqualTimesAtomicNumber(const size_t _number, const size_t _times) const
 | 
|---|
| 122 | {
 | 
|---|
| 123 |   size_t count = 0;
 | 
|---|
| 124 |   for (nodes_t::const_iterator iter = nodes.begin(); iter != nodes.end(); ++iter) {
 | 
|---|
| 125 |     if ((iter->first).getAtomicNumber() == _number)
 | 
|---|
| 126 |       count += iter->second;
 | 
|---|
| 127 |   }
 | 
|---|
| 128 |   return (count >= _times);
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | void HomologyGraph::printNodes(std::ostream& ost) const
 | 
|---|
| 132 | {
 | 
|---|
| 133 |   for (nodes_t::const_iterator nodeiter = nodes.begin();
 | 
|---|
| 134 |       nodeiter != nodes.end();
 | 
|---|
| 135 |       ++nodeiter) {
 | 
|---|
| 136 |     if ( nodeiter != nodes.begin())
 | 
|---|
| 137 |       ost << ", ";
 | 
|---|
| 138 |     ost << nodeiter->second << "x " << nodeiter->first;
 | 
|---|
| 139 |   }
 | 
|---|
| 140 | }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | void HomologyGraph::printEdges(std::ostream& ost) const
 | 
|---|
| 143 | {
 | 
|---|
| 144 |   for (edges_t::const_iterator edgeiter = edges.begin();
 | 
|---|
| 145 |       edgeiter != edges.end();
 | 
|---|
| 146 |       ++edgeiter) {
 | 
|---|
| 147 |     if ( edgeiter != edges.begin())
 | 
|---|
| 148 |       ost << ", ";
 | 
|---|
| 149 |     ost << edgeiter->second << "x " << edgeiter->first;
 | 
|---|
| 150 |   }
 | 
|---|
| 151 | }
 | 
|---|
| 152 | 
 | 
|---|
| 153 | std::ostream& operator<<(std::ostream& ost, const HomologyGraph &graph)
 | 
|---|
| 154 | {
 | 
|---|
| 155 |   graph.printNodes(ost);
 | 
|---|
| 156 |   graph.printEdges(ost);
 | 
|---|
| 157 |   return ost;
 | 
|---|
| 158 | }
 | 
|---|
| 159 | 
 | 
|---|
| 160 | //
 | 
|---|
| 161 | //// we need to explicitly instantiate the serialization functions
 | 
|---|
| 162 | //BOOST_CLASS_EXPORT_IMPLEMENT(HomologyGraph)
 | 
|---|