Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tile_hash_table.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2004 C. Boemann <cbo@boemann.dk>
3 * SPDX-FileCopyrightText: 2009 Dmitry Kazakov <dimula73@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#ifndef KIS_TILEHASHTABLE_H_
9#define KIS_TILEHASHTABLE_H_
10
11#include "kis_tile.h"
12
13
14
23template<class T>
25{
26public:
27 typedef T TileType;
30
34
35 /* virtual? */
37
38 bool isEmpty() {
39 return !m_numTiles;
40 }
41
42 bool tileExists(qint32 col, qint32 row);
43
50 TileTypeSP getExistingTile(qint32 col, qint32 row);
51
60 TileTypeSP getTileLazy(qint32 col, qint32 row, bool& newTile);
61
72 TileTypeSP getReadOnlyTileLazy(qint32 col, qint32 row, bool &existingTile);
73 void addTile(TileTypeSP tile);
74 bool deleteTile(TileTypeSP tile);
75 bool deleteTile(qint32 col, qint32 row);
76
77 void clear();
78
82
83 qint32 numTiles() {
84 return m_numTiles;
85 }
86
87 void debugPrintInfo();
88 void debugMaxListLength(qint32 &min, qint32 &max);
89
90private:
91
92 TileTypeSP getTileMinefieldWalk(qint32 col, qint32 row, qint32 idx);
93 TileTypeSP getTile(qint32 col, qint32 row, qint32 idx);
94 void linkTile(TileTypeSP tile, qint32 idx);
95 bool unlinkTile(qint32 col, qint32 row, qint32 idx);
96
98 inline KisTileData* defaultTileDataImp() const;
99
100 static inline quint32 calculateHash(qint32 col, qint32 row);
101
102 inline qint32 debugChainLen(qint32 idx);
104 void sanityChecksumCheck();
105private:
106 template<class U, class LockerType> friend class KisTileHashTableIteratorTraits;
107
108 static const qint32 TABLE_SIZE = 1024;
111
114
115 mutable QReadWriteLock m_lock;
116};
117
119
120
121
131template<class T, class LockerType>
133{
134public:
135 typedef T TileType;
137
146
149
150 void next() {
151 if (m_tile) {
152 m_tile = m_tile->next();
153 if (!m_tile) {
154 qint32 idx = nextNonEmptyList(m_index + 1);
156 m_index = idx;
157 m_tile = m_hashTable->m_hashTable[idx];
158 } else {
159 //EOList reached
160 m_index = -1;
161 // m_tile.clear(); // already null
162 }
163 }
164 }
165 }
166
167 TileTypeSP tile() const {
168 return m_tile;
169 }
170 bool isDone() const {
171 return !m_tile;
172 }
173
174 // disable the method if we didn't lock for writing
175 template <class Helper = LockerType>
176 typename std::enable_if<std::is_same<Helper, QWriteLocker>::value, void>::type
179 next();
180
181 const qint32 idx = m_hashTable->calculateHash(tile->col(), tile->row());
182 m_hashTable->unlinkTile(tile->col(), tile->row(), idx);
183 }
184
185 // disable the method if we didn't lock for writing
186 template <class Helper = LockerType>
187 typename std::enable_if<std::is_same<Helper, QWriteLocker>::value, void>::type
190 next();
191
192 const qint32 idx = m_hashTable->calculateHash(tile->col(), tile->row());
193 m_hashTable->unlinkTile(tile->col(), tile->row(), idx);
194
195 newHashTable->addTile(tile);
196 }
197
198protected:
200 qint32 m_index;
202 LockerType m_locker;
203
204protected:
205 qint32 nextNonEmptyList(qint32 startIdx) {
206 qint32 idx = startIdx;
207
209 !m_hashTable->m_hashTable[idx]) {
210 idx++;
211 }
212
213 return idx;
214 }
215private:
216 Q_DISABLE_COPY(KisTileHashTableIteratorTraits)
217};
218
219
223
224#endif /* KIS_TILEHASHTABLE_H_ */
std::enable_if< std::is_same< Helper, QWriteLocker >::value, void >::type moveCurrentToHashTable(KisTileHashTableTraits< T > *newHashTable)
qint32 nextNonEmptyList(qint32 startIdx)
std::enable_if< std::is_same< Helper, QWriteLocker >::value, void >::type deleteCurrent()
KisTileHashTableTraits< T > * m_hashTable
KisTileHashTableIteratorTraits(KisTileHashTableTraits< T > *ht)
TileTypeSP getTileMinefieldWalk(qint32 col, qint32 row, qint32 idx)
KisTileData * refAndFetchDefaultTileData() const
static const qint32 TABLE_SIZE
TileTypeSP getReadOnlyTileLazy(qint32 col, qint32 row, bool &existingTile)
TileTypeSP getExistingTile(qint32 col, qint32 row)
void addTile(TileTypeSP tile)
KisTileData * defaultTileData() const
bool deleteTile(TileTypeSP tile)
void debugMaxListLength(qint32 &min, qint32 &max)
void setDefaultTileData(KisTileData *defaultTileData)
KisSharedPtr< T > TileTypeSP
static quint32 calculateHash(qint32 col, qint32 row)
bool tileExists(qint32 col, qint32 row)
KisTileHashTableTraits(KisMementoManager *mm)
qint32 debugChainLen(qint32 idx)
void setDefaultTileDataImp(KisTileData *defaultTileData)
KisTileData * defaultTileDataImp() const
TileTypeSP getTile(qint32 col, qint32 row, qint32 idx)
KisWeakSharedPtr< T > TileTypeWSP
TileTypeSP getTileLazy(qint32 col, qint32 row, bool &newTile)
void linkTile(TileTypeSP tile, qint32 idx)
KisMementoManager * m_mementoManager
bool unlinkTile(qint32 col, qint32 row, qint32 idx)
KisTileHashTableTraits< KisTile > KisTileHashTable
KisTileHashTableIteratorTraits< KisTile, QWriteLocker > KisTileHashTableIterator
KisTileHashTableIteratorTraits< KisTile, QReadLocker > KisTileHashTableConstIterator
struct Tile * newTile(struct rect r)
Definition pixels.c:231