Grok 13.0.2
SparseCache.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2024 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17#include "grk_includes.h"
18
19#include <map>
20
21namespace grk
22{
23template<typename T>
25{
26 public:
27 SparseCache(uint64_t maxChunkSize)
28 : chunkSize_(std::min<uint64_t>(maxChunkSize, 1024)), currChunk_(nullptr), currChunkIndex_(0)
29 {}
30 virtual ~SparseCache(void)
31 {
32 for(auto& ch : chunks)
33 {
34 for(size_t i = 0; i < chunkSize_; ++i)
35 delete ch.second[i];
36 delete[] ch.second;
37 }
38 }
39
40 T* tryGet(uint64_t index)
41 {
42 uint64_t chunkIndex = index / chunkSize_;
43 uint64_t itemIndex = index % chunkSize_;
44 if(currChunk_ == nullptr || (chunkIndex != currChunkIndex_))
45 {
46 auto iter = chunks.find(chunkIndex);
47 if(iter != chunks.end())
48 {
49 currChunk_ = iter->second;
50 currChunkIndex_ = chunkIndex; // Update currChunkIndex_ when the chunk is found
51 }
52 else
53 {
54 return nullptr;
55 }
56 }
57 return currChunk_[itemIndex];
58 }
59
60 T* get(uint64_t index)
61 {
62 uint64_t chunkIndex = index / chunkSize_;
63 uint64_t itemIndex = index % chunkSize_;
64 if(currChunk_ == nullptr || (chunkIndex != currChunkIndex_))
65 {
66 currChunkIndex_ = chunkIndex;
67 auto iter = chunks.find(chunkIndex);
68 if(iter != chunks.end())
69 {
70 currChunk_ = iter->second;
71 }
72 else
73 {
74 currChunk_ = new T*[chunkSize_];
75 memset(currChunk_, 0, chunkSize_ * sizeof(T*));
76 chunks[chunkIndex] = currChunk_;
77 }
78 }
79 auto item = currChunk_[itemIndex];
80 if(!item)
81 {
82 item = create(index);
83 currChunk_[itemIndex] = item;
84 }
85 return item;
86 }
87
88 protected:
89 virtual T* create(uint64_t index) = 0;
90
91 private:
92 std::map<uint64_t, T**> chunks;
93 uint64_t chunkSize_;
96};
97
98} // namespace grk
Definition SparseCache.h:25
T * tryGet(uint64_t index)
Definition SparseCache.h:40
virtual T * create(uint64_t index)=0
T * get(uint64_t index)
Definition SparseCache.h:60
T ** currChunk_
Definition SparseCache.h:94
std::map< uint64_t, T ** > chunks
Definition SparseCache.h:92
uint64_t currChunkIndex_
Definition SparseCache.h:95
SparseCache(uint64_t maxChunkSize)
Definition SparseCache.h:27
virtual ~SparseCache(void)
Definition SparseCache.h:30
uint64_t chunkSize_
Definition SparseCache.h:93
Copyright (C) 2016-2024 Grok Image Compression Inc.
Definition ICacheable.h:20