Debian Codegraph @

Source/Reasoner/Kernel/Cache/CCacheTaggingPool.cpp

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "CCacheTaggingPool.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Kernel {
29
30 namespace Cache {
31
32
33 CCacheTaggingPool::CCacheTaggingPool(cint64 poolSize) {
34 mTagPool = nullptr;
35 mPoolIndex = 0;
36 mPoolSize = poolSize;
37 mPoolSizeMask = mPoolSize-1;
38 mNextPoolStartTag = 0;
39
40 qsrand(QTime::currentTime().msec());
41 }
42
43
44 CCacheTaggingPool::~CCacheTaggingPool() {
45 delete[] mTagPool;
46 }
47
48 cint64 CCacheTaggingPool::takeNextTag() {
49 cint64 nextTag = 0;
50 if (!hasMoreTags()) {
51 createNextTaggingPool();
52 }
53 nextTag = mTagPool[mPoolIndex++];
54 return nextTag;
55 }
56
57 bool CCacheTaggingPool::hasMoreTags() {
58 return mTagPool && mPoolIndex < mPoolSize;
59 }
60
61 CCacheTaggingPool* CCacheTaggingPool::createNextTaggingPool() {
62 if (!mTagPool) {
63 mTagPool = new cint64[mPoolSize];
64 }
65 for (cint64 i = 0; i < mPoolSize; ++i) {
66 mTagPool[i] = i+mNextPoolStartTag;
67 }
68 mNextPoolStartTag += mPoolSize;
69 // build random permutation using Fisher-Yates algorithm
70 for (cint64 i = mPoolSize-1; i > 0; --i) {
71 cint64 randomIndex = qrand() % (i+1);
72 cint64 tmpVal = mTagPool[randomIndex];
73 mTagPool[randomIndex] = mTagPool[i];
74 mTagPool[i] = tmpVal;
75 }
76 mPoolIndex = 0;
77 return this;
78 }
79
80
81 }; // end namespace Cache
82
83 }; // end namespace Kernel
84
85 }; // end namespace Reasoner
86
87 }; // end namespace Konclude