Debian Codegraph @

Source/Reasoner/Preprocess/CConceptUnambiguousHasher.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 "CConceptUnambiguousHasher.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Preprocess {
29
30 CConceptUnambiguousHasher::CConceptUnambiguousHasher(CConcept* concept, bool tagRequired) : mConcept(concept),mTagRequired(tagRequired) {
31 mHashValue = calculateHashValue();
32 }
33
34
35 cint64 CConceptUnambiguousHasher::calculateHashValue() {
36 cint64 hashValue = mConcept->getOperatorCode() << 50;
37 if (mTagRequired) {
38 hashValue += mConcept->getConceptTag();
39 } else {
40 cint64 hashValueExt = ((cint64)mConcept->getRole() << 3)*7;
41 hashValueExt += ((cint64)mConcept->getNominalIndividual())*13;
42 hashValueExt += mConcept->getParameter()*23;
43 hashValue += hashValueExt;
44 cint64 hashValueOps1 = 0;
45 cint64 hashValueOps2 = 1;
46 CSortedNegLinker<CConcept*>* opLinkerIt = mConcept->getOperandList();
47 while (opLinkerIt) {
48 hashValueOps1 += ((cint64)opLinkerIt->getData()) << (opLinkerIt->isNegated()+3);
49 hashValueOps2 *= ((cint64)opLinkerIt->getData()) << (opLinkerIt->isNegated()+3);
50 opLinkerIt = opLinkerIt->getNext();
51 }
52 hashValue += hashValueOps1+hashValueOps2;
53 }
54 return hashValue;
55 }
56
57
58 cint64 CConceptUnambiguousHasher::getConceptHashValue() const {
59 return mHashValue;
60 }
61
62 bool CConceptUnambiguousHasher::operator==(const CConceptUnambiguousHasher& expressionHasher) const {
63 if (mHashValue != expressionHasher.getConceptHashValue()) {
64 return false;
65 } else {
66 if (mTagRequired != expressionHasher.mTagRequired) {
67 return false;
68 }
69 if (mConcept->getOperatorCode() != expressionHasher.mConcept->getOperatorCode()) {
70 return false;
71 }
72 if (mTagRequired) {
73 if (mConcept->getConceptTag() != expressionHasher.mConcept->getConceptTag()) {
74 return false;
75 }
76 } else {
77 if (mConcept->getRole() != expressionHasher.mConcept->getRole()) {
78 return false;
79 }
80 if (mConcept->getNominalIndividual() != expressionHasher.mConcept->getNominalIndividual()) {
81 return false;
82 }
83 if (mConcept->getParameter() != expressionHasher.mConcept->getParameter()) {
84 return false;
85 }
86 CSortedNegLinker<CConcept*>* opLinker1It = mConcept->getOperandList();
87 CSortedNegLinker<CConcept*>* opLinker2 = expressionHasher.mConcept->getOperandList();
88 while (opLinker1It) {
89 bool foundConcept = false;
90 CSortedNegLinker<CConcept*>* opLinker2It = opLinker2;
91 while (opLinker2It && !foundConcept) {
92 if (opLinker1It->getData() == opLinker2It->getData() && opLinker1It->isNegated() == opLinker2It->isNegated()) {
93 foundConcept = true;
94 } else {
95 opLinker2It = opLinker2It->getNext();
96 }
97 }
98 if (foundConcept) {
99 if (opLinker2 == opLinker2It) {
100 opLinker2 = opLinker2->getNext();
101 }
102 } else {
103 return false;
104 }
105 opLinker1It = opLinker1It->getNext();
106 }
107 }
108 }
109 return true;
110 }
111
112
113 }; // end namespace Preprocess
114
115 }; // end namespace Reasoner
116
117 }; // end namespace Konclude