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 | /** Returns the iterator to the atom \a *key.
|
---|
118 | *
|
---|
119 | * @param key atom to find
|
---|
120 | * @return iterator to atom if found, to end() - else
|
---|
121 | */
|
---|
122 | AtomIdSet::const_iterator AtomIdSet::find(const atom * const key) const
|
---|
123 | {
|
---|
124 | return const_iterator(atoms.find(key->getId()), FromIdToAtom());
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Returns the iterator to the atom \a *key.
|
---|
128 | *
|
---|
129 | * @param id atomic id to find
|
---|
130 | * @return iterator to atom if found, to end() - else
|
---|
131 | */
|
---|
132 | AtomIdSet::const_iterator AtomIdSet::find(const atomId_t &id) const
|
---|
133 | {
|
---|
134 | return const_iterator(atoms.find(id), FromIdToAtom());
|
---|
135 | }
|
---|
136 |
|
---|
137 | /** Inserts a given atom into the set.
|
---|
138 | *
|
---|
139 | * @param key atom to insert
|
---|
140 | * @return pair of iterator and bool that states whether element is already present (true) or not (false)
|
---|
141 | */
|
---|
142 | std::pair<AtomIdSet::iterator, bool> AtomIdSet::insert(const atom * const key)
|
---|
143 | {
|
---|
144 | std::pair<atomIdSet::iterator, bool> iter =
|
---|
145 | atoms.insert(key->getId());
|
---|
146 | std::pair<iterator, bool> retiter (std::make_pair(iterator(iter.first), iter.second));
|
---|
147 |
|
---|
148 | return retiter;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** Inserts a given atom into the set.
|
---|
152 | *
|
---|
153 | * @param id atomic id to insert
|
---|
154 | * @return pair of iterator and bool that states whether element is already present (true) or not (false)
|
---|
155 | */
|
---|
156 | std::pair<AtomIdSet::iterator, bool> AtomIdSet::insert(const atomId_t &id)
|
---|
157 | {
|
---|
158 | std::pair<atomIdSet::iterator, bool> iter =
|
---|
159 | atoms.insert(id);
|
---|
160 | std::pair<iterator, bool> retiter (std::make_pair(iterator(iter.first), iter.second));
|
---|
161 |
|
---|
162 | return retiter;
|
---|
163 | }
|
---|
164 |
|
---|
165 | AtomIdSet::const_iterator AtomIdSet::erase(AtomIdSet::const_iterator &loc)
|
---|
166 | {
|
---|
167 | const_iterator iter = loc;
|
---|
168 | ++iter;
|
---|
169 | atom * const _atom = const_cast<atom *>(*loc);
|
---|
170 | atoms.erase( _atom->getId() );
|
---|
171 | return iter;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** Erase an atom from the list.
|
---|
175 | *
|
---|
176 | * @param *key key to atom in list
|
---|
177 | * @return iterator to just after removed item (compliant with standard)
|
---|
178 | */
|
---|
179 | AtomIdSet::const_iterator AtomIdSet::erase(const atom * const key)
|
---|
180 | {
|
---|
181 | const_iterator iter = find(key);
|
---|
182 | if (iter != end()){
|
---|
183 | ++iter;
|
---|
184 | atoms.erase( key->getId() );
|
---|
185 | }
|
---|
186 | return iter;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Erase an atom from the list.
|
---|
190 | *
|
---|
191 | * @param id atomic id atom in list to erase
|
---|
192 | * @return iterator to just after removed item (compliant with standard)
|
---|
193 | */
|
---|
194 | AtomIdSet::const_iterator AtomIdSet::erase(const atomId_t &id)
|
---|
195 | {
|
---|
196 | atomIdSet::const_iterator iter = atoms.find(id);
|
---|
197 | if (iter != atoms.end()){
|
---|
198 | ++iter;
|
---|
199 | atoms.erase( id );
|
---|
200 | }
|
---|
201 | return iterator(iter, FromIdToAtom());
|
---|
202 | }
|
---|