| 1 | /*
 | 
|---|
| 2 |  * SetValueMap.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jul 4, 2012
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef SETVALUEMAP_HPP_
 | 
|---|
| 9 | #define SETVALUEMAP_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | // include config.h
 | 
|---|
| 13 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 14 | #include <config.h>
 | 
|---|
| 15 | #endif
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <map>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "IndexSet.hpp"
 | 
|---|
| 20 | #include "SetValue.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | class SetValueMapTest;
 | 
|---|
| 25 | 
 | 
|---|
| 26 | /** This class represents a map from IndexSet to SetValue<T>.
 | 
|---|
| 27 |  *
 | 
|---|
| 28 |  * Each SetValue knows its IndexSet but we also need the inverse
 | 
|---|
| 29 |  * association from IndexSet to SetValue.
 | 
|---|
| 30 |  *
 | 
|---|
| 31 |  */
 | 
|---|
| 32 | template <typename T>
 | 
|---|
| 33 | class SetValueMap
 | 
|---|
| 34 | {
 | 
|---|
| 35 |   //!> grant unit access
 | 
|---|
| 36 |   friend class SetValueMapTest;
 | 
|---|
| 37 | public:
 | 
|---|
| 38 |   /** Either adds a new SetValue<T> to the map or changes the value the present
 | 
|---|
| 39 |    * instance.
 | 
|---|
| 40 |    *
 | 
|---|
| 41 |    * @param ptr associated IndexSet
 | 
|---|
| 42 |    * @param _value new value of the SetValue
 | 
|---|
| 43 |    */
 | 
|---|
| 44 |   void addValue(const IndexSet::ptr& ptr, const T &_value) {
 | 
|---|
| 45 |     typename Lookup_t::iterator iter = Lookup.find(ptr);
 | 
|---|
| 46 |     if (iter != Lookup.end()) {
 | 
|---|
| 47 |       iter->second->setValue(_value);
 | 
|---|
| 48 |     } else {
 | 
|---|
| 49 |       Lookup.insert( std::make_pair( ptr, typename SetValue<T>::ptr(new SetValue<T>(ptr, _value)) ) );
 | 
|---|
| 50 |     }
 | 
|---|
| 51 |   }
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   /** Getter for the SetValue to a specific IndexSet.
 | 
|---|
| 54 |    *
 | 
|---|
| 55 |    * @param ptr IndexSet
 | 
|---|
| 56 |    * @return SetValue associated with this IndexSet
 | 
|---|
| 57 |    */
 | 
|---|
| 58 |   typename SetValue<T>::ptr & getValue(const IndexSet::ptr &ptr) {
 | 
|---|
| 59 |     ASSERT( isIndexSetPresent(ptr),
 | 
|---|
| 60 |         "SetValueMap<T>::getValue() - IndexSet "+toString(*ptr)+" is unknown.");
 | 
|---|
| 61 |     typename Lookup_t::iterator iter = Lookup.find(ptr);
 | 
|---|
| 62 |     ASSERT( *iter->second->getIndexSet() == *ptr,
 | 
|---|
| 63 |         "SetValueMap<T>::getValue() - returned SetValue associated to set "+
 | 
|---|
| 64 |         toString(*iter->second->getIndexSet())+"does not match with desired set "
 | 
|---|
| 65 |         +toString(*ptr)+".");
 | 
|---|
| 66 |     return iter->second;
 | 
|---|
| 67 |   }
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   /** Getter for the SetValue to a specific IndexSet.
 | 
|---|
| 70 |    *
 | 
|---|
| 71 |    * @param ptr IndexSet
 | 
|---|
| 72 |    * @return SetValue associated with this IndexSet
 | 
|---|
| 73 |    */
 | 
|---|
| 74 |   const typename SetValue<T>::ptr & getConstValue(const IndexSet::ptr &ptr) const {
 | 
|---|
| 75 |     ASSERT( isIndexSetPresent(ptr),
 | 
|---|
| 76 |         "SetValueMap<T>::getValue() - IndexSet "+toString(*ptr)+" is unknown.");
 | 
|---|
| 77 |     typename Lookup_t::const_iterator iter = Lookup.find(ptr);
 | 
|---|
| 78 |     ASSERT( *iter->second->getIndexSet() == *ptr,
 | 
|---|
| 79 |         "SetValueMap<T>::getValue() - returned SetValue associated to set "+
 | 
|---|
| 80 |         toString(*iter->second->getIndexSet())+"does not match with desired set "
 | 
|---|
| 81 |         +toString(*ptr)+".");
 | 
|---|
| 82 |     return iter->second;
 | 
|---|
| 83 |   }
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   bool removeValue(const IndexSet::ptr &ptr) {
 | 
|---|
| 86 |     if (isIndexSetPresent(ptr)) {
 | 
|---|
| 87 |       Lookup.erase(ptr);
 | 
|---|
| 88 |       return true;
 | 
|---|
| 89 |     } else {
 | 
|---|
| 90 |       return false;
 | 
|---|
| 91 |     }
 | 
|---|
| 92 |   }
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   /** Checks whether a given IndexSet \a ptr is known.
 | 
|---|
| 95 |    *
 | 
|---|
| 96 |    * @param ptr IndexSet to check
 | 
|---|
| 97 |    * @return true - set present, false - else
 | 
|---|
| 98 |    */
 | 
|---|
| 99 |   bool isIndexSetPresent(const IndexSet::ptr& ptr) const {
 | 
|---|
| 100 |     typename Lookup_t::const_iterator iter = Lookup.find(ptr);
 | 
|---|
| 101 |     return (iter != Lookup.end());
 | 
|---|
| 102 |   }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | private:
 | 
|---|
| 105 |   //!> typedef for the internal lookup
 | 
|---|
| 106 |   typedef std::map< IndexSet::ptr, typename SetValue<T>::ptr, IndexSetContainer::Comparator_t > Lookup_t;
 | 
|---|
| 107 |   //!> internal map to provide the lookup
 | 
|---|
| 108 |   Lookup_t Lookup;
 | 
|---|
| 109 | 
 | 
|---|
| 110 | public:
 | 
|---|
| 111 |   //!> typedef to const iterator of internal map such that easy const traversal is possible
 | 
|---|
| 112 |   typedef typename Lookup_t::const_iterator const_iterator;
 | 
|---|
| 113 | 
 | 
|---|
| 114 |   /** Getter for first node of iternal map.
 | 
|---|
| 115 |    *
 | 
|---|
| 116 |    * @return Lookup::begin()
 | 
|---|
| 117 |    */
 | 
|---|
| 118 |   const_iterator begin() const {
 | 
|---|
| 119 |     return Lookup.begin();
 | 
|---|
| 120 |   }
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   /** Getter for last and one node of iternal map.
 | 
|---|
| 123 |    *
 | 
|---|
| 124 |    * @return Lookup::end()
 | 
|---|
| 125 |    */
 | 
|---|
| 126 |   const_iterator end() const {
 | 
|---|
| 127 |     return Lookup.end();
 | 
|---|
| 128 |   }
 | 
|---|
| 129 | };
 | 
|---|
| 130 | 
 | 
|---|
| 131 | #endif /* SETVALUEMAP_HPP_ */
 | 
|---|