| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * AtomDescriptor.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Feb 5, 2010
 | 
|---|
| 27 |  *      Author: crueger
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| 38 | #include "Descriptors/AtomDescriptor_impl.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "World.hpp"
 | 
|---|
| 41 | #include "Atom/atom.hpp"
 | 
|---|
| 42 | #include "CodePatterns/Observer/ObservedContainer_impl.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include <boost/bind.hpp>
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include <iostream>
 | 
|---|
| 47 | 
 | 
|---|
| 48 | using namespace std;
 | 
|---|
| 49 | 
 | 
|---|
| 50 | typedef World::AtomSet::internal_iterator atoms_iter_t;
 | 
|---|
| 51 | typedef World::AtomSet::const_iterator atoms_const_iter_t;
 | 
|---|
| 52 | 
 | 
|---|
| 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;
 | 
|---|
| 72 | }
 | 
|---|
| 73 | 
 | 
|---|
| 74 | atom* AtomDescriptor::find(){
 | 
|---|
| 75 |   return impl->find();
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | const atom* AtomDescriptor::find() const {
 | 
|---|
| 79 |   return const_cast<const impl_t &>(*impl).find();
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | std::vector<atom*> AtomDescriptor::findAll(){
 | 
|---|
| 83 |   return impl->findAll();
 | 
|---|
| 84 | }
 | 
|---|
| 85 | 
 | 
|---|
| 86 | std::vector<const atom*> AtomDescriptor::findAll() const {
 | 
|---|
| 87 |   return const_cast<const impl_t &>(*impl).findAll();
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | AtomDescriptor::impl_ptr AtomDescriptor::get_impl() const{
 | 
|---|
| 91 |   return impl;
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | 
 | 
|---|
| 95 | 
 | 
|---|
| 96 | 
 | 
|---|
| 97 | /**************************** implementation ********************/
 | 
|---|
| 98 | 
 | 
|---|
| 99 | AtomDescriptor_impl::AtomDescriptor_impl()
 | 
|---|
| 100 | {
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | AtomDescriptor_impl::~AtomDescriptor_impl()
 | 
|---|
| 104 | {
 | 
|---|
| 105 | }
 | 
|---|
| 106 | 
 | 
|---|
| 107 | World::AtomSet& AtomDescriptor_impl::getAtoms(){
 | 
|---|
| 108 |   return World::getInstance().atoms;
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | const World::AtomSet& AtomDescriptor_impl::getAtoms() const {
 | 
|---|
| 112 |   return const_cast<const World &>(World::getInstance()).atoms;
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | atom* AtomDescriptor_impl::find() {
 | 
|---|
| 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;
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 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 | 
 | 
|---|
| 127 | vector<atom*> AtomDescriptor_impl::findAll() {
 | 
|---|
| 128 |   vector<atom*> res;
 | 
|---|
| 129 |   World::AtomSet atoms = getAtoms();
 | 
|---|
| 130 |   for_each(atoms.begin_internal(),
 | 
|---|
| 131 |            atoms.end_internal(),
 | 
|---|
| 132 |            boost::bind(static_cast<void (AtomDescriptor_impl::*)(
 | 
|---|
| 133 |                std::vector<atom*> *,
 | 
|---|
| 134 |                std::pair<atomId_t,atom*>)>(&AtomDescriptor_impl::checkAndAdd),
 | 
|---|
| 135 |                        this,&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 |                        this,&res,_1));
 | 
|---|
| 148 |   return res;
 | 
|---|
| 149 | }
 | 
|---|
| 150 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 163 | /************************** Universe and Emptyset *****************/
 | 
|---|
| 164 | 
 | 
|---|
| 165 | AtomAllDescriptor_impl::AtomAllDescriptor_impl()
 | 
|---|
| 166 | {}
 | 
|---|
| 167 | 
 | 
|---|
| 168 | AtomAllDescriptor_impl::~AtomAllDescriptor_impl()
 | 
|---|
| 169 | {}
 | 
|---|
| 170 | 
 | 
|---|
| 171 | bool AtomAllDescriptor_impl::predicate(std::pair<atomId_t,const atom*>) const{
 | 
|---|
| 172 |   return true;
 | 
|---|
| 173 | }
 | 
|---|
| 174 | 
 | 
|---|
| 175 | AtomDescriptor AllAtoms(){
 | 
|---|
| 176 |   return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomAllDescriptor_impl));
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | AtomNoneDescriptor_impl::AtomNoneDescriptor_impl()
 | 
|---|
| 180 | {}
 | 
|---|
| 181 | 
 | 
|---|
| 182 | AtomNoneDescriptor_impl::~AtomNoneDescriptor_impl()
 | 
|---|
| 183 | {}
 | 
|---|
| 184 | 
 | 
|---|
| 185 | bool AtomNoneDescriptor_impl::predicate(std::pair<atomId_t,const atom*>) const{
 | 
|---|
| 186 |   return false;
 | 
|---|
| 187 | }
 | 
|---|
| 188 | 
 | 
|---|
| 189 | AtomDescriptor NoAtoms(){
 | 
|---|
| 190 |   return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomNoneDescriptor_impl));
 | 
|---|
| 191 | }
 | 
|---|
| 192 | 
 | 
|---|
| 193 | /************************** Operator stuff ************************/
 | 
|---|
| 194 | 
 | 
|---|
| 195 | // AND
 | 
|---|
| 196 | AtomAndDescriptor_impl::AtomAndDescriptor_impl(AtomDescriptor::impl_ptr _lhs, AtomDescriptor::impl_ptr _rhs) :
 | 
|---|
| 197 |     lhs(_lhs), rhs(_rhs)
 | 
|---|
| 198 | {}
 | 
|---|
| 199 | 
 | 
|---|
| 200 | AtomAndDescriptor_impl::~AtomAndDescriptor_impl()
 | 
|---|
| 201 | {}
 | 
|---|
| 202 | 
 | 
|---|
| 203 | bool AtomAndDescriptor_impl::predicate(std::pair<atomId_t,const atom*> atom) const{
 | 
|---|
| 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);
 | 
|---|
| 209 | }
 | 
|---|
| 210 | 
 | 
|---|
| 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(){
 | 
|---|
| 217 | }
 | 
|---|
| 218 | 
 | 
|---|
| 219 | bool AtomOrDescriptor_impl::predicate(std::pair<atomId_t,const atom*> atom) const{
 | 
|---|
| 220 |   return lhs->predicate(atom) || rhs->predicate(atom);
 | 
|---|
| 221 | }
 | 
|---|
| 222 | 
 | 
|---|
| 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);
 | 
|---|
| 226 | }
 | 
|---|
| 227 | 
 | 
|---|
| 228 | // NOT
 | 
|---|
| 229 | 
 | 
|---|
| 230 | AtomNotDescriptor_impl::AtomNotDescriptor_impl(AtomDescriptor::impl_ptr _arg) :
 | 
|---|
| 231 |     arg(_arg)
 | 
|---|
| 232 | {}
 | 
|---|
| 233 | 
 | 
|---|
| 234 | 
 | 
|---|
| 235 | AtomNotDescriptor_impl::~AtomNotDescriptor_impl()
 | 
|---|
| 236 | {
 | 
|---|
| 237 | }
 | 
|---|
| 238 | 
 | 
|---|
| 239 | bool AtomNotDescriptor_impl::predicate(std::pair<atomId_t,const atom*> atom) const{
 | 
|---|
| 240 |  return !(arg->predicate(atom));
 | 
|---|
| 241 | }
 | 
|---|
| 242 | 
 | 
|---|
| 243 | AtomDescriptor operator!(const AtomDescriptor &arg){
 | 
|---|
| 244 |   AtomDescriptor::impl_ptr newImpl = AtomDescriptor::impl_ptr(new AtomNotDescriptor_impl(arg.get_impl()));
 | 
|---|
| 245 |   return AtomDescriptor(newImpl);
 | 
|---|
| 246 | }
 | 
|---|