/* * 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_CQTMANAGEDRESTRICTEDMODIFICATIONSHARINGHASH_CPP #define KONCLUDE_UTILITIES_CONTAINER_CQTMANAGEDRESTRICTEDMODIFICATIONSHARINGHASH_CPP #include "CQtManagedRestrictedModificationSharingHash.h" namespace Konclude { namespace Utilities { namespace Container { template CQtManagedRestrictedModificationSharingHash::CQtManagedRestrictedModificationSharingHash(CContext* context) : mContext(context) { mMemMan = CContext::getMemoryAllocationManager(mContext); mCurrentHash = CObjectParameterizingAllocator< CQtManagedRestrictedModificationHash, CContext* >::allocateAndConstructAndParameterize(mMemMan, mContext); mSharingHash = nullptr; mSharingDuplicatedCount = 0; } template CQtManagedRestrictedModificationSharingHash* CQtManagedRestrictedModificationSharingHash::init(CContext* context) { mContext = context; mMemMan = CContext::getMemoryAllocationManager(mContext); mCurrentHash->init(mContext); mSharingHash = nullptr; mSharingDuplicatedCount = 0; return this; } template CQtManagedRestrictedModificationSharingHash* CQtManagedRestrictedModificationSharingHash::init(const CQtManagedRestrictedModificationSharingHash &other, CContext* context) { mContext = context; mMemMan = CContext::getMemoryAllocationManager(mContext); initSharing(other); return this; } template CQtManagedRestrictedModificationSharingHash* CQtManagedRestrictedModificationSharingHash::init(CQtManagedRestrictedModificationSharingHash *other, CContext* context) { mContext = context; mMemMan = CContext::getMemoryAllocationManager(mContext); if (other) { initSharing(*other); } else { mCurrentHash->clear(); mSharingHash = nullptr; mSharingDuplicatedCount = 0; } return this; } template CQtManagedRestrictedModificationSharingHash& CQtManagedRestrictedModificationSharingHash::operator=(const CQtManagedRestrictedModificationSharingHash &other) { initSharing(other); return *this; } template CQtManagedRestrictedModificationSharingHash* CQtManagedRestrictedModificationSharingHash::initSharing(const CQtManagedRestrictedModificationSharingHash &other) { mCurrentHash->clear(); mSharingHash = nullptr; mSharingDuplicatedCount = 0; if (!other.mSharingHash && other.mCurrentHash->size() <= 100) { *mCurrentHash = *other.mCurrentHash; mSharingHash = nullptr; } else if (!other.mSharingHash && other.mCurrentHash->size() > 100) { mCurrentHash->clear(); mSharingHash = other.mCurrentHash; } else { if (other.mCurrentHash->size() * 3 > other.mSharingHash->size()) { *mCurrentHash = *other.mCurrentHash; cint64 remainShareCount = other.mSharingDuplicatedCount; for (typename CQtManagedRestrictedModificationHash::const_iterator it = other.mSharingHash->constBegin(), itEnd = other.mSharingHash->constEnd(); it != itEnd;) { if (remainShareCount <= 0 || !other.mCurrentHash->contains(it.key())) { typename CQtManagedRestrictedModificationHash::const_iterator itBase = it; while (it != itEnd && it.key() == itBase.key()) { mCurrentHash->insertMulti(it.key(), it.value()); ++it; } } else { remainShareCount--; ++it; } } mSharingHash = nullptr; } else { *mCurrentHash = *other.mCurrentHash; mSharingHash = other.mSharingHash; } } return this; } template void CQtManagedRestrictedModificationSharingHash::unshareAll() { if (mSharingHash) { cint64 remainShareCount = mSharingDuplicatedCount; for (typename CQtManagedRestrictedModificationHash::const_iterator it = mSharingHash->constBegin(), itEnd = mSharingHash->constEnd(); it != itEnd;) { if (remainShareCount <= 0 || !mCurrentHash->contains(it.key())) { typename CQtManagedRestrictedModificationHash::const_iterator itBase = it; while (it != itEnd && it.key() == itBase.key()) { mCurrentHash->insertMulti(it.key(), it.value()); ++it; } } else { remainShareCount--; ++it; } } mSharingDuplicatedCount = 0; mSharingHash = nullptr; } } template int CQtManagedRestrictedModificationSharingHash::size() const { cint64 count = mCurrentHash->count(); if (mSharingHash) { count += mSharingHash->count(); count -= mSharingDuplicatedCount; } return count; } template bool CQtManagedRestrictedModificationSharingHash::isEmpty() const { return size() == 0; } template int CQtManagedRestrictedModificationSharingHash::count() const { return size(); } template void CQtManagedRestrictedModificationSharingHash::clear() { mCurrentHash->clear(); mSharingHash = nullptr; mSharingDuplicatedCount = 0; } template bool CQtManagedRestrictedModificationSharingHash::contains(const Key &key) const { if (mCurrentHash->contains(key)) { return true; } if (mSharingHash && mSharingHash->contains(key)) { return true; } return false; } template T& CQtManagedRestrictedModificationSharingHash::operator[](const Key &key) { unshare(key); return (*mCurrentHash)[key]; } template const T CQtManagedRestrictedModificationSharingHash::operator[](const Key &key) const { return value(key); } template const T CQtManagedRestrictedModificationSharingHash::value(const Key &key) const { if (mCurrentHash->contains(key)) { return mCurrentHash->value(key); } if (mSharingHash) { return mSharingHash->value(key); } return mCurrentHash->value(key); } template const T CQtManagedRestrictedModificationSharingHash::value(const Key &key, const T &defaultValue) const { if (mCurrentHash->contains(key)) { return mCurrentHash->value(key); } if (mSharingHash && mSharingHash->contains(key)) { return mSharingHash->value(key); } return defaultValue; } template CQtManagedRestrictedModificationSharingHash* CQtManagedRestrictedModificationSharingHash::insert(const Key &key, const T &value) { unshare(key); mCurrentHash->insert(key, value); return this; } template CQtManagedRestrictedModificationSharingHash* CQtManagedRestrictedModificationSharingHash::insertMulti(const Key &key, const T &value) { unshare(key); mCurrentHash->insertMulti(key, value); return this; } template void CQtManagedRestrictedModificationSharingHash::unshare(const Key & key) { if (mSharingHash && mSharingHash->contains(key) && !mCurrentHash->contains(key)) { for (typename CQtManagedRestrictedModificationHash::const_iterator it = mSharingHash->find(key), itEnd = mSharingHash->constEnd(); it != itEnd && it.key() == key; ++it) { mCurrentHash->insertMulti(key, it.value()); mSharingDuplicatedCount++; } } } }; // end namespace Container }; // end namespace Utilities }; // end namespace Konclude #endif // KONCLUDE_UTILITIES_CONTAINER_CQTMANAGEDRESTRICTEDMODIFICATIONSHARINGHASH_CPP