| 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 | bool removeValue(const IndexSet::ptr &ptr) {
|
|---|
| 70 | if (isIndexSetPresent(ptr)) {
|
|---|
| 71 | Lookup.erase(ptr);
|
|---|
| 72 | return true;
|
|---|
| 73 | } else {
|
|---|
| 74 | return false;
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /** Checks whether a given IndexSet \a ptr is known.
|
|---|
| 79 | *
|
|---|
| 80 | * @param ptr IndexSet to check
|
|---|
| 81 | * @return true - set present, false - else
|
|---|
| 82 | */
|
|---|
| 83 | bool isIndexSetPresent(const IndexSet::ptr& ptr) const {
|
|---|
| 84 | typename Lookup_t::const_iterator iter = Lookup.find(ptr);
|
|---|
| 85 | return (iter != Lookup.end());
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | private:
|
|---|
| 89 | //!> typedef for the internal lookup
|
|---|
| 90 | typedef std::map< IndexSet::ptr, typename SetValue<T>::ptr, IndexSetContainer::Comparator_t > Lookup_t;
|
|---|
| 91 | //!> internal map to provide the lookup
|
|---|
| 92 | Lookup_t Lookup;
|
|---|
| 93 |
|
|---|
| 94 | public:
|
|---|
| 95 | //!> typedef to const iterator of internal map such that easy const traversal is possible
|
|---|
| 96 | typedef typename Lookup_t::const_iterator const_iterator;
|
|---|
| 97 |
|
|---|
| 98 | /** Getter for first node of iternal map.
|
|---|
| 99 | *
|
|---|
| 100 | * @return Lookup::begin()
|
|---|
| 101 | */
|
|---|
| 102 | const_iterator begin() const {
|
|---|
| 103 | return Lookup.begin();
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** Getter for last and one node of iternal map.
|
|---|
| 107 | *
|
|---|
| 108 | * @return Lookup::end()
|
|---|
| 109 | */
|
|---|
| 110 | const_iterator end() const {
|
|---|
| 111 | return Lookup.end();
|
|---|
| 112 | }
|
|---|
| 113 | };
|
|---|
| 114 |
|
|---|
| 115 | #endif /* SETVALUEMAP_HPP_ */
|
|---|