Loading...
Searching...
No Matches
levelSetCache.hpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25# ifndef __BITPIT_LEVELSET_CACHE_HPP__
26# define __BITPIT_LEVELSET_CACHE_HPP__
27
28#include "levelSetCommon.hpp"
29
30#include "bitpit_common.hpp"
31#include "bitpit_containers.hpp"
32#include "bitpit_patchkernel.hpp"
33
34#include <cassert>
35#include <functional>
36#include <memory>
37#include <vector>
38
39namespace bitpit {
40
41class SendBuffer;
42class RecvBuffer;
43
51template<typename key_t>
53{
54public:
55 virtual ~LevelSetCache() = default;
56
57 virtual std::unique_ptr<LevelSetCache<key_t>> clone() const = 0;
58
59 virtual bool contains(const key_t &key) const = 0;
60
61 template<typename Keys>
62 std::size_t erase(const Keys &keys);
63 virtual std::size_t erase(const key_t &key) = 0;
64
65 virtual void reserve(std::size_t n) = 0;
66 virtual void shrink_to_fit() = 0;
67 virtual void clear() = 0;
68
69 virtual bool isVolatile() const = 0;
70
71 virtual void dump(std::ostream &stream) = 0;
72 virtual void restore(std::istream &stream) = 0;
73
74#if BITPIT_ENABLE_MPI
75 virtual std::size_t getEntryBinarySize() const = 0;
76 virtual void writeBuffer(const std::vector<key_t> &keys, SendBuffer &buffer) const = 0;
77 virtual void readBuffer(const std::vector<key_t> &keys, RecvBuffer &buffer) = 0;
78#endif
79
80protected:
81 LevelSetCache() = default;
82};
83
99template<typename value_t>
101{
102public:
103 LevelSetValueCacheBaseEntry(bool valid);
104
105 bool isValid() const;
106
107protected:
108 const static value_t m_dummyValue;
109
110private:
111 bool m_valid;
112};
113
114template<typename value_t>
116{
117public:
119 LevelSetValueCacheEntry(const value_t &value);
120
121 const value_t & operator*() const;
122
123private:
124 std::reference_wrapper<const value_t> m_value;
125
126};
127
128template<>
130{
132 explicit LevelSetValueCacheEntry(const bool &value);
133 explicit LevelSetValueCacheEntry(const std::vector<bool>::reference &value);
134
135 bool operator*() const;
136
137private:
138 static const std::vector<bool> m_dummyVector;
139
140 const bool &m_value;
141
142 bool m_useVectorValue;
143 std::vector<bool>::const_reference m_vectorValue;
144};
145
153template<typename key_t, typename value_t>
155{
156public:
158
159 virtual Entry findEntry(const key_t &key) const = 0;
160 virtual Entry insertEntry(const key_t &key, const value_t &value) = 0;
161 virtual Entry insertEntry(const key_t &key, value_t &&value) = 0;
162
163};
164
173template<typename key_t, typename container_t, typename value_t = typename container_t::value_type, typename reference_t = value_t &, typename const_reference_t = const value_t &>
174class LevelSetContainerBaseCache : public LevelSetValueCache<key_t, value_t>
175{
176public:
178
179 typedef container_t container_type;
180 typedef const key_t &key_type;
181 typedef value_t value_type;
182
183 typedef reference_t reference;
184 typedef const_reference_t const_reference;
185
186 typedef typename container_t::iterator iterator;
187 typedef typename container_t::const_iterator const_iterator;
188
189 iterator begin();
190 iterator end();
191
192 const_iterator cbegin() const;
193 const_iterator cend() const;
194
195 const_iterator begin() const;
196 const_iterator end() const;
197
198 bool contains(const key_t &key) const override;
199
200 Entry findEntry(const key_t &key) const override;
201 Entry insertEntry(const key_t &key, const value_t &value) override;
202 Entry insertEntry(const key_t &key, value_t &&value) override;
203
204 reference at(const key_t &key);
205 const_reference at(const key_t &key) const;
206
207 reference operator[](const key_t &key);
208 const_reference operator[](const key_t &key) const;
209
210 virtual iterator find(const key_t &key) = 0;
211 virtual const_iterator find(const key_t &key) const = 0;
212
213 virtual iterator insert(const key_t &key, const value_t &value) = 0;
214 virtual iterator insert(const key_t &key, value_t &&value) = 0;
215
216#if BITPIT_ENABLE_MPI
217 std::size_t getEntryBinarySize() const override;
218 void writeBuffer(const std::vector<key_t> &ids, SendBuffer &buffer) const override;
219 void readBuffer(const std::vector<key_t> &ids, RecvBuffer &buffer) override;
220#endif
221
222protected:
223 container_t m_container;
224
225 template<typename... Args>
226 LevelSetContainerBaseCache(Args&&... args);
227
228 virtual key_t getKey(const const_iterator &itr) const = 0;
229 virtual reference getValue(const iterator &itr) const = 0;
230 virtual const_reference getValue(const const_iterator &itr) const = 0;
231};
232
241template<typename key_t, typename container_t>
242class LevelSetContainerCache : public LevelSetContainerBaseCache<key_t, container_t>
243{
244public:
246
247 typedef typename Base::key_type key_type;
248 typedef typename Base::value_type value_type;
249 typedef typename Base::container_type container_type;
250
251 typedef typename Base::iterator iterator;
252 typedef typename Base::const_iterator const_iterator;
253
254 typedef typename Base::reference reference;
255 typedef typename Base::const_reference const_reference;
256
257};
258
259template<typename key_t, typename value_t>
260class LevelSetContainerCache<key_t, std::unordered_map<key_t, value_t>> : public LevelSetContainerBaseCache<key_t,
261 std::unordered_map<key_t, value_t>,
262 typename std::unordered_map<key_t, value_t>::mapped_type>
263{
264public:
265 typedef LevelSetContainerBaseCache<key_t,
266 std::unordered_map<key_t, value_t>,
267 typename std::unordered_map<key_t, value_t>::mapped_type>
268 Base;
269
270 typedef typename Base::key_type key_type;
271 typedef typename Base::value_type value_type;
272 typedef typename Base::container_type container_type;
273
274 typedef typename Base::iterator iterator;
275 typedef typename Base::const_iterator const_iterator;
276
277 typedef typename Base::reference reference;
278 typedef typename Base::const_reference const_reference;
279
280 LevelSetContainerCache(std::size_t capacity = 0);
281
282 std::unique_ptr<LevelSetCache<key_t>> clone() const override;
283
284 void reserve(std::size_t n) override;
285 void shrink_to_fit() override;
286 void clear() override;
287
288 iterator find(const key_t &key) override;
289 const_iterator find(const key_t &key) const override;
290
291 iterator insert(const key_t &key, const value_t &value) override;
292 iterator insert(const key_t &key, value_t &&value) override;
293 std::size_t erase(const key_t &key) override;
294
295 bool isVolatile() const override;
296
297 void dump(std::ostream &stream) override;
298 void restore(std::istream &stream) override;
299
300private:
301 key_t getKey(const const_iterator &itr) const override;
302 reference getValue(const iterator &itr) const override;
303 const_reference getValue(const const_iterator &itr) const override;
304};
305
306
307template<typename key_t, typename value_t>
308class LevelSetContainerCache<key_t, std::vector<value_t>> : public LevelSetContainerBaseCache<key_t,
309 std::vector<value_t>,
310 typename std::vector<value_t>::value_type,
311 typename std::vector<value_t>::reference,
312 typename std::vector<value_t>::const_reference>
313{
314public:
315 typedef LevelSetContainerBaseCache<key_t,
316 std::vector<value_t>,
317 typename std::vector<value_t>::value_type,
318 typename std::vector<value_t>::reference,
319 typename std::vector<value_t>::const_reference>
320 Base;
321
322 typedef typename Base::key_type key_type;
323 typedef typename Base::value_type value_type;
324 typedef typename Base::container_type container_type;
325
326 typedef typename Base::iterator iterator;
327 typedef typename Base::const_iterator const_iterator;
328
329 typedef typename Base::reference reference;
330 typedef typename Base::const_reference const_reference;
331
332 LevelSetContainerCache(std::size_t capacity = 0);
333
334 std::unique_ptr<LevelSetCache<key_t>> clone() const override;
335
336 void reserve(std::size_t n) override;
337 void shrink_to_fit() override;
338 void clear() override;
339
340 iterator find(const key_t &key) override;
341 const_iterator find(const key_t &key) const override;
342
343 iterator insert(const key_t &key, const value_t &value) override;
344 iterator insert(const key_t &key, value_t &&value) override;
345 std::size_t erase(const key_t &key) override;
346
347 bool isVolatile() const override;
348
349 void dump(std::ostream &stream) override;
350 void restore(std::istream &stream) override;
351
352protected:
353 key_t getKey(const const_iterator &itr) const override;
354 reference getValue(const iterator &itr) const override;
355 const_reference getValue(const const_iterator &itr) const override;
356
357private:
358 std::vector<bool> m_isCached;
359};
360
361template<typename key_t, typename value_t>
362class LevelSetContainerCache<key_t, PiercedVector<value_t, key_t>> : public LevelSetContainerBaseCache<key_t,
363 PiercedVector<value_t, key_t>,
364 typename PiercedVector<value_t, key_t>::value_type,
365 typename PiercedVector<value_t, key_t>::reference,
366 typename PiercedVector<value_t, key_t>::const_reference>
367{
368public:
369 typedef LevelSetContainerBaseCache<key_t,
374 Base;
375
376 typedef typename Base::key_type key_type;
377 typedef typename Base::value_type value_type;
378 typedef typename Base::container_type container_type;
379
380 typedef typename Base::iterator iterator;
381 typedef typename Base::const_iterator const_iterator;
382
383 typedef typename Base::reference reference;
384 typedef typename Base::const_reference const_reference;
385
386 LevelSetContainerCache(std::size_t capacity = 0);
387
388 std::unique_ptr<LevelSetCache<key_t>> clone() const override;
389
390 void reserve(std::size_t n) override;
391 void shrink_to_fit() override;
392 void clear() override;
393
394 iterator find(const key_t &key) override;
395 const_iterator find(const key_t &key) const override;
396
397 iterator insert(const key_t &key, const value_t &value) override;
398 iterator insert(const key_t &key, value_t &&value) override;
399 std::size_t erase(const key_t &key) override;
400
401 bool isVolatile() const override;
402
403 void dump(std::ostream &stream) override;
404 void restore(std::istream &stream) override;
405
406protected:
407 key_t getKey(const const_iterator &itr) const override;
408 reference getValue(const iterator &itr) const override;
409 const_reference getValue(const const_iterator &itr) const override;
410};
411
412template<typename key_t, typename value_t>
413class LevelSetContainerCache<key_t, PiercedStorage<value_t, key_t>> : public LevelSetContainerBaseCache<key_t,
414 PiercedStorage<value_t, key_t>,
415 typename PiercedStorage<value_t, key_t>::value_type,
416 typename PiercedStorage<value_t, key_t>::reference,
417 typename PiercedStorage<value_t, key_t>::const_reference>
418{
419public:
420 typedef LevelSetContainerBaseCache<key_t,
425 Base;
426
427 typedef typename Base::key_type key_type;
428 typedef typename Base::value_type value_type;
429 typedef typename Base::container_type container_type;
430
431 typedef typename Base::iterator iterator;
432 typedef typename Base::const_iterator const_iterator;
433
434 typedef typename Base::reference reference;
435 typedef typename Base::const_reference const_reference;
436
438
439 std::unique_ptr<LevelSetCache<key_t>> clone() const override;
440
441 void reserve(std::size_t n) override;
442 void shrink_to_fit() override;
443 void clear() override;
444
445 iterator find(const key_t &key) override;
446 const_iterator find(const key_t &key) const override;
447
448 iterator insert(const key_t &key, const value_t &value) override;
449 iterator insert(const key_t &key, value_t &&value) override;
450 std::size_t erase(const key_t &key) override;
451
452 bool isVolatile() const override;
453
454 void dump(std::ostream &stream) override;
455 void restore(std::istream &stream) override;
456
457protected:
458 key_t getKey(const const_iterator &itr) const override;
459 reference getValue(const iterator &itr) const override;
460 const_reference getValue(const const_iterator &itr) const override;
461
462private:
464};
465
473template<typename key_t>
475{
476public:
477 virtual ~LevelSetCacheFactory() = default;
478
479 virtual std::unique_ptr<LevelSetCache<key_t>> create() const = 0;
480
481protected:
482 LevelSetCacheFactory() = default;
483};
484
492template<typename key_t, typename value_t>
494{
495protected:
496 LevelSetValueCacheFactory() = default;
497
498};
499
507template<typename key_t, typename container_t>
508class LevelSetContainerBaseCacheFactory : public LevelSetValueCacheFactory<key_t, typename LevelSetContainerCache<key_t, container_t>::value_type>
509{
510protected:
512
513};
514
522template<typename key_t, typename container_t>
524{
525public:
527
528 std::unique_ptr<LevelSetCache<key_t>> create() const override;
529
530};
531
532template<typename key_t, typename value_t>
533class LevelSetContainerCacheFactory<key_t, std::unordered_map<key_t ,value_t>> : public LevelSetContainerBaseCacheFactory<key_t, std::unordered_map<key_t, value_t>>
534{
535public:
537
538 std::unique_ptr<LevelSetCache<key_t>> create() const override;
539
540};
541
542template<typename key_t, typename value_t>
543class LevelSetContainerCacheFactory<key_t, std::vector<value_t>> : public LevelSetContainerBaseCacheFactory<key_t, std::vector<value_t>>
544{
545public:
547
548 std::unique_ptr<LevelSetCache<key_t>> create() const override;
549
550};
551
552template<typename key_t, typename value_t>
553class LevelSetContainerCacheFactory<key_t, PiercedVector<value_t, key_t>> : public LevelSetContainerBaseCacheFactory<key_t, PiercedVector<value_t, key_t>>
554{
555public:
557
558 std::unique_ptr<LevelSetCache<key_t>> create() const override;
559
560};
561
562template<typename key_t, typename value_t>
563class LevelSetContainerCacheFactory<key_t, PiercedStorage<value_t, key_t>> : public LevelSetContainerBaseCacheFactory<key_t, PiercedStorage<value_t, key_t>>
564{
565public:
567
569
570 std::unique_ptr<LevelSetCache<key_t>> create() const override;
571
572protected:
573 const PiercedKernel<key_t> *m_kernel;
575};
576
589template<typename key_t>
591
592public:
593 class Item
594 {
595 public:
596 Item(const std::shared_ptr<LevelSetCacheFactory<key_t>> &factory = nullptr);
597
598 Item(const Item &other);
599 Item(Item &&other) = default;
600
601 Item & operator=(const Item &other);
602 Item & operator=(Item &&other) = default;
603
604 bool hasFactory() const;
605
606 bool hasCache() const;
608 void destroyCache();
609
610 LevelSetCache<key_t> * getCache(bool allowCreation = true);
611 const LevelSetCache<key_t> * getCache(bool allowCreation = true) const;
612
613 template<typename value_t>
614 LevelSetValueCache<key_t, value_t> * getCache(bool allowCreation = true);
615 template<typename value_t>
616 const LevelSetValueCache<key_t, value_t> * getCache(bool allowCreation = true) const;
617
618 private:
619 std::shared_ptr<LevelSetCacheFactory<key_t>> m_factory;
620 mutable std::unique_ptr<LevelSetCache<key_t>> m_cache;
621
622 LevelSetCache<key_t> * _createCache() const;
623 };
624
625 typedef key_t key_type;
626
627 typedef std::vector<Item> Caches;
628
629 typedef typename Caches::iterator iterator;
630 typedef typename Caches::const_iterator const_iterator;
631
632 static const std::size_t NULL_CACHE_ID;
633
634 LevelSetCacheCollection() = default;
637
638 virtual ~LevelSetCacheCollection() = default;
639
640 virtual std::unique_ptr<LevelSetCacheCollection<key_t>> clone() const;
641
642 iterator begin();
643 iterator end();
644
645 const_iterator begin() const;
646 const_iterator end() const;
647
648 const_iterator cbegin() const;
649 const_iterator cend() const;
650
651 std::size_t size() const;
652
653 Item & operator[](std::size_t index);
654 const Item & operator[](std::size_t index) const;
655
656 Item & at(std::size_t index);
657 const Item & at(std::size_t index) const;
658
659 template<typename container_t, typename... Args>
660 std::size_t insert(std::size_t index, Args&&... args);
661 void erase(std::size_t index);
662
663 void clear();
664
665protected:
666 Caches m_caches;
667
668};
669
671{
672public:
674
675 typedef Base::key_type key_type;
676
678
679 template<typename value_t>
681
683
684 template<typename container_t, typename... Args, typename std::enable_if<std::is_same<bitpit::PiercedStorage<typename container_t::value_type>, container_t>::value>::type * = nullptr>
685 std::size_t insert(std::size_t index, Args&&... args);
686 template<typename container_t, typename... Args, typename std::enable_if<!std::is_same<bitpit::PiercedStorage<typename container_t::value_type>, container_t>::value>::type * = nullptr>
687 std::size_t insert(std::size_t index, Args&&... args);
688
689protected:
690 const PiercedKernel<long> *m_kernel;
691};
692
693}
694
695// Include template implementations
696#include "levelSetCache.tpp"
697
698#endif
The class ElementCacheCollection allows to store a collection of caches for the elements of a mesh.
std::size_t insert(std::size_t index, Args &&... args)
ElementCacheCollection(const PiercedKernel< key_type > *kernel)
The class LevelSetCacheCollection::Item defines the items stored in a cache collection....
Item(const std::shared_ptr< LevelSetCacheFactory< key_t > > &factory=nullptr)
LevelSetCache< key_t > * getCache(bool allowCreation=true)
LevelSetCache< key_t > * createCache()
The class LevelSetCacheCollection allows to store a collection of caches.
std::size_t insert(std::size_t index, Args &&... args)
void erase(std::size_t index)
Caches m_caches
Caches owned by the collection.
virtual std::unique_ptr< LevelSetCacheCollection< key_t > > clone() const
Item & operator[](std::size_t index)
Item & at(std::size_t index)
static const std::size_t NULL_CACHE_ID
The class LevelSetCacheFactory provides basic functionalities for cache factories.
The class LevelSetCache is the base class for defining caches.
std::size_t erase(const Keys &keys)
The class LevelSetContainerBaseCacheFactory provides basic functionalities for cache factories.
The class LevelSetContainerBaseCache is the base class for defining caches that stores the values ins...
Entry findEntry(const key_t &key) const override
void writeBuffer(const std::vector< key_t > &ids, SendBuffer &buffer) const override
Entry insertEntry(const key_t &key, const value_t &value) override
std::size_t getEntryBinarySize() const override
reference at(const key_t &key)
reference operator[](const key_t &key)
void readBuffer(const std::vector< key_t > &ids, RecvBuffer &buffer) override
bool contains(const key_t &key) const override
The class LevelSetContainerCacheFactory provides basic functionalities for cache factories.
The class LevelSetContainerCache is the class for defining caches that stores the values inside a con...
The class LevelSetValueCacheEntry allows to get read-only access to a value stored in the cache.
const value_t & operator*() const
The class LevelSetValueCacheFactory provides basic functionalities for cache factories.
The class LevelSetCache is the base class for defining caches that store values.
Metafunction for generating a pierced kernel.
Metafunction for generating a pierced storage.
container_t::reference reference
container_t::const_reference const_reference
Metafunction for generating a pierced vector.
PiercedVectorStorage< value_t, id_t >::value_type value_type
Buffer to be used for receive communications.
Buffer to be used for send communications.
--- layout: doxygen_footer ---