| [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"
 | 
|---|
 | 18 | 
 | 
|---|
 | 19 | class element;
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | class Formula
 | 
|---|
 | 22 | {
 | 
|---|
 | 23 | public:
 | 
|---|
 | 24 |   typedef element*                                      key_type;
 | 
|---|
 | 25 |   typedef unsigned int                                  mapped_type;
 | 
|---|
 | 26 |   typedef std::pair<key_type, mapped_type>              value_type;
 | 
|---|
 | 27 |   typedef std::vector<unsigned int>                     set_t;
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 |   template <class result_type>
 | 
|---|
 | 30 |   class _iterator : public std::iterator <std::bidirectional_iterator_tag,
 | 
|---|
 | 31 |                                          result_type>
 | 
|---|
 | 32 |   {
 | 
|---|
 | 33 |   public:
 | 
|---|
 | 34 |     _iterator(set_t&);
 | 
|---|
 | 35 |     _iterator(set_t&,size_t);
 | 
|---|
| [a6d6a9] | 36 |     _iterator(const _iterator &rhs);
 | 
|---|
| [6f43ab] | 37 |     ~_iterator();
 | 
|---|
 | 38 |     _iterator   &operator=(const _iterator &);
 | 
|---|
 | 39 |     bool         operator==(const _iterator&);
 | 
|---|
 | 40 |     bool         operator!=(const _iterator&);
 | 
|---|
 | 41 |     _iterator    operator++();
 | 
|---|
 | 42 |     _iterator    operator++(int);
 | 
|---|
 | 43 |     _iterator    operator--();
 | 
|---|
 | 44 |     _iterator    operator--(int);
 | 
|---|
 | 45 |     result_type  operator*();
 | 
|---|
 | 46 |     result_type *operator->();
 | 
|---|
 | 47 |   private:
 | 
|---|
 | 48 |     set_t *set;
 | 
|---|
 | 49 |     size_t pos; // cannot use an iterator, because we need a position to access the element
 | 
|---|
 | 50 |   };
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 |   typedef _iterator<value_type>                 iterator;
 | 
|---|
 | 53 |   typedef _iterator<const value_type>           const_iterator;
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |   Formula();
 | 
|---|
| [d03bb1] | 56 |   Formula(const std::string&);
 | 
|---|
| [6f43ab] | 57 |   Formula(const Formula&);
 | 
|---|
 | 58 |   virtual ~Formula();
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 |   Formula &operator=(const Formula&);
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |   std::string toString() const;
 | 
|---|
| [d03bb1] | 63 |   void fromString(const std::string&) throw(ParseError);
 | 
|---|
| [6f43ab] | 64 |   bool checkOut(std::ostream*) const;
 | 
|---|
 | 65 | 
 | 
|---|
| [a6d6a9] | 66 |   unsigned int getElementCount() const;
 | 
|---|
| [6f43ab] | 67 |   bool hasElement(const element*)       const;
 | 
|---|
 | 68 |   bool hasElement(atomicNumber_t)       const;
 | 
|---|
 | 69 |   bool hasElement(const std::string&)   const;
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 |   void operator+=(const element*);
 | 
|---|
 | 72 |   void operator+=(atomicNumber_t);
 | 
|---|
 | 73 |   void operator+=(const std::string&);
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 |   void operator-=(const element*);
 | 
|---|
 | 76 |   void operator-=(atomicNumber_t);
 | 
|---|
 | 77 |   void operator-=(const std::string&);
 | 
|---|
 | 78 | 
 | 
|---|
| [d03bb1] | 79 |   void addElements(const element*,unsigned int);
 | 
|---|
 | 80 |   void addElements(atomicNumber_t,unsigned int);
 | 
|---|
 | 81 |   void addElements(const std::string&,unsigned int);
 | 
|---|
 | 82 | 
 | 
|---|
| [6f43ab] | 83 |   // only const versions, because someone might try to increment a previously
 | 
|---|
 | 84 |   // not set element
 | 
|---|
| [a6d6a9] | 85 |   const unsigned int operator[](const element*) const;
 | 
|---|
 | 86 |   const unsigned int operator[](atomicNumber_t) const;
 | 
|---|
 | 87 |   const unsigned int operator[](std::string)    const;
 | 
|---|
| [6f43ab] | 88 | 
 | 
|---|
 | 89 |   bool operator==(const Formula&) const;
 | 
|---|
 | 90 |   bool operator!=(const Formula&) const;
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |   iterator       begin();
 | 
|---|
 | 93 |   const_iterator begin() const;
 | 
|---|
 | 94 |   iterator       end();
 | 
|---|
 | 95 |   const_iterator end()   const;
 | 
|---|
 | 96 | 
 | 
|---|
| [d03bb1] | 97 |   void clear();
 | 
|---|
 | 98 | 
 | 
|---|
| [6f43ab] | 99 | private:
 | 
|---|
| [d8a0ec] | 100 |   // this contains all counts of elements in the formula
 | 
|---|
 | 101 |   // the size of the actual structure might be used in comparisons
 | 
|---|
 | 102 |   // so be sure not to keep any zero elements at the end
 | 
|---|
 | 103 |   // 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
 | 
|---|
 | 104 |   set_t elementCounts;
 | 
|---|
| [6f43ab] | 105 |   unsigned int numElements;
 | 
|---|
 | 106 | };
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | std::ostream &operator<<(std::ostream&,const Formula&);
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 | #endif /* FORMULA_HPP_ */
 | 
|---|