| [6f43ab] | 1 | /*
 | 
|---|
 | 2 |  * Formula.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Jul 21, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef FORMULA_HPP_
 | 
|---|
 | 9 | #define FORMULA_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | #include <vector>
 | 
|---|
 | 12 | #include <iosfwd>
 | 
|---|
 | 13 | #include <iterator>
 | 
|---|
 | 14 | 
 | 
|---|
| [d03bb1] | 15 | #include "Exceptions/ParseError.hpp"
 | 
|---|
 | 16 | 
 | 
|---|
| [6f43ab] | 17 | #include "types.hpp"
 | 
|---|
| [d223d5] | 18 | #include "Helpers/helpers.hpp"
 | 
|---|
| [6f43ab] | 19 | 
 | 
|---|
 | 20 | class element;
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | class Formula
 | 
|---|
 | 23 | {
 | 
|---|
 | 24 | public:
 | 
|---|
| [426f2a] | 25 |   typedef const element*                                key_type;
 | 
|---|
| [6f43ab] | 26 |   typedef unsigned int                                  mapped_type;
 | 
|---|
 | 27 |   typedef std::pair<key_type, mapped_type>              value_type;
 | 
|---|
 | 28 |   typedef std::vector<unsigned int>                     set_t;
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 |   template <class result_type>
 | 
|---|
 | 31 |   class _iterator : public std::iterator <std::bidirectional_iterator_tag,
 | 
|---|
 | 32 |                                          result_type>
 | 
|---|
 | 33 |   {
 | 
|---|
 | 34 |   public:
 | 
|---|
 | 35 |     _iterator(set_t&);
 | 
|---|
 | 36 |     _iterator(set_t&,size_t);
 | 
|---|
| [a6d6a9] | 37 |     _iterator(const _iterator &rhs);
 | 
|---|
| [6f43ab] | 38 |     ~_iterator();
 | 
|---|
 | 39 |     _iterator   &operator=(const _iterator &);
 | 
|---|
 | 40 |     bool         operator==(const _iterator&);
 | 
|---|
 | 41 |     bool         operator!=(const _iterator&);
 | 
|---|
 | 42 |     _iterator    operator++();
 | 
|---|
 | 43 |     _iterator    operator++(int);
 | 
|---|
 | 44 |     _iterator    operator--();
 | 
|---|
 | 45 |     _iterator    operator--(int);
 | 
|---|
 | 46 |     result_type  operator*();
 | 
|---|
 | 47 |     result_type *operator->();
 | 
|---|
 | 48 |   private:
 | 
|---|
 | 49 |     set_t *set;
 | 
|---|
 | 50 |     size_t pos; // cannot use an iterator, because we need a position to access the element
 | 
|---|
 | 51 |   };
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 |   typedef _iterator<value_type>                 iterator;
 | 
|---|
 | 54 |   typedef _iterator<const value_type>           const_iterator;
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 |   Formula();
 | 
|---|
| [d03bb1] | 57 |   Formula(const std::string&);
 | 
|---|
| [6f43ab] | 58 |   Formula(const Formula&);
 | 
|---|
 | 59 |   virtual ~Formula();
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 |   Formula &operator=(const Formula&);
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 |   std::string toString() const;
 | 
|---|
| [d03bb1] | 64 |   void fromString(const std::string&) throw(ParseError);
 | 
|---|
| [6f43ab] | 65 |   bool checkOut(std::ostream*) const;
 | 
|---|
 | 66 | 
 | 
|---|
| [a6d6a9] | 67 |   unsigned int getElementCount() const;
 | 
|---|
| [426f2a] | 68 |   bool hasElement(key_type)             const;
 | 
|---|
| [6f43ab] | 69 |   bool hasElement(atomicNumber_t)       const;
 | 
|---|
 | 70 |   bool hasElement(const std::string&)   const;
 | 
|---|
 | 71 | 
 | 
|---|
| [426f2a] | 72 |   void operator+=(key_type);
 | 
|---|
| [6f43ab] | 73 |   void operator+=(atomicNumber_t);
 | 
|---|
 | 74 |   void operator+=(const std::string&);
 | 
|---|
 | 75 | 
 | 
|---|
| [426f2a] | 76 |   void operator-=(key_type);
 | 
|---|
| [6f43ab] | 77 |   void operator-=(atomicNumber_t);
 | 
|---|
 | 78 |   void operator-=(const std::string&);
 | 
|---|
 | 79 | 
 | 
|---|
| [426f2a] | 80 |   void addElements(key_type,unsigned int);
 | 
|---|
| [d03bb1] | 81 |   void addElements(atomicNumber_t,unsigned int);
 | 
|---|
 | 82 |   void addElements(const std::string&,unsigned int);
 | 
|---|
 | 83 | 
 | 
|---|
| [9d5803] | 84 |   void addFormula(const Formula&,unsigned int);
 | 
|---|
 | 85 | 
 | 
|---|
| [426f2a] | 86 |   enumeration<key_type> enumerateElements() const;
 | 
|---|
 | 87 | 
 | 
|---|
| [6f43ab] | 88 |   // only const versions, because someone might try to increment a previously
 | 
|---|
 | 89 |   // not set element
 | 
|---|
| [426f2a] | 90 |   const unsigned int operator[](key_type) const;
 | 
|---|
| [a6d6a9] | 91 |   const unsigned int operator[](atomicNumber_t) const;
 | 
|---|
 | 92 |   const unsigned int operator[](std::string)    const;
 | 
|---|
| [6f43ab] | 93 | 
 | 
|---|
 | 94 |   bool operator==(const Formula&) const;
 | 
|---|
 | 95 |   bool operator!=(const Formula&) const;
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 |   iterator       begin();
 | 
|---|
 | 98 |   const_iterator begin() const;
 | 
|---|
 | 99 |   iterator       end();
 | 
|---|
 | 100 |   const_iterator end()   const;
 | 
|---|
 | 101 | 
 | 
|---|
| [d03bb1] | 102 |   void clear();
 | 
|---|
 | 103 | 
 | 
|---|
| [6f43ab] | 104 | private:
 | 
|---|
| [4d1d43] | 105 |   void parseFromString(std::string::const_iterator&,std::string::const_iterator&,char) throw(ParseError);
 | 
|---|
 | 106 |   int parseMaybeNumber(std::string::const_iterator &it,std::string::const_iterator &end) throw(ParseError);
 | 
|---|
| [d8a0ec] | 107 |   // this contains all counts of elements in the formula
 | 
|---|
 | 108 |   // the size of the actual structure might be used in comparisons
 | 
|---|
 | 109 |   // so be sure not to keep any zero elements at the end
 | 
|---|
 | 110 |   // e.g. {5;4;0;0;0;2;1} or {0;1;0;0;2} are ok, but {1;2;3;0;0;0} is bad
 | 
|---|
 | 111 |   set_t elementCounts;
 | 
|---|
| [6f43ab] | 112 |   unsigned int numElements;
 | 
|---|
 | 113 | };
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 | std::ostream &operator<<(std::ostream&,const Formula&);
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | #endif /* FORMULA_HPP_ */
 | 
|---|