Grok 10.0.0
SequentialCache.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2022 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 <vector>
20
21namespace grk
22{
23// dynamic array of pointers of type T
24// hybrid design : combination of std::vector and simple array
25template<typename T>
27{
28 public:
30 SequentialPtrCache(uint64_t maxChunkSize)
31 : currChunk_(nullptr), chunkSize_(std::min<uint64_t>(maxChunkSize, kSequentialChunkSize)),
32 index_(0)
33 {}
34 virtual ~SequentialPtrCache(void)
35 {
36 for(auto& ch : chunks)
37 {
38 for(size_t i = 0; i < chunkSize_; ++i)
39 delete ch[i];
40 delete[] ch;
41 }
42 }
43 void rewind(void)
44 {
45 if(chunks.empty())
46 return;
47 index_ = 0;
48 currChunk_ = chunks[0];
49 }
50 // get next item
51 T* get()
52 {
53 uint64_t itemIndex = index_ % chunkSize_;
54 uint64_t chunkIndex = index_ / chunkSize_;
55 bool isInitialized = (currChunk_ != nullptr);
56 bool isLastChunk = (chunkIndex == chunks.size() - 1);
57 bool isEndOfChunk = (itemIndex == chunkSize_ - 1);
58 bool createNewChunk = !isInitialized || (isLastChunk && isEndOfChunk);
59 itemIndex++;
60 if(createNewChunk || isEndOfChunk)
61 {
62 itemIndex = 0;
63 chunkIndex++;
64 if(createNewChunk)
65 {
66 currChunk_ = new T*[chunkSize_];
67 memset(currChunk_, 0, chunkSize_ * sizeof(T*));
68 chunks.push_back(currChunk_);
69 }
70 else
71 {
72 currChunk_ = chunks[chunkIndex];
73 }
74 }
75 auto item = currChunk_[itemIndex];
76 // create new item if null
77 if(!item)
78 {
79 item = create();
80 currChunk_[itemIndex] = item;
81 }
82 if(isInitialized)
83 index_++;
84 return item;
85 }
86
87 protected:
88 virtual T* create(void)
89 {
90 auto item = new T();
91 return item;
92 }
93
94 private:
95 std::vector<T**> chunks;
97 uint64_t chunkSize_;
98 uint64_t index_;
99 static constexpr uint64_t kSequentialChunkSize = 1024;
100};
101
102} // namespace grk
Definition: SequentialCache.h:27
SequentialPtrCache(void)
Definition: SequentialCache.h:29
virtual ~SequentialPtrCache(void)
Definition: SequentialCache.h:34
uint64_t index_
Definition: SequentialCache.h:98
void rewind(void)
Definition: SequentialCache.h:43
T * get()
Definition: SequentialCache.h:51
T ** currChunk_
Definition: SequentialCache.h:96
uint64_t chunkSize_
Definition: SequentialCache.h:97
virtual T * create(void)
Definition: SequentialCache.h:88
static constexpr uint64_t kSequentialChunkSize
Definition: SequentialCache.h:99
SequentialPtrCache(uint64_t maxChunkSize)
Definition: SequentialCache.h:30
std::vector< T ** > chunks
Definition: SequentialCache.h:95
Copyright (C) 2016-2022 Grok Image Compression Inc.
Definition: ICacheable.h:20