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