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