1 | /*
|
---|
2 | * Formula.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jul 21, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | // include config.h
|
---|
9 | #ifdef HAVE_CONFIG_H
|
---|
10 | #include <config.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include "Helpers/MemDebug.hpp"
|
---|
14 |
|
---|
15 | #include "Formula.hpp"
|
---|
16 |
|
---|
17 | #include <sstream>
|
---|
18 |
|
---|
19 | #include "World.hpp"
|
---|
20 | #include "periodentafel.hpp"
|
---|
21 | #include "element.hpp"
|
---|
22 | #include "Helpers/Assert.hpp"
|
---|
23 | #include "Helpers/Range.hpp"
|
---|
24 |
|
---|
25 | using namespace std;
|
---|
26 |
|
---|
27 | Formula::Formula() :
|
---|
28 | numElements(0)
|
---|
29 | {}
|
---|
30 |
|
---|
31 | Formula::Formula(const Formula &src) :
|
---|
32 | elementCounts(src.elementCounts),
|
---|
33 | numElements(src.numElements)
|
---|
34 | {}
|
---|
35 |
|
---|
36 | Formula::Formula(const string &formula) :
|
---|
37 | numElements(0)
|
---|
38 | {
|
---|
39 | fromString(formula);
|
---|
40 | }
|
---|
41 |
|
---|
42 | Formula::~Formula()
|
---|
43 | {}
|
---|
44 |
|
---|
45 | Formula &Formula::operator=(const Formula &rhs){
|
---|
46 | // No self-assignment check needed
|
---|
47 | elementCounts=rhs.elementCounts;
|
---|
48 | numElements=rhs.numElements;
|
---|
49 | return *this;
|
---|
50 | }
|
---|
51 |
|
---|
52 | std::string Formula::toString() const{
|
---|
53 | stringstream sstr;
|
---|
54 | for(const_iterator iter=end();iter!=begin();){
|
---|
55 | --iter;
|
---|
56 | sstr << (*iter).first->getSymbol();
|
---|
57 | if((*iter).second>1)
|
---|
58 | sstr << (*iter).second;
|
---|
59 | }
|
---|
60 | return sstr.str();
|
---|
61 | }
|
---|
62 |
|
---|
63 | void Formula::fromString(const std::string &formula) throw(ParseError){
|
---|
64 | // make this transactional, in case an error is thrown
|
---|
65 | Formula res;
|
---|
66 | string::const_iterator begin = formula.begin();
|
---|
67 | string::const_iterator end = formula.end();
|
---|
68 | res.parseFromString(begin,end,static_cast<char>(0));
|
---|
69 | (*this)=res;
|
---|
70 | }
|
---|
71 |
|
---|
72 | int Formula::parseMaybeNumber(string::const_iterator &it,string::const_iterator &end) throw(ParseError){
|
---|
73 | static const range<char> Numbers = makeRange('0',static_cast<char>('9'+1));
|
---|
74 | int count = 0;
|
---|
75 | while(it!=end && Numbers.isInRange(*it))
|
---|
76 | count = (count*10) + ((*it++)-Numbers.first);
|
---|
77 | // one is implicit
|
---|
78 | count = (count!=0)?count:1;
|
---|
79 | return count;
|
---|
80 | }
|
---|
81 |
|
---|
82 | void Formula::parseFromString(string::const_iterator &it,string::const_iterator &end,char delimiter) throw(ParseError){
|
---|
83 | // some constants needed for parsing... Assumes ASCII, change if other encodings are used
|
---|
84 | static const range<char> CapitalLetters = makeRange('A',static_cast<char>('Z'+1));
|
---|
85 | static const range<char> SmallLetters = makeRange('a',static_cast<char>('z'+1));
|
---|
86 | map<char,char> delimiters;
|
---|
87 | delimiters['('] = ')';
|
---|
88 | delimiters['['] = ']';
|
---|
89 | // clean the formula
|
---|
90 | clear();
|
---|
91 | for(/*send from above*/;it!=end && *it!=delimiter;/*updated in loop*/){
|
---|
92 | // we might have a sub formula
|
---|
93 | if(delimiters.count(*it)){
|
---|
94 | Formula sub;
|
---|
95 | char nextdelim=delimiters[*it];
|
---|
96 | sub.parseFromString(++it,end,nextdelim);
|
---|
97 | if(!sub.getElementCount()){
|
---|
98 | throw(ParseError(__FILE__,__LINE__));
|
---|
99 | }
|
---|
100 | int count = parseMaybeNumber(++it,end);
|
---|
101 | addFormula(sub,count);
|
---|
102 | continue;
|
---|
103 | }
|
---|
104 | string shorthand;
|
---|
105 | // Atom names start with a capital letter
|
---|
106 | if(!CapitalLetters.isInRange(*it))
|
---|
107 | throw(ParseError(__FILE__,__LINE__));
|
---|
108 | shorthand+=(*it++);
|
---|
109 | // the rest of the name follows
|
---|
110 | while(it!=end && SmallLetters.isInRange(*it))
|
---|
111 | shorthand+=(*it++);
|
---|
112 | int count = parseMaybeNumber(it,end);
|
---|
113 | // test if the shorthand exists
|
---|
114 | if(!World::getInstance().getPeriode()->FindElement(shorthand))
|
---|
115 | throw(ParseError(__FILE__,__LINE__));
|
---|
116 | // done, we can get the next one
|
---|
117 | addElements(shorthand,count);
|
---|
118 | }
|
---|
119 | if(it==end && delimiter!=0){
|
---|
120 | throw(ParseError(__FILE__,__LINE__));
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | bool Formula::checkOut(ostream *output) const{
|
---|
125 | bool result = true;
|
---|
126 | int No = 1;
|
---|
127 |
|
---|
128 | if (output != NULL) {
|
---|
129 | *output << "# Ion type data (PP = PseudoPotential, Z = atomic number)" << endl;
|
---|
130 | *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl;
|
---|
131 | for(const_iterator iter=begin(); iter!=end();++iter){
|
---|
132 | result = result && (*iter).first->Checkout(output, No++, (*iter).second);
|
---|
133 | }
|
---|
134 | return result;
|
---|
135 | } else
|
---|
136 | return false;
|
---|
137 | }
|
---|
138 |
|
---|
139 | unsigned int Formula::getElementCount() const{
|
---|
140 | return numElements;
|
---|
141 | }
|
---|
142 |
|
---|
143 | bool Formula::hasElement(const element *element) const{
|
---|
144 | ASSERT(element,"Invalid pointer in Formula::hasElement(element*)");
|
---|
145 | return hasElement(element->getNumber());
|
---|
146 | }
|
---|
147 |
|
---|
148 | bool Formula::hasElement(atomicNumber_t Z) const{
|
---|
149 | ASSERT(Z>0,"Invalid atomic Number");
|
---|
150 | ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel");
|
---|
151 | return elementCounts.size()>=Z && elementCounts[Z-1];
|
---|
152 | }
|
---|
153 |
|
---|
154 | bool Formula::hasElement(const string &shorthand) const{
|
---|
155 | const element * element = World::getInstance().getPeriode()->FindElement(shorthand);
|
---|
156 | return hasElement(element);
|
---|
157 | }
|
---|
158 |
|
---|
159 | void Formula::operator+=(const element *element){
|
---|
160 | ASSERT(element,"Invalid pointer in increment of Formula");
|
---|
161 | operator+=(element->getNumber());
|
---|
162 | }
|
---|
163 |
|
---|
164 | void Formula::operator+=(atomicNumber_t Z){
|
---|
165 | ASSERT(Z>0,"Invalid atomic Number");
|
---|
166 | ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel");
|
---|
167 | elementCounts.resize(max<atomicNumber_t>(Z,elementCounts.size()),0); // No-op when we already have the right size
|
---|
168 | // might need to update number of elements
|
---|
169 | if(!elementCounts[Z-1]){
|
---|
170 | numElements++;
|
---|
171 | }
|
---|
172 | elementCounts[Z-1]++; // atomic numbers start at 1
|
---|
173 | }
|
---|
174 |
|
---|
175 | void Formula::operator+=(const string &shorthand){
|
---|
176 | const element * element = World::getInstance().getPeriode()->FindElement(shorthand);
|
---|
177 | operator+=(element);
|
---|
178 | }
|
---|
179 |
|
---|
180 | void Formula::operator-=(const element *element){
|
---|
181 | ASSERT(element,"Invalid pointer in decrement of Formula");
|
---|
182 | operator-=(element->getNumber());
|
---|
183 | }
|
---|
184 |
|
---|
185 | void Formula::operator-=(atomicNumber_t Z){
|
---|
186 | ASSERT(Z>0,"Invalid atomic Number");
|
---|
187 | ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel");
|
---|
188 | ASSERT(elementCounts.size()>=Z && elementCounts[Z-1], "Element not in Formula upon decrement");
|
---|
189 | elementCounts[Z-1]--; // atomic numbers start at 1
|
---|
190 | // might need to update number of elements
|
---|
191 | if(!elementCounts[Z-1]){
|
---|
192 | numElements--;
|
---|
193 | // resize the Array if this was at the last position
|
---|
194 | if(Z==elementCounts.size()){
|
---|
195 | // find the first element from the back that is not equal to zero
|
---|
196 | set_t::reverse_iterator riter = find_if(elementCounts.rbegin(),
|
---|
197 | elementCounts.rend(),
|
---|
198 | bind1st(not_equal_to<mapped_type>(),0));
|
---|
199 | // see how many elements are in this range
|
---|
200 | set_t::reverse_iterator::difference_type diff = riter - elementCounts.rbegin();
|
---|
201 | elementCounts.resize(elementCounts.size()-diff);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | void Formula::operator-=(const string &shorthand){
|
---|
207 | const element * element = World::getInstance().getPeriode()->FindElement(shorthand);
|
---|
208 | operator-=(element);
|
---|
209 | }
|
---|
210 |
|
---|
211 | void Formula::addElements(const element *element,unsigned int count){
|
---|
212 | ASSERT(element,"Invalid pointer in Formula::addElements(element*)");
|
---|
213 | addElements(element->getNumber(),count);
|
---|
214 | }
|
---|
215 |
|
---|
216 | void Formula::addElements(atomicNumber_t Z,unsigned int count){
|
---|
217 | if(count==0) return;
|
---|
218 | ASSERT(Z>0,"Invalid atomic Number");
|
---|
219 | ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel");
|
---|
220 | elementCounts.resize(max<atomicNumber_t>(Z,elementCounts.size()),0); // No-op when we already have the right size
|
---|
221 | // might need to update number of elements
|
---|
222 | if(!elementCounts[Z-1]){
|
---|
223 | numElements++;
|
---|
224 | }
|
---|
225 | elementCounts[Z-1]+=count;
|
---|
226 | }
|
---|
227 |
|
---|
228 | void Formula::addElements(const string &shorthand,unsigned int count){
|
---|
229 | const element * element = World::getInstance().getPeriode()->FindElement(shorthand);
|
---|
230 | addElements(element,count);
|
---|
231 | }
|
---|
232 |
|
---|
233 | void Formula::addFormula(const Formula &formula,unsigned int n){
|
---|
234 | for(Formula::const_iterator iter=formula.begin();iter!=formula.end();++iter){
|
---|
235 | this->addElements(iter->first,iter->second*n);
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | enumeration<Formula::key_type> Formula::enumerateElements() const{
|
---|
240 | enumeration<key_type> res(1);
|
---|
241 | for(Formula::const_iterator iter=begin();iter!=end();++iter){
|
---|
242 | res.add(iter->first);
|
---|
243 | }
|
---|
244 | return res;
|
---|
245 | }
|
---|
246 |
|
---|
247 | const unsigned int Formula::operator[](const element *element) const{
|
---|
248 | ASSERT(element,"Invalid pointer in access of Formula");
|
---|
249 | return operator[](element->getNumber());
|
---|
250 | }
|
---|
251 |
|
---|
252 | const unsigned int Formula::operator[](atomicNumber_t Z) const{
|
---|
253 | ASSERT(Z>0,"Invalid atomic Number");
|
---|
254 | ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel");
|
---|
255 | if(elementCounts.size()<Z)
|
---|
256 | return 0;
|
---|
257 | return elementCounts[Z-1]; // atomic numbers start at 1
|
---|
258 | }
|
---|
259 |
|
---|
260 | const unsigned int Formula::operator[](string shorthand) const{
|
---|
261 | const element * element = World::getInstance().getPeriode()->FindElement(shorthand);
|
---|
262 | return operator[](element);
|
---|
263 | }
|
---|
264 |
|
---|
265 | bool Formula::operator==(const Formula &rhs) const{
|
---|
266 | // quick check... number of elements used
|
---|
267 | if(numElements != rhs.numElements){
|
---|
268 | return false;
|
---|
269 | }
|
---|
270 | // second quick check, size of vectors (== last element in formula)
|
---|
271 | if(elementCounts.size()!=rhs.elementCounts.size()){
|
---|
272 | return false;
|
---|
273 | }
|
---|
274 | // slow check: all elements
|
---|
275 | // direct access to internal structure means all element-counts have to be compared
|
---|
276 | // this avoids access to periodentafel to find elements though and is probably faster
|
---|
277 | // in total
|
---|
278 | return equal(elementCounts.begin(),
|
---|
279 | elementCounts.end(),
|
---|
280 | rhs.elementCounts.begin());
|
---|
281 | }
|
---|
282 |
|
---|
283 | bool Formula::operator!=(const Formula &rhs) const{
|
---|
284 | return !operator==(rhs);
|
---|
285 | }
|
---|
286 |
|
---|
287 | Formula::iterator Formula::begin(){
|
---|
288 | return iterator(elementCounts,0);
|
---|
289 | }
|
---|
290 | Formula::const_iterator Formula::begin() const{
|
---|
291 | // this is the only place where this is needed, so this is better than making it mutable
|
---|
292 | return const_iterator(const_cast<set_t&>(elementCounts),0);
|
---|
293 | }
|
---|
294 | Formula::iterator Formula::end(){
|
---|
295 | return iterator(elementCounts);
|
---|
296 | }
|
---|
297 | Formula::const_iterator Formula::end() const{
|
---|
298 | // this is the only place where this is needed, so this is better than making it mutable
|
---|
299 | return const_iterator(const_cast<set_t&>(elementCounts));
|
---|
300 | }
|
---|
301 |
|
---|
302 | void Formula::clear(){
|
---|
303 | elementCounts.clear();
|
---|
304 | numElements = 0;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**************** Iterator structure ********************/
|
---|
308 |
|
---|
309 | template <class result_type>
|
---|
310 | Formula::_iterator<result_type>::_iterator(set_t &_set) :
|
---|
311 | set(&_set)
|
---|
312 | {
|
---|
313 | pos=set->size();
|
---|
314 | }
|
---|
315 |
|
---|
316 | template <class result_type>
|
---|
317 | Formula::_iterator<result_type>::_iterator(set_t &_set,size_t _pos) :
|
---|
318 | set(&_set),pos(_pos)
|
---|
319 | {
|
---|
320 | ASSERT(pos<=set->size(),"invalid position in iterator construction");
|
---|
321 | while(pos<set->size() && (*set)[pos]==0) ++pos;
|
---|
322 | }
|
---|
323 |
|
---|
324 | template <class result_type>
|
---|
325 | Formula::_iterator<result_type>::_iterator(const _iterator &rhs) :
|
---|
326 | set(rhs.set),pos(rhs.pos)
|
---|
327 | {}
|
---|
328 |
|
---|
329 | template <class result_type>
|
---|
330 | Formula::_iterator<result_type>::~_iterator(){}
|
---|
331 |
|
---|
332 | template <class result_type>
|
---|
333 | Formula::_iterator<result_type>&
|
---|
334 | Formula::_iterator<result_type>::operator=(const _iterator<result_type> &rhs){
|
---|
335 | set=rhs.set;
|
---|
336 | pos=rhs.pos;
|
---|
337 | return *this;
|
---|
338 | }
|
---|
339 |
|
---|
340 | template <class result_type>
|
---|
341 | bool
|
---|
342 | Formula::_iterator<result_type>::operator==(const _iterator<result_type> &rhs){
|
---|
343 | return set==rhs.set && pos==rhs.pos;
|
---|
344 | }
|
---|
345 |
|
---|
346 | template <class result_type>
|
---|
347 | bool
|
---|
348 | Formula::_iterator<result_type>::operator!=(const _iterator<result_type> &rhs){
|
---|
349 | return !operator==(rhs);
|
---|
350 | }
|
---|
351 |
|
---|
352 | template <class result_type>
|
---|
353 | Formula::_iterator<result_type>
|
---|
354 | Formula::_iterator<result_type>::operator++(){
|
---|
355 | ASSERT(pos!=set->size(),"Incrementing Formula::iterator beyond end");
|
---|
356 | pos++;
|
---|
357 | while(pos<set->size() && (*set)[pos]==0) ++pos;
|
---|
358 | return *this;
|
---|
359 | }
|
---|
360 |
|
---|
361 | template <class result_type>
|
---|
362 | Formula::_iterator<result_type>
|
---|
363 | Formula::_iterator<result_type>::operator++(int){
|
---|
364 | Formula::_iterator<result_type> retval = *this;
|
---|
365 | ++(*this);
|
---|
366 | return retval;
|
---|
367 | }
|
---|
368 |
|
---|
369 | template <class result_type>
|
---|
370 | Formula::_iterator<result_type>
|
---|
371 | Formula::_iterator<result_type>::operator--(){
|
---|
372 | ASSERT(pos!=0,"Decrementing Formula::iterator beyond begin");
|
---|
373 | pos--;
|
---|
374 | while(pos>0 && (*set)[pos]==0) --pos;
|
---|
375 | return *this;
|
---|
376 | }
|
---|
377 |
|
---|
378 | template <class result_type>
|
---|
379 | Formula::_iterator<result_type>
|
---|
380 | Formula::_iterator<result_type>::operator--(int){
|
---|
381 | Formula::_iterator<result_type> retval = *this;
|
---|
382 | --(*this);
|
---|
383 | return retval;
|
---|
384 | }
|
---|
385 |
|
---|
386 | template <class result_type>
|
---|
387 | result_type
|
---|
388 | Formula::_iterator<result_type>::operator*(){
|
---|
389 | const element *element = World::getInstance().getPeriode()->FindElement(pos+1);
|
---|
390 | ASSERT(element,"Element with position of iterator not found");
|
---|
391 | return make_pair(element,(*set)[pos]);
|
---|
392 | }
|
---|
393 |
|
---|
394 | template <class result_type>
|
---|
395 | result_type*
|
---|
396 | Formula::_iterator<result_type>::operator->(){
|
---|
397 | // no one can keep this value around, so a static is ok to avoid temporaries
|
---|
398 | static value_type value=make_pair(reinterpret_cast<element*>(0),0); // no default constructor for std::pair
|
---|
399 | const element *element = World::getInstance().getPeriode()->FindElement(pos+1);
|
---|
400 | ASSERT(element,"Element with position of iterator not found");
|
---|
401 | value = make_pair(element,(*set)[pos]);
|
---|
402 | return &value;
|
---|
403 | }
|
---|
404 |
|
---|
405 | // explicit instantiation of all iterator template methods
|
---|
406 | // this is quite ugly, but there is no better way unless we expose iterator implementation
|
---|
407 |
|
---|
408 | // instantiate Formula::iterator
|
---|
409 | template Formula::iterator::_iterator(set_t&);
|
---|
410 | template Formula::iterator::_iterator(set_t&,size_t);
|
---|
411 | template Formula::iterator::_iterator(const Formula::iterator&);
|
---|
412 | template Formula::iterator::~_iterator();
|
---|
413 | template Formula::iterator &Formula::iterator::operator=(const Formula::iterator&);
|
---|
414 | template bool Formula::iterator::operator==(const Formula::iterator&);
|
---|
415 | template bool Formula::iterator::operator!=(const Formula::iterator&);
|
---|
416 | template Formula::iterator Formula::iterator::operator++();
|
---|
417 | template Formula::iterator Formula::iterator::operator++(int);
|
---|
418 | template Formula::iterator Formula::iterator::operator--();
|
---|
419 | template Formula::iterator Formula::iterator::operator--(int);
|
---|
420 | template Formula::value_type Formula::iterator::operator*();
|
---|
421 | template Formula::value_type *Formula::iterator::operator->();
|
---|
422 |
|
---|
423 | // instantiate Formula::const_iterator
|
---|
424 | template Formula::const_iterator::_iterator(set_t&);
|
---|
425 | template Formula::const_iterator::_iterator(set_t&,size_t);
|
---|
426 | template Formula::const_iterator::_iterator(const Formula::const_iterator&);
|
---|
427 | template Formula::const_iterator::~_iterator();
|
---|
428 | template Formula::const_iterator &Formula::const_iterator::operator=(const Formula::const_iterator&);
|
---|
429 | template bool Formula::const_iterator::operator==(const Formula::const_iterator&);
|
---|
430 | template bool Formula::const_iterator::operator!=(const Formula::const_iterator&);
|
---|
431 | template Formula::const_iterator Formula::const_iterator::operator++();
|
---|
432 | template Formula::Formula::const_iterator Formula::const_iterator::operator++(int);
|
---|
433 | template Formula::Formula::const_iterator Formula::const_iterator::operator--();
|
---|
434 | template Formula::Formula::const_iterator Formula::const_iterator::operator--(int);
|
---|
435 | template const Formula::value_type Formula::const_iterator::operator*();
|
---|
436 | template const Formula::value_type *Formula::const_iterator::operator->();
|
---|
437 |
|
---|
438 | /********************** I/O of Formulas ************************************************/
|
---|
439 |
|
---|
440 | std::ostream &operator<<(std::ostream &ost,const Formula &formula){
|
---|
441 | ost << formula.toString();
|
---|
442 | return ost;
|
---|
443 | }
|
---|