Grok 20.3.2
SequentialCache.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 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 */
17
18#pragma once
19
20#include <vector>
21
22namespace grk
23{
24
33template<typename T>
35{
36public:
42
47 SequentialCache(uint64_t maxChunkSize)
48 : currChunk_(nullptr), chunkSize_(std::min<uint64_t>(maxChunkSize, kSequentialChunkSize)),
49 index_(0), firstElement_(true)
50 {}
51
55 virtual ~SequentialCache(void)
56 {
57 for(auto& ch : chunks)
58 {
59 for(size_t i = 0; i < chunkSize_; ++i)
60 delete ch[i];
61 delete[] ch;
62 }
63 }
64
68 void rewind(void)
69 {
70 if(chunks.empty())
71 return;
72 index_ = 0;
73 currChunk_ = chunks[0];
74 firstElement_ = true;
75 }
76
80 T* next()
81 {
82 uint64_t itemIndex = index_ % chunkSize_;
83 uint64_t chunkIndex = index_ / chunkSize_;
84 bool isInitialized = (currChunk_ != nullptr);
85 bool isLastChunk = (chunkIndex == chunks.size() - 1);
86 bool isEndOfChunk = (itemIndex == chunkSize_ - 1);
87 bool createNewChunk = !isInitialized || (isLastChunk && isEndOfChunk);
88 if(!firstElement_)
89 itemIndex++;
90 if(createNewChunk || isEndOfChunk)
91 {
92 itemIndex = 0;
93 if(!firstElement_)
94 chunkIndex++;
95 if(createNewChunk)
96 {
97 currChunk_ = new T*[chunkSize_];
98 memset(currChunk_, 0, chunkSize_ * sizeof(T*));
99 chunks.push_back(currChunk_);
100 }
101 else
102 {
103 currChunk_ = chunks[chunkIndex];
104 }
105 }
106 auto item = currChunk_[itemIndex];
107 // create new item if null
108 if(!item)
109 {
110 item = create();
111 currChunk_[itemIndex] = item;
112 }
113 if(!firstElement_)
114 index_++;
115 firstElement_ = false;
116 return item;
117 }
118
119protected:
120 virtual T* create(void)
121 {
122 return new T();
123 }
124
125private:
126 std::vector<T**> chunks;
128 uint64_t chunkSize_;
129 uint64_t index_;
131 static constexpr uint64_t kSequentialChunkSize = 1024;
132};
133
134} // namespace grk
void rewind(void)
Rewinds state of cache, in order to read from beginning.
Definition SequentialCache.h:68
uint64_t chunkSize_
Definition SequentialCache.h:128
SequentialCache(uint64_t maxChunkSize)
Constructs a SequentialCache.
Definition SequentialCache.h:47
bool firstElement_
Definition SequentialCache.h:130
static constexpr uint64_t kSequentialChunkSize
Definition SequentialCache.h:131
T * next()
Gets next pointer in cache.
Definition SequentialCache.h:80
virtual ~SequentialCache(void)
Destroys a SequentialCache.
Definition SequentialCache.h:55
std::vector< T ** > chunks
Definition SequentialCache.h:126
virtual T * create(void)
Definition SequentialCache.h:120
SequentialCache(void)
Constructs a SequentialCache.
Definition SequentialCache.h:41
T ** currChunk_
Definition SequentialCache.h:127
uint64_t index_
Definition SequentialCache.h:129
ResWindow.
Definition CompressedChunkCache.h:36