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