| [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 |  | 
|---|
| [56f73b] | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
|  | 15 |  | 
|---|
|  | 16 |  | 
|---|
| [6f43ab] | 17 | #include <vector> | 
|---|
|  | 18 | #include <iosfwd> | 
|---|
|  | 19 | #include <iterator> | 
|---|
|  | 20 |  | 
|---|
|  | 21 | #include "types.hpp" | 
|---|
|  | 22 |  | 
|---|
| [ad011c] | 23 | #include "CodePatterns/enumeration.hpp" | 
|---|
| [a0064e] | 24 |  | 
|---|
| [b88fe4] | 25 | #include <exception> | 
|---|
| [e5c233] | 26 | #include <boost/exception/all.hpp> | 
|---|
| [b88fe4] | 27 |  | 
|---|
|  | 28 | /** ============================= Begin of Exceptions ===================== */ | 
|---|
|  | 29 |  | 
|---|
|  | 30 | /** Base class for all exceptions regarding Formula. | 
|---|
|  | 31 | * | 
|---|
|  | 32 | */ | 
|---|
|  | 33 | struct FormulaException : virtual std::exception, virtual boost::exception { }; | 
|---|
| [6f43ab] | 34 |  | 
|---|
| [b88fe4] | 35 | /** Exception information for FormulaException: string. | 
|---|
|  | 36 | */ | 
|---|
|  | 37 | typedef boost::error_info<struct tag_FormulaString, const std::string> FormulaString; | 
|---|
|  | 38 |  | 
|---|
|  | 39 | /** Exception thrown when a string cannot be parsed as a formula. | 
|---|
|  | 40 | */ | 
|---|
|  | 41 | struct FormulaStringParseException : virtual FormulaException { }; | 
|---|
|  | 42 |  | 
|---|
|  | 43 | /** ============================== end of Exceptions ====================== */ | 
|---|
|  | 44 |  | 
|---|
|  | 45 | class element; | 
|---|
| [a0064e] | 46 |  | 
|---|
| [6f43ab] | 47 | class Formula | 
|---|
|  | 48 | { | 
|---|
|  | 49 | public: | 
|---|
| [426f2a] | 50 | typedef const element*                                key_type; | 
|---|
| [6f43ab] | 51 | typedef unsigned int                                  mapped_type; | 
|---|
|  | 52 | typedef std::pair<key_type, mapped_type>              value_type; | 
|---|
|  | 53 | typedef std::vector<unsigned int>                     set_t; | 
|---|
|  | 54 |  | 
|---|
|  | 55 | template <class result_type> | 
|---|
|  | 56 | class _iterator : public std::iterator <std::bidirectional_iterator_tag, | 
|---|
|  | 57 | result_type> | 
|---|
|  | 58 | { | 
|---|
|  | 59 | public: | 
|---|
|  | 60 | _iterator(set_t&); | 
|---|
|  | 61 | _iterator(set_t&,size_t); | 
|---|
| [a6d6a9] | 62 | _iterator(const _iterator &rhs); | 
|---|
| [6f43ab] | 63 | ~_iterator(); | 
|---|
|  | 64 | _iterator   &operator=(const _iterator &); | 
|---|
|  | 65 | bool         operator==(const _iterator&); | 
|---|
|  | 66 | bool         operator!=(const _iterator&); | 
|---|
|  | 67 | _iterator    operator++(); | 
|---|
|  | 68 | _iterator    operator++(int); | 
|---|
|  | 69 | _iterator    operator--(); | 
|---|
|  | 70 | _iterator    operator--(int); | 
|---|
|  | 71 | result_type  operator*(); | 
|---|
|  | 72 | result_type *operator->(); | 
|---|
|  | 73 | private: | 
|---|
|  | 74 | set_t *set; | 
|---|
|  | 75 | size_t pos; // cannot use an iterator, because we need a position to access the element | 
|---|
|  | 76 | }; | 
|---|
|  | 77 |  | 
|---|
|  | 78 | typedef _iterator<value_type>                 iterator; | 
|---|
|  | 79 | typedef _iterator<const value_type>           const_iterator; | 
|---|
|  | 80 |  | 
|---|
|  | 81 | Formula(); | 
|---|
| [d03bb1] | 82 | Formula(const std::string&); | 
|---|
| [6f43ab] | 83 | Formula(const Formula&); | 
|---|
|  | 84 | virtual ~Formula(); | 
|---|
|  | 85 |  | 
|---|
|  | 86 | Formula &operator=(const Formula&); | 
|---|
|  | 87 |  | 
|---|
|  | 88 | std::string toString() const; | 
|---|
| [266875] | 89 | void fromString(const std::string&) throw(FormulaStringParseException); | 
|---|
| [6f43ab] | 90 |  | 
|---|
| [a6d6a9] | 91 | unsigned int getElementCount() const; | 
|---|
| [426f2a] | 92 | bool hasElement(key_type)             const; | 
|---|
| [6f43ab] | 93 | bool hasElement(atomicNumber_t)       const; | 
|---|
|  | 94 | bool hasElement(const std::string&)   const; | 
|---|
|  | 95 |  | 
|---|
| [426f2a] | 96 | void operator+=(key_type); | 
|---|
| [6f43ab] | 97 | void operator+=(atomicNumber_t); | 
|---|
|  | 98 | void operator+=(const std::string&); | 
|---|
|  | 99 |  | 
|---|
| [426f2a] | 100 | void operator-=(key_type); | 
|---|
| [6f43ab] | 101 | void operator-=(atomicNumber_t); | 
|---|
|  | 102 | void operator-=(const std::string&); | 
|---|
|  | 103 |  | 
|---|
| [426f2a] | 104 | void addElements(key_type,unsigned int); | 
|---|
| [d03bb1] | 105 | void addElements(atomicNumber_t,unsigned int); | 
|---|
|  | 106 | void addElements(const std::string&,unsigned int); | 
|---|
|  | 107 |  | 
|---|
| [9d5803] | 108 | void addFormula(const Formula&,unsigned int); | 
|---|
|  | 109 |  | 
|---|
| [426f2a] | 110 | enumeration<key_type> enumerateElements() const; | 
|---|
|  | 111 |  | 
|---|
| [6f43ab] | 112 | // only const versions, because someone might try to increment a previously | 
|---|
|  | 113 | // not set element | 
|---|
| [426f2a] | 114 | const unsigned int operator[](key_type) const; | 
|---|
| [a6d6a9] | 115 | const unsigned int operator[](atomicNumber_t) const; | 
|---|
|  | 116 | const unsigned int operator[](std::string)    const; | 
|---|
| [6f43ab] | 117 |  | 
|---|
|  | 118 | bool operator==(const Formula&) const; | 
|---|
|  | 119 | bool operator!=(const Formula&) const; | 
|---|
|  | 120 |  | 
|---|
|  | 121 | iterator       begin(); | 
|---|
|  | 122 | const_iterator begin() const; | 
|---|
|  | 123 | iterator       end(); | 
|---|
|  | 124 | const_iterator end()   const; | 
|---|
|  | 125 |  | 
|---|
| [d03bb1] | 126 | void clear(); | 
|---|
|  | 127 |  | 
|---|
| [6f43ab] | 128 | private: | 
|---|
| [266875] | 129 | void parseFromString(std::string::const_iterator&,std::string::const_iterator&,char) throw(FormulaStringParseException); | 
|---|
|  | 130 | int parseMaybeNumber(std::string::const_iterator &it,std::string::const_iterator &end) throw(FormulaStringParseException); | 
|---|
| [d8a0ec] | 131 | // this contains all counts of elements in the formula | 
|---|
|  | 132 | // the size of the actual structure might be used in comparisons | 
|---|
|  | 133 | // so be sure not to keep any zero elements at the end | 
|---|
|  | 134 | // 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 | 
|---|
|  | 135 | set_t elementCounts; | 
|---|
| [6f43ab] | 136 | unsigned int numElements; | 
|---|
|  | 137 | }; | 
|---|
|  | 138 |  | 
|---|
|  | 139 | std::ostream &operator<<(std::ostream&,const Formula&); | 
|---|
|  | 140 |  | 
|---|
|  | 141 | #endif /* FORMULA_HPP_ */ | 
|---|