source: src/Fragmentation/Summation/Summation_impl.hpp

Candidate_v1.6.1
Last change on this file was 3d444a, checked in by Frederik Heber <heber@…>, 8 years ago

(Orthogonal)Summation's Sum() can be further simplified now.

  • Property mode set to 100644
File size: 2.5 KB
Line 
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/Assert.hpp"
18#include "CodePatterns/IteratorAdaptors.hpp"
19#include "CodePatterns/Log.hpp"
20
21#include "Fragmentation/Summation/Summation.hpp"
22
23#include "Fragmentation/Summation/printKeyNames.hpp"
24#include "Fragmentation/Summation/ZeroInstance.hpp"
25
26/** Constructor of class Summation.
27 *
28 */
29template <class T>
30Summation<T>::Summation(
31 InputSets_t &indices,
32 InputValues_t& values,
33 SubsetMap::ptr _subsetmap) :
34 subsetmap(_subsetmap),
35 zeroinstance(ZeroInstance<T>())
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 {
49 LOG(2, "DEBUG: Using given SubsetMap.");
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) {
55 LOG(2, "DEBUG: Adding set " << **indexiter << " with value " << *valueiter << ".");
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
65template <class T>
66T Summation<T>::operator()(const size_t level) const
67{
68 return Sum(level);
69}
70
71template <class T>
72T Summation<T>::Sum(const size_t level) const
73{
74 T sum(zeroinstance);
75 for(typename SetValueMap<T>::const_iterator iter = setvalues.begin();
76 (iter != setvalues.end()) && (iter->first->size() <= level); ++iter) {
77 const T tempvalue = (iter->second)->getValue();
78 sum += tempvalue;
79 LOG(3, "DEBUG: Value from subset "+toString(*(iter->second->getIndexSet()))
80 +" is "+toString(tempvalue)+".");
81 }
82 return sum;
83}
84
85
86#endif /* SUMMATION_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.