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