Peano
Loading...
Searching...
No Matches
STDStackMap.h
Go to the documentation of this file.
1// This file is part of the Peano project. For conditions of distribution and
2// use, please see the copyright notice at www.peano-framework.org
3#pragma once
4
5
6#include "maps.h"
8
9#include <map>
10
11
12namespace peano4 {
13 namespace maps {
14 template <typename T>
15 class STDStackMap;
16 }
17}
18
19
20template <typename T>
22 private:
24
25 std::map< StackKey, T* > _data;
26
31
36 void createStack(const StackKey& key);
37 public:
39
40 bool empty(int treeId, int stackId) const;
41
59 T* getForPush(int treeId, int stackId);
60 T* getForPush(const StackKey& key);
61
67 T* getForPop(int treeId, int stackId);
68 T* getForPop(const StackKey& key);
69
79 void clear();
80
90 void clear(int spacetree);
91
92 std::string toString() const;
93
98 std::set<StackKey> getKeys();
99
115 void garbageCollection(int spacetree);
116
120 bool holdsStack(int treeId, int stackId) const;
121 bool holdsStack(const StackKey& key) const;
122};
123
124
125#include "tarch/multicore/Lock.h"
126
127
128template <typename T>
129tarch::logging::Log peano4::maps::STDStackMap<T>::_log( "peano4::maps::STDStackMap<T>" );
130
131
132template <typename T>
134 if ( _data.count( key )==0 ) {
135 _data.insert(
136 std::pair< StackKey, T* >(
137 key,
138 new T()
139 )
140 );
141 }
142}
143
144
145template <typename T>
146bool peano4::maps::STDStackMap<T>::empty(int treeId, int stackId) const {
147 return _data.count( StackKey(treeId,stackId) )==0
148 or _data.at( StackKey(treeId,stackId) )->empty();
149}
150
151
152template <typename T>
153T* peano4::maps::STDStackMap<T>::getForPush(int treeId, int stackId) {
154 return getForPush( StackKey(treeId,stackId) );
155}
156
157
158template <typename T>
160 tarch::multicore::Lock lock(_semaphore);
161 createStack(key);
162 return _data[key];
163}
164
165
166template <typename T>
167T* peano4::maps::STDStackMap<T>::getForPop(int treeId, int stackId) {
168 return getForPop( StackKey(treeId,stackId) );
169}
170
171
172template <typename T>
174 tarch::multicore::Lock lock(_semaphore);
175 assertion( _data.count(key)==1 );
176 return _data.at( key );
177}
178
179
180template <typename T>
182 std::ostringstream msg;
183 msg << "(" << _data.size();
184 for (auto& p: _data) {
185 msg << ","
186 << p.first.first << "x" << p.first.second << ":" << p.second->size();
187 }
188 msg << ")";
189 return msg.str();
190}
191
192
193template <typename T>
194std::set<peano4::maps::StackKey> peano4::maps::STDStackMap<T>::getKeys() {
195 tarch::multicore::Lock lock(_semaphore);
196 std::set<peano4::maps::StackKey> result;
197 for (auto& p: _data) {
198 result.insert(p.first);
199 }
200 return result;
201}
202
203
204template <typename T>
205bool peano4::maps::STDStackMap<T>::holdsStack(int treeId, int stackId) const {
206 return holdsStack( StackKey(treeId,stackId) );
207}
208
209
210template <typename T>
212 return _data.count(key)==1;
213}
214
215
216template <typename T>
218 for (auto& p: _data) {
219 delete p.second;
220 }
221
222 _data.clear();
223}
224
225
226template <typename T>
228 assertionMsg( _data.empty(), "forgot to call clear()" );
229}
230
231
232template <typename T>
234 const int localTreeId = peano4::parallel::Node::getInstance().getLocalTreeId(spacetree);
235 for (auto& p: _data) {
236 bool isCorrectOwner = p.first.first==localTreeId;
237 bool isEmptyTemporaryStack = isCorrectOwner
238 and
239 p.second->empty()
240 and
242
243
244 if ( isEmptyTemporaryStack ) {
245 logDebug( "garbageCollection(...)", "remove stack " << p.first.first << "x" << p.first.second << ": " << p.second->toString() );
246 delete p.second;
247 p.second = new T();
248 }
249 }
250}
251
252
253template <typename T>
255 const int localTreeId = peano4::parallel::Node::getInstance().getLocalTreeId(spacetree);
256 for (auto& p: _data) {
257 if (
258 p.first.first==localTreeId
259 ) {
260 logDebug( "garbageCollection(...)", "remove stack " << p.first.first << "x" << p.first.second << ": " << p.second->toString() );
261 delete p.second;
262 p.second = new T();
263 }
264 }
265}
266
267
#define assertionMsg(expr, message)
#define assertion(expr)
#define logDebug(methodName, logMacroMessageStream)
Definition Log.h:50
T * getForPush(int treeId, int stackId)
Get the stack belonging to a tree.
void garbageCollection(int spacetree)
Invoke garbage collection.
std::set< StackKey > getKeys()
This one should be const, but I might need a semaphore, so I have to omit the const qualifier.
T * getForPop(int treeId, int stackId)
bool empty(int treeId, int stackId) const
void createStack(const StackKey &key)
This routine is not thread-safe, i.e.
void clear()
Clear the whole stack map.
std::map< StackKey, T * > _data
Definition STDStackMap.h:25
bool holdsStack(int treeId, int stackId) const
For debugging/assertions.
tarch::multicore::BooleanSemaphore _semaphore
Semaphore to protect _data.
Definition STDStackMap.h:30
static tarch::logging::Log _log
Definition STDStackMap.h:23
std::string toString() const
int getLocalTreeId(int treeId) const
Definition Node.cpp:125
static bool isStorageStackNumber(int number)
There are communication stacks and storage stacks.
Definition Node.cpp:198
static Node & getInstance()
This operation returns the singleton instance.
Definition Node.cpp:108
Log Device.
Definition Log.h:516
Create a lock around a boolean semaphore region.
Definition Lock.h:19
std::pair< int, int > StackKey
Unique key identifying a stack.
Definition maps.h:32