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