Debian Codegraph @

Source/Utilities/Container/CSortedNegLinker.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 #ifndef KONCLUDE_UTILITIES_CONTAINER_CSORTEDNEGLINKER_CPP
22 #define KONCLUDE_UTILITIES_CONTAINER_CSORTEDNEGLINKER_CPP
23
24
25 #include "CSortedNegLinker.h"
26
27
28 namespace Konclude {
29
30 namespace Utilities {
31
32 namespace Container {
33
34 template<class T, class S, class C>
35 CSortedNegLinkerBase<T,S,C>::CSortedNegLinkerBase() : CNegLinkerBase<T,S>() {
36 }
37
38 template<class T, class S, class C>
39 CSortedNegLinkerBase<T,S,C>::CSortedNegLinkerBase(const T& dataElement, bool negation, CSortedNegLinkerBase<T,S,C>* nextLink) : CNegLinkerBase<T,S>(dataElement,negation,nextLink) {
40 }
41
42 template<class T, class S, class C>
43 CSortedNegLinkerBase<T,S,C>::~CSortedNegLinkerBase() {
44 }
45
46
47 template<class T, class S, class C>
48 S* CSortedNegLinkerBase<T,S,C>::insertNextSorted(CSortedNegLinkerBase<T,S,C>* nextLink) {
49 CSortedNegLinkerBase<T,S,C>* firstLinker = this;
50 while (nextLink) {
51
52 CSortedNegLinkerBase<T,S,C>* nIns = nextLink;
53
54 nextLink = nextLink->getNext();
55 nIns->setNext(nullptr);
56
57 CSortedNegLinkerBase<T,S,C>* list = firstLinker;
58 CSortedNegLinkerBase<T,S,C>* lastList = nullptr;
59 bool inserted = false;
60 const T& dataIns = nIns->getData();
61 bool dataInsNegated = nIns->isNegated();
62 while (list) {
63 const T& dataList = list->getData();
64 bool dataListNegated = list->isNegated();
65 // compare items
66 if (isSortedBeforeCompare(dataIns,dataInsNegated,dataList,dataListNegated)) {
67 if (lastList == nullptr){
68 firstLinker = nIns->insertNext(firstLinker);
69 } else {
70 lastList->insertNext(nIns);
71 }
72 inserted = true;
73 break;
74 }
75 lastList = list;
76 list = (CSortedNegLinkerBase<T,S,C>*)list->next;
77 }
78 if (!inserted) {
79 lastList->insertNext(nIns);
80 }
81 }
82 return (S*)firstLinker;
83 }
84
85
86
87 template<class T, class S, class C>
88 S* CSortedNegLinkerBase<T,S,C>::insertSortedNextSorted(CSortedNegLinkerBase<T,S,C>* nextLink) {
89 // items from nextLink are already sorted!
90 CSortedNegLinkerBase<T,S,C>* firstLinker = this;
91
92 CSortedNegLinkerBase<T,S,C>* list = firstLinker;
93 CSortedNegLinkerBase<T,S,C>* lastList = nullptr;
94
95 while (nextLink) {
96
97 CSortedNegLinkerBase<T,S,C>* nIns = nextLink;
98
99 nextLink = nextLink->getNext();
100 nIns->setNext(nullptr);
101
102 bool inserted = false;
103 const T& dataIns = nIns->getData();
104 bool dataInsNegated = nIns->isNegated();
105 while (list) {
106 const T& dataList = list->getData();
107 bool dataListNegated = list->isNegated();
108 if (isSortedBeforeCompare(dataIns,dataInsNegated,dataList,dataListNegated)) {
109 if (lastList == 0){
110 firstLinker = nIns->insertNext(firstLinker);
111 list = (CSortedNegLinkerBase<T,S,C>*)firstLinker->next;
112 lastList = firstLinker;
113 } else {
114 lastList->insertNext(nIns);
115 lastList = (CSortedNegLinkerBase<T,S,C>*)lastList->next;
116 // list is already on the correct position
117 }
118 inserted = true;
119 break;
120 }
121 lastList = list;
122 list = (CSortedNegLinkerBase<T,S,C>*)list->next;
123 }
124 if (!inserted) {
125 lastList->insertNext(nIns);
126 lastList = nIns;
127 }
128 }
129 return (S*)firstLinker;
130 }
131
132
133 template<class T, class S, class C>
134 bool CSortedNegLinkerBase<T,S,C>::isSortedBeforeCompare(const T& checkData, const bool& checkNegation, const T& linkedData, const bool& linkedNegation) const {
135 return C::sortedNegLinkerDataCompare(checkData,checkNegation,linkedData,linkedNegation);
136 }
137
138
139 }; // end namespace Container
140
141 }; // end namespace Utilities
142
143 }; // end namespace Konclude
144
145 #endif // KONCLUDE_UTILITIES_CONTAINER_CSORTEDNEGLINKER_CPP