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