Debian Codegraph @

Source/Reasoner/Kernel/Process/CNodeSwitchHistory.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 "CNodeSwitchHistory.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Kernel {
29
30 namespace Process {
31
32
33 CNodeSwitchHistory::CNodeSwitchHistory(CProcessContext* processContext) : mContext(processContext) {
34 mDownData = nullptr;
35 mLevelingCount = 5;
36 }
37
38
39 CNodeSwitchHistory::~CNodeSwitchHistory() {
40 }
41
42
43 CNodeSwitchHistory* CNodeSwitchHistory::initSwitchHistory(CNodeSwitchHistory* prevSwitchHistory) {
44 if (prevSwitchHistory) {
45 CNodeSwitchHistoryLinkData* prevDownData = prevSwitchHistory->mDownData;
46 mDownData = prevDownData;
47 } else {
48 mDownData = nullptr;
49 }
50 return this;
51 }
52
53
54 CNodeSwitchHistory* CNodeSwitchHistory::addIndividualProcessNodeSwitch(CIndividualProcessNode* individual, cint64 indiSwitchTag) {
55 cint64 indiAncDepth = individual->getIndividualAncestorDepth();
56 return addIndividualProcessNodeSwitch(indiAncDepth,individual->getIndividualNodeID(),indiSwitchTag);
57 }
58
59
60 CNodeSwitchHistory* CNodeSwitchHistory::updateLastIndividualProcessNodeSwitch(cint64 indiAncDepth, cint64 indiID) {
61 CNodeSwitchHistoryLinkData* data = mDownData;
62 while (data) {
63 data->mNodeAncestorDepth = qMin(data->mNodeAncestorDepth,indiAncDepth);
64 data->mNodeIndividualID = qMin(data->mNodeIndividualID,indiID);
65 data = data->mUpData;
66 }
67 return this;
68 }
69
70
71 CNodeSwitchHistory* CNodeSwitchHistory::addIndividualProcessNodeSwitch(cint64 indiAncDepth, cint64 indiID, cint64 indiSwitchTag) {
72 CNodeSwitchHistoryLinkData* newData = CObjectAllocator< CNodeSwitchHistoryLinkData >::allocateAndConstruct(mContext->getUsedMemoryAllocationManager());
73 newData->mNextData = mDownData;
74 if (mDownData) {
75 newData->mLevelCount = mDownData->mLevelCount;
76 }
77 newData->mNodeSwitchTag = indiSwitchTag;
78 newData->mNodeAncestorDepth = indiAncDepth;
79 newData->mNodeIndividualID = indiID;
80 ++newData->mLevelCount;
81 mDownData = newData;
82 updateUpperData(newData,indiSwitchTag);
83 return this;
84 }
85
86
87 CNodeSwitchHistory* CNodeSwitchHistory::updateUpperData(CNodeSwitchHistoryLinkData* data, cint64 indiSwitchTag) {
88 if ((data->mLevelCount % mLevelingCount) == 0) {
89 CNodeSwitchHistoryLinkData* upperData = nullptr;
90 CNodeSwitchHistoryLinkData* tmpData = data;
91 cint64 minAncDepth = std::numeric_limits<cint64>::max();
92 cint64 minIndiID = std::numeric_limits<cint64>::max();
93 for (cint64 i = 0; i < mLevelingCount; ++i, tmpData = tmpData->mNextData) {
94 minAncDepth = qMin(minAncDepth,tmpData->mNodeAncestorDepth);
95 minIndiID = qMin(minIndiID,tmpData->mNodeIndividualID);
96 }
97 if (tmpData && tmpData->mUpData) {
98 upperData = tmpData->mUpData;
99 }
100 CNodeSwitchHistoryLinkData* newUpperData = CObjectAllocator< CNodeSwitchHistoryLinkData >::allocateAndConstruct(mContext->getUsedMemoryAllocationManager());
101 if (upperData) {
102 newUpperData->mLevelCount = upperData->mLevelCount;
103 }
104 newUpperData->mNextData = upperData;
105 newUpperData->mNodeSwitchTag = indiSwitchTag;
106 newUpperData->mDownData = data;
107 data->mUpData = newUpperData;
108 ++newUpperData->mLevelCount;
109 newUpperData->mNodeAncestorDepth = minAncDepth;
110 newUpperData->mNodeIndividualID = minIndiID;
111 updateUpperData(newUpperData,indiSwitchTag);
112 }
113 return this;
114 }
115
116
117 cint64 CNodeSwitchHistory::getMinIndividualAncestorDepth(cint64 indiSwitchTag) {
118 cint64 minAncDepth,minIndiID;
119 getMinIndividualAncestorDepthAndNodeID(indiSwitchTag,minAncDepth,minIndiID);
120 return minAncDepth;
121 }
122
123
124
125 cint64 CNodeSwitchHistory::getMinIndividualNodeID(cint64 indiSwitchTag) {
126 cint64 minAncDepth,minIndiID;
127 getMinIndividualAncestorDepthAndNodeID(indiSwitchTag,minAncDepth,minIndiID);
128 return minIndiID;
129 }
130
131
132 bool CNodeSwitchHistory::getMinIndividualAncestorDepthAndNodeID(cint64 indiSwitchTag, cint64& minAncDepth, cint64& minIndiID) {
133 minAncDepth = std::numeric_limits<cint64>::max();
134 minIndiID = std::numeric_limits<cint64>::max();
135 CNodeSwitchHistoryLinkData* data = mDownData;
136 // first walk bottom up
137 while (data && data->mNextData) {
138 if (data->mNodeSwitchTag == indiSwitchTag) {
139 return true;
140 }
141 if (data->mNextData->mNodeSwitchTag == indiSwitchTag) {
142 minIndiID = qMin(minIndiID,data->mNodeIndividualID);
143 minAncDepth = qMin(minAncDepth,data->mNodeAncestorDepth);
144 return true;
145 }
146 if (data->mNextData->mNodeSwitchTag < indiSwitchTag) {
147 break;
148 }
149 if (data->mUpData) {
150 data = data->mUpData;
151 } else {
152 minIndiID = qMin(minIndiID,data->mNodeIndividualID);
153 minAncDepth = qMin(minAncDepth,data->mNodeAncestorDepth);
154 data = data->mNextData;
155 }
156 }
157 // second walk top down
158 while (data) {
159 while (data->mNextData && data->mNextData->mNodeSwitchTag > indiSwitchTag) {
160 minIndiID = qMin(minIndiID,data->mNodeIndividualID);
161 minAncDepth = qMin(minAncDepth,data->mNodeAncestorDepth);
162 data = data->mNextData;
163 }
164 if (data->mNextData && data->mNextData->mNodeSwitchTag == indiSwitchTag) {
165 minIndiID = qMin(minIndiID,data->mNodeIndividualID);
166 minAncDepth = qMin(minAncDepth,data->mNodeAncestorDepth);
167 return true;
168 } else {
169 if (data->mDownData) {
170 // go down
171 data = data->mDownData;
172 } else {
173 if (data->mNodeSwitchTag > indiSwitchTag) {
174 minIndiID = qMin(minIndiID,data->mNodeIndividualID);
175 minAncDepth = qMin(minAncDepth,data->mNodeAncestorDepth);
176 }
177 data = data->mNextData;
178 }
179 }
180 }
181 return true;
182 }
183
184
185 }; // end namespace Process
186
187 }; // end namespace Kernel
188
189 }; // end namespace Reasoner
190
191 }; // end namespace Konclude