/* * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. * * This file is part of the reasoning system Konclude. * For details and support, see . * * Konclude is free software: you can redistribute it and/or modify * it under the terms of version 3 of the GNU Lesser General Public * License (LGPLv3) as published by the Free Software Foundation. * * Konclude is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU (Lesser) General Public License for more details. * * You should have received a copy of the GNU (Lesser) General Public * License along with Konclude. If not, see . * */ #ifndef KONCLUDE_UTILITIES_CONTAINER_CLINKER_CPP #define KONCLUDE_UTILITIES_CONTAINER_CLINKER_CPP #include "CLinker.h" namespace Konclude { namespace Utilities { namespace Container { template CLinkerBase::CLinkerBase() { next = 0; } template CLinkerBase::CLinkerBase(const T& dataElement, CLinkerBase* nextLink) { next = 0; setNext(nextLink); data = dataElement; } template CLinkerBase::~CLinkerBase() { } template S* CLinkerBase::initLinker(const T& dataElement, CLinkerBase* nextLink) { next = 0; setNext(nextLink); data = dataElement; return (S*)this; } template bool CLinkerBase::hasNext() const { return next != 0; } template S* CLinkerBase::insertNext(CLinkerBase* nextLink) { if (nextLink) { CLinkerBase *tmpNext = next; next = nextLink; if (tmpNext) { nextLink->append(tmpNext); } } return (S*)this; } template inline S* CLinkerBase::clearNext() { next = nullptr; return (S*)this; } template S* CLinkerBase::getNext() const { return (S*)next; } template S* CLinkerBase::setNext(CLinkerBase* nextLink) { next = nextLink; return (S*)this; } template T& CLinkerBase::getData() { return data; } template S* CLinkerBase::setData(const T& dataElement) { data = dataElement; return (S*)this; } template S* CLinkerBase::append(CLinkerBase* appendingList) { CLinkerBase* last = getLastListLink(); last->setNext(appendingList); return (S*)this; } template S* CLinkerBase::getRemovedNext() { CLinkerBase* tmpNext = next; if (next) { CLinkerBase* tmpNextNext = next->next; next->next = 0; setNext(tmpNextNext); } return (S*)tmpNext; } template S* CLinkerBase::getLastListLink() const { const CLinkerBase* lastListLink = this; while (lastListLink->hasNext()) { lastListLink = lastListLink->getNext(); } return (S*)lastListLink; } template cint64 CLinkerBase::getCount() const { int linkerCount = 0; const CLinkerBase* itemLinker = this; while (itemLinker != nullptr) { ++linkerCount; itemLinker = itemLinker->getNext(); } return linkerCount; } template cint64 CLinkerBase::size() const { return getCount(); } template bool CLinkerBase::contains(const T& dataElement) const { CLinkerBase* itemLinker = this; while (itemLinker != nullptr) { if (itemLinker->getData() == dataElement) { return true; } itemLinker = itemLinker->getNext(); } return false; } template S* CLinkerBase::removeOne(const T& dataElement) { CLinkerBase* loc = this; if (loc->getData() == dataElement) { loc = loc->getNext(); return (S*)loc; } else { while (loc) { CLinkerBase* nxt = loc->getNext(); if (nxt) { if (nxt->getData() == dataElement) { loc->setNext(nxt->getNext()); return (S*)this; } } loc = loc->getNext(); } } return (S*)this; } template S* CLinkerBase::removeAll(const T& dataElement) { CLinkerBase* first = this; CLinkerBase* loc = this; while (loc->getData() == dataElement) { loc = loc->getNext(); first = loc; } while (loc) { CLinkerBase* nxt = loc->getNext(); if (nxt) { if (nxt->getData() == dataElement) { loc->setNext(nxt->getNext()); } } loc = loc->getNext(); } return (S*)this; } }; // end namespace Container }; // end namespace Utilities }; // end namespace Konclude #endif // KONCLUDE_UTILITIES_CONTAINER_CLINKER_CPP