| 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 | 
 | 
|---|
| 17 | #include <boost/function.hpp>
 | 
|---|
| 18 | #include <boost/shared_ptr.hpp>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "IndexSet.hpp"
 | 
|---|
| 21 | #include "IndexSetContainer.hpp"
 | 
|---|
| 22 | #include "ZeroInstance.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "CodePatterns/Cacheable.hpp"
 | 
|---|
| 25 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 26 | #include "CodePatterns/Observer/Observable.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | class SetValueTest;
 | 
|---|
| 29 | 
 | 
|---|
| 30 | template <class T>
 | 
|---|
| 31 | class SetValue : public Observable
 | 
|---|
| 32 | {
 | 
|---|
| 33 |   //!> grant unit test access to allow setting static function
 | 
|---|
| 34 |   friend class SetValueTest;
 | 
|---|
| 35 |   //!> grant OrthogonalSummation access to static lookup functions
 | 
|---|
| 36 |   template <class TT> friend class OrthogonalSummation;
 | 
|---|
| 37 |   //!> grant Summation access to static lookup functions
 | 
|---|
| 38 |   template <class TT> friend class Summation;
 | 
|---|
| 39 | public:
 | 
|---|
| 40 |   typedef boost::shared_ptr< SetValue<T> > ptr;
 | 
|---|
| 41 | 
 | 
|---|
| 42 |   SetValue(const IndexSet_ptr &_indices, const T &_value) :
 | 
|---|
| 43 |     Observable("SubsetValue"),
 | 
|---|
| 44 |     value(_value),
 | 
|---|
| 45 |     indices(_indices),
 | 
|---|
| 46 |     contribution(this,boost::bind(&SetValue<T>::calcSum,this),"contribution")
 | 
|---|
| 47 |   {};
 | 
|---|
| 48 | 
 | 
|---|
| 49 |   SetValue & operator+=(const SetValue &other) {
 | 
|---|
| 50 |     if (indices->contains(*other.indices)) {
 | 
|---|
| 51 |       OBSERVE;
 | 
|---|
| 52 |       value += other.value;
 | 
|---|
| 53 |     }
 | 
|---|
| 54 |     return *this;
 | 
|---|
| 55 |   }
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   SetValue & operator-=(const SetValue &other) {
 | 
|---|
| 58 |     if (indices->contains(*other.indices)) {
 | 
|---|
| 59 |       OBSERVE;
 | 
|---|
| 60 |       value -= other.value;
 | 
|---|
| 61 |     }
 | 
|---|
| 62 |     return *this;
 | 
|---|
| 63 |   }
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   const IndexSet_ptr& getIndexSet() const {
 | 
|---|
| 66 |     return indices;
 | 
|---|
| 67 |   }
 | 
|---|
| 68 | 
 | 
|---|
| 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 |    */
 | 
|---|
| 81 |   const T getValue() const {
 | 
|---|
| 82 |     return value;
 | 
|---|
| 83 |   }
 | 
|---|
| 84 | 
 | 
|---|
| 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 |    */
 | 
|---|
| 91 |   void setValue(const T& _value) {
 | 
|---|
| 92 |     OBSERVE;
 | 
|---|
| 93 |     value = _value;
 | 
|---|
| 94 |   }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | private:
 | 
|---|
| 97 |   // prohibit copies
 | 
|---|
| 98 |   SetValue(const SetValue<T> &_value);
 | 
|---|
| 99 | 
 | 
|---|
| 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 |    *
 | 
|---|
| 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.
 | 
|---|
| 112 |    *
 | 
|---|
| 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
 | 
|---|
| 116 |    */
 | 
|---|
| 117 |   const T calcSum() {
 | 
|---|
| 118 |     LOG(2, "DEBUG: Summing up contribution for " << *indices << ".");
 | 
|---|
| 119 |     // we initialize with value
 | 
|---|
| 120 |     T result = getValue();
 | 
|---|
| 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.");
 | 
|---|
| 139 |       }
 | 
|---|
| 140 |     } // else we return ZeroInstance<T>() as contribution
 | 
|---|
| 141 |     return result;
 | 
|---|
| 142 |   }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | private:
 | 
|---|
| 145 |   //!> value associated with this IndexSet
 | 
|---|
| 146 |   T value;
 | 
|---|
| 147 | 
 | 
|---|
| 148 |   //!> shared_ptr to the index set
 | 
|---|
| 149 |   IndexSet_ptr indices;
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   //!> stores the (orthogonal) contribution for this SubsetValue
 | 
|---|
| 152 |   Cacheable<T> contribution;
 | 
|---|
| 153 | };
 | 
|---|
| 154 | 
 | 
|---|
| 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;
 | 
|---|
| 160 | 
 | 
|---|
| 161 | #endif /* SETVALUE_HPP_ */
 | 
|---|