OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
AmrParticleLevelCounter.h
Go to the documentation of this file.
1 #ifndef AMR_PARTICLE_LEVEL_COUNTER_H
2 #define AMR_PARTICLE_LEVEL_COUNTER_H
3 
4 #include <map>
5 #include <numeric>
6 #include <functional>
7 #include <iterator>
8 
17 template <
18  class Key,
19  class T,
20  class Compare = std::less<Key>,
21  class Allocator = std::allocator<std::pair<const Key, T> >
23 {
24 
25 public:
27  typedef typename std::map<Key, T>::size_type size_type;
29  typedef typename std::map<Key, T>::const_iterator const_iterator;
30 
31 public:
32 
34 
40  void increment(const Key& level, T nTimes = T(1)) { count_m[level] += nTimes; }
41 
47  void decrement(const Key& level, T nTimes = T(1)) { increment(level, -nTimes); }
48 
49  T& operator[](T level) { return count_m[level]; }
50 
51  const T& operator[](T level) const { return count_m[level]; }
52 
53  size_type size() const { return count_m.size(); }
54 
55  bool empty() const { return count_m.empty(); }
56 
57  iterator begin() { return count_m.begin(); }
58  const_iterator begin() const { return count_m.begin(); }
59 
60  iterator end() { return count_m.end(); }
61  const_iterator end() const { return count_m.end(); }
62 
63 
69  T begin(T level) const {
70  auto end = count_m.begin();
71 
72  // make sure to stay within container
73  T size = count_m.size();
74  std::advance(end, (level > size) ? size : level);
75 
76  return std::accumulate(count_m.begin(), end, 0,
77  [](T sum, const value_type& value_pair) {
78  return sum + value_pair.second;
79  });
80  }
81 
82 
88  T end(T level) const { return begin(level + 1); }
89 
90 
96  void remove(T num, T begin) {
97  int inum = int(num);
98  while ( inum > -1 ) {
99  T level = which(begin + inum);
100  --count_m[level];
101  --inum;
102  }
103  }
104 
105 
111  return begin( count_m.size() );
112  }
113 
114 
118  T getLocalNumUpToLevel(T level) const {
119  PAssert_GE(level, T(0));
120  T sum = 0;
121  for (T i = 0; i <= level; ++i) {
122  sum += end(i) - begin(i);
123  }
124  return sum;
125  }
126 
127 
131  T getLocalNumAtLevel(T level) const {
132  PAssert_GE(level, T(0));
133  return end(level) - begin(level);
134  }
135 
136 private:
142  T which(T idx) {
143  T level = 0;
144 
145  while ( idx >= end(level) && level < size() )
146  ++level;
147 
148  return level;
149  }
150 
151 
152 private:
157  std::map<Key, T> count_m;
158 };
159 
160 #endif
T getLocalNumAtLevel(T level) const
const_iterator begin() const
Definition: rbendmap.h:8
void increment(const Key &level, T nTimes=T(1))
void decrement(const Key &level, T nTimes=T(1))
const T & operator[](T level) const
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1213
std::map< Key, T >::size_type size_type
#define PAssert_GE(a, b)
Definition: PAssert.h:124
T getLocalNumUpToLevel(T level) const
T * value_type(const SliceIterator< T > &)
std::map< Key, T >::value_type value_type
std::map< Key, T >::const_iterator const_iterator
std::string::iterator iterator
Definition: MSLang.h:16
std::map< Key, T >::iterator iterator
const_iterator end() const