1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * MoleculeDescriptor.cpp
|
---|
10 | *
|
---|
11 | * Created on: Feb 5, 2010
|
---|
12 | * Author: crueger
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
23 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
|
---|
24 |
|
---|
25 | #include "World.hpp"
|
---|
26 | #include "CodePatterns/ObservedContainer_impl.hpp"
|
---|
27 |
|
---|
28 | #include "molecule.hpp"
|
---|
29 |
|
---|
30 | #include <boost/bind.hpp>
|
---|
31 | #include <iostream>
|
---|
32 |
|
---|
33 | using namespace std;
|
---|
34 |
|
---|
35 | typedef World::MoleculeSet::internal_iterator molecules_iter_t;
|
---|
36 |
|
---|
37 | /************************ Forwarding object **************************************/
|
---|
38 |
|
---|
39 |
|
---|
40 | MoleculeDescriptor::MoleculeDescriptor(impl_ptr _impl) :
|
---|
41 | impl(_impl)
|
---|
42 | {}
|
---|
43 |
|
---|
44 | MoleculeDescriptor::MoleculeDescriptor(const MoleculeDescriptor& src) :
|
---|
45 | impl(src.get_impl())
|
---|
46 | {}
|
---|
47 |
|
---|
48 | MoleculeDescriptor::~MoleculeDescriptor()
|
---|
49 | {}
|
---|
50 |
|
---|
51 | MoleculeDescriptor& MoleculeDescriptor::operator=(MoleculeDescriptor &src){
|
---|
52 | if(&src!=this) {
|
---|
53 | impl=src.get_impl();
|
---|
54 | }
|
---|
55 | return *this;
|
---|
56 | }
|
---|
57 |
|
---|
58 | molecule* MoleculeDescriptor::find(){
|
---|
59 | return impl->find();
|
---|
60 | }
|
---|
61 |
|
---|
62 | std::vector<molecule*> MoleculeDescriptor::findAll(){
|
---|
63 | return impl->findAll();
|
---|
64 | }
|
---|
65 |
|
---|
66 | MoleculeDescriptor::impl_ptr MoleculeDescriptor::get_impl() const{
|
---|
67 | return impl;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 |
|
---|
73 | /**************************** implementation ********************/
|
---|
74 |
|
---|
75 | MoleculeDescriptor_impl::MoleculeDescriptor_impl()
|
---|
76 | {
|
---|
77 | }
|
---|
78 |
|
---|
79 | MoleculeDescriptor_impl::~MoleculeDescriptor_impl()
|
---|
80 | {
|
---|
81 | }
|
---|
82 |
|
---|
83 | World::MoleculeSet& MoleculeDescriptor_impl::getMolecules(){
|
---|
84 | return World::getInstance().molecules;
|
---|
85 | }
|
---|
86 |
|
---|
87 | molecule* MoleculeDescriptor_impl::find() {
|
---|
88 | World::MoleculeSet &molecules = getMolecules();
|
---|
89 | molecules_iter_t res = find_if(molecules.begin_internal(),molecules.end_internal(),boost::bind(&MoleculeDescriptor_impl::predicate,this,_1));
|
---|
90 | return (res!=molecules.end_internal())?((*res).second):0;
|
---|
91 | }
|
---|
92 |
|
---|
93 | vector<molecule*> MoleculeDescriptor_impl::findAll() {
|
---|
94 | vector<molecule*> res;
|
---|
95 | World::MoleculeSet &molecules = getMolecules();
|
---|
96 | for_each(molecules.begin_internal(),
|
---|
97 | molecules.end_internal(),
|
---|
98 | boost::bind(&MoleculeDescriptor_impl::checkAndAdd,
|
---|
99 | this,&res,_1));
|
---|
100 | return res;
|
---|
101 | }
|
---|
102 |
|
---|
103 | void MoleculeDescriptor_impl::checkAndAdd(std::vector<molecule*> *v,std::pair<moleculeId_t,molecule*> p){
|
---|
104 | if(predicate(p)){
|
---|
105 | v->push_back(p.second);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /************************** Universe and Emptyset *****************/
|
---|
110 |
|
---|
111 | MoleculeAllDescriptor_impl::MoleculeAllDescriptor_impl()
|
---|
112 | {}
|
---|
113 |
|
---|
114 | MoleculeAllDescriptor_impl::~MoleculeAllDescriptor_impl()
|
---|
115 | {}
|
---|
116 |
|
---|
117 | bool MoleculeAllDescriptor_impl::predicate(std::pair<moleculeId_t,molecule*>){
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 |
|
---|
121 | MoleculeDescriptor AllMolecules(){
|
---|
122 | return MoleculeDescriptor(MoleculeDescriptor::impl_ptr(new MoleculeAllDescriptor_impl));
|
---|
123 | }
|
---|
124 |
|
---|
125 | MoleculeNoneDescriptor_impl::MoleculeNoneDescriptor_impl()
|
---|
126 | {}
|
---|
127 |
|
---|
128 | MoleculeNoneDescriptor_impl::~MoleculeNoneDescriptor_impl()
|
---|
129 | {}
|
---|
130 |
|
---|
131 | bool MoleculeNoneDescriptor_impl::predicate(std::pair<moleculeId_t,molecule*>){
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 |
|
---|
135 | MoleculeDescriptor NoMolecules(){
|
---|
136 | return MoleculeDescriptor(MoleculeDescriptor::impl_ptr(new MoleculeNoneDescriptor_impl));
|
---|
137 | }
|
---|
138 |
|
---|
139 | /************************** Operator stuff ************************/
|
---|
140 |
|
---|
141 | // AND
|
---|
142 | MoleculeAndDescriptor_impl::MoleculeAndDescriptor_impl(MoleculeDescriptor::impl_ptr _lhs, MoleculeDescriptor::impl_ptr _rhs) :
|
---|
143 | lhs(_lhs), rhs(_rhs)
|
---|
144 | {}
|
---|
145 |
|
---|
146 | MoleculeAndDescriptor_impl::~MoleculeAndDescriptor_impl()
|
---|
147 | {}
|
---|
148 |
|
---|
149 | bool MoleculeAndDescriptor_impl::predicate(std::pair<moleculeId_t,molecule*> molecule){
|
---|
150 | return lhs->predicate(molecule) && rhs->predicate(molecule);
|
---|
151 | }
|
---|
152 | MoleculeDescriptor operator&&(const MoleculeDescriptor &lhs, const MoleculeDescriptor &rhs){
|
---|
153 | MoleculeDescriptor::impl_ptr newImpl = MoleculeDescriptor::impl_ptr(new MoleculeAndDescriptor_impl(lhs.get_impl(),rhs.get_impl()));
|
---|
154 | return MoleculeDescriptor(newImpl);
|
---|
155 | }
|
---|
156 |
|
---|
157 | // OR
|
---|
158 | MoleculeOrDescriptor_impl::MoleculeOrDescriptor_impl(MoleculeDescriptor::impl_ptr _lhs ,MoleculeDescriptor::impl_ptr _rhs) :
|
---|
159 | lhs(_lhs), rhs(_rhs)
|
---|
160 | {}
|
---|
161 |
|
---|
162 | MoleculeOrDescriptor_impl::~MoleculeOrDescriptor_impl(){
|
---|
163 | }
|
---|
164 |
|
---|
165 | bool MoleculeOrDescriptor_impl::predicate(std::pair<moleculeId_t,molecule*> molecule){
|
---|
166 | return lhs->predicate(molecule) || rhs->predicate(molecule);
|
---|
167 | }
|
---|
168 |
|
---|
169 | MoleculeDescriptor operator||(const MoleculeDescriptor &lhs, const MoleculeDescriptor &rhs){
|
---|
170 | MoleculeDescriptor::impl_ptr newImpl = MoleculeDescriptor::impl_ptr(new MoleculeOrDescriptor_impl(lhs.get_impl(),rhs.get_impl()));
|
---|
171 | return MoleculeDescriptor(newImpl);
|
---|
172 | }
|
---|
173 |
|
---|
174 | // NOT
|
---|
175 |
|
---|
176 | MoleculeNotDescriptor_impl::MoleculeNotDescriptor_impl(MoleculeDescriptor::impl_ptr _arg) :
|
---|
177 | arg(_arg)
|
---|
178 | {}
|
---|
179 |
|
---|
180 |
|
---|
181 | MoleculeNotDescriptor_impl::~MoleculeNotDescriptor_impl()
|
---|
182 | {
|
---|
183 | }
|
---|
184 |
|
---|
185 | bool MoleculeNotDescriptor_impl::predicate(std::pair<moleculeId_t,molecule*> molecule){
|
---|
186 | return !(arg->predicate(molecule));
|
---|
187 | }
|
---|
188 |
|
---|
189 | MoleculeDescriptor operator!(const MoleculeDescriptor &arg){
|
---|
190 | MoleculeDescriptor::impl_ptr newImpl = MoleculeDescriptor::impl_ptr(new MoleculeNotDescriptor_impl(arg.get_impl()));
|
---|
191 | return MoleculeDescriptor(newImpl);
|
---|
192 | }
|
---|