| 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 |  | 
|---|
| 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); | 
|---|
| 36 | ~_iterator(); | 
|---|
| 37 | _iterator   &operator=(const _iterator &); | 
|---|
| 38 | bool         operator==(const _iterator&); | 
|---|
| 39 | bool         operator!=(const _iterator&); | 
|---|
| 40 | _iterator    operator++(); | 
|---|
| 41 | _iterator    operator++(int); | 
|---|
| 42 | _iterator    operator--(); | 
|---|
| 43 | _iterator    operator--(int); | 
|---|
| 44 | result_type  operator*(); | 
|---|
| 45 | result_type *operator->(); | 
|---|
| 46 | private: | 
|---|
| 47 | set_t *set; | 
|---|
| 48 | size_t pos; // cannot use an iterator, because we need a position to access the element | 
|---|
| 49 | }; | 
|---|
| 50 |  | 
|---|
| 51 | typedef _iterator<value_type>                 iterator; | 
|---|
| 52 | typedef _iterator<const value_type>           const_iterator; | 
|---|
| 53 | typedef std::reverse_iterator<iterator>       reverse_iterator; | 
|---|
| 54 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | 
|---|
| 55 |  | 
|---|
| 56 | Formula(); | 
|---|
| 57 | Formula(const std::string&); | 
|---|
| 58 | Formula(const Formula&); | 
|---|
| 59 | virtual ~Formula(); | 
|---|
| 60 |  | 
|---|
| 61 | Formula &operator=(const Formula&); | 
|---|
| 62 |  | 
|---|
| 63 | std::string toString() const; | 
|---|
| 64 | void fromString(const std::string&) throw(ParseError); | 
|---|
| 65 | bool checkOut(std::ostream*) const; | 
|---|
| 66 |  | 
|---|
| 67 | unsigned int getElementCount(); | 
|---|
| 68 | bool hasElement(const element*)       const; | 
|---|
| 69 | bool hasElement(atomicNumber_t)       const; | 
|---|
| 70 | bool hasElement(const std::string&)   const; | 
|---|
| 71 |  | 
|---|
| 72 | void operator+=(const element*); | 
|---|
| 73 | void operator+=(atomicNumber_t); | 
|---|
| 74 | void operator+=(const std::string&); | 
|---|
| 75 |  | 
|---|
| 76 | void operator-=(const element*); | 
|---|
| 77 | void operator-=(atomicNumber_t); | 
|---|
| 78 | void operator-=(const std::string&); | 
|---|
| 79 |  | 
|---|
| 80 | void addElements(const element*,unsigned int); | 
|---|
| 81 | void addElements(atomicNumber_t,unsigned int); | 
|---|
| 82 | void addElements(const std::string&,unsigned int); | 
|---|
| 83 |  | 
|---|
| 84 | // only const versions, because someone might try to increment a previously | 
|---|
| 85 | // not set element | 
|---|
| 86 | const unsigned int &operator[](const element*) const; | 
|---|
| 87 | const unsigned int &operator[](atomicNumber_t) const; | 
|---|
| 88 | const unsigned int &operator[](std::string)    const; | 
|---|
| 89 |  | 
|---|
| 90 | bool operator==(const Formula&) const; | 
|---|
| 91 | bool operator!=(const Formula&) const; | 
|---|
| 92 |  | 
|---|
| 93 | iterator       begin(); | 
|---|
| 94 | const_iterator begin() const; | 
|---|
| 95 | iterator       end(); | 
|---|
| 96 | const_iterator end()   const; | 
|---|
| 97 |  | 
|---|
| 98 | reverse_iterator       rbegin(); | 
|---|
| 99 | const_reverse_iterator rbegin() const; | 
|---|
| 100 | reverse_iterator       rend(); | 
|---|
| 101 | const_reverse_iterator rend()   const; | 
|---|
| 102 |  | 
|---|
| 103 | void clear(); | 
|---|
| 104 |  | 
|---|
| 105 | private: | 
|---|
| 106 | mutable set_t elementCounts; // we might need to resize even at const places | 
|---|
| 107 | unsigned int numElements; | 
|---|
| 108 | }; | 
|---|
| 109 |  | 
|---|
| 110 | std::ostream &operator<<(std::ostream&,const Formula&); | 
|---|
| 111 |  | 
|---|
| 112 | #endif /* FORMULA_HPP_ */ | 
|---|