COPASI API  4.16.103
CNormalCall.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/compareExpressions/CNormalCall.cpp,v $
3 // $Revision: 1.3 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2011/03/07 19:26:19 $
7 // End CVS Header
8 
9 // Copyright (C) 2011 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 #include "CNormalCall.h"
20 
21 #include <sstream>
22 
23 #include "CNormalFraction.h"
24 
25 CNormalCall::CNormalCall(): CNormalBase(), mName(""), mType(CNormalCall::INVALID)
26 {};
27 
28 /**
29  * Copy Constructor
30  */
32 {
33  this->mName = src.mName;
34  this->mType = src.mType;
35  this->setFractions(src.mFractions);
36 }
37 
38 /**
39  * Assignment operator
40  */
42 {
43  if (&src != this)
44  {
45  this->mName = src.mName;
46  this->mType = src.mType;
47  this->setFractions(src.mFractions);
48  }
49 
50  return *this;
51 }
52 
53 /**
54  * Smaller operator
55  */
56 bool CNormalCall::operator<(const CNormalCall& rhs) const
57 {
58  bool result = false;
59 
60  if (this->mType < rhs.mType)
61  {
62  result = true;
63  }
64  else
65  {
66  if (this->mName < rhs.mName)
67  {
68  result = true;
69  }
70  else
71  {
72  if (this->mName == rhs.mName)
73  {
74  if (this->mFractions.size() < rhs.mFractions.size())
75  {
76  result = true;
77  }
78  else
79  {
80  if (this->mFractions.size() == rhs.mFractions.size())
81  {
82  bool smaller = true;
83  std::vector<CNormalFraction*>::const_iterator it = this->mFractions.begin(), endit = this->mFractions.end();
84  std::vector<CNormalFraction*>::const_iterator it2 = rhs.mFractions.begin();
85 
86  while (it != endit && smaller == true)
87  {
88  smaller = ((*it2) < (*it));
89  ++it;
90  ++it2;
91  }
92 
93  if (smaller)
94  {
95  result = true;
96  }
97  }
98  }
99  }
100  }
101  }
102 
103  return result;
104 }
105 
106 /**
107  * Destructor
108  */
110 {
111  // delete all fractions
112  std::vector<CNormalFraction*>::iterator it = this->mFractions.begin(), endit = this->mFractions.end();
113 
114  while (it != endit)
115  {
116  delete *it;
117  ++it;
118  }
119 }
120 
122 {
123  return new CNormalCall(*this);
124 }
125 
126 /**
127  * Retrieve the number of summands of this sum.
128  * @return int
129  */
131 {
132  return this->mFractions.size();
133 }
134 
135 /**
136  * Add fraction to this sum.
137  * @return true.
138  */
139 bool CNormalCall::add(const CNormalFraction& fraction)
140 {
141  this->mFractions.push_back(new CNormalFraction(fraction));
142  return true;
143 }
144 
145 /**
146  * Retrieve the set of fractions of this sum.
147  * @return mFractions.
148  */
149 const std::vector<CNormalFraction*>& CNormalCall::getFractions() const
150 {
151  return this->mFractions;
152 }
153 
154 /**
155  * Examine equality of two sums.
156  * @return bool.
157  */
158 bool CNormalCall::operator==(const CNormalCall & rhs) const
159 {
160  bool result = true;
161 
162  if (this->mType == rhs.mType)
163  {
164  if (this->mName == rhs.mName)
165  {
166  if (this->mFractions.size() == rhs.mFractions.size())
167  {
168  std::vector<CNormalFraction*>::const_iterator it = this->mFractions.begin();
169  std::vector<CNormalFraction*>::const_iterator it2 = rhs.mFractions.begin();
170  std::vector<CNormalFraction*>::const_iterator endit = this->mFractions.end();
171 
172  while (it != endit && result == true)
173  {
174  result = ((**it) == (**it2));
175  ++it;
176  ++it2;
177  }
178  }
179  else
180  {
181  result = false;
182  }
183  }
184  else
185  {
186  result = false;
187  }
188  }
189  else
190  {
191  result = false;
192  }
193 
194  return result;
195 }
196 
197 /**
198  * Sets the fractions of this product.
199  */
200 void CNormalCall::setFractions(const std::vector<CNormalFraction*>& set)
201 {
202  // delete all fractions
203  std::vector<CNormalFraction*>::iterator it = this->mFractions.begin(), endit = this->mFractions.end();
204 
205  while (it != endit)
206  {
207  delete *it;
208  ++it;
209  }
210 
211  this->mFractions.clear();
212  std::vector<CNormalFraction*>::const_iterator it2 = set.begin(), endit2 = set.end();
213 
214  while (it2 != endit2)
215  {
216  this->add(**it2);
217  ++it2;
218  }
219 }
220 
221 std::string CNormalCall::toString() const
222 {
223  std::ostringstream os;
224  os << this->mName << "(";
225  std::vector<CNormalFraction*>::const_iterator it = this->mFractions.begin(), endit = this->mFractions.end();
226 
227  while (it != endit)
228  {
229  os << (**it);
230  ++it;
231 
232  if (it != endit)
233  {
234  os << ",";
235  }
236  }
237 
238  os << ")";
239  return os.str();
240 }
241 
243 {
244  // simplify all children
245  std::vector<CNormalFraction*>::iterator it = this->mFractions.begin(), endit = this->mFractions.end();
246 
247  while (it != endit)
248  {
249  (*it)->simplify();
250  ++it;
251  }
252 
253  return true;
254 }
255 
256 std::ostream & operator<< (std::ostream &os, const CNormalCall & d)
257 {
258  os << d.toString();
259  return os;
260 }
261 
262 const std::string& CNormalCall::getName() const
263 {
264  return this->mName;
265 }
266 
267 void CNormalCall::setName(const std::string& name)
268 {
269  this->mName = name;
270 }
271 
273 {
274  return this->mType;
275 }
276 
278 {
279  this->mType = type;
280 }
std::string mName
Definition: CNormalCall.h:48
virtual std::string toString() const
bool operator<(const CNormalCall &rhs) const
Definition: CNormalCall.cpp:56
virtual bool simplify()
void setType(Type type)
const std::string & getName() const
CNormalCall & operator=(const CNormalCall &src)
Definition: CNormalCall.cpp:41
const std::vector< CNormalFraction * > & getFractions() const
void setName(const std::string &name)
std::vector< CNormalFraction * > mFractions
Definition: CNormalCall.h:60
virtual ~CNormalCall()
std::ostream & operator<<(std::ostream &os, const CNormalCall &d)
int getSize() const
bool add(const CNormalFraction &fraction)
Type getType() const
void setFractions(const std::vector< CNormalFraction * > &set)
bool operator==(const CNormalCall &rhs) const
virtual CNormalBase * copy() const