| [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 |  | 
|---|
| [1c51c8] | 23 | /* | 
|---|
|  | 24 | * MoleculeDescriptor.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 |  | 
|---|
| [9eb71b3] | 35 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 36 |  | 
|---|
| [1c51c8] | 37 | #include "Descriptors/MoleculeDescriptor.hpp" | 
|---|
|  | 38 | #include "Descriptors/MoleculeDescriptor_impl.hpp" | 
|---|
|  | 39 |  | 
|---|
|  | 40 | #include "World.hpp" | 
|---|
| [02ce36] | 41 | #include "CodePatterns/Observer/ObservedContainer_impl.hpp" | 
|---|
| [1c51c8] | 42 |  | 
|---|
|  | 43 | #include "molecule.hpp" | 
|---|
|  | 44 |  | 
|---|
|  | 45 | #include <boost/bind.hpp> | 
|---|
|  | 46 | #include <iostream> | 
|---|
|  | 47 |  | 
|---|
|  | 48 | using namespace std; | 
|---|
|  | 49 |  | 
|---|
| [51be2a] | 50 | typedef World::MoleculeSet::internal_iterator molecules_iter_t; | 
|---|
| [97445f] | 51 | typedef World::MoleculeSet::const_iterator const_molecules_iter_t; | 
|---|
| [1c51c8] | 52 |  | 
|---|
|  | 53 | /************************ Forwarding object **************************************/ | 
|---|
|  | 54 |  | 
|---|
|  | 55 |  | 
|---|
|  | 56 | MoleculeDescriptor::MoleculeDescriptor(impl_ptr _impl) : | 
|---|
|  | 57 | impl(_impl) | 
|---|
|  | 58 | {} | 
|---|
|  | 59 |  | 
|---|
|  | 60 | MoleculeDescriptor::MoleculeDescriptor(const MoleculeDescriptor& src) : | 
|---|
|  | 61 | impl(src.get_impl()) | 
|---|
|  | 62 | {} | 
|---|
|  | 63 |  | 
|---|
|  | 64 | MoleculeDescriptor::~MoleculeDescriptor() | 
|---|
|  | 65 | {} | 
|---|
|  | 66 |  | 
|---|
|  | 67 | MoleculeDescriptor& MoleculeDescriptor::operator=(MoleculeDescriptor &src){ | 
|---|
|  | 68 | if(&src!=this) { | 
|---|
|  | 69 | impl=src.get_impl(); | 
|---|
|  | 70 | } | 
|---|
|  | 71 | return *this; | 
|---|
|  | 72 | } | 
|---|
|  | 73 |  | 
|---|
|  | 74 | molecule* MoleculeDescriptor::find(){ | 
|---|
|  | 75 | return impl->find(); | 
|---|
|  | 76 | } | 
|---|
|  | 77 |  | 
|---|
| [97445f] | 78 | const molecule* MoleculeDescriptor::find() const { | 
|---|
|  | 79 | return const_cast<const impl_t &>(*impl).find(); | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
| [1c51c8] | 82 | std::vector<molecule*> MoleculeDescriptor::findAll(){ | 
|---|
|  | 83 | return impl->findAll(); | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
| [97445f] | 86 | std::vector<const molecule*> MoleculeDescriptor::findAll() const{ | 
|---|
|  | 87 | return const_cast<const impl_t &>(*impl).findAll(); | 
|---|
|  | 88 | } | 
|---|
|  | 89 |  | 
|---|
| [1c51c8] | 90 | MoleculeDescriptor::impl_ptr MoleculeDescriptor::get_impl() const{ | 
|---|
|  | 91 | return impl; | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 |  | 
|---|
|  | 95 |  | 
|---|
|  | 96 |  | 
|---|
|  | 97 | /**************************** implementation ********************/ | 
|---|
|  | 98 |  | 
|---|
|  | 99 | MoleculeDescriptor_impl::MoleculeDescriptor_impl() | 
|---|
|  | 100 | { | 
|---|
|  | 101 | } | 
|---|
|  | 102 |  | 
|---|
|  | 103 | MoleculeDescriptor_impl::~MoleculeDescriptor_impl() | 
|---|
|  | 104 | { | 
|---|
|  | 105 | } | 
|---|
|  | 106 |  | 
|---|
|  | 107 | World::MoleculeSet& MoleculeDescriptor_impl::getMolecules(){ | 
|---|
| [23b547] | 108 | return World::getInstance().molecules; | 
|---|
| [1c51c8] | 109 | } | 
|---|
|  | 110 |  | 
|---|
| [97445f] | 111 | const World::MoleculeSet& MoleculeDescriptor_impl::getMolecules() const{ | 
|---|
|  | 112 | return World::getInstance().molecules; | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
| [1c51c8] | 115 | molecule* MoleculeDescriptor_impl::find() { | 
|---|
| [a1e0b6] | 116 | World::MoleculeSet &molecules = getMolecules(); | 
|---|
| [97445f] | 117 | molecules_iter_t res = find_if( | 
|---|
|  | 118 | molecules.begin_internal(),molecules.end_internal(), | 
|---|
|  | 119 | boost::bind(&MoleculeDescriptor_impl::predicate, | 
|---|
|  | 120 | this,_1)); | 
|---|
| [51be2a] | 121 | return (res!=molecules.end_internal())?((*res).second):0; | 
|---|
| [1c51c8] | 122 | } | 
|---|
|  | 123 |  | 
|---|
| [97445f] | 124 | const molecule* MoleculeDescriptor_impl::find() const { | 
|---|
|  | 125 | const World::MoleculeSet &molecules = getMolecules(); | 
|---|
|  | 126 | const_molecules_iter_t res = find_if( | 
|---|
|  | 127 | molecules.begin(),molecules.end(), | 
|---|
|  | 128 | boost::bind(&MoleculeDescriptor_impl::predicate, | 
|---|
|  | 129 | this,_1)); | 
|---|
|  | 130 | return (res!=molecules.end())?((*res).second):0; | 
|---|
|  | 131 | } | 
|---|
|  | 132 |  | 
|---|
| [1c51c8] | 133 | vector<molecule*> MoleculeDescriptor_impl::findAll() { | 
|---|
|  | 134 | vector<molecule*> res; | 
|---|
| [a1e0b6] | 135 | World::MoleculeSet &molecules = getMolecules(); | 
|---|
| [8cce2b] | 136 | for_each(molecules.begin_internal(), | 
|---|
|  | 137 | molecules.end_internal(), | 
|---|
| [97445f] | 138 | boost::bind(static_cast<void (MoleculeDescriptor_impl::*)( | 
|---|
|  | 139 | std::vector<molecule*> *, | 
|---|
|  | 140 | std::pair<moleculeId_t,molecule*>)>(&MoleculeDescriptor_impl::checkAndAdd), | 
|---|
|  | 141 | this,boost::cref(&res),_1)); | 
|---|
| [1c51c8] | 142 | return res; | 
|---|
|  | 143 | } | 
|---|
|  | 144 |  | 
|---|
| [97445f] | 145 | vector<const molecule*> MoleculeDescriptor_impl::findAll() const { | 
|---|
|  | 146 | vector<const molecule*> res; | 
|---|
|  | 147 | const World::MoleculeSet &molecules = getMolecules(); | 
|---|
|  | 148 | for_each(molecules.begin(), | 
|---|
|  | 149 | molecules.end(), | 
|---|
|  | 150 | boost::bind(static_cast<void (MoleculeDescriptor_impl::*)( | 
|---|
|  | 151 | std::vector<const molecule*> *, | 
|---|
|  | 152 | std::pair<moleculeId_t,const molecule*>) const>(&MoleculeDescriptor_impl::checkAndAdd), | 
|---|
|  | 153 | boost::cref(this),&res,_1)); | 
|---|
|  | 154 | return res; | 
|---|
|  | 155 | } | 
|---|
|  | 156 |  | 
|---|
|  | 157 | void MoleculeDescriptor_impl::checkAndAdd( | 
|---|
|  | 158 | std::vector<molecule*> *v, | 
|---|
|  | 159 | std::pair<moleculeId_t,molecule*> p){ | 
|---|
|  | 160 | if(predicate(p)){ | 
|---|
|  | 161 | v->push_back(p.second); | 
|---|
|  | 162 | } | 
|---|
|  | 163 | } | 
|---|
|  | 164 |  | 
|---|
|  | 165 | void MoleculeDescriptor_impl::checkAndAdd( | 
|---|
|  | 166 | std::vector<const molecule*> *v, | 
|---|
|  | 167 | std::pair<moleculeId_t,const molecule*> p) const{ | 
|---|
| [8cce2b] | 168 | if(predicate(p)){ | 
|---|
|  | 169 | v->push_back(p.second); | 
|---|
|  | 170 | } | 
|---|
|  | 171 | } | 
|---|
|  | 172 |  | 
|---|
| [1c51c8] | 173 | /************************** Universe and Emptyset *****************/ | 
|---|
|  | 174 |  | 
|---|
|  | 175 | MoleculeAllDescriptor_impl::MoleculeAllDescriptor_impl() | 
|---|
|  | 176 | {} | 
|---|
|  | 177 |  | 
|---|
|  | 178 | MoleculeAllDescriptor_impl::~MoleculeAllDescriptor_impl() | 
|---|
|  | 179 | {} | 
|---|
|  | 180 |  | 
|---|
| [97445f] | 181 | bool MoleculeAllDescriptor_impl::predicate(const std::pair<moleculeId_t,const molecule*>) const{ | 
|---|
| [1c51c8] | 182 | return true; | 
|---|
|  | 183 | } | 
|---|
|  | 184 |  | 
|---|
|  | 185 | MoleculeDescriptor AllMolecules(){ | 
|---|
|  | 186 | return MoleculeDescriptor(MoleculeDescriptor::impl_ptr(new MoleculeAllDescriptor_impl)); | 
|---|
|  | 187 | } | 
|---|
|  | 188 |  | 
|---|
|  | 189 | MoleculeNoneDescriptor_impl::MoleculeNoneDescriptor_impl() | 
|---|
|  | 190 | {} | 
|---|
|  | 191 |  | 
|---|
|  | 192 | MoleculeNoneDescriptor_impl::~MoleculeNoneDescriptor_impl() | 
|---|
|  | 193 | {} | 
|---|
|  | 194 |  | 
|---|
| [97445f] | 195 | bool MoleculeNoneDescriptor_impl::predicate(const std::pair<moleculeId_t,const molecule*>) const{ | 
|---|
| [1c51c8] | 196 | return false; | 
|---|
|  | 197 | } | 
|---|
|  | 198 |  | 
|---|
|  | 199 | MoleculeDescriptor NoMolecules(){ | 
|---|
|  | 200 | return MoleculeDescriptor(MoleculeDescriptor::impl_ptr(new MoleculeNoneDescriptor_impl)); | 
|---|
|  | 201 | } | 
|---|
|  | 202 |  | 
|---|
|  | 203 | /************************** Operator stuff ************************/ | 
|---|
|  | 204 |  | 
|---|
|  | 205 | // AND | 
|---|
|  | 206 | MoleculeAndDescriptor_impl::MoleculeAndDescriptor_impl(MoleculeDescriptor::impl_ptr _lhs, MoleculeDescriptor::impl_ptr _rhs) : | 
|---|
|  | 207 | lhs(_lhs), rhs(_rhs) | 
|---|
|  | 208 | {} | 
|---|
|  | 209 |  | 
|---|
|  | 210 | MoleculeAndDescriptor_impl::~MoleculeAndDescriptor_impl() | 
|---|
|  | 211 | {} | 
|---|
|  | 212 |  | 
|---|
| [97445f] | 213 | bool MoleculeAndDescriptor_impl::predicate(const std::pair<moleculeId_t,const molecule*> molecule) const{ | 
|---|
| [1c51c8] | 214 | return lhs->predicate(molecule) && rhs->predicate(molecule); | 
|---|
|  | 215 | } | 
|---|
|  | 216 | MoleculeDescriptor operator&&(const MoleculeDescriptor &lhs, const MoleculeDescriptor &rhs){ | 
|---|
|  | 217 | MoleculeDescriptor::impl_ptr newImpl = MoleculeDescriptor::impl_ptr(new MoleculeAndDescriptor_impl(lhs.get_impl(),rhs.get_impl())); | 
|---|
|  | 218 | return MoleculeDescriptor(newImpl); | 
|---|
|  | 219 | } | 
|---|
|  | 220 |  | 
|---|
|  | 221 | // OR | 
|---|
|  | 222 | MoleculeOrDescriptor_impl::MoleculeOrDescriptor_impl(MoleculeDescriptor::impl_ptr _lhs ,MoleculeDescriptor::impl_ptr _rhs) : | 
|---|
|  | 223 | lhs(_lhs), rhs(_rhs) | 
|---|
|  | 224 | {} | 
|---|
|  | 225 |  | 
|---|
|  | 226 | MoleculeOrDescriptor_impl::~MoleculeOrDescriptor_impl(){ | 
|---|
|  | 227 | } | 
|---|
|  | 228 |  | 
|---|
| [97445f] | 229 | bool MoleculeOrDescriptor_impl::predicate(const std::pair<moleculeId_t,const molecule*> molecule) const{ | 
|---|
| [1c51c8] | 230 | return lhs->predicate(molecule) || rhs->predicate(molecule); | 
|---|
|  | 231 | } | 
|---|
|  | 232 |  | 
|---|
|  | 233 | MoleculeDescriptor  operator||(const MoleculeDescriptor &lhs, const MoleculeDescriptor &rhs){ | 
|---|
|  | 234 | MoleculeDescriptor::impl_ptr newImpl = MoleculeDescriptor::impl_ptr(new MoleculeOrDescriptor_impl(lhs.get_impl(),rhs.get_impl())); | 
|---|
|  | 235 | return MoleculeDescriptor(newImpl); | 
|---|
|  | 236 | } | 
|---|
|  | 237 |  | 
|---|
|  | 238 | // NOT | 
|---|
|  | 239 |  | 
|---|
|  | 240 | MoleculeNotDescriptor_impl::MoleculeNotDescriptor_impl(MoleculeDescriptor::impl_ptr _arg) : | 
|---|
|  | 241 | arg(_arg) | 
|---|
|  | 242 | {} | 
|---|
|  | 243 |  | 
|---|
|  | 244 |  | 
|---|
|  | 245 | MoleculeNotDescriptor_impl::~MoleculeNotDescriptor_impl() | 
|---|
|  | 246 | { | 
|---|
|  | 247 | } | 
|---|
|  | 248 |  | 
|---|
| [97445f] | 249 | bool MoleculeNotDescriptor_impl::predicate(const std::pair<moleculeId_t,const molecule*> molecule) const{ | 
|---|
| [1c51c8] | 250 | return !(arg->predicate(molecule)); | 
|---|
|  | 251 | } | 
|---|
|  | 252 |  | 
|---|
|  | 253 | MoleculeDescriptor operator!(const MoleculeDescriptor &arg){ | 
|---|
|  | 254 | MoleculeDescriptor::impl_ptr newImpl = MoleculeDescriptor::impl_ptr(new MoleculeNotDescriptor_impl(arg.get_impl())); | 
|---|
|  | 255 | return MoleculeDescriptor(newImpl); | 
|---|
|  | 256 | } | 
|---|