| 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_CDYNAMICREFERENCEVECTOR_CPP |
| 22 |
#define KONCLUDE_UTILITIES_CONTAINER_CDYNAMICREFERENCEVECTOR_CPP |
| 23 |
|
| 24 |
|
| 25 |
#include "CDynamicReferenceVector.h" |
| 26 |
|
| 27 |
|
| 28 |
namespace Konclude { |
| 29 |
|
| 30 |
namespace Utilities { |
| 31 |
|
| 32 |
namespace Container { |
| 33 |
|
| 34 |
template<class T, class S, cint64 bucket2Size> |
| 35 |
CDynamicReferenceVectorBase<T,S,bucket2Size>::CDynamicReferenceVectorBase(CContext* context) : mContext(context) { |
| 36 |
clear(); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
template<class T, class S, cint64 bucket2Size> |
| 41 |
CDynamicReferenceVectorBase<T,S,bucket2Size>::~CDynamicReferenceVectorBase() { |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
template<class T, class S, cint64 bucket2Size> |
| 46 |
const T CDynamicReferenceVectorBase<T,S,bucket2Size>::getData(cint64 index, const T& defaultValue) { |
| 47 |
if (mRootBucket && index >= 0 && index < mMaxIndex) { |
| 48 |
return mRootBucket->getData(index,defaultValue); |
| 49 |
} |
| 50 |
return defaultValue; |
| 51 |
} |
| 52 |
|
| 53 |
template<class T, class S, cint64 bucket2Size> |
| 54 |
const T CDynamicReferenceVectorBase<T,S,bucket2Size>::getData(cint64 index, const T& defaultValue, bool* localFlag) { |
| 55 |
if (mRootBucket && index >= 0 && index < mMaxIndex) { |
| 56 |
return mRootBucket->getData(index,defaultValue,localFlag); |
| 57 |
} |
| 58 |
return defaultValue; |
| 59 |
} |
| 60 |
|
| 61 |
template<class T, class S, cint64 bucket2Size> |
| 62 |
const T CDynamicReferenceVectorBase<T,S,bucket2Size>::getLocalData(cint64 index, const T& defaultValue) { |
| 63 |
if (mRootBucket && index >= 0 && index < mMaxIndex) { |
| 64 |
return mRootBucket->getLocalData(index,defaultValue); |
| 65 |
} |
| 66 |
return defaultValue; |
| 67 |
} |
| 68 |
|
| 69 |
template<class T, class S, cint64 bucket2Size> |
| 70 |
const T CDynamicReferenceVectorBase<T,S,bucket2Size>::getReferenceData(cint64 index, const T& defaultValue) { |
| 71 |
if (mRootBucket && index >= 0 && index < mMaxIndex) { |
| 72 |
return mRootBucket->getReferenceData(index,defaultValue); |
| 73 |
} |
| 74 |
return defaultValue; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
template<class T, class S, cint64 bucket2Size> |
| 79 |
S* CDynamicReferenceVectorBase<T,S,bucket2Size>::setLocalData(cint64 index, const T& data) { |
| 80 |
mMaxItem = qMax(index,mMaxItem); |
| 81 |
if (!mRootBucket) { |
| 82 |
mRootBucket = CObjectParameterizingAllocator< CDynamicReferenceBucketBase<T,bucket2Size>,CContext* >::allocateAndConstructAndParameterize(CContext::getMemoryAllocationManager(mContext),mContext); |
| 83 |
mRootBucket->initBucket(0,nullptr); |
| 84 |
mRootBucketDepth = 0; |
| 85 |
mMaxIndex = 2<<bucket2Size; |
| 86 |
} |
| 87 |
while (index >= mMaxIndex) { |
| 88 |
CDynamicReferenceBucketBase<T,bucket2Size>* newRootBucket = CObjectParameterizingAllocator< CDynamicReferenceBucketBase<T,bucket2Size>,CContext* >::allocateAndConstructAndParameterize(CContext::getMemoryAllocationManager(mContext),mContext); |
| 89 |
mRootBucketDepth += bucket2Size+1; |
| 90 |
newRootBucket->initBucket(mRootBucketDepth,nullptr); |
| 91 |
newRootBucket->installBucket(0,mRootBucket); |
| 92 |
mRootBucket = newRootBucket; |
| 93 |
if (mMaxIndex == 0) { |
| 94 |
mMaxIndex = 2<<bucket2Size; |
| 95 |
} else { |
| 96 |
mMaxIndex *= 2<<bucket2Size; |
| 97 |
} |
| 98 |
} |
| 99 |
mRootBucket->setLocalData(index,data); |
| 100 |
return (S*)this; |
| 101 |
} |
| 102 |
|
| 103 |
template<class T, class S, cint64 bucket2Size> |
| 104 |
S* CDynamicReferenceVectorBase<T,S,bucket2Size>::setData(cint64 index, const T& data) { |
| 105 |
setLocalData(index,data); |
| 106 |
return (S*)this; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
template<class T, class S, cint64 bucket2Size> |
| 112 |
bool CDynamicReferenceVectorBase<T,S,bucket2Size>::hasLocalData(cint64 index) { |
| 113 |
if (mRootBucket && index >= 0 && index < mMaxIndex) { |
| 114 |
return mRootBucket->hasLocalData(index); |
| 115 |
} |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
template<class T, class S, cint64 bucket2Size> |
| 120 |
bool CDynamicReferenceVectorBase<T,S,bucket2Size>::hasData(cint64 index) { |
| 121 |
if (mRootBucket && index >= 0 && index < mMaxIndex) { |
| 122 |
return mRootBucket->hasData(index); |
| 123 |
} |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
template<class T, class S, cint64 bucket2Size> |
| 128 |
bool CDynamicReferenceVectorBase<T,S,bucket2Size>::hasReferencedData(cint64 index) { |
| 129 |
if (mRootBucket && index >= 0 && index < mMaxIndex) { |
| 130 |
return mRootBucket->hasReferencedData(index); |
| 131 |
} |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
template<class T, class S, cint64 bucket2Size> |
| 137 |
S* CDynamicReferenceVectorBase<T,S,bucket2Size>::clear() { |
| 138 |
mMaxIndex = 0; |
| 139 |
mRootBucketDepth = 0; |
| 140 |
mMaxItem = -1; |
| 141 |
mRootBucket = nullptr; |
| 142 |
mReferenceVector = nullptr; |
| 143 |
mReferencedMaxItem = -1; |
| 144 |
return (S*)this; |
| 145 |
} |
| 146 |
|
| 147 |
template<class T, class S, cint64 bucket2Size> |
| 148 |
S* CDynamicReferenceVectorBase<T,S,bucket2Size>::referenceVector(CDynamicReferenceVectorBase<T,S,bucket2Size>* refVector) { |
| 149 |
clear(); |
| 150 |
mReferenceVector = refVector; |
| 151 |
if (mReferenceVector && mReferenceVector->mMaxIndex > 0) { |
| 152 |
mRootBucket = CObjectParameterizingAllocator< CDynamicReferenceBucketBase<T,bucket2Size>,CContext* >::allocateAndConstructAndParameterize(CContext::getMemoryAllocationManager(mContext),mContext); |
| 153 |
mRootBucket->initBucket(refVector->mRootBucketDepth,mReferenceVector->mRootBucket); |
| 154 |
mRootBucketDepth = refVector->mRootBucketDepth; |
| 155 |
mMaxIndex = refVector->mMaxIndex; |
| 156 |
mMaxItem = refVector->mMaxItem; |
| 157 |
mReferencedMaxItem = refVector->mMaxItem; |
| 158 |
} |
| 159 |
return (S*)this; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
template<class T, class S, cint64 bucket2Size> |
| 164 |
cint64 CDynamicReferenceVectorBase<T,S,bucket2Size>::getItemCount() { |
| 165 |
return mMaxItem+1; |
| 166 |
} |
| 167 |
|
| 168 |
template<class T, class S, cint64 bucket2Size> |
| 169 |
cint64 CDynamicReferenceVectorBase<T,S,bucket2Size>::getItemMaxIndex() { |
| 170 |
return mMaxItem; |
| 171 |
} |
| 172 |
|
| 173 |
template<class T, class S, cint64 bucket2Size> |
| 174 |
cint64 CDynamicReferenceVectorBase<T,S,bucket2Size>::getReferencedItemCount() { |
| 175 |
return mReferencedMaxItem+1; |
| 176 |
} |
| 177 |
|
| 178 |
template<class T, class S, cint64 bucket2Size> |
| 179 |
cint64 CDynamicReferenceVectorBase<T,S,bucket2Size>::getReferencedItemMaxIndex() { |
| 180 |
return mReferencedMaxItem; |
| 181 |
} |
| 182 |
|
| 183 |
}; // end namespace Container |
| 184 |
|
| 185 |
}; // end namespace Utilities |
| 186 |
|
| 187 |
}; // end namespace Konclude |
| 188 |
|
| 189 |
#endif // KONCLUDE_UTILITIES_CONTAINER_CDYNAMICREFERENCEVECTOR_CPP |