| 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_CQTMANAGEDRESTRICTEDMODIFICATIONSHARINGHASH_CPP |
| 22 |
#define KONCLUDE_UTILITIES_CONTAINER_CQTMANAGEDRESTRICTEDMODIFICATIONSHARINGHASH_CPP |
| 23 |
|
| 24 |
|
| 25 |
#include "CQtManagedRestrictedModificationSharingHash.h" |
| 26 |
|
| 27 |
|
| 28 |
namespace Konclude { |
| 29 |
|
| 30 |
namespace Utilities { |
| 31 |
|
| 32 |
namespace Container { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
template <class Key, class T> |
| 38 |
CQtManagedRestrictedModificationSharingHash<Key, T>::CQtManagedRestrictedModificationSharingHash(CContext* context) : mContext(context) { |
| 39 |
mMemMan = CContext::getMemoryAllocationManager(mContext); |
| 40 |
mCurrentHash = CObjectParameterizingAllocator< CQtManagedRestrictedModificationHash<Key, T>, CContext* >::allocateAndConstructAndParameterize(mMemMan, mContext); |
| 41 |
mSharingHash = nullptr; |
| 42 |
mSharingDuplicatedCount = 0; |
| 43 |
} |
| 44 |
|
| 45 |
template <class Key, class T> |
| 46 |
CQtManagedRestrictedModificationSharingHash<Key, T>* CQtManagedRestrictedModificationSharingHash<Key, T>::init(CContext* context) { |
| 47 |
mContext = context; |
| 48 |
mMemMan = CContext::getMemoryAllocationManager(mContext); |
| 49 |
mCurrentHash->init(mContext); |
| 50 |
mSharingHash = nullptr; |
| 51 |
mSharingDuplicatedCount = 0; |
| 52 |
return this; |
| 53 |
} |
| 54 |
|
| 55 |
template <class Key, class T> |
| 56 |
CQtManagedRestrictedModificationSharingHash<Key, T>* CQtManagedRestrictedModificationSharingHash<Key, T>::init(const CQtManagedRestrictedModificationSharingHash<Key, T> &other, CContext* context) { |
| 57 |
mContext = context; |
| 58 |
mMemMan = CContext::getMemoryAllocationManager(mContext); |
| 59 |
initSharing(other); |
| 60 |
return this; |
| 61 |
} |
| 62 |
|
| 63 |
template <class Key, class T> |
| 64 |
CQtManagedRestrictedModificationSharingHash<Key, T>* CQtManagedRestrictedModificationSharingHash<Key, T>::init(CQtManagedRestrictedModificationSharingHash<Key, T> *other, CContext* context) { |
| 65 |
mContext = context; |
| 66 |
mMemMan = CContext::getMemoryAllocationManager(mContext); |
| 67 |
if (other) { |
| 68 |
initSharing(*other); |
| 69 |
} else { |
| 70 |
mCurrentHash->clear(); |
| 71 |
mSharingHash = nullptr; |
| 72 |
mSharingDuplicatedCount = 0; |
| 73 |
} |
| 74 |
return this; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
template <class Key, class T> |
| 80 |
CQtManagedRestrictedModificationSharingHash<Key, T>& CQtManagedRestrictedModificationSharingHash<Key, T>::operator=(const CQtManagedRestrictedModificationSharingHash<Key, T> &other) { |
| 81 |
initSharing(other); |
| 82 |
return *this; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
template <class Key, class T> |
| 87 |
CQtManagedRestrictedModificationSharingHash<Key, T>* CQtManagedRestrictedModificationSharingHash<Key, T>::initSharing(const CQtManagedRestrictedModificationSharingHash<Key, T> &other) { |
| 88 |
|
| 89 |
mCurrentHash->clear(); |
| 90 |
mSharingHash = nullptr; |
| 91 |
mSharingDuplicatedCount = 0; |
| 92 |
|
| 93 |
if (!other.mSharingHash && other.mCurrentHash->size() <= 100) { |
| 94 |
*mCurrentHash = *other.mCurrentHash; |
| 95 |
mSharingHash = nullptr; |
| 96 |
} else if (!other.mSharingHash && other.mCurrentHash->size() > 100) { |
| 97 |
mCurrentHash->clear(); |
| 98 |
mSharingHash = other.mCurrentHash; |
| 99 |
} else { |
| 100 |
|
| 101 |
if (other.mCurrentHash->size() * 3 > other.mSharingHash->size()) { |
| 102 |
*mCurrentHash = *other.mCurrentHash; |
| 103 |
cint64 remainShareCount = other.mSharingDuplicatedCount; |
| 104 |
for (typename CQtManagedRestrictedModificationHash<Key, T>::const_iterator it = other.mSharingHash->constBegin(), itEnd = other.mSharingHash->constEnd(); it != itEnd;) { |
| 105 |
if (remainShareCount <= 0 || !other.mCurrentHash->contains(it.key())) { |
| 106 |
typename CQtManagedRestrictedModificationHash<Key, T>::const_iterator itBase = it; |
| 107 |
while (it != itEnd && it.key() == itBase.key()) { |
| 108 |
mCurrentHash->insertMulti(it.key(), it.value()); |
| 109 |
++it; |
| 110 |
} |
| 111 |
} else { |
| 112 |
remainShareCount--; |
| 113 |
++it; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
mSharingHash = nullptr; |
| 118 |
} else { |
| 119 |
*mCurrentHash = *other.mCurrentHash; |
| 120 |
mSharingHash = other.mSharingHash; |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
return this; |
| 126 |
} |
| 127 |
|
| 128 |
template <class Key, class T> |
| 129 |
void CQtManagedRestrictedModificationSharingHash<Key, T>::unshareAll() { |
| 130 |
if (mSharingHash) { |
| 131 |
cint64 remainShareCount = mSharingDuplicatedCount; |
| 132 |
for (typename CQtManagedRestrictedModificationHash<Key, T>::const_iterator it = mSharingHash->constBegin(), itEnd = mSharingHash->constEnd(); it != itEnd;) { |
| 133 |
if (remainShareCount <= 0 || !mCurrentHash->contains(it.key())) { |
| 134 |
typename CQtManagedRestrictedModificationHash<Key, T>::const_iterator itBase = it; |
| 135 |
while (it != itEnd && it.key() == itBase.key()) { |
| 136 |
mCurrentHash->insertMulti(it.key(), it.value()); |
| 137 |
++it; |
| 138 |
} |
| 139 |
} else { |
| 140 |
remainShareCount--; |
| 141 |
++it; |
| 142 |
} |
| 143 |
} |
| 144 |
mSharingDuplicatedCount = 0; |
| 145 |
mSharingHash = nullptr; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
template <class Key, class T> |
| 150 |
int CQtManagedRestrictedModificationSharingHash<Key, T>::size() const { |
| 151 |
cint64 count = mCurrentHash->count(); |
| 152 |
if (mSharingHash) { |
| 153 |
count += mSharingHash->count(); |
| 154 |
count -= mSharingDuplicatedCount; |
| 155 |
} |
| 156 |
return count; |
| 157 |
} |
| 158 |
|
| 159 |
template <class Key, class T> |
| 160 |
bool CQtManagedRestrictedModificationSharingHash<Key, T>::isEmpty() const { |
| 161 |
return size() == 0; |
| 162 |
} |
| 163 |
|
| 164 |
template <class Key, class T> |
| 165 |
int CQtManagedRestrictedModificationSharingHash<Key, T>::count() const { |
| 166 |
return size(); |
| 167 |
} |
| 168 |
|
| 169 |
template <class Key, class T> |
| 170 |
void CQtManagedRestrictedModificationSharingHash<Key, T>::clear() { |
| 171 |
mCurrentHash->clear(); |
| 172 |
mSharingHash = nullptr; |
| 173 |
mSharingDuplicatedCount = 0; |
| 174 |
} |
| 175 |
|
| 176 |
template <class Key, class T> |
| 177 |
bool CQtManagedRestrictedModificationSharingHash<Key, T>::contains(const Key &key) const { |
| 178 |
if (mCurrentHash->contains(key)) { |
| 179 |
return true; |
| 180 |
} |
| 181 |
if (mSharingHash && mSharingHash->contains(key)) { |
| 182 |
return true; |
| 183 |
} |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
template <class Key, class T> |
| 188 |
T& CQtManagedRestrictedModificationSharingHash<Key, T>::operator[](const Key &key) { |
| 189 |
unshare(key); |
| 190 |
return (*mCurrentHash)[key]; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
template <class Key, class T> |
| 195 |
const T CQtManagedRestrictedModificationSharingHash<Key, T>::operator[](const Key &key) const { |
| 196 |
return value(key); |
| 197 |
} |
| 198 |
|
| 199 |
template <class Key, class T> |
| 200 |
const T CQtManagedRestrictedModificationSharingHash<Key, T>::value(const Key &key) const { |
| 201 |
if (mCurrentHash->contains(key)) { |
| 202 |
return mCurrentHash->value(key); |
| 203 |
} |
| 204 |
if (mSharingHash) { |
| 205 |
return mSharingHash->value(key); |
| 206 |
} |
| 207 |
return mCurrentHash->value(key); |
| 208 |
} |
| 209 |
|
| 210 |
template <class Key, class T> |
| 211 |
const T CQtManagedRestrictedModificationSharingHash<Key, T>::value(const Key &key, const T &defaultValue) const { |
| 212 |
if (mCurrentHash->contains(key)) { |
| 213 |
return mCurrentHash->value(key); |
| 214 |
} |
| 215 |
if (mSharingHash && mSharingHash->contains(key)) { |
| 216 |
return mSharingHash->value(key); |
| 217 |
} |
| 218 |
return defaultValue; |
| 219 |
} |
| 220 |
|
| 221 |
template <class Key, class T> |
| 222 |
CQtManagedRestrictedModificationSharingHash<Key, T>* CQtManagedRestrictedModificationSharingHash<Key, T>::insert(const Key &key, const T &value) { |
| 223 |
unshare(key); |
| 224 |
mCurrentHash->insert(key, value); |
| 225 |
return this; |
| 226 |
} |
| 227 |
|
| 228 |
template <class Key, class T> |
| 229 |
CQtManagedRestrictedModificationSharingHash<Key, T>* CQtManagedRestrictedModificationSharingHash<Key, T>::insertMulti(const Key &key, const T &value) { |
| 230 |
unshare(key); |
| 231 |
mCurrentHash->insertMulti(key, value); |
| 232 |
return this; |
| 233 |
} |
| 234 |
|
| 235 |
template <class Key, class T> |
| 236 |
void CQtManagedRestrictedModificationSharingHash<Key, T>::unshare(const Key & key) { |
| 237 |
if (mSharingHash && mSharingHash->contains(key) && !mCurrentHash->contains(key)) { |
| 238 |
for (typename CQtManagedRestrictedModificationHash<Key, T>::const_iterator it = mSharingHash->find(key), itEnd = mSharingHash->constEnd(); it != itEnd && it.key() == key; ++it) { |
| 239 |
mCurrentHash->insertMulti(key, it.value()); |
| 240 |
mSharingDuplicatedCount++; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
}; // end namespace Container |
| 250 |
|
| 251 |
}; // end namespace Utilities |
| 252 |
|
| 253 |
}; // end namespace Konclude |
| 254 |
|
| 255 |
#endif // KONCLUDE_UTILITIES_CONTAINER_CQTMANAGEDRESTRICTEDMODIFICATIONSHARINGHASH_CPP |
| 256 |
|