| [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"
 | 
|---|
| [58952a] | 22 | #include "ZeroInstance.hpp"
 | 
|---|
| [d699de] | 23 | 
 | 
|---|
| [30abdc] | 24 | #include "CodePatterns/Cacheable.hpp"
 | 
|---|
 | 25 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [7bed4e] | 26 | #include "CodePatterns/Observer/Observable.hpp"
 | 
|---|
 | 27 | 
 | 
|---|
| [30abdc] | 28 | class SetValueTest;
 | 
|---|
 | 29 | 
 | 
|---|
| [d699de] | 30 | template <class T>
 | 
|---|
| [7bed4e] | 31 | class SetValue : public Observable
 | 
|---|
| [d699de] | 32 | {
 | 
|---|
| [30abdc] | 33 |   //!> grant unit test access to allow setting static function
 | 
|---|
 | 34 |   friend class SetValueTest;
 | 
|---|
| [1e7dd4] | 35 |   //!> grant OrthogonalSummation access to static lookup functions
 | 
|---|
 | 36 |   template <class TT> friend class OrthogonalSummation;
 | 
|---|
| [c40e15d] | 37 |   //!> grant Summation access to static lookup functions
 | 
|---|
 | 38 |   template <class TT> friend class Summation;
 | 
|---|
| [d699de] | 39 | public:
 | 
|---|
| [30abdc] | 40 |   typedef boost::shared_ptr< SetValue<T> > ptr;
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 |   SetValue(const IndexSet_ptr &_indices, const T &_value) :
 | 
|---|
| [7bed4e] | 43 |     Observable("SubsetValue"),
 | 
|---|
| [d699de] | 44 |     value(_value),
 | 
|---|
| [30abdc] | 45 |     indices(_indices),
 | 
|---|
 | 46 |     contribution(this,boost::bind(&SetValue<T>::calcSum,this),"contribution")
 | 
|---|
| [d699de] | 47 |   {};
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 |   SetValue & operator+=(const SetValue &other) {
 | 
|---|
| [30abdc] | 50 |     if (indices->contains(*other.indices)) {
 | 
|---|
 | 51 |       OBSERVE;
 | 
|---|
| [d699de] | 52 |       value += other.value;
 | 
|---|
| [30abdc] | 53 |     }
 | 
|---|
| [d699de] | 54 |     return *this;
 | 
|---|
 | 55 |   }
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 |   SetValue & operator-=(const SetValue &other) {
 | 
|---|
| [30abdc] | 58 |     if (indices->contains(*other.indices)) {
 | 
|---|
 | 59 |       OBSERVE;
 | 
|---|
| [d699de] | 60 |       value -= other.value;
 | 
|---|
| [30abdc] | 61 |     }
 | 
|---|
| [d699de] | 62 |     return *this;
 | 
|---|
 | 63 |   }
 | 
|---|
 | 64 | 
 | 
|---|
| [30abdc] | 65 |   const IndexSet_ptr& getIndexSet() const {
 | 
|---|
| [d699de] | 66 |     return indices;
 | 
|---|
 | 67 |   }
 | 
|---|
 | 68 | 
 | 
|---|
| [30abdc] | 69 |   /** Getter for the contribution.
 | 
|---|
 | 70 |    *
 | 
|---|
 | 71 |    * @return contribution
 | 
|---|
 | 72 |    */
 | 
|---|
 | 73 |   const T getContribution() const {
 | 
|---|
 | 74 |     return *contribution;
 | 
|---|
 | 75 |   }
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 |   /** Getter for the value.
 | 
|---|
 | 78 |    *
 | 
|---|
 | 79 |    * @return value
 | 
|---|
 | 80 |    */
 | 
|---|
| [7bed4e] | 81 |   const T getValue() const {
 | 
|---|
 | 82 |     return value;
 | 
|---|
 | 83 |   }
 | 
|---|
 | 84 | 
 | 
|---|
| [30abdc] | 85 |   /** Setter for the value.
 | 
|---|
 | 86 |    *
 | 
|---|
 | 87 |    * This function is observed to notify all sets that contain us of a change.
 | 
|---|
 | 88 |    *
 | 
|---|
 | 89 |    * @param _value value to set value to
 | 
|---|
 | 90 |    */
 | 
|---|
| [d699de] | 91 |   void setValue(const T& _value) {
 | 
|---|
| [7bed4e] | 92 |     OBSERVE;
 | 
|---|
| [d699de] | 93 |     value = _value;
 | 
|---|
 | 94 |   }
 | 
|---|
 | 95 | 
 | 
|---|
| [7bed4e] | 96 | private:
 | 
|---|
 | 97 |   // prohibit copies
 | 
|---|
 | 98 |   SetValue(const SetValue<T> &_value);
 | 
|---|
 | 99 | 
 | 
|---|
| [30abdc] | 100 | private:
 | 
|---|
 | 101 |   //!> static lookup function to get to the subsets
 | 
|---|
 | 102 |   static boost::function< IndexSetContainer::ptr & (const IndexSet_ptr &)> lookupSubset;
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |   //!> static lookup function to get the SetValue for a specific IndexSet
 | 
|---|
 | 105 |   static boost::function< typename SetValue<T>::ptr & (const IndexSet_ptr &)> lookupValue;
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 |   /** Calculates the contribution for this subset.
 | 
|---|
 | 108 |    *
 | 
|---|
| [58952a] | 109 |    * We calculate by value minus the summed contributions(!) from all subsets
 | 
|---|
 | 110 |    * of this subset. If the value is ZeroInstance<T> however, we set its
 | 
|---|
 | 111 |    * contribution to zero, as it is designated thereby as not contributing.
 | 
|---|
| [30abdc] | 112 |    *
 | 
|---|
| [58952a] | 113 |    * \note This is internal function that is bound to the Cacheable contribution.
 | 
|---|
 | 114 |    *
 | 
|---|
 | 115 |    * @return value minus the summed contribution's from all subsets
 | 
|---|
| [30abdc] | 116 |    */
 | 
|---|
 | 117 |   const T calcSum() {
 | 
|---|
| [1b5a40] | 118 |     LOG(2, "DEBUG: Summing up contribution for " << *indices << ".");
 | 
|---|
| [30abdc] | 119 |     // we initialize with value
 | 
|---|
 | 120 |     T result = getValue();
 | 
|---|
| [58952a] | 121 |     // ZeroInstance<T>() is a specific value, if set, this subset does not contribute
 | 
|---|
 | 122 |     if (result != ZeroInstance<T>()) { // compare directly, not numerically!
 | 
|---|
 | 123 |       // then subtract contributions from all subsets
 | 
|---|
 | 124 |       const IndexSetContainer::ptr &container = lookupSubset(indices);
 | 
|---|
 | 125 |       if (container) {
 | 
|---|
 | 126 |         const IndexSetContainer::Container_t &subsets = container->getContainer();
 | 
|---|
 | 127 |         for (IndexSetContainer::Container_t::const_iterator iter = subsets.begin();
 | 
|---|
 | 128 |             iter != subsets.end(); ++iter) {
 | 
|---|
 | 129 |           LOG(3, "DEBUG: Current subset is " << **iter << ".");
 | 
|---|
 | 130 |           // NOTE: our subset is not contained in the number of subsets
 | 
|---|
 | 131 |           typename SetValue<T>::ptr ptr = lookupValue(*iter);
 | 
|---|
 | 132 |           if (ptr)
 | 
|---|
 | 133 |             result -= ptr->getContribution();
 | 
|---|
 | 134 |           else
 | 
|---|
 | 135 |             ELOG(1, "Cannot find " << **iter << " via lookup.");
 | 
|---|
 | 136 |         }
 | 
|---|
 | 137 |       } else {
 | 
|---|
 | 138 |         ELOG(1, "Cannot find subsets for " << *indices << " via lookup.");
 | 
|---|
| [30abdc] | 139 |       }
 | 
|---|
| [58952a] | 140 |     } // else we return ZeroInstance<T>() as contribution
 | 
|---|
| [30abdc] | 141 |     return result;
 | 
|---|
 | 142 |   }
 | 
|---|
 | 143 | 
 | 
|---|
| [7bed4e] | 144 | private:
 | 
|---|
| [d699de] | 145 |   //!> value associated with this IndexSet
 | 
|---|
 | 146 |   T value;
 | 
|---|
| [7bed4e] | 147 | 
 | 
|---|
| [30abdc] | 148 |   //!> shared_ptr to the index set
 | 
|---|
 | 149 |   IndexSet_ptr indices;
 | 
|---|
 | 150 | 
 | 
|---|
 | 151 |   //!> stores the (orthogonal) contribution for this SubsetValue
 | 
|---|
 | 152 |   Cacheable<T> contribution;
 | 
|---|
| [d699de] | 153 | };
 | 
|---|
 | 154 | 
 | 
|---|
| [30abdc] | 155 | template <class T>
 | 
|---|
 | 156 | boost::function< IndexSetContainer::ptr & (const IndexSet_ptr &)> SetValue<T>::lookupSubset = NULL;
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | template <class T>
 | 
|---|
 | 159 | boost::function< typename SetValue<T>::ptr & (const IndexSet_ptr &)> SetValue<T>::lookupValue = NULL;
 | 
|---|
| [d699de] | 160 | 
 | 
|---|
 | 161 | #endif /* SETVALUE_HPP_ */
 | 
|---|