| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2016 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 |  * AtomFragmentsMap.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Mar 7, 2016
 | 
|---|
| 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/Log.hpp"
 | 
|---|
| 39 | #include "CodePatterns/Singleton_impl.hpp"
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include "AtomFragmentsMap.hpp"
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "AtomIdSet.hpp"
 | 
|---|
| 44 | #include "Fragmentation/Graph.hpp"
 | 
|---|
| 45 | #include "Fragmentation/KeySet.hpp"
 | 
|---|
| 46 | 
 | 
|---|
| 47 | void AtomFragmentsMap::insert(
 | 
|---|
| 48 |     const Graph &_graph)
 | 
|---|
| 49 | {
 | 
|---|
| 50 |   /// create a map of atom to keyset (below equal MaxOrder)
 | 
|---|
| 51 |   LOG(1, "INFO: Placing all atoms and their keysets into internal map.");
 | 
|---|
| 52 |   for (Graph::const_iterator keysetiter = _graph.begin();
 | 
|---|
| 53 |       keysetiter != _graph.end(); ++keysetiter) {
 | 
|---|
| 54 |     const KeySet &keyset = keysetiter->first;
 | 
|---|
| 55 |     LOG(2, "DEBUG: Current keyset is " << keyset);
 | 
|---|
| 56 |     if (keyset.empty())
 | 
|---|
| 57 |       continue;
 | 
|---|
| 58 |     for (KeySet::const_iterator keyiter = keyset.begin();
 | 
|---|
| 59 |         keyiter != keyset.end(); ++keyiter) {
 | 
|---|
| 60 |       // either create new list ...
 | 
|---|
| 61 |       std::pair<AtomFragmentsMap_t::iterator, bool> inserter =
 | 
|---|
| 62 |           atommap.insert( std::make_pair(*keyiter, keysets_t(1, keyset) ));
 | 
|---|
| 63 |       // ... or push to end
 | 
|---|
| 64 |       if (inserter.second) {
 | 
|---|
| 65 |         LOG(3, "DEBUG: Created new entry in map.");
 | 
|---|
| 66 |       } else {
 | 
|---|
| 67 |         LOG(3, "DEBUG: Added keyset to present entry.");
 | 
|---|
| 68 |         inserter.first->second.push_back(keyset);
 | 
|---|
| 69 |       }
 | 
|---|
| 70 |     }
 | 
|---|
| 71 |     // add empty entry to fullkeysets, too (if already present, we ignore that)
 | 
|---|
| 72 |     fullkeysets.insert( std::make_pair(keyset, indices_t()) );
 | 
|---|
| 73 |   }
 | 
|---|
| 74 |   LOG(2, "DEBUG: There are now " << atommap.size() << " entries in lookup.");
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | AtomFragmentsMap::AtomFragmentsMap_t AtomFragmentsMap::getMap(
 | 
|---|
| 78 |     const std::vector<atomId_t> &_candidates,
 | 
|---|
| 79 |     size_t _MaxOrder) const
 | 
|---|
| 80 | {
 | 
|---|
| 81 |   typedef std::vector<atomId_t> candidates_t;
 | 
|---|
| 82 |   AtomFragmentsMap_t fragmentmap;
 | 
|---|
| 83 |   for (candidates_t::const_iterator candidateiter = _candidates.begin();
 | 
|---|
| 84 |       candidateiter != _candidates.end(); ++candidateiter) {
 | 
|---|
| 85 |     const atomId_t atomid = *candidateiter;
 | 
|---|
| 86 |     const AtomFragmentsMap_t::const_iterator iter = atommap.find(atomid);
 | 
|---|
| 87 |     ASSERT( iter != atommap.end(),
 | 
|---|
| 88 |         "AtomFragmentsMap::getMap() - could not find atom "
 | 
|---|
| 89 |         +toString(atomid)+" in lookup.");
 | 
|---|
| 90 |     // due to MaxOrder we need to copy selectively and hence look at each KeySet in turn
 | 
|---|
| 91 |     const keysets_t &keysets = iter->second;
 | 
|---|
| 92 |     for (keysets_t::const_iterator keyiter = keysets.begin();
 | 
|---|
| 93 |         keyiter != keysets.end(); ++keyiter) {
 | 
|---|
| 94 |       const KeySet &keyset = *keyiter;
 | 
|---|
| 95 |       if ((keyset.size() > _MaxOrder) || (keyset.empty()))
 | 
|---|
| 96 |         continue;
 | 
|---|
| 97 |       std::pair<AtomFragmentsMap_t::iterator, bool> inserter =
 | 
|---|
| 98 |           fragmentmap.insert( std::make_pair(atomid, keysets_t(1, keyset) ));
 | 
|---|
| 99 |       // ... or push to end
 | 
|---|
| 100 |       if (inserter.second) {
 | 
|---|
| 101 |         LOG(3, "DEBUG: Created new entry in lookup map.");
 | 
|---|
| 102 |       } else {
 | 
|---|
| 103 |         LOG(3, "DEBUG: Added keyset to present entry in lookup map.");
 | 
|---|
| 104 |         inserter.first->second.push_back(keyset);
 | 
|---|
| 105 |       }
 | 
|---|
| 106 |     }
 | 
|---|
| 107 |   }
 | 
|---|
| 108 |   LOG(4, "DEBUG: Copied part of lookup map contains " << fragmentmap.size() << " keys.");
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   return fragmentmap;
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | bool AtomFragmentsMap::addFullKeyset(const KeySet &_keyset, const indices_t &_fullkeyset)
 | 
|---|
| 114 | {
 | 
|---|
| 115 |   FragmentFullKeysetMap_t::iterator iter = fullkeysets.find(_keyset);
 | 
|---|
| 116 |   if ((iter == fullkeysets.end()) || (!iter->second.empty()))
 | 
|---|
| 117 |     return false;
 | 
|---|
| 118 |   else {
 | 
|---|
| 119 |     iter->second = _fullkeyset;
 | 
|---|
| 120 |   }
 | 
|---|
| 121 |   return true;
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | const AtomFragmentsMap::indices_t &
 | 
|---|
| 125 | AtomFragmentsMap::getFullKeyset(const KeySet &_keyset) const
 | 
|---|
| 126 | {
 | 
|---|
| 127 |   static indices_t emptyset;
 | 
|---|
| 128 |   FragmentFullKeysetMap_t::const_iterator iter = fullkeysets.find(_keyset);
 | 
|---|
| 129 |   if (iter == fullkeysets.end())
 | 
|---|
| 130 |     return emptyset;
 | 
|---|
| 131 |   else
 | 
|---|
| 132 |     return iter->second;
 | 
|---|
| 133 | }
 | 
|---|
| 134 | 
 | 
|---|
| 135 | bool AtomFragmentsMap::checkCompleteness() const
 | 
|---|
| 136 | {
 | 
|---|
| 137 |   bool status = true;
 | 
|---|
| 138 |   for (FragmentFullKeysetMap_t::const_iterator iter = fullkeysets.begin();
 | 
|---|
| 139 |       iter != fullkeysets.end(); ++iter)
 | 
|---|
| 140 |     status &= (iter->second.size() != 0);
 | 
|---|
| 141 | 
 | 
|---|
| 142 |   return status;
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | void AtomFragmentsMap::clear()
 | 
|---|
| 146 | {
 | 
|---|
| 147 |   atommap.clear();
 | 
|---|
| 148 |   fullkeysets.clear();
 | 
|---|
| 149 | }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | CONSTRUCT_SINGLETON(AtomFragmentsMap)
 | 
|---|
| 152 | 
 | 
|---|
| 153 | 
 | 
|---|
| 154 | // we need to explicitly instantiate the serialization functions
 | 
|---|
| 155 | BOOST_CLASS_EXPORT_IMPLEMENT(AtomFragmentsMap)
 | 
|---|
| 156 | 
 | 
|---|