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