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