| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-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/>. | 
|---|
| [bcf653] | 21 | */ | 
|---|
|  | 22 |  | 
|---|
| [fc1b24] | 23 | /* | 
|---|
|  | 24 | * AtomDescriptor.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Feb 5, 2010 | 
|---|
|  | 27 | *      Author: crueger | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
| [bf3817] | 30 | // include config.h | 
|---|
|  | 31 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 32 | #include <config.h> | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 36 |  | 
|---|
| [7a1ce5] | 37 | #include "Descriptors/AtomDescriptor.hpp" | 
|---|
|  | 38 | #include "Descriptors/AtomDescriptor_impl.hpp" | 
|---|
| [fc1b24] | 39 |  | 
|---|
|  | 40 | #include "World.hpp" | 
|---|
| [6f0841] | 41 | #include "Atom/atom.hpp" | 
|---|
| [02ce36] | 42 | #include "CodePatterns/Observer/ObservedContainer_impl.hpp" | 
|---|
| [7a1ce5] | 43 |  | 
|---|
| [fc1b24] | 44 | #include <boost/bind.hpp> | 
|---|
| [6d574a] | 45 |  | 
|---|
| [7a1ce5] | 46 | #include <iostream> | 
|---|
| [fc1b24] | 47 |  | 
|---|
|  | 48 | using namespace std; | 
|---|
|  | 49 |  | 
|---|
| [4d7462] | 50 | typedef World::AtomSet::internal_iterator atoms_iter_t; | 
|---|
| [795c0f] | 51 | typedef World::AtomSet::const_iterator atoms_const_iter_t; | 
|---|
| [fc1b24] | 52 |  | 
|---|
| [7a1ce5] | 53 | /************************ Forwarding object **************************************/ | 
|---|
|  | 54 |  | 
|---|
|  | 55 |  | 
|---|
|  | 56 | AtomDescriptor::AtomDescriptor(impl_ptr _impl) : | 
|---|
|  | 57 | impl(_impl) | 
|---|
|  | 58 | {} | 
|---|
|  | 59 |  | 
|---|
|  | 60 | AtomDescriptor::AtomDescriptor(const AtomDescriptor& src) : | 
|---|
|  | 61 | impl(src.get_impl()) | 
|---|
|  | 62 | {} | 
|---|
|  | 63 |  | 
|---|
|  | 64 | AtomDescriptor::~AtomDescriptor() | 
|---|
|  | 65 | {} | 
|---|
|  | 66 |  | 
|---|
|  | 67 | AtomDescriptor& AtomDescriptor::operator=(AtomDescriptor &src){ | 
|---|
|  | 68 | if(&src!=this) { | 
|---|
|  | 69 | impl=src.get_impl(); | 
|---|
|  | 70 | } | 
|---|
|  | 71 | return *this; | 
|---|
| [fc1b24] | 72 | } | 
|---|
|  | 73 |  | 
|---|
| [7a1ce5] | 74 | atom* AtomDescriptor::find(){ | 
|---|
|  | 75 | return impl->find(); | 
|---|
| [d1c5e2] | 76 | } | 
|---|
|  | 77 |  | 
|---|
| [795c0f] | 78 | const atom* AtomDescriptor::find() const { | 
|---|
|  | 79 | return const_cast<const impl_t &>(*impl).find(); | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
| [7a1ce5] | 82 | std::vector<atom*> AtomDescriptor::findAll(){ | 
|---|
|  | 83 | return impl->findAll(); | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
| [795c0f] | 86 | std::vector<const atom*> AtomDescriptor::findAll() const { | 
|---|
|  | 87 | return const_cast<const impl_t &>(*impl).findAll(); | 
|---|
|  | 88 | } | 
|---|
|  | 89 |  | 
|---|
| [7a1ce5] | 90 | AtomDescriptor::impl_ptr AtomDescriptor::get_impl() const{ | 
|---|
|  | 91 | return impl; | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 |  | 
|---|
|  | 95 |  | 
|---|
|  | 96 |  | 
|---|
|  | 97 | /**************************** implementation ********************/ | 
|---|
|  | 98 |  | 
|---|
|  | 99 | AtomDescriptor_impl::AtomDescriptor_impl() | 
|---|
| [fc1b24] | 100 | { | 
|---|
|  | 101 | } | 
|---|
|  | 102 |  | 
|---|
| [7a1ce5] | 103 | AtomDescriptor_impl::~AtomDescriptor_impl() | 
|---|
|  | 104 | { | 
|---|
|  | 105 | } | 
|---|
| [fc1b24] | 106 |  | 
|---|
| [7042f45] | 107 | World::AtomSet& AtomDescriptor_impl::getAtoms(){ | 
|---|
| [23b547] | 108 | return World::getInstance().atoms; | 
|---|
| [fc1b24] | 109 | } | 
|---|
|  | 110 |  | 
|---|
| [795c0f] | 111 | const World::AtomSet& AtomDescriptor_impl::getAtoms() const { | 
|---|
|  | 112 | return const_cast<const World &>(World::getInstance()).atoms; | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
| [7a1ce5] | 115 | atom* AtomDescriptor_impl::find() { | 
|---|
| [4d7462] | 116 | World::AtomSet &atoms = getAtoms(); | 
|---|
|  | 117 | atoms_iter_t res = find_if(atoms.begin_internal(),atoms.end_internal(),boost::bind(&AtomDescriptor_impl::predicate,this,_1)); | 
|---|
|  | 118 | return (res!=atoms.end_internal())?((*res).second):0; | 
|---|
| [fc1b24] | 119 | } | 
|---|
|  | 120 |  | 
|---|
| [795c0f] | 121 | const atom* AtomDescriptor_impl::find() const { | 
|---|
|  | 122 | const World::AtomSet &atoms = getAtoms(); | 
|---|
|  | 123 | atoms_const_iter_t res = find_if(atoms.begin(),atoms.end(),boost::bind(&AtomDescriptor_impl::predicate,this,_1)); | 
|---|
|  | 124 | return (res!=atoms.end())?((*res).second):0; | 
|---|
|  | 125 | } | 
|---|
|  | 126 |  | 
|---|
| [7a1ce5] | 127 | vector<atom*> AtomDescriptor_impl::findAll() { | 
|---|
| [fc1b24] | 128 | vector<atom*> res; | 
|---|
| [7042f45] | 129 | World::AtomSet atoms = getAtoms(); | 
|---|
| [8cce2b] | 130 | for_each(atoms.begin_internal(), | 
|---|
|  | 131 | atoms.end_internal(), | 
|---|
| [795c0f] | 132 | boost::bind(static_cast<void (AtomDescriptor_impl::*)( | 
|---|
|  | 133 | std::vector<atom*> *, | 
|---|
|  | 134 | std::pair<atomId_t,atom*>)>(&AtomDescriptor_impl::checkAndAdd), | 
|---|
|  | 135 | this,boost::cref(&res),_1)); | 
|---|
|  | 136 | return res; | 
|---|
|  | 137 | } | 
|---|
|  | 138 |  | 
|---|
|  | 139 | vector<const atom*> AtomDescriptor_impl::findAll() const { | 
|---|
|  | 140 | vector<const atom*> res; | 
|---|
|  | 141 | const World::AtomSet &atoms = getAtoms(); | 
|---|
|  | 142 | for_each(atoms.begin(), | 
|---|
|  | 143 | atoms.end(), | 
|---|
|  | 144 | boost::bind(static_cast<void (AtomDescriptor_impl::*)( | 
|---|
|  | 145 | std::vector<const atom*> *, | 
|---|
|  | 146 | std::pair<atomId_t,const atom*>) const>(&AtomDescriptor_impl::checkAndAdd), | 
|---|
|  | 147 | boost::cref(this),&res,_1)); | 
|---|
| [7a1ce5] | 148 | return res; | 
|---|
| [fc1b24] | 149 | } | 
|---|
| [d1c5e2] | 150 |  | 
|---|
| [8cce2b] | 151 | void AtomDescriptor_impl::checkAndAdd(std::vector<atom*> *v,std::pair<atomId_t,atom*> p){ | 
|---|
|  | 152 | if(predicate(p)){ | 
|---|
|  | 153 | v->push_back(p.second); | 
|---|
|  | 154 | } | 
|---|
|  | 155 | } | 
|---|
|  | 156 |  | 
|---|
| [795c0f] | 157 | void AtomDescriptor_impl::checkAndAdd(std::vector<const atom*> *v,std::pair<atomId_t,const atom*> p) const{ | 
|---|
|  | 158 | if(predicate(p)){ | 
|---|
|  | 159 | v->push_back(p.second); | 
|---|
|  | 160 | } | 
|---|
|  | 161 | } | 
|---|
|  | 162 |  | 
|---|
| [7a1ce5] | 163 | /************************** Universe and Emptyset *****************/ | 
|---|
| [d1c5e2] | 164 |  | 
|---|
| [7a1ce5] | 165 | AtomAllDescriptor_impl::AtomAllDescriptor_impl() | 
|---|
|  | 166 | {} | 
|---|
| [d1c5e2] | 167 |  | 
|---|
| [7a1ce5] | 168 | AtomAllDescriptor_impl::~AtomAllDescriptor_impl() | 
|---|
|  | 169 | {} | 
|---|
| [d1c5e2] | 170 |  | 
|---|
| [795c0f] | 171 | bool AtomAllDescriptor_impl::predicate(std::pair<atomId_t,const atom*>) const{ | 
|---|
| [7a1ce5] | 172 | return true; | 
|---|
| [d1c5e2] | 173 | } | 
|---|
|  | 174 |  | 
|---|
| [7a1ce5] | 175 | AtomDescriptor AllAtoms(){ | 
|---|
|  | 176 | return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomAllDescriptor_impl)); | 
|---|
| [d1c5e2] | 177 | } | 
|---|
|  | 178 |  | 
|---|
| [7a1ce5] | 179 | AtomNoneDescriptor_impl::AtomNoneDescriptor_impl() | 
|---|
|  | 180 | {} | 
|---|
|  | 181 |  | 
|---|
|  | 182 | AtomNoneDescriptor_impl::~AtomNoneDescriptor_impl() | 
|---|
|  | 183 | {} | 
|---|
|  | 184 |  | 
|---|
| [795c0f] | 185 | bool AtomNoneDescriptor_impl::predicate(std::pair<atomId_t,const atom*>) const{ | 
|---|
| [7a1ce5] | 186 | return false; | 
|---|
| [d1c5e2] | 187 | } | 
|---|
|  | 188 |  | 
|---|
| [7a1ce5] | 189 | AtomDescriptor NoAtoms(){ | 
|---|
|  | 190 | return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomNoneDescriptor_impl)); | 
|---|
| [d1c5e2] | 191 | } | 
|---|
|  | 192 |  | 
|---|
| [7a1ce5] | 193 | /************************** Operator stuff ************************/ | 
|---|
| [d1c5e2] | 194 |  | 
|---|
| [7a1ce5] | 195 | // AND | 
|---|
|  | 196 | AtomAndDescriptor_impl::AtomAndDescriptor_impl(AtomDescriptor::impl_ptr _lhs, AtomDescriptor::impl_ptr _rhs) : | 
|---|
|  | 197 | lhs(_lhs), rhs(_rhs) | 
|---|
|  | 198 | {} | 
|---|
| [d1c5e2] | 199 |  | 
|---|
| [7a1ce5] | 200 | AtomAndDescriptor_impl::~AtomAndDescriptor_impl() | 
|---|
|  | 201 | {} | 
|---|
| [d1c5e2] | 202 |  | 
|---|
| [795c0f] | 203 | bool AtomAndDescriptor_impl::predicate(std::pair<atomId_t,const atom*> atom) const{ | 
|---|
| [7a1ce5] | 204 | return lhs->predicate(atom) && rhs->predicate(atom); | 
|---|
|  | 205 | } | 
|---|
|  | 206 | AtomDescriptor operator&&(const AtomDescriptor &lhs, const AtomDescriptor &rhs){ | 
|---|
|  | 207 | AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomAndDescriptor_impl(lhs.get_impl(),rhs.get_impl())); | 
|---|
|  | 208 | return AtomDescriptor(newImpl); | 
|---|
| [d1c5e2] | 209 | } | 
|---|
|  | 210 |  | 
|---|
| [7a1ce5] | 211 | // OR | 
|---|
|  | 212 | AtomOrDescriptor_impl::AtomOrDescriptor_impl(AtomDescriptor::impl_ptr _lhs ,AtomDescriptor::impl_ptr _rhs) : | 
|---|
|  | 213 | lhs(_lhs), rhs(_rhs) | 
|---|
|  | 214 | {} | 
|---|
|  | 215 |  | 
|---|
|  | 216 | AtomOrDescriptor_impl::~AtomOrDescriptor_impl(){ | 
|---|
| [d1c5e2] | 217 | } | 
|---|
|  | 218 |  | 
|---|
| [795c0f] | 219 | bool AtomOrDescriptor_impl::predicate(std::pair<atomId_t,const atom*> atom) const{ | 
|---|
| [7a1ce5] | 220 | return lhs->predicate(atom) || rhs->predicate(atom); | 
|---|
| [d1c5e2] | 221 | } | 
|---|
|  | 222 |  | 
|---|
| [7a1ce5] | 223 | AtomDescriptor  operator||(const AtomDescriptor &lhs, const AtomDescriptor &rhs){ | 
|---|
|  | 224 | AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomOrDescriptor_impl(lhs.get_impl(),rhs.get_impl())); | 
|---|
|  | 225 | return AtomDescriptor(newImpl); | 
|---|
| [d1c5e2] | 226 | } | 
|---|
|  | 227 |  | 
|---|
|  | 228 | // NOT | 
|---|
|  | 229 |  | 
|---|
| [7a1ce5] | 230 | AtomNotDescriptor_impl::AtomNotDescriptor_impl(AtomDescriptor::impl_ptr _arg) : | 
|---|
|  | 231 | arg(_arg) | 
|---|
|  | 232 | {} | 
|---|
| [d1c5e2] | 233 |  | 
|---|
|  | 234 |  | 
|---|
| [7a1ce5] | 235 | AtomNotDescriptor_impl::~AtomNotDescriptor_impl() | 
|---|
| [d1c5e2] | 236 | { | 
|---|
|  | 237 | } | 
|---|
|  | 238 |  | 
|---|
| [795c0f] | 239 | bool AtomNotDescriptor_impl::predicate(std::pair<atomId_t,const atom*> atom) const{ | 
|---|
| [d1c5e2] | 240 | return !(arg->predicate(atom)); | 
|---|
|  | 241 | } | 
|---|
|  | 242 |  | 
|---|
| [7a1ce5] | 243 | AtomDescriptor operator!(const AtomDescriptor &arg){ | 
|---|
|  | 244 | AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomNotDescriptor_impl(arg.get_impl())); | 
|---|
|  | 245 | return AtomDescriptor(newImpl); | 
|---|
| [d1c5e2] | 246 | } | 
|---|