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