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