| 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_CDYNREFVECTOR_H |
| 22 |
#define KONCLUDE_UTILITIES_CDYNREFVECTOR_H |
| 23 |
|
| 24 |
// Libraries includes |
| 25 |
#include <QVector> |
| 26 |
#include <QPair> |
| 27 |
|
| 28 |
// Namespace includes |
| 29 |
#include "CDynRefBucket.hpp" |
| 30 |
#include "CAllocationObject.h" |
| 31 |
|
| 32 |
// Other includes |
| 33 |
|
| 34 |
|
| 35 |
// Logger includes |
| 36 |
#include "Logger/CLogger.h" |
| 37 |
|
| 38 |
using namespace Konclude::Logger; |
| 39 |
|
| 40 |
|
| 41 |
namespace Konclude { |
| 42 |
|
| 43 |
namespace Utilities { |
| 44 |
|
| 45 |
|
| 46 |
/*! |
| 47 |
* |
| 48 |
* \class CDynRefVector |
| 49 |
* \author Andreas Steigmiller |
| 50 |
* \version 0.1 |
| 51 |
* \brief TODO |
| 52 |
* |
| 53 |
*/ |
| 54 |
template<class T> |
| 55 |
class CDynRefVector : public CAllocationObject { |
| 56 |
// public methods |
| 57 |
public: |
| 58 |
//! Constructor |
| 59 |
CDynRefVector(); |
| 60 |
|
| 61 |
//! Destructor |
| 62 |
virtual ~CDynRefVector(); |
| 63 |
|
| 64 |
void reference(CDynRefVector<T> *vec); |
| 65 |
void clear(); |
| 66 |
|
| 67 |
bool isExtendingReference(); |
| 68 |
|
| 69 |
bool isLocal(qint64 index); |
| 70 |
bool isReference(qint64 index); |
| 71 |
|
| 72 |
|
| 73 |
T *getData(qint64 index); |
| 74 |
T *getLocalData(qint64 index); |
| 75 |
T *getReferenceData(qint64 index); |
| 76 |
|
| 77 |
void setLocalData(qint64 index, T *data); |
| 78 |
void setData(qint64 index, T *data); |
| 79 |
|
| 80 |
qint64 getItemCount(); |
| 81 |
|
| 82 |
|
| 83 |
// protected methods |
| 84 |
protected: |
| 85 |
|
| 86 |
// private methods |
| 87 |
private: |
| 88 |
|
| 89 |
// private variables |
| 90 |
private: |
| 91 |
QVector<CDynRefBucket<T> *> *refDataVec; |
| 92 |
CDynRefVector<T> *refVec; |
| 93 |
|
| 94 |
qint64 bucketSize; |
| 95 |
qint64 bucketCount; |
| 96 |
qint64 itemCount; |
| 97 |
|
| 98 |
qint64 extendSize; |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
template<class T> |
| 103 |
CDynRefVector<T>::CDynRefVector() { |
| 104 |
refVec = 0; |
| 105 |
refDataVec = 0; |
| 106 |
bucketSize = 32; |
| 107 |
bucketCount = 0; |
| 108 |
itemCount = 0; |
| 109 |
extendSize = 8; |
| 110 |
} |
| 111 |
|
| 112 |
template<class T> |
| 113 |
CDynRefVector<T>::~CDynRefVector() { |
| 114 |
clear(); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
template<class T> |
| 119 |
qint64 CDynRefVector<T>::getItemCount() { |
| 120 |
return itemCount; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
template<class T> |
| 125 |
void CDynRefVector<T>::setData(qint64 index, T *data) { |
| 126 |
// writing is always local |
| 127 |
setLocalData(index,data); |
| 128 |
} |
| 129 |
|
| 130 |
template<class T> |
| 131 |
void CDynRefVector<T>::setLocalData(qint64 index, T *data) { |
| 132 |
itemCount = qMax(index+1,itemCount); |
| 133 |
if (!refDataVec) { |
| 134 |
qint64 initialSize = bucketCount; |
| 135 |
if (initialSize < extendSize) { |
| 136 |
initialSize = extendSize; |
| 137 |
} |
| 138 |
refDataVec = new QVector<CDynRefBucket<T> *>(bucketCount); |
| 139 |
for (qint64 i = 0; i < bucketCount; ++i) { |
| 140 |
(*refDataVec)[i] = new CDynRefBucket<T>(bucketSize); |
| 141 |
if (refVec) { |
| 142 |
(*refDataVec)[i]->reference((*refVec->refDataVec)[i]); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
qint64 bucketIndex = index / bucketSize; |
| 148 |
if (bucketIndex >= bucketCount) { |
| 149 |
if (bucketIndex >= refDataVec->size()) { |
| 150 |
qint64 newVecSize = (bucketIndex / extendSize + 1) * extendSize; |
| 151 |
refDataVec->resize(newVecSize); |
| 152 |
} |
| 153 |
bucketCount = bucketIndex+1; |
| 154 |
} |
| 155 |
|
| 156 |
if (refDataVec) { |
| 157 |
if (!(*refDataVec)[bucketIndex]) { |
| 158 |
(*refDataVec)[bucketIndex] = new CDynRefBucket<T>; |
| 159 |
} |
| 160 |
|
| 161 |
qint64 bucketItemIndex = index % bucketSize; |
| 162 |
(*refDataVec)[bucketIndex]->setLocalData(bucketItemIndex,data); |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
template<class T> |
| 169 |
T *CDynRefVector<T>::getData(qint64 index) { |
| 170 |
|
| 171 |
T *val = 0; |
| 172 |
CDynRefBucket<T> *bucket = 0; |
| 173 |
|
| 174 |
qint64 bucketIndex = index / bucketSize; |
| 175 |
|
| 176 |
if (!refDataVec) { |
| 177 |
if (refVec) { |
| 178 |
if (bucketIndex < bucketCount) { |
| 179 |
bucket = (*refVec->refDataVec)[bucketIndex]; |
| 180 |
} |
| 181 |
} |
| 182 |
} else { |
| 183 |
if (bucketIndex < bucketCount) { |
| 184 |
bucket = (*refDataVec)[bucketIndex]; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if (bucket) { |
| 189 |
qint64 bucketItemIndex = index % bucketSize; |
| 190 |
val = bucket->getData(bucketItemIndex); |
| 191 |
} |
| 192 |
return val; |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
template<class T> |
| 198 |
T *CDynRefVector<T>::getLocalData(qint64 index){ |
| 199 |
T *val = 0; |
| 200 |
CDynRefBucket<T> *bucket = 0; |
| 201 |
|
| 202 |
qint64 bucketIndex = index / bucketSize; |
| 203 |
|
| 204 |
if (refDataVec) { |
| 205 |
if (bucketIndex < bucketCount) { |
| 206 |
bucket = (*refDataVec)[bucketIndex]; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (bucket) { |
| 211 |
qint64 bucketItemIndex = index % bucketSize; |
| 212 |
val = bucket->getLocalData(bucketItemIndex); |
| 213 |
} |
| 214 |
return val; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
template<class T> |
| 220 |
bool CDynRefVector<T>::isLocal(qint64 index) { |
| 221 |
CDynRefBucket<T> *bucket = 0; |
| 222 |
|
| 223 |
qint64 bucketIndex = index / bucketSize; |
| 224 |
|
| 225 |
if (refDataVec) { |
| 226 |
if (bucketIndex < bucketCount) { |
| 227 |
bucket = (*refDataVec)[bucketIndex]; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
if (bucket) { |
| 232 |
qint64 bucketItemIndex = index % bucketSize; |
| 233 |
return bucket->isLocal(bucketItemIndex); |
| 234 |
} |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
template<class T> |
| 241 |
bool CDynRefVector<T>::isReference(qint64 index) { |
| 242 |
CDynRefBucket<T> *bucket = 0; |
| 243 |
|
| 244 |
qint64 bucketIndex = index / bucketSize; |
| 245 |
bool referenced = false; |
| 246 |
|
| 247 |
if (!refDataVec) { |
| 248 |
if (refVec) { |
| 249 |
if (bucketIndex < bucketCount) { |
| 250 |
bucket = (*refVec->refDataVec)[bucketIndex]; |
| 251 |
referenced = true; |
| 252 |
} |
| 253 |
} |
| 254 |
} else { |
| 255 |
if (bucketIndex < bucketCount) { |
| 256 |
bucket = (*refDataVec)[bucketIndex]; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if (bucket) { |
| 261 |
qint64 bucketItemIndex = index % bucketSize; |
| 262 |
if (referenced) { |
| 263 |
return bucket->isLocal(bucketItemIndex); |
| 264 |
} |
| 265 |
return bucket->isReference(bucketItemIndex); |
| 266 |
} |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
template<class T> |
| 273 |
T *CDynRefVector<T>::getReferenceData(qint64 index){ |
| 274 |
T *val = 0; |
| 275 |
CDynRefBucket<T> *bucket = 0; |
| 276 |
|
| 277 |
qint64 bucketIndex = index / bucketSize; |
| 278 |
bool referenced = false; |
| 279 |
|
| 280 |
if (!refDataVec) { |
| 281 |
if (refVec) { |
| 282 |
if (bucketIndex < bucketCount) { |
| 283 |
bucket = (*refVec->refDataVec)[bucketIndex]; |
| 284 |
referenced = true; |
| 285 |
} |
| 286 |
} |
| 287 |
} else { |
| 288 |
if (bucketIndex < bucketCount) { |
| 289 |
bucket = (*refDataVec)[bucketIndex]; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if (bucket) { |
| 294 |
qint64 bucketItemIndex = index % bucketSize; |
| 295 |
if (referenced) { |
| 296 |
val = bucket->getData(bucketItemIndex); |
| 297 |
} else { |
| 298 |
val = bucket->getReferenceData(bucketItemIndex); |
| 299 |
} |
| 300 |
} |
| 301 |
return val; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
template<class T> |
| 307 |
void CDynRefVector<T>::reference(CDynRefVector<T> *vec) { |
| 308 |
clear(); |
| 309 |
|
| 310 |
|
| 311 |
CDynRefVector<T> *refVector = vec; |
| 312 |
// search the first vector which has in fact data |
| 313 |
while (refVector != 0 && refVector->refDataVec == 0) { |
| 314 |
refVector = refVector->refVec; |
| 315 |
} |
| 316 |
refVec = refVector; |
| 317 |
|
| 318 |
if (refVec) { |
| 319 |
itemCount = refVec->itemCount; |
| 320 |
bucketCount = refVec->bucketCount; |
| 321 |
bucketSize = refVec->bucketSize; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
template<class T> |
| 327 |
void CDynRefVector<T>::clear() { |
| 328 |
if (refDataVec) { |
| 329 |
for (qint64 i = 0; i < bucketCount; ++i) { |
| 330 |
if ((*refDataVec)[i] != 0) { |
| 331 |
delete (*refDataVec)[i]; |
| 332 |
} |
| 333 |
} |
| 334 |
delete refDataVec; |
| 335 |
refDataVec = 0; |
| 336 |
} |
| 337 |
bucketCount = 0; |
| 338 |
itemCount = 0; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
template<class T> |
| 343 |
bool CDynRefVector<T>::isExtendingReference() { |
| 344 |
if (refDataVec) { |
| 345 |
return true; |
| 346 |
} |
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
}; // end namespace Utilities |
| 354 |
|
| 355 |
}; // end namespace Konclude |
| 356 |
|
| 357 |
#endif // KONCLUDE_UTILITIES_CDYNREFVECTOR_H |