| 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_CLINKER_CPP |
| 22 |
#define KONCLUDE_UTILITIES_CONTAINER_CLINKER_CPP |
| 23 |
|
| 24 |
|
| 25 |
#include "CLinker.h" |
| 26 |
|
| 27 |
|
| 28 |
namespace Konclude { |
| 29 |
|
| 30 |
namespace Utilities { |
| 31 |
|
| 32 |
namespace Container { |
| 33 |
|
| 34 |
template<class T, class S> |
| 35 |
CLinkerBase<T,S>::CLinkerBase() { |
| 36 |
next = 0; |
| 37 |
} |
| 38 |
|
| 39 |
template<class T, class S> |
| 40 |
CLinkerBase<T,S>::CLinkerBase(const T& dataElement, CLinkerBase<T,S>* nextLink) { |
| 41 |
next = 0; |
| 42 |
setNext(nextLink); |
| 43 |
data = dataElement; |
| 44 |
} |
| 45 |
|
| 46 |
template<class T, class S> |
| 47 |
CLinkerBase<T,S>::~CLinkerBase() { |
| 48 |
} |
| 49 |
|
| 50 |
template<class T, class S> |
| 51 |
S* CLinkerBase<T,S>::initLinker(const T& dataElement, CLinkerBase<T,S>* nextLink) { |
| 52 |
next = 0; |
| 53 |
setNext(nextLink); |
| 54 |
data = dataElement; |
| 55 |
return (S*)this; |
| 56 |
} |
| 57 |
|
| 58 |
template<class T, class S> |
| 59 |
bool CLinkerBase<T,S>::hasNext() const { |
| 60 |
return next != 0; |
| 61 |
} |
| 62 |
|
| 63 |
template<class T, class S> |
| 64 |
S* CLinkerBase<T,S>::insertNext(CLinkerBase<T,S>* nextLink) { |
| 65 |
if (nextLink) { |
| 66 |
CLinkerBase<T,S> *tmpNext = next; |
| 67 |
next = nextLink; |
| 68 |
if (tmpNext) { |
| 69 |
nextLink->append(tmpNext); |
| 70 |
} |
| 71 |
} |
| 72 |
return (S*)this; |
| 73 |
} |
| 74 |
|
| 75 |
template<class T, class S> |
| 76 |
inline S* CLinkerBase<T,S>::clearNext() { |
| 77 |
next = nullptr; |
| 78 |
return (S*)this; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
template<class T, class S> |
| 83 |
S* CLinkerBase<T,S>::getNext() const { |
| 84 |
return (S*)next; |
| 85 |
} |
| 86 |
|
| 87 |
template<class T, class S> |
| 88 |
S* CLinkerBase<T,S>::setNext(CLinkerBase<T,S>* nextLink) { |
| 89 |
next = nextLink; |
| 90 |
return (S*)this; |
| 91 |
} |
| 92 |
|
| 93 |
template<class T, class S> |
| 94 |
T& CLinkerBase<T,S>::getData() { |
| 95 |
return data; |
| 96 |
} |
| 97 |
|
| 98 |
template<class T, class S> |
| 99 |
S* CLinkerBase<T,S>::setData(const T& dataElement) { |
| 100 |
data = dataElement; |
| 101 |
return (S*)this; |
| 102 |
} |
| 103 |
|
| 104 |
template<class T, class S> |
| 105 |
S* CLinkerBase<T,S>::append(CLinkerBase<T,S>* appendingList) { |
| 106 |
CLinkerBase<T,S>* last = getLastListLink(); |
| 107 |
last->setNext(appendingList); |
| 108 |
return (S*)this; |
| 109 |
} |
| 110 |
|
| 111 |
template<class T, class S> |
| 112 |
S* CLinkerBase<T,S>::getRemovedNext() { |
| 113 |
CLinkerBase<T,S>* tmpNext = next; |
| 114 |
if (next) { |
| 115 |
CLinkerBase<T,S>* tmpNextNext = next->next; |
| 116 |
next->next = 0; |
| 117 |
setNext(tmpNextNext); |
| 118 |
} |
| 119 |
return (S*)tmpNext; |
| 120 |
} |
| 121 |
|
| 122 |
template<class T, class S> |
| 123 |
S* CLinkerBase<T,S>::getLastListLink() const { |
| 124 |
const CLinkerBase<T,S>* lastListLink = this; |
| 125 |
while (lastListLink->hasNext()) { |
| 126 |
lastListLink = lastListLink->getNext(); |
| 127 |
} |
| 128 |
return (S*)lastListLink; |
| 129 |
} |
| 130 |
|
| 131 |
template<class T, class S> |
| 132 |
cint64 CLinkerBase<T,S>::getCount() const { |
| 133 |
int linkerCount = 0; |
| 134 |
const CLinkerBase<T,S>* itemLinker = this; |
| 135 |
while (itemLinker != nullptr) { |
| 136 |
++linkerCount; |
| 137 |
itemLinker = itemLinker->getNext(); |
| 138 |
} |
| 139 |
return linkerCount; |
| 140 |
} |
| 141 |
|
| 142 |
template<class T, class S> |
| 143 |
cint64 CLinkerBase<T,S>::size() const { |
| 144 |
return getCount(); |
| 145 |
} |
| 146 |
|
| 147 |
template<class T, class S> |
| 148 |
bool CLinkerBase<T,S>::contains(const T& dataElement) const { |
| 149 |
CLinkerBase<T,S>* itemLinker = this; |
| 150 |
while (itemLinker != nullptr) { |
| 151 |
if (itemLinker->getData() == dataElement) { |
| 152 |
return true; |
| 153 |
} |
| 154 |
itemLinker = itemLinker->getNext(); |
| 155 |
} |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
template<class T, class S> |
| 161 |
S* CLinkerBase<T,S>::removeOne(const T& dataElement) { |
| 162 |
CLinkerBase<T,S>* loc = this; |
| 163 |
if (loc->getData() == dataElement) { |
| 164 |
loc = loc->getNext(); |
| 165 |
return (S*)loc; |
| 166 |
} else { |
| 167 |
while (loc) { |
| 168 |
CLinkerBase<T,S>* nxt = loc->getNext(); |
| 169 |
if (nxt) { |
| 170 |
if (nxt->getData() == dataElement) { |
| 171 |
loc->setNext(nxt->getNext()); |
| 172 |
return (S*)this; |
| 173 |
} |
| 174 |
} |
| 175 |
loc = loc->getNext(); |
| 176 |
} |
| 177 |
} |
| 178 |
return (S*)this; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
template<class T, class S> |
| 183 |
S* CLinkerBase<T,S>::removeAll(const T& dataElement) { |
| 184 |
CLinkerBase<T,S>* first = this; |
| 185 |
CLinkerBase<T,S>* loc = this; |
| 186 |
while (loc->getData() == dataElement) { |
| 187 |
loc = loc->getNext(); |
| 188 |
first = loc; |
| 189 |
} |
| 190 |
while (loc) { |
| 191 |
CLinkerBase<T,S>* nxt = loc->getNext(); |
| 192 |
if (nxt) { |
| 193 |
if (nxt->getData() == dataElement) { |
| 194 |
loc->setNext(nxt->getNext()); |
| 195 |
} |
| 196 |
} |
| 197 |
loc = loc->getNext(); |
| 198 |
} |
| 199 |
return (S*)this; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
}; // end namespace Container |
| 204 |
|
| 205 |
}; // end namespace Utilities |
| 206 |
|
| 207 |
}; // end namespace Konclude |
| 208 |
|
| 209 |
#endif // KONCLUDE_UTILITIES_CONTAINER_CLINKER_CPP |