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