| [d699de] | 1 | /*
 | 
|---|
 | 2 |  * SetValue.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Jun 25, 2012
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef SETVALUE_HPP_
 | 
|---|
 | 9 | #define SETVALUE_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | 
 | 
|---|
 | 12 | // include config.h
 | 
|---|
 | 13 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 14 | #include <config.h>
 | 
|---|
 | 15 | #endif
 | 
|---|
 | 16 | 
 | 
|---|
| [30abdc] | 17 | #include <boost/function.hpp>
 | 
|---|
 | 18 | #include <boost/shared_ptr.hpp>
 | 
|---|
 | 19 | 
 | 
|---|
| [d699de] | 20 | #include "IndexSet.hpp"
 | 
|---|
| [30abdc] | 21 | #include "IndexSetContainer.hpp"
 | 
|---|
| [d699de] | 22 | 
 | 
|---|
| [30abdc] | 23 | #include "CodePatterns/Cacheable.hpp"
 | 
|---|
 | 24 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [7bed4e] | 25 | #include "CodePatterns/Observer/Observable.hpp"
 | 
|---|
 | 26 | 
 | 
|---|
| [30abdc] | 27 | class SetValueTest;
 | 
|---|
 | 28 | 
 | 
|---|
| [d699de] | 29 | template <class T>
 | 
|---|
| [7bed4e] | 30 | class SetValue : public Observable
 | 
|---|
| [d699de] | 31 | {
 | 
|---|
| [30abdc] | 32 |   //!> grant unit test access to allow setting static function
 | 
|---|
 | 33 |   friend class SetValueTest;
 | 
|---|
| [1e7dd4] | 34 |   //!> grant OrthogonalSummation access to static lookup functions
 | 
|---|
 | 35 |   template <class TT> friend class OrthogonalSummation;
 | 
|---|
| [d699de] | 36 | public:
 | 
|---|
| [30abdc] | 37 |   typedef boost::shared_ptr< SetValue<T> > ptr;
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 |   SetValue(const IndexSet_ptr &_indices, const T &_value) :
 | 
|---|
| [7bed4e] | 40 |     Observable("SubsetValue"),
 | 
|---|
| [d699de] | 41 |     value(_value),
 | 
|---|
| [30abdc] | 42 |     indices(_indices),
 | 
|---|
 | 43 |     contribution(this,boost::bind(&SetValue<T>::calcSum,this),"contribution")
 | 
|---|
| [d699de] | 44 |   {};
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 |   SetValue & operator+=(const SetValue &other) {
 | 
|---|
| [30abdc] | 47 |     if (indices->contains(*other.indices)) {
 | 
|---|
 | 48 |       OBSERVE;
 | 
|---|
| [d699de] | 49 |       value += other.value;
 | 
|---|
| [30abdc] | 50 |     }
 | 
|---|
| [d699de] | 51 |     return *this;
 | 
|---|
 | 52 |   }
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 |   SetValue & operator-=(const SetValue &other) {
 | 
|---|
| [30abdc] | 55 |     if (indices->contains(*other.indices)) {
 | 
|---|
 | 56 |       OBSERVE;
 | 
|---|
| [d699de] | 57 |       value -= other.value;
 | 
|---|
| [30abdc] | 58 |     }
 | 
|---|
| [d699de] | 59 |     return *this;
 | 
|---|
 | 60 |   }
 | 
|---|
 | 61 | 
 | 
|---|
| [30abdc] | 62 |   const IndexSet_ptr& getIndexSet() const {
 | 
|---|
| [d699de] | 63 |     return indices;
 | 
|---|
 | 64 |   }
 | 
|---|
 | 65 | 
 | 
|---|
| [30abdc] | 66 |   /** Getter for the contribution.
 | 
|---|
 | 67 |    *
 | 
|---|
 | 68 |    * @return contribution
 | 
|---|
 | 69 |    */
 | 
|---|
 | 70 |   const T getContribution() const {
 | 
|---|
 | 71 |     return *contribution;
 | 
|---|
 | 72 |   }
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |   /** Getter for the value.
 | 
|---|
 | 75 |    *
 | 
|---|
 | 76 |    * @return value
 | 
|---|
 | 77 |    */
 | 
|---|
| [7bed4e] | 78 |   const T getValue() const {
 | 
|---|
 | 79 |     return value;
 | 
|---|
 | 80 |   }
 | 
|---|
 | 81 | 
 | 
|---|
| [30abdc] | 82 |   /** Setter for the value.
 | 
|---|
 | 83 |    *
 | 
|---|
 | 84 |    * This function is observed to notify all sets that contain us of a change.
 | 
|---|
 | 85 |    *
 | 
|---|
 | 86 |    * @param _value value to set value to
 | 
|---|
 | 87 |    */
 | 
|---|
| [d699de] | 88 |   void setValue(const T& _value) {
 | 
|---|
| [7bed4e] | 89 |     OBSERVE;
 | 
|---|
| [d699de] | 90 |     value = _value;
 | 
|---|
 | 91 |   }
 | 
|---|
 | 92 | 
 | 
|---|
| [7bed4e] | 93 | private:
 | 
|---|
 | 94 |   // prohibit copies
 | 
|---|
 | 95 |   SetValue(const SetValue<T> &_value);
 | 
|---|
 | 96 | 
 | 
|---|
| [30abdc] | 97 | private:
 | 
|---|
 | 98 |   //!> static lookup function to get to the subsets
 | 
|---|
 | 99 |   static boost::function< IndexSetContainer::ptr & (const IndexSet_ptr &)> lookupSubset;
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 |   //!> static lookup function to get the SetValue for a specific IndexSet
 | 
|---|
 | 102 |   static boost::function< typename SetValue<T>::ptr & (const IndexSet_ptr &)> lookupValue;
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |   /** Calculates the contribution for this subset.
 | 
|---|
 | 105 |    *
 | 
|---|
 | 106 |    * Internal function that is bound to the cacheable
 | 
|---|
 | 107 |    *
 | 
|---|
 | 108 |    * @return
 | 
|---|
 | 109 |    */
 | 
|---|
 | 110 |   const T calcSum() {
 | 
|---|
 | 111 |     LOG(1, "INFO: Summing up contribution for " << *indices << ".");
 | 
|---|
 | 112 |     // we initialize with value
 | 
|---|
 | 113 |     T result = getValue();
 | 
|---|
 | 114 |     // then subtract contributions from all subsets
 | 
|---|
 | 115 |     const IndexSetContainer::ptr &container = lookupSubset(indices);
 | 
|---|
 | 116 |     if (container) {
 | 
|---|
 | 117 |       const IndexSetContainer::Container_t &subsets = container->getContainer();
 | 
|---|
 | 118 |       for (IndexSetContainer::Container_t::const_iterator iter = subsets.begin();
 | 
|---|
 | 119 |           iter != subsets.end(); ++iter) {
 | 
|---|
 | 120 |         LOG(2, "INFO: Current subset is " << **iter << ".");
 | 
|---|
 | 121 |         // NOTE: our subset is not contained in the number of subsets
 | 
|---|
 | 122 |         typename SetValue<T>::ptr ptr = lookupValue(*iter);
 | 
|---|
 | 123 |         if (ptr)
 | 
|---|
 | 124 |           result -= ptr->getContribution();
 | 
|---|
 | 125 |         else
 | 
|---|
 | 126 |           ELOG(1, "Cannot find " << **iter << " via lookup.");
 | 
|---|
 | 127 |       }
 | 
|---|
 | 128 |     } else {
 | 
|---|
 | 129 |       ELOG(1, "Cannot find subsets for " << *indices << " via lookup.");
 | 
|---|
 | 130 |     }
 | 
|---|
 | 131 |     return result;
 | 
|---|
 | 132 |   }
 | 
|---|
 | 133 | 
 | 
|---|
| [7bed4e] | 134 | private:
 | 
|---|
| [d699de] | 135 |   //!> value associated with this IndexSet
 | 
|---|
 | 136 |   T value;
 | 
|---|
| [7bed4e] | 137 | 
 | 
|---|
| [30abdc] | 138 |   //!> shared_ptr to the index set
 | 
|---|
 | 139 |   IndexSet_ptr indices;
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 |   //!> stores the (orthogonal) contribution for this SubsetValue
 | 
|---|
 | 142 |   Cacheable<T> contribution;
 | 
|---|
| [d699de] | 143 | };
 | 
|---|
 | 144 | 
 | 
|---|
| [30abdc] | 145 | template <class T>
 | 
|---|
 | 146 | boost::function< IndexSetContainer::ptr & (const IndexSet_ptr &)> SetValue<T>::lookupSubset = NULL;
 | 
|---|
 | 147 | 
 | 
|---|
 | 148 | template <class T>
 | 
|---|
 | 149 | boost::function< typename SetValue<T>::ptr & (const IndexSet_ptr &)> SetValue<T>::lookupValue = NULL;
 | 
|---|
| [d699de] | 150 | 
 | 
|---|
 | 151 | #endif /* SETVALUE_HPP_ */
 | 
|---|