1 | /*
|
---|
2 | * Summation_impl.hpp
|
---|
3 | *
|
---|
4 | * Created on: Jun 30, 2012
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef SUMMATION_IMPL_HPP_
|
---|
9 | #define SUMMATION_IMPL_HPP_
|
---|
10 |
|
---|
11 |
|
---|
12 | // include config.h
|
---|
13 | #ifdef HAVE_CONFIG_H
|
---|
14 | #include <config.h>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #include "CodePatterns/IteratorAdaptors.hpp"
|
---|
18 | #include "CodePatterns/Log.hpp"
|
---|
19 |
|
---|
20 | /** Constructor of class Summation.
|
---|
21 | *
|
---|
22 | */
|
---|
23 | template <class T>
|
---|
24 | Summation<T>::Summation(
|
---|
25 | InputSets_t &indices,
|
---|
26 | InputValues_t& values,
|
---|
27 | SubsetMap::ptr _subsetmap) :
|
---|
28 | subsetmap(_subsetmap)
|
---|
29 | {
|
---|
30 | ASSERT( indices.size() == values.size(),
|
---|
31 | "Summation<T>::Summation() - indices and values mismatch in size: "
|
---|
32 | +toString(indices.size())+" != "+toString(values.size())+".");
|
---|
33 | /// place each index
|
---|
34 | /// create own map if none is given
|
---|
35 | if (!subsetmap) {
|
---|
36 | typename InputSets_t::iterator iter = indices.begin();
|
---|
37 | IndexSetContainer container(*iter);
|
---|
38 | for (; iter != indices.end(); ++iter)
|
---|
39 | container.insert(*iter);
|
---|
40 | subsetmap.reset(new SubsetMap(container));
|
---|
41 | } else {
|
---|
42 | LOG(1, "INFO: Using given SubsetMap.");
|
---|
43 | }
|
---|
44 | /// instantiate all SubSetValue's by requesting the IndexSet from the Subsetmap
|
---|
45 | typename InputSets_t::iterator indexiter = indices.begin();
|
---|
46 | typename InputValues_t::iterator valueiter = values.begin();
|
---|
47 | for (;valueiter != values.end(); ++indexiter, ++valueiter) {
|
---|
48 | LOG(1, "INFO: Adding set " << **indexiter << " with value " << *valueiter << ".");
|
---|
49 | setvalues.addValue( *indexiter, *valueiter );
|
---|
50 | }
|
---|
51 | /// bind static lookup functions for SetValue<T>
|
---|
52 | SetValue<T>::lookupSubset =
|
---|
53 | boost::bind(&SubsetMap::getSubsets, boost::ref(*subsetmap), _1);
|
---|
54 | SetValue<T>::lookupValue =
|
---|
55 | boost::bind(&SetValueMap<T>::getValue, boost::ref(setvalues), _1);
|
---|
56 | }
|
---|
57 |
|
---|
58 | template <class T>
|
---|
59 | T Summation<T>::operator()(const size_t level) const
|
---|
60 | {
|
---|
61 | return Sum(level);
|
---|
62 | }
|
---|
63 |
|
---|
64 | template <class T>
|
---|
65 | T Summation<T>::Sum(const size_t level) const
|
---|
66 | {
|
---|
67 | typename SetValueMap<T>::const_iterator iter = setvalues.begin();
|
---|
68 | T sum; // we must assume here that cstor initializes to 0
|
---|
69 | if (iter->first->size() <= level)
|
---|
70 | sum = (iter->second)->getValue();
|
---|
71 | LOG(1, "DEBUG: Value from subset "+toString(*(iter->second->getIndexSet()))
|
---|
72 | +" is "+toString(sum)+".");
|
---|
73 | for(++iter;(iter != setvalues.end()) && (iter->first->size() <= level); ++iter) {
|
---|
74 | const T tempvalue = (iter->second)->getValue();
|
---|
75 | sum += tempvalue;
|
---|
76 | LOG(1, "DEBUG: Value from subset "+toString(*(iter->second->getIndexSet()))
|
---|
77 | +" is "+toString(tempvalue)+".");
|
---|
78 | }
|
---|
79 | return sum;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | #endif /* SUMMATION_IMPL_HPP_ */
|
---|