source: src/Fragmentation/Summation/SetValueMap.hpp@ 8a91f5

Fix_IndependentFragmentGrids
Last change on this file since 8a91f5 was 8a91f5, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: (Orthogonal)Summation::Sum() summed small fragments then large ones.

  • added rbegin() and rend() to SetValueMap's interface.
  • this fails for the new individual fragment SamplingGrids because there we assume that we always sum a grid of smaller extension onto a larger one. now, we go in reverse order through the list of setvalues and sum from largest down to smallest.
  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * SetValueMap.hpp
3 *
4 * Created on: Jul 4, 2012
5 * Author: heber
6 */
7
8#ifndef SETVALUEMAP_HPP_
9#define SETVALUEMAP_HPP_
10
11
12// include config.h
13#ifdef HAVE_CONFIG_H
14#include <config.h>
15#endif
16
17#include <map>
18
19#include "IndexSet.hpp"
20#include "SetValue.hpp"
21
22#include "CodePatterns/Assert.hpp"
23
24class SetValueMapTest;
25
26/** This class represents a map from IndexSet to SetValue<T>.
27 *
28 * Each SetValue knows its IndexSet but we also need the inverse
29 * association from IndexSet to SetValue.
30 *
31 */
32template <typename T>
33class SetValueMap
34{
35 //!> grant unit access
36 friend class SetValueMapTest;
37public:
38 /** Either adds a new SetValue<T> to the map or changes the value the present
39 * instance.
40 *
41 * @param ptr associated IndexSet
42 * @param _value new value of the SetValue
43 */
44 void addValue(const IndexSet::ptr& ptr, const T &_value) {
45 typename Lookup_t::iterator iter = Lookup.find(ptr);
46 if (iter != Lookup.end()) {
47 iter->second->setValue(_value);
48 } else {
49 Lookup.insert( std::make_pair( ptr, typename SetValue<T>::ptr(new SetValue<T>(ptr, _value)) ) );
50 }
51 }
52
53 /** Getter for the SetValue to a specific IndexSet.
54 *
55 * @param ptr IndexSet
56 * @return SetValue associated with this IndexSet
57 */
58 typename SetValue<T>::ptr & getValue(const IndexSet::ptr &ptr) {
59 ASSERT( isIndexSetPresent(ptr),
60 "SetValueMap<T>::getValue() - IndexSet "+toString(*ptr)+" is unknown.");
61 typename Lookup_t::iterator iter = Lookup.find(ptr);
62 ASSERT( *iter->second->getIndexSet() == *ptr,
63 "SetValueMap<T>::getValue() - returned SetValue associated to set "+
64 toString(*iter->second->getIndexSet())+"does not match with desired set "
65 +toString(*ptr)+".");
66 return iter->second;
67 }
68
69 /** Getter for the SetValue to a specific IndexSet.
70 *
71 * @param ptr IndexSet
72 * @return SetValue associated with this IndexSet
73 */
74 const typename SetValue<T>::ptr & getConstValue(const IndexSet::ptr &ptr) const {
75 ASSERT( isIndexSetPresent(ptr),
76 "SetValueMap<T>::getValue() - IndexSet "+toString(*ptr)+" is unknown.");
77 typename Lookup_t::const_iterator iter = Lookup.find(ptr);
78 ASSERT( *iter->second->getIndexSet() == *ptr,
79 "SetValueMap<T>::getValue() - returned SetValue associated to set "+
80 toString(*iter->second->getIndexSet())+"does not match with desired set "
81 +toString(*ptr)+".");
82 return iter->second;
83 }
84
85 bool removeValue(const IndexSet::ptr &ptr) {
86 if (isIndexSetPresent(ptr)) {
87 Lookup.erase(ptr);
88 return true;
89 } else {
90 return false;
91 }
92 }
93
94 /** Checks whether a given IndexSet \a ptr is known.
95 *
96 * @param ptr IndexSet to check
97 * @return true - set present, false - else
98 */
99 bool isIndexSetPresent(const IndexSet::ptr& ptr) const {
100 typename Lookup_t::const_iterator iter = Lookup.find(ptr);
101 return (iter != Lookup.end());
102 }
103
104private:
105 //!> typedef for the internal lookup
106 typedef std::map< IndexSet::ptr, typename SetValue<T>::ptr, IndexSetContainer::Comparator_t > Lookup_t;
107 //!> internal map to provide the lookup
108 Lookup_t Lookup;
109
110public:
111 //!> typedef to const iterator of internal map such that easy const traversal is possible
112 typedef typename Lookup_t::const_iterator const_iterator;
113 //!> typedef to const iterator of internal map such that easy const traversal is possible
114 typedef typename Lookup_t::const_reverse_iterator const_reverse_iterator;
115
116 /** Getter for first node of internal map.
117 *
118 * @return Lookup::begin()
119 */
120 const_iterator begin() const {
121 return Lookup.begin();
122 }
123
124 /** Getter for last and one node of internal map.
125 *
126 * @return Lookup::end()
127 */
128 const_iterator end() const {
129 return Lookup.end();
130 }
131
132 /** Getter for last node of internal map.
133 *
134 * @return Lookup::rbegin()
135 */
136 const_reverse_iterator rbegin() const {
137 return Lookup.rbegin();
138 }
139
140 /** Getter for one before first node of internal map.
141 *
142 * @return Lookup::rend()
143 */
144 const_reverse_iterator rend() const {
145 return Lookup.rend();
146 }
147};
148
149#endif /* SETVALUEMAP_HPP_ */
Note: See TracBrowser for help on using the repository browser.