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