FiberVISH 0.2
Fish - The Fiber Bundle API for the Vish Visualization Shell
FiberSpace.hpp
1#ifndef __FIBER_GRID_FIBER_HPP
2#define __FIBER_GRID_FIBER_HPP "Created 27.02.2001 21:42:27 by werner"
3
4#include "GridAPI.h"
5#include <fiber/field/Field.hpp>
6#include <memcore/TypeInfo.hpp>
7#include <memcore/Ageable.hpp>
8#include <memcore/shared_mutex.hpp>
9
10#include <functional>
11#include <set>
12#include <list>
13
14#include <fiber/field/BaseSpace.hpp>
15
16namespace Fiber
17{
18 using MemCore::WeakPtr;
19 using MemCore::NullPtr;
20 using std::set;
21 using std::list;
22
31template <class ID>
33{
35 Fields_t Fields;
36
37 mutable MemCore::shared_mutex fields_mutex;
38
41
42protected:
49 {
50 LOCK_WR lk(fields_mutex);
51
52 typename Fields_t::const_iterator f = Fields.find(id);
53 if (f==Fields.end() )
54 {
55 touch();
56 }
57 return Fields[ id ];
58 }
59
60
61 Fields_t copy_Fields_map() const
62 {
63 LOCK_RO lk(fields_mutex);
64 return Fields;
65 }
66
67public:
68
74 {
75 LOCK_RO lk(fields_mutex);
76 for(typename Fields_t::const_iterator f = Fields.begin();
77 f!=Fields.end(); f++)
78 {
79 if (const RefPtr<Field>&F = f->second)
80 {
83 {
84 return FID;
85 }
86 }
87 }
88 return nullptr;
89 }
90
91
94 {}
95
96
100 size_t getNumberOfFields() const
101 {
102 LOCK_RO lk(fields_mutex);
103 return Fields.size();
104 }
105
106
111 Field&operator[](const ID&id)
112 {
113 LOCK_WR lk(fields_mutex);
114
115 typename Fields_t::const_iterator f = Fields.find(id);
116 if (f==Fields.end() )
117 {
118 touch();
119 }
120
122
123 RefPtr<Field>&F = Fields[ id ];
124 if (!F) F = new Field(FIC);
125
126 return *F;
127 }
128
129 void mkFieldID(const ID&id)
130 {
131 LOCK_WR lk(fields_mutex);
132 touch();
133 Fields[ id ] = nullptr;
134 }
135
136 void insertField(const ID&id, const RefPtr<Field>&F)
137 {
138 LOCK_WR lk(fields_mutex);
139
140 typename Fields_t::const_iterator f = Fields.find(id);
141 if (f!=Fields.end() )
142 {
143 if (f->second == F)
144 return;
145 }
146
147 touch();
148 Fields[ id ] = F;
149 }
150
154 const RefPtr<Field>&find(const ID&id) const
155 {
156 LOCK_RO lk(fields_mutex);
157
158 typename Fields_t::const_iterator f = Fields.find(id);
159 if (f==Fields.end() )
160 return Field::NullPtrField();
161
162 return f->second;
163 }
164
165 RefPtr<CreativeArrayBase> find(const ID&id, const RefPtr<FragmentID>&f) const
166 {
167 const RefPtr<Field>&F = find(id);
168 if (F)
169 return F->getCreator(f);
170
171 return nullptr;
172 }
173
174 RefPtr<CreativeArrayBase> find(const ID&id, const string&FragmentName) const
175 {
176 const RefPtr<Field>&F = find(id);
177 if (F)
178 return F->getCreatorByName(FragmentName);
179
180 return nullptr;
181 }
182
187 const RefPtr<Field>&operator()(const ID&id) const
188 {
189 return find(id);
190 }
191
192 void iterate(const std::function<bool(const ID&, const RefPtr<Field>&)>&F) const
193 {
194 LOCK_RO lk(fields_mutex);
195
196 for(const auto&[id,f] : Fields)
197 {
198 if (!F(id,f))
199 return;
200 }
201 }
202
203
209 bool remove(const ID&id)
210 {
211 LOCK_WR lk(fields_mutex);
212
213 typename Fields_t::const_iterator f = Fields.find(id);
214 if (f==Fields.end() )
215 return false;
216
217 Fields.erase(f);
218 touch();
219 return true;
220 }
221
229 {
230 Youngest = *this;
231 Oldest = *this;
232
233 LOCK_RO lk(fields_mutex);
234 for(typename Fields_t::const_iterator f = Fields.begin();
235 f!=Fields.end(); f++)
236 {
237 if (f->second->isYoungerThan( Youngest) ) Youngest = *f->second;
238 if (f->second->isOlderThan ( Oldest ) ) Oldest = *f->second;
239 }
240 }
241
242
247 {
248 if (Fields.size()<1)
249 return nullptr;
250
251 LOCK_RO lk(fields_mutex);
252 typename Fields_t::const_iterator it = Fields.begin();
253 RefPtr<Field> result = it->second;
254
255 if (!result) // no field stored? bad. we are screwed.
256 return nullptr;
257
258 size_t MaxFragmentsSoFar = result -> nFragments();
259 for(; it != Fields.end(); it++)
260 {
261 RefPtr<Field> candidate = it->second;
262 if (!candidate)
263 continue;
264
265 size_t fragments = candidate->nFragments();
266 if (fragments > MaxFragmentsSoFar)
267 {
268 MaxFragmentsSoFar = fragments;
269 result = candidate;
270 }
271 }
272 return result;
273 }
274
279 {
280 LOCK_RO lk(fields_mutex);
281 int n = 0;
282 for(typename Fields_t::const_iterator it = Fields.begin(); it != Fields.end(); it++)
283 {
284 RefPtr<Field> F = it->second;
285 if (!F)
286 continue;
287
288 n += F->getMemoryUsage(UsedMemory, WantedMemory);
289 }
290 return n;
291 }
292
293
302 int providesFieldType(const Fiber::TypeList_t&AcceptedFields) const
303 {
304 LOCK_RO lk(fields_mutex);
305 int n = 0;
306 for(typename Fields_t::const_iterator it = Fields.begin(); it != Fields.end(); it++)
307 {
308 RefPtr<Field> F = it->second;
309 if (!F)
310 continue;
311
312 if (F->isType( AcceptedFields ) )
313 n++;
314 }
315 return n;
316 }
317
318 string xml() const
319 {
320 string retval;
321
322 retval = " <FIBERSPACE>\n";
323
324 LOCK_RO lk(fields_mutex);
325 for(typename Fields_t::const_iterator
326 it = Fields.begin(); it != Fields.end(); it++)
327 {
328 retval += " <FIELD>\n";
329 if (it->first)
330 {
331 retval += it->first->xml();
332 if (const RefPtr<Field>&F = it->second)
333 {
334 retval += F->xml();
335 }
336 }
337 else
338 retval += " <?INVALID>\n";
339 retval += " </FIELD>\n";
340 }
341 retval += " </FIBERSPACE>\n";
342 return retval;
343 }
344
345
346 void clear_fields()
347 {
348 LOCK_WR lk(fields_mutex);
349
350 for(auto Fit : Fields)
351 {
352 if (const RefPtr<Field>&F = Fit.second)
353 F->clear();
354 }
355
356 //
357 // it's important to clear and destruct the fields first
358 // because they might require the myFieldCreator while destructing.
359 // without explicit clearance this myFieldCreator would be destructed
360 // first and the fields were missing this creator.
361 //
362 Fields.clear();
363 }
364
365
366 const RefPtr<Field>&getFirstField() const
367 {
368 LOCK_RO lk(fields_mutex);
369
370 if( Fields.empty() )
371 return Field::NullPtrField();
372
373 return Fields.begin()->second;
374 }
375};
376
377
378} /* namespace Fiber */
379
380#endif /* __FIBER_GRID_FIBER_HPP */
bool empty() const noexcept
const_iterator end() const noexcept
void clear() noexcept
size_type erase(const key_type &__x)
size_type size() const noexcept
iterator find(const key_type &__x)
const_iterator begin() const noexcept
A base class for dependencies on a BaseSpace.
Definition BaseSpace.hpp:19
An iterator with an optional DataCreator, which is just a class to intercept creation of data along a...
Definition CreativeIterator.hpp:34
Base class for fields defined on some base space.
Definition FiberSpace.hpp:33
FiberMap()
Default constructor.
Definition FiberSpace.hpp:93
bool remove(const ID &id)
Remove an Field.
Definition FiberSpace.hpp:209
RefPtr< Field > & create_entry(const ID &id)
Get a modifyable field entry.
Definition FiberSpace.hpp:48
int providesFieldType(const Fiber::TypeList_t &AcceptedFields) const
Check how many fields exist here that conform to the given type.
Definition FiberSpace.hpp:302
Field & operator[](const ID &id)
Access field by name, create one, in case.
Definition FiberSpace.hpp:111
RefPtr< Field > findMostFragmentedField() const
Find the Field with the most fragments.
Definition FiberSpace.hpp:246
RefPtr< FragmentIDCollection > getFragmentIDCollection() const
Return the associated fragment ID collection of the first field that has one.
Definition FiberSpace.hpp:73
const RefPtr< Field > & find(const ID &id) const
Find an entry.
Definition FiberSpace.hpp:154
size_t getNumberOfFields() const
Get the number of fields contained here.
Definition FiberSpace.hpp:100
const RefPtr< Field > & operator()(const ID &id) const
Find an entry, operator interface to the find() member function for convenience.
Definition FiberSpace.hpp:187
int getMemoryUsage(memsize_t &UsedMemory, memsize_t &WantedMemory) const
Memory usage.
Definition FiberSpace.hpp:278
void getMinMaxAge(Ageable &Youngest, Ageable &Oldest) const
Get the youngest/oldest age of all fields contained here, including the fiber space itself taking int...
Definition FiberSpace.hpp:228
A Field is a collection of CreativeArrayBase reference pointers which are accessed via FragmentID obj...
Definition Field.hpp:245
Ageable & touch() noexcept
MemSizeConfig< sizeof(void *)>::memsize_t memsize_t
Given a fragmented field of curvilinear coordinates, (3D array of coordinates), build a uniform Grid ...
Definition FAQ.dox:2
std::nullptr_t NullPtr
A set of types.
Definition FiberType.hpp:213