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