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