| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. 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 |  * AtomIdSet.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Feb 21, 2012
 | 
|---|
| 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 "AtomIdSet.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include <boost/foreach.hpp>
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "Atom/atom.hpp"
 | 
|---|
| 43 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 44 | #include "Fragmentation/KeySet.hpp"
 | 
|---|
| 45 | #include "World.hpp"
 | 
|---|
| 46 | 
 | 
|---|
| 47 | atom * FromIdToAtom::operator()(atomId_t id) const {
 | 
|---|
| 48 |   return World::getInstance().getAtom(AtomById(id));
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | /** Constructor for class AtomIdSet.
 | 
|---|
| 52 |  *
 | 
|---|
| 53 |  * @param _atoms atoms to put into this set
 | 
|---|
| 54 |  */
 | 
|---|
| 55 | AtomIdSet::AtomIdSet(const atomIdSet &_atoms) :
 | 
|---|
| 56 |     atoms(_atoms)
 | 
|---|
| 57 | {}
 | 
|---|
| 58 | 
 | 
|---|
| 59 | /** Constructor for class AtomIdSet.
 | 
|---|
| 60 |  *
 | 
|---|
| 61 |  * @param _keyset keyset to put into this set
 | 
|---|
| 62 |  */
 | 
|---|
| 63 | AtomIdSet::AtomIdSet(const KeySet &_keyset) :
 | 
|---|
| 64 |   atoms(atomIdSet(_keyset.begin(), _keyset.end()))
 | 
|---|
| 65 | {}
 | 
|---|
| 66 | 
 | 
|---|
| 67 | /** Constructor for class AtomIdSet.
 | 
|---|
| 68 |  *
 | 
|---|
| 69 |  * @param _atoms atoms to put into this set
 | 
|---|
| 70 |  */
 | 
|---|
| 71 | AtomIdSet::AtomIdSet(const std::vector<atom *> &_atoms)
 | 
|---|
| 72 | {
 | 
|---|
| 73 |   BOOST_FOREACH(const atom * _atom, _atoms) {
 | 
|---|
| 74 |     insert(_atom->getId());
 | 
|---|
| 75 |   }
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | /** Constructor for class AtomIdSet.
 | 
|---|
| 79 |  *
 | 
|---|
| 80 |  */
 | 
|---|
| 81 | AtomIdSet::AtomIdSet()
 | 
|---|
| 82 | {}
 | 
|---|
| 83 | 
 | 
|---|
| 84 | /** Destructor for class AtomIdSet.
 | 
|---|
| 85 |  *
 | 
|---|
| 86 |  */
 | 
|---|
| 87 | AtomIdSet::~AtomIdSet()
 | 
|---|
| 88 | {}
 | 
|---|
| 89 | 
 | 
|---|
| 90 | /** Returns iterator to first atim via a transform_iterator.
 | 
|---|
| 91 |  *
 | 
|---|
| 92 |  * @return iterator to first atom in this set
 | 
|---|
| 93 |  */
 | 
|---|
| 94 | AtomIdSet::iterator AtomIdSet::begin(){
 | 
|---|
| 95 |   return iterator(atoms.begin(), FromIdToAtom());
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | /** Returns constant iterator to first atim via a transform_iterator.
 | 
|---|
| 99 |  *
 | 
|---|
| 100 |  * @return const iterator to first atom in this set
 | 
|---|
| 101 |  */
 | 
|---|
| 102 | AtomIdSet::const_iterator AtomIdSet::begin() const{
 | 
|---|
| 103 |   return const_iterator(atoms.begin(), FromIdToAtom());
 | 
|---|
| 104 | }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | /** Returns iterator to one beyond last atom via a transform_iterator.
 | 
|---|
| 107 |  *
 | 
|---|
| 108 |  * @return iterator to one beyond last atom in this set
 | 
|---|
| 109 |  */
 | 
|---|
| 110 | AtomIdSet::iterator AtomIdSet::end(){
 | 
|---|
| 111 |   return iterator(atoms.end(), FromIdToAtom());
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | /** Returns constant iterator to one beyond last atom via a transform_iterator.
 | 
|---|
| 115 |  *
 | 
|---|
| 116 |  * @return const iterator to one beyond last atom in this set
 | 
|---|
| 117 |  */
 | 
|---|
| 118 | AtomIdSet::const_iterator AtomIdSet::end() const{
 | 
|---|
| 119 |   return const_iterator(atoms.end(), FromIdToAtom());
 | 
|---|
| 120 | }
 | 
|---|
| 121 | 
 | 
|---|
| 122 | /** Returns true if this set is empty.
 | 
|---|
| 123 |  *
 | 
|---|
| 124 |  * @return true - set is empty, false - there is at least one atom
 | 
|---|
| 125 |  */
 | 
|---|
| 126 | bool AtomIdSet::empty() const
 | 
|---|
| 127 | {
 | 
|---|
| 128 |   return (atoms.empty());
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | /** Returns the number of members of this set.
 | 
|---|
| 132 |  *
 | 
|---|
| 133 |  * @return number of members in set
 | 
|---|
| 134 |  */
 | 
|---|
| 135 | size_t AtomIdSet::size() const
 | 
|---|
| 136 | {
 | 
|---|
| 137 |   // set has unique members, hence just return its size
 | 
|---|
| 138 |   return atoms.size();
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | /** Predicate whether given atomic id is contained.
 | 
|---|
| 142 |  *
 | 
|---|
| 143 |  * @param id id to check
 | 
|---|
| 144 |  * @return true - is contained, false - is not
 | 
|---|
| 145 |  */
 | 
|---|
| 146 | bool AtomIdSet::contains(const atomId_t &id) const
 | 
|---|
| 147 | {
 | 
|---|
| 148 |   return (atoms.find(id) != atoms.end());
 | 
|---|
| 149 | }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | /** Predicate whether given atom is contained.
 | 
|---|
| 152 |  *
 | 
|---|
| 153 |  * @param key atom to check
 | 
|---|
| 154 |  * @return true - is contained, false - is not
 | 
|---|
| 155 |  */
 | 
|---|
| 156 | bool AtomIdSet::contains(const atom * const key) const
 | 
|---|
| 157 | {
 | 
|---|
| 158 |   return contains(key->getId());
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | /** Returns the iterator to the atom \a *key.
 | 
|---|
| 162 |  *
 | 
|---|
| 163 |  * @param key atom to find
 | 
|---|
| 164 |  * @return iterator to atom if found, to end() - else
 | 
|---|
| 165 |  */
 | 
|---|
| 166 | AtomIdSet::const_iterator AtomIdSet::find(const atom * const key) const
 | 
|---|
| 167 | {
 | 
|---|
| 168 |   return find(key->getId());
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | /** Returns the iterator to the atom \a *key.
 | 
|---|
| 172 |  *
 | 
|---|
| 173 |  * @param id atomic id to find
 | 
|---|
| 174 |  * @return iterator to atom if found, to end() - else
 | 
|---|
| 175 |  */
 | 
|---|
| 176 | AtomIdSet::const_iterator AtomIdSet::find(const atomId_t &id) const
 | 
|---|
| 177 | {
 | 
|---|
| 178 |   return const_iterator(atoms.find(id), FromIdToAtom());
 | 
|---|
| 179 | }
 | 
|---|
| 180 | 
 | 
|---|
| 181 | /** Inserts a given atom into the set.
 | 
|---|
| 182 |  *
 | 
|---|
| 183 |  * @param key atom to insert
 | 
|---|
| 184 |  * @return pair of iterator and bool that states whether element is already present (true) or not (false)
 | 
|---|
| 185 |  */
 | 
|---|
| 186 | std::pair<AtomIdSet::iterator, bool> AtomIdSet::insert(const atom * const key)
 | 
|---|
| 187 | {
 | 
|---|
| 188 |   std::pair<atomIdSet::iterator, bool> iter =
 | 
|---|
| 189 |       atoms.insert(key->getId());
 | 
|---|
| 190 |   std::pair<iterator, bool> retiter (std::make_pair(iterator(iter.first), iter.second));
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   return retiter;
 | 
|---|
| 193 | }
 | 
|---|
| 194 | 
 | 
|---|
| 195 | /** Inserts a given atom into the set.
 | 
|---|
| 196 |  *
 | 
|---|
| 197 |  * @param id atomic id to insert
 | 
|---|
| 198 |  * @return pair of iterator and bool that states whether element is already present (true) or not (false)
 | 
|---|
| 199 |  */
 | 
|---|
| 200 | std::pair<AtomIdSet::iterator, bool> AtomIdSet::insert(const atomId_t &id)
 | 
|---|
| 201 | {
 | 
|---|
| 202 |   std::pair<atomIdSet::iterator, bool> iter =
 | 
|---|
| 203 |       atoms.insert(id);
 | 
|---|
| 204 |   std::pair<iterator, bool> retiter (std::make_pair(iterator(iter.first), iter.second));
 | 
|---|
| 205 | 
 | 
|---|
| 206 |   return retiter;
 | 
|---|
| 207 | }
 | 
|---|
| 208 | 
 | 
|---|
| 209 | AtomIdSet::const_iterator AtomIdSet::erase(AtomIdSet::const_iterator &loc)
 | 
|---|
| 210 | {
 | 
|---|
| 211 |   const_iterator iter = loc;
 | 
|---|
| 212 |   ++iter;
 | 
|---|
| 213 |   atom * const _atom = const_cast<atom *>(*loc);
 | 
|---|
| 214 |   atoms.erase( _atom->getId() );
 | 
|---|
| 215 |   return iter;
 | 
|---|
| 216 | }
 | 
|---|
| 217 | 
 | 
|---|
| 218 | /** Erase an atom from the list.
 | 
|---|
| 219 |  *
 | 
|---|
| 220 |  * @param *key key to atom in list
 | 
|---|
| 221 |  * @return iterator to just after removed item (compliant with standard)
 | 
|---|
| 222 |  */
 | 
|---|
| 223 | AtomIdSet::const_iterator AtomIdSet::erase(const atom * const key)
 | 
|---|
| 224 | {
 | 
|---|
| 225 |   const_iterator iter = find(key);
 | 
|---|
| 226 |   if (iter != end()){
 | 
|---|
| 227 |     ++iter;
 | 
|---|
| 228 |     atoms.erase( key->getId() );
 | 
|---|
| 229 |   }
 | 
|---|
| 230 |   return iter;
 | 
|---|
| 231 | }
 | 
|---|
| 232 | 
 | 
|---|
| 233 | /** Erase an atom from the list.
 | 
|---|
| 234 |  *
 | 
|---|
| 235 |  * @param id atomic id atom in list to erase
 | 
|---|
| 236 |  * @return iterator to just after removed item (compliant with standard)
 | 
|---|
| 237 |  */
 | 
|---|
| 238 | AtomIdSet::const_iterator AtomIdSet::erase(const atomId_t &id)
 | 
|---|
| 239 | {
 | 
|---|
| 240 |   atomIdSet::const_iterator iter = atoms.find(id);
 | 
|---|
| 241 |   if (iter != atoms.end()){
 | 
|---|
| 242 |     ++iter;
 | 
|---|
| 243 |     atoms.erase( id );
 | 
|---|
| 244 |   }
 | 
|---|
| 245 |   return iterator(iter, FromIdToAtom());
 | 
|---|
| 246 | }
 | 
|---|