25# ifndef __BITPIT_LEVELSET_CACHE_TPP__
26# define __BITPIT_LEVELSET_CACHE_TPP__
33template<
typename value_t>
43template<
typename value_t>
55template<
typename value_t>
64template<
typename value_t>
76template<
typename value_t>
88template<
typename value_t>
102template<
typename key_t>
103template<
typename Keys>
106 std::size_t nDeletedEntries = 0;
107 for (
const key_t &key : keys) {
108 nDeletedEntries += erase(key);
111 return nDeletedEntries;
119template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
120template<
typename... Args>
122 : m_container(std::forward<Args>(args)...)
131template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
134 return m_container.begin();
142template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
145 return m_container.end();
153template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
156 return m_container.cbegin();
164template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
167 return m_container.cend();
175template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
178 return m_container.cbegin();
186template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
189 return m_container.cend();
198template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
201 return (find(key) != m_container.cend());
214template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
217 const_iterator itr = find(key);
235template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
238 const_iterator itr = insert(key, value);
240 return Entry(getValue(itr));
253template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
256 const_iterator itr = insert(key, std::move(value));
258 return Entry(getValue(itr));
269template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
283template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
286 return getValue(find(key));
297template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
300 return getValue(find(key));
311template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
314 return getValue(find(key));
323template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
326 return sizeof(bool) +
sizeof(value_t);
335template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
338 for (
const key_t &key : keys) {
339 const_iterator itr = find(key);
341 buffer << static_cast<unsigned char>(1);
342 buffer << getValue(itr);
344 buffer << static_cast<unsigned char>(0);
355template<
typename key_t,
typename container_t,
typename value_t,
typename reference_t,
typename const_reference_t>
359 for (
const key_t &key : keys) {
360 unsigned char isCached;
376template<
typename key_t,
typename value_t>
380 Base::m_container.reserve(capacity);
388template<
typename key_t,
typename value_t>
399template<
typename key_t,
typename value_t>
402 Base::m_container.reserve(n);
408template<
typename key_t,
typename value_t>
417template<
typename key_t,
typename value_t>
420 Base::m_container.clear();
432template<
typename key_t,
typename value_t>
435 return Base::m_container.find(key);
447template<
typename key_t,
typename value_t>
450 return Base::m_container.find(key);
463template<
typename key_t,
typename value_t>
466 auto itr = Base::m_container.find(key);
467 if (itr != Base::m_container.end()) {
470 itr = Base::m_container.insert({key, value}).first;
486template<
typename key_t,
typename value_t>
489 auto itr = Base::m_container.find(key);
490 if (itr != Base::m_container.end()) {
491 itr->second = std::move(value);
493 itr = Base::m_container.insert({key, std::move(value)}).first;
505template<
typename key_t,
typename value_t>
508 return Base::m_container.
erase(key);
539template<
typename key_t,
typename value_t>
550template<
typename key_t,
typename value_t>
554 for (
const auto &entry : Base::m_container) {
565template<
typename key_t,
typename value_t>
568 Base::m_container.clear();
570 std::size_t nEntries;
572 Base::m_container.reserve(nEntries);
574 for (std::size_t i = 0; i < nEntries; ++i) {
581 Base::m_container[key] = std::move(value);
591template<
typename key_t,
typename value_t>
603template<
typename key_t,
typename value_t>
615template<
typename key_t,
typename value_t>
626template<
typename key_t,
typename value_t>
630 Base::m_container.reserve(capacity);
631 m_isCached.reserve(capacity);
639template<
typename key_t,
typename value_t>
650template<
typename key_t,
typename value_t>
653 Base::m_container.reserve(n);
659template<
typename key_t,
typename value_t>
662 if (m_isCached.empty()) {
666 std::size_t capacity = m_isCached.size();
667 while (capacity > 0 && !m_isCached[capacity - 1]) {
671 Base::m_container.resize(capacity);
672 m_isCached.resize(capacity);
678template<
typename key_t,
typename value_t>
681 std::fill(m_isCached.begin(), m_isCached.end(),
false);
693template<
typename key_t,
typename value_t>
696 if (key <
static_cast<key_t
>(Base::m_container.size()) && m_isCached[key]) {
697 return (Base::m_container.begin() + key);
699 return Base::m_container.
end();
712template<
typename key_t,
typename value_t>
715 if (key <
static_cast<key_t
>(Base::m_container.size()) && m_isCached[key]) {
716 return (Base::m_container.cbegin() + key);
718 return Base::m_container.
cend();
732template<
typename key_t,
typename value_t>
735 if (key >=
static_cast<key_t
>(Base::m_container.size())) {
736 Base::m_container.resize(key + 1);
737 m_isCached.resize(key + 1);
740 auto itr = Base::m_container.
begin() + key;
743 m_isCached[key] =
true;
758template<
typename key_t,
typename value_t>
761 if (key >=
static_cast<key_t
>(Base::m_container.size())) {
762 Base::m_container.resize(key + 1);
763 m_isCached.resize(key + 1);
766 auto itr = Base::m_container.
begin() + key;
768 *itr = std::move(value);
769 m_isCached[key] =
true;
780template<
typename key_t,
typename value_t>
783 if (
static_cast<std::size_t
>(key) >= m_isCached.size()) {
787 m_isCached[key] =
false;
820template<
typename key_t,
typename value_t>
831template<
typename key_t,
typename value_t>
843template<
typename key_t,
typename value_t>
856template<
typename key_t,
typename value_t>
859 return static_cast<key_t
>((itr - Base::m_container.begin()));
868template<
typename key_t,
typename value_t>
880template<
typename key_t,
typename value_t>
892template<
typename key_t,
typename value_t>
896 Base::m_container.reserve(capacity);
904template<
typename key_t,
typename value_t>
915template<
typename key_t,
typename value_t>
918 Base::m_container.reserve(n);
924template<
typename key_t,
typename value_t>
927 Base::m_container.squeeze();
933template<
typename key_t,
typename value_t>
936 Base::m_container.clear();
948template<
typename key_t,
typename value_t>
951 return Base::m_container.find(key);
963template<
typename key_t,
typename value_t>
966 return Base::m_container.find(key);
979template<
typename key_t,
typename value_t>
982 auto itr = Base::m_container.find(key);
983 if (itr == Base::m_container.end()) {
984 itr = Base::m_container.reclaim(key);
1002template<
typename key_t,
typename value_t>
1005 auto itr = Base::m_container.find(key);
1006 if (itr == Base::m_container.end()) {
1007 itr = Base::m_container.reclaim(key);
1010 *itr = std::move(value);
1021template<
typename key_t,
typename value_t>
1024 if (!Base::m_container.contains(key)) {
1028 Base::m_container.
erase(key);
1061template<
typename key_t,
typename value_t>
1072template<
typename key_t,
typename value_t>
1075 Base::m_container.dump(stream);
1083template<
typename key_t,
typename value_t>
1086 Base::m_container.restore(stream);
1095template<
typename key_t,
typename value_t>
1107template<
typename key_t,
typename value_t>
1119template<
typename key_t,
typename value_t>
1131template<
typename key_t,
typename value_t>
1133 :
Base(1, kernel, syncMode),
1134 m_isCached(1, kernel, syncMode)
1136 m_isCached.fill(
false);
1144template<
typename key_t,
typename value_t>
1155template<
typename key_t,
typename value_t>
1166template<
typename key_t,
typename value_t>
1175template<
typename key_t,
typename value_t>
1178 m_isCached.fill(
false);
1190template<
typename key_t,
typename value_t>
1193 iterator itr = Base::m_container.find(key);
1194 std::size_t rawId = itr.getRawIndex();
1196 if (m_isCached.rawAt(rawId)) {
1199 return Base::m_container.end();
1212template<
typename key_t,
typename value_t>
1215 const_iterator itr = Base::m_container.find(key);
1216 std::size_t rawId = itr.getRawIndex();
1218 if (m_isCached.rawAt(rawId)) {
1221 return Base::m_container.cend();
1235template<
typename key_t,
typename value_t>
1238 auto itr = Base::m_container.find(key);
1239 std::size_t rawId = itr.getRawIndex();
1242 m_isCached.rawAt(rawId) =
true;
1257template<
typename key_t,
typename value_t>
1260 auto itr = Base::m_container.find(key);
1261 std::size_t rawId = itr.getRawIndex();
1263 *itr = std::move(value);
1264 m_isCached.rawAt(rawId) =
true;
1275template<
typename key_t,
typename value_t>
1284 auto itr = Base::m_container.find(key);
1285 if (itr == Base::m_container.end()) {
1289 std::size_t rawId = itr.getRawIndex();
1291 m_isCached.rawAt(rawId) =
false;
1324template<
typename key_t,
typename value_t>
1327 return (Base::m_container.getSyncMode() != PiercedSyncMaster::SyncMode::SYNC_MODE_DISABLED);
1335template<
typename key_t,
typename value_t>
1338 Base::m_container.dump(stream);
1339 m_isCached.dump(stream);
1347template<
typename key_t,
typename value_t>
1350 Base::m_container.restore(stream);
1351 m_isCached.restore(stream);
1360template<
typename key_t,
typename value_t>
1372template<
typename key_t,
typename value_t>
1384template<
typename key_t,
typename value_t>
1395template<
typename key_t,
typename value_t>
1406template<
typename key_t,
typename value_t>
1417template<
typename key_t,
typename value_t>
1429template<
typename key_t,
typename value_t>
1432 m_kernel(kernel), m_syncMode(syncMode)
1441template<
typename key_t,
typename value_t>
1461template<
typename key_t>
1463 : m_factory(factory),
1473template<
typename key_t>
1475 : m_factory(other.m_factory),
1476 m_cache(other.m_cache->
clone())
1486template<
typename key_t>
1489 m_factory = other.m_factory;
1490 m_cache = other.m_cache->clone();
1500template<
typename key_t>
1511template<
typename key_t>
1526template<
typename key_t>
1529 return _createCache();
1541template<
typename key_t>
1546 return m_cache.get();
1551 m_cache = m_factory->create();
1552 return m_cache.get();
1562template<
typename key_t>
1565 return m_cache.reset();
1576template<
typename key_t>
1590template<
typename key_t>
1595 return m_cache.get();
1599 if (allowCreation) {
1600 return _createCache();
1615template<
typename key_t>
1616template<
typename value_t>
1630template<
typename key_t>
1631template<
typename value_t>
1649template<
typename key_t>
1657template<
typename key_t>
1661 for (std::size_t i = 0; i < other.
m_caches.size(); ++i) {
1671template<
typename key_t>
1682template<
typename key_t>
1685 return m_caches.begin();
1693template<
typename key_t>
1696 return m_caches.end();
1704template<
typename key_t>
1707 return m_caches.cbegin();
1715template<
typename key_t>
1718 return m_caches.cend();
1726template<
typename key_t>
1729 return m_caches.cbegin();
1737template<
typename key_t>
1740 return m_caches.cend();
1748template<
typename key_t>
1751 return m_caches.size();
1762template<
typename id_t>
1763template<
typename container_t,
typename... Args>
1766 std::size_t nCaches = m_caches.size();
1775 if (index == NULL_CACHE_ID) {
1777 while (index < nCaches) {
1778 if (!m_caches[index].hasFactory()) {
1786 typedef typename LevelSetContainerCache<id_t, container_t>::value_type value_type;
1791 if (index < nCaches) {
1792 m_caches.emplace(m_caches.begin() + index, factory);
1794 for (std::size_t k = nCaches; k < index; ++k) {
1795 m_caches.emplace_back(
Item());
1797 m_caches.emplace_back(factory);
1808template<
typename key_t>
1812 if (index == NULL_CACHE_ID) {
1814 }
else if (index >= m_caches.size()) {
1822 m_caches[index] =
Item();
1828template<
typename key_t>
1832 m_caches.shrink_to_fit();
1845template<
typename key_t>
1861template<
typename key_t>
1864 return m_caches[index];
1876template<
typename key_t>
1891template<
typename key_t>
1894 return m_caches.at(index);
1904template<
typename container_t,
typename... Args,
typename std::enable_if<std::is_same<bitpit::PiercedStorage<typename container_t::value_type>, container_t>::value>::type *>
1919template<
typename container_t,
typename... Args,
typename std::enable_if<!std::is_same<bitpit::PiercedStorage<typename container_t::value_type>, container_t>::value>::type *>
1922 return Base::insert<container_t, Args...>(index, std::forward<Args>(args)...);
std::size_t insert(std::size_t index, Args &&... args)
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
const_iterator cend() const
Item & operator[](std::size_t index)
Item & at(std::size_t index)
const_iterator cbegin() const
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 LevelSetContainerBaseCache is the base class for defining caches that stores the values ins...
Entry findEntry(const key_t &key) const override
LevelSetContainerBaseCache(Args &&... args)
void writeBuffer(const std::vector< key_t > &ids, SendBuffer &buffer) const override
const_iterator cbegin() const
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
const_iterator cend() const
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...
LevelSetValueCacheBaseEntry(bool valid)
static const value_t m_dummyValue
The class LevelSetValueCacheEntry allows to get read-only access to a value stored in the cache.
LevelSetValueCacheEntry()
const value_t & operator*() const
The class LevelSetCache is the base class for defining caches that store values.
Metafunction for generating a pierced kernel.
Buffer to be used for receive communications.
Buffer to be used for send communications.
void write(std::ostream &stream, const std::vector< bool > &container)
void read(std::istream &stream, std::vector< bool > &container)
#define BITPIT_UNUSED(variable)