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