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