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