Grok  9.5.0
SparseCache.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2021 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 
21 namespace grk
22 {
23 template<typename T>
25 {
26  public:
27  SparseCache(uint64_t maxChunkSize)
28  : m_chunkSize(std::min<uint64_t>(maxChunkSize, 1024)), m_currChunk(nullptr),
30  {}
31  virtual ~SparseCache(void)
32  {
33  for(auto& ch : chunks)
34  {
35  for(size_t i = 0; i < m_chunkSize; ++i)
36  delete ch.second[i];
37  delete[] ch.second;
38  }
39  }
40 
41  T* tryGet(uint64_t index)
42  {
43  uint64_t chunkIndex = index / m_chunkSize;
44  uint64_t itemIndex = index % m_chunkSize;
45  if(m_currChunk == nullptr || (chunkIndex != m_currChunkIndex))
46  {
47  auto iter = chunks.find(chunkIndex);
48  if(iter != chunks.end())
49  m_currChunk = iter->second;
50  else
51  return nullptr;
52  }
53  return m_currChunk[itemIndex];
54  }
55 
56  T* get(uint64_t index)
57  {
58  uint64_t chunkIndex = index / m_chunkSize;
59  uint64_t itemIndex = index % m_chunkSize;
60  if(m_currChunk == nullptr || (chunkIndex != m_currChunkIndex))
61  {
62  m_currChunkIndex = chunkIndex;
63  auto iter = chunks.find(chunkIndex);
64  if(iter != chunks.end())
65  {
66  m_currChunk = iter->second;
67  }
68  else
69  {
70  m_currChunk = new T*[m_chunkSize];
71  memset(m_currChunk, 0, m_chunkSize * sizeof(T*));
72  chunks[chunkIndex] = m_currChunk;
73  }
74  }
75  auto item = m_currChunk[itemIndex];
76  if(!item)
77  {
78  item = create(index);
79  m_currChunk[itemIndex] = item;
80  }
81  return item;
82  }
83 
84  protected:
85  virtual T* create(uint64_t index)
86  {
87  GRK_UNUSED(index);
88  auto item = new T();
89  return item;
90  }
91 
92  private:
93  std::map<uint64_t, T**> chunks;
94  uint64_t m_chunkSize;
96  uint64_t m_currChunkIndex;
97 };
98 
99 } // namespace grk
Definition: SparseCache.h:25
uint64_t m_chunkSize
Definition: SparseCache.h:94
T * tryGet(uint64_t index)
Definition: SparseCache.h:41
T ** m_currChunk
Definition: SparseCache.h:95
T * get(uint64_t index)
Definition: SparseCache.h:56
std::map< uint64_t, T ** > chunks
Definition: SparseCache.h:93
SparseCache(uint64_t maxChunkSize)
Definition: SparseCache.h:27
virtual ~SparseCache(void)
Definition: SparseCache.h:31
uint64_t m_currChunkIndex
Definition: SparseCache.h:96
virtual T * create(uint64_t index)
Definition: SparseCache.h:85
#define GRK_UNUSED(x)
Definition: grk_includes.h:87
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: ICacheable.h:20