Debian Codegraph @

Source/Test/CHashModificationThread.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 "CHashModificationThread.h"
22
23
24 namespace Konclude {
25
26 namespace Test {
27
28
29
30 CHashModificationThread::CHashModificationThread(CQtHash<cint64,cint64>** qtOriginalHashes, cint64 hashesCount, cint64 modificationCountPerStep, bool extendBaseHashes) {
31 mModificationSteps = 0;
32 mQtOriginalHashes = qtOriginalHashes;
33 mQtModifiedHashes = 0;
34 mMemoryPoolProvider = 0;
35 mUseModifiedHashes = false;
36 mHashesCount = hashesCount;
37 mContextBase = 0;
38 mMemoryPoolAllocationMan = 0;
39 mCanceled = false;
40 mModificationCountPerStep = modificationCountPerStep;
41 mExtendBaseHashes = extendBaseHashes;
42 }
43
44 CHashModificationThread::CHashModificationThread(CQtManagedRestrictedModificationHash<cint64,cint64>** qtModifiedHashes, cint64 hashesCount, CMemoryPoolProvider* memoryPoolProvider, cint64 modificationCountPerStep, bool extendBaseHashes) {
45 mModificationSteps = 0;
46 mQtOriginalHashes = 0;
47 mQtModifiedHashes = qtModifiedHashes;
48 mMemoryPoolProvider = memoryPoolProvider;
49 mUseModifiedHashes = true;
50 mMemoryPoolAllocationMan = new CMemoryPoolAllocationManager(mMemoryPoolProvider);
51 mContextBase = new CContextBase(mMemoryPoolAllocationMan);
52 mHashesCount = hashesCount;
53 mCanceled = false;
54 mModificationCountPerStep = modificationCountPerStep;
55 mExtendBaseHashes = extendBaseHashes;
56 }
57
58 CHashModificationThread::~CHashModificationThread() {
59 quit();
60 wait();
61 delete mMemoryPoolAllocationMan;
62 delete mContextBase;
63 }
64
65
66 void CHashModificationThread::startModifications() {
67 start();
68 moveToThread(this);
69 }
70
71 void CHashModificationThread::stopModifications() {
72 mCanceled = true;
73 }
74
75 cint64 CHashModificationThread::getModificationStepsCount() {
76 return mModificationSteps;
77 }
78
79
80 void CHashModificationThread::run() {
81 qint64 modCountPerStep = mModificationCountPerStep;
82 qint64 maxCacheValue = 100;
83
84 while (!mCanceled) {
85 qint64 modHashIndex = qrand() % mHashesCount;
86 if (!mUseModifiedHashes) {
87 CQtHash<cint64,cint64> *baseHash = mQtOriginalHashes[modHashIndex];
88 CQtHash<cint64,cint64> *modHash = 0;
89 if (mExtendBaseHashes) {
90 modHash = new CQtHash<cint64,cint64>(*baseHash);
91 } else {
92 modHash = new CQtHash<cint64,cint64>();
93 }
94 for (cint64 i = 0; i < modCountPerStep; ++i) {
95 qint64 modHashKey = qrand() % maxCacheValue;
96 cint64 modHashValue = modHash->value(modHashKey,0);
97 modHash->insert(modHashKey,++modHashValue);
98 }
99 delete modHash;
100 ++mModificationSteps;
101 } else {
102 CQtManagedRestrictedModificationHash<cint64,cint64> *baseHash = mQtModifiedHashes[modHashIndex];
103 CQtManagedRestrictedModificationHash<cint64,cint64> *modHash = CObjectAllocator<CQtManagedRestrictedModificationHash<cint64,cint64> >::allocateAndConstruct(mMemoryPoolAllocationMan);
104 if (mExtendBaseHashes) {
105 modHash->init(baseHash,mContextBase);
106 } else {
107 modHash->init(mContextBase);
108 }
109 for (cint64 i = 0; i < modCountPerStep; ++i) {
110 qint64 modHashKey = qrand() % maxCacheValue;
111 cint64 modHashValue = modHash->value(modHashKey,0);
112 modHash->insert(modHashKey,++modHashValue);
113 modHashValue = modHash->value(modHashKey,0);
114 }
115 mMemoryPoolAllocationMan->releaseAllMemory();
116 ++mModificationSteps;
117 }
118 }
119 }
120
121
122
123
124 }; // end namespace Test
125
126 }; // end namespace Konclude