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