1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "BasicHashtable"
#include <math.h>
#include <utils/Log.h>
#include <utils/BasicHashtable.h>
#include <utils/misc.h>
namespace android {
BasicHashtableImpl::BasicHashtableImpl(size_t entrySize, bool hasTrivialDestructor,
size_t minimumInitialCapacity, float loadFactor) :
mBucketSize(entrySize + sizeof(Bucket)), mHasTrivialDestructor(hasTrivialDestructor),
mLoadFactor(loadFactor), mSize(0),
mFilledBuckets(0), mBuckets(NULL) {
determineCapacity(minimumInitialCapacity, mLoadFactor, &mBucketCount, &mCapacity);
}
BasicHashtableImpl::BasicHashtableImpl(const BasicHashtableImpl& other) :
mBucketSize(other.mBucketSize), mHasTrivialDestructor(other.mHasTrivialDestructor),
mCapacity(other.mCapacity), mLoadFactor(other.mLoadFactor),
mSize(other.mSize), mFilledBuckets(other.mFilledBuckets),
mBucketCount(other.mBucketCount), mBuckets(other.mBuckets) {
if (mBuckets) {
SharedBuffer::bufferFromData(mBuckets)->acquire();
}
}
void BasicHashtableImpl::dispose() {
if (mBuckets) {
releaseBuckets(mBuckets, mBucketCount);
}
}
void BasicHashtableImpl::clone() {
if (mBuckets) {
void* newBuckets = allocateBuckets(mBucketCount);
copyBuckets(mBuckets, newBuckets, mBucketCount);
releaseBuckets(mBuckets, mBucketCount);
mBuckets = newBuckets;
}
}
void BasicHashtableImpl::setTo(const BasicHashtableImpl& other) {
if (mBuckets) {
releaseBuckets(mBuckets, mBucketCount);
}
mCapacity = other.mCapacity;
mLoadFactor = other.mLoadFactor;
mSize = other.mSize;
mFilledBuckets = other.mFilledBuckets;
mBucketCount = other.mBucketCount;
mBuckets = other.mBuckets;
if (mBuckets) {
SharedBuffer::bufferFromData(mBuckets)->acquire();
}
}
void BasicHashtableImpl::clear() {
if (mBuckets) {
if (mFilledBuckets) {
SharedBuffer* sb = SharedBuffer::bufferFromData(mBuckets);
if (sb->onlyOwner()) {
destroyBuckets(mBuckets, mBucketCount);
for (size_t i = 0; i < mSize; i++) {
Bucket& bucket = bucketAt(mBuckets, i);
bucket.cookie = 0;
}
} else {
releaseBuckets(mBuckets, mBucketCount);
mBuckets = NULL;
}
mFilledBuckets = 0;
}
mSize = 0;
}
}
ssize_t BasicHashtableImpl::next(ssize_t index) const {
if (mSize) {
while (size_t(++index) < mBucketCount) {
const Bucket& bucket = bucketAt(mBuckets, index);
if (bucket.cookie & Bucket::PRESENT) {
return index;
}
}
}
return -1;
}
ssize_t BasicHashtableImpl::find(ssize_t index, hash_t hash,
const void* __restrict__ key) const {
if (!mSize) {
return -1;
}
hash = trimHash(hash);
if (index < 0) {
index = chainStart(hash, mBucketCount);
const Bucket& bucket = bucketAt(mBuckets, size_t(index));
if (bucket.cookie & Bucket::PRESENT) {
if (compareBucketKey(bucket, key)) {
return index;
}
} else {
if (!(bucket.cookie & Bucket::COLLISION)) {
return -1;
}
}
}
size_t inc = chainIncrement(hash, mBucketCount);
for (;;) {
index = chainSeek(index, inc, mBucketCount);
const Bucket& bucket = bucketAt(mBuckets, size_t(index));
if (bucket.cookie & Bucket::PRESENT) {
if ((bucket.cookie & Bucket::HASH_MASK) == hash
&& compareBucketKey(bucket, key)) {
return index;
}
}
if (!(bucket.cookie & Bucket::COLLISION)) {
return -1;
}
}
}
size_t BasicHashtableImpl::add(hash_t hash, const void* entry) {
if (!mBuckets) {
mBuckets = allocateBuckets(mBucketCount);
} else {
edit();
}
hash = trimHash(hash);
for (;;) {
size_t index = chainStart(hash, mBucketCount);
Bucket* bucket = &bucketAt(mBuckets, size_t(index));
if (bucket->cookie & Bucket::PRESENT) {
size_t inc = chainIncrement(hash, mBucketCount);
do {
bucket->cookie |= Bucket::COLLISION;
index = chainSeek(index, inc, mBucketCount);
bucket = &bucketAt(mBuckets, size_t(index));
} while (bucket->cookie & Bucket::PRESENT);
}
uint32_t collision = bucket->cookie & Bucket::COLLISION;
if (!collision) {
if (mFilledBuckets >= mCapacity) {
rehash(mCapacity * 2, mLoadFactor);
continue;
}
mFilledBuckets += 1;
}
bucket->cookie = collision | Bucket::PRESENT | hash;
mSize += 1;
initializeBucketEntry(*bucket, entry);
return index;
}
}
void BasicHashtableImpl::removeAt(size_t index) {
edit();
Bucket& bucket = bucketAt(mBuckets, index);
bucket.cookie &= ~Bucket::PRESENT;
if (!(bucket.cookie & Bucket::COLLISION)) {
mFilledBuckets -= 1;
}
mSize -= 1;
if (!mHasTrivialDestructor) {
destroyBucketEntry(bucket);
}
}
void BasicHashtableImpl::rehash(size_t minimumCapacity, float loadFactor) {
if (minimumCapacity < mSize) {
minimumCapacity = mSize;
}
size_t newBucketCount, newCapacity;
determineCapacity(minimumCapacity, loadFactor, &newBucketCount, &newCapacity);
if (newBucketCount != mBucketCount || newCapacity != mCapacity) {
if (mBuckets) {
void* newBuckets;
if (mSize) {
newBuckets = allocateBuckets(newBucketCount);
for (size_t i = 0; i < mBucketCount; i++) {
const Bucket& fromBucket = bucketAt(mBuckets, i);
if (fromBucket.cookie & Bucket::PRESENT) {
hash_t hash = fromBucket.cookie & Bucket::HASH_MASK;
size_t index = chainStart(hash, newBucketCount);
Bucket* toBucket = &bucketAt(newBuckets, size_t(index));
if (toBucket->cookie & Bucket::PRESENT) {
size_t inc = chainIncrement(hash, newBucketCount);
do {
toBucket->cookie |= Bucket::COLLISION;
index = chainSeek(index, inc, newBucketCount);
toBucket = &bucketAt(newBuckets, size_t(index));
} while (toBucket->cookie & Bucket::PRESENT);
}
toBucket->cookie = Bucket::PRESENT | hash;
initializeBucketEntry(*toBucket, fromBucket.entry);
}
}
} else {
newBuckets = NULL;
}
releaseBuckets(mBuckets, mBucketCount);
mBuckets = newBuckets;
mFilledBuckets = mSize;
}
mBucketCount = newBucketCount;
mCapacity = newCapacity;
}
mLoadFactor = loadFactor;
}
void* BasicHashtableImpl::allocateBuckets(size_t count) const {
size_t bytes = count * mBucketSize;
SharedBuffer* sb = SharedBuffer::alloc(bytes);
LOG_ALWAYS_FATAL_IF(!sb, "Could not allocate %u bytes for hashtable with %u buckets.",
uint32_t(bytes), uint32_t(count));
void* buckets = sb->data();
for (size_t i = 0; i < count; i++) {
Bucket& bucket = bucketAt(buckets, i);
bucket.cookie = 0;
}
return buckets;
}
void BasicHashtableImpl::releaseBuckets(void* __restrict__ buckets, size_t count) const {
SharedBuffer* sb = SharedBuffer::bufferFromData(buckets);
if (sb->release(SharedBuffer::eKeepStorage) == 1) {
destroyBuckets(buckets, count);
SharedBuffer::dealloc(sb);
}
}
void BasicHashtableImpl::destroyBuckets(void* __restrict__ buckets, size_t count) const {
if (!mHasTrivialDestructor) {
for (size_t i = 0; i < count; i++) {
Bucket& bucket = bucketAt(buckets, i);
if (bucket.cookie & Bucket::PRESENT) {
destroyBucketEntry(bucket);
}
}
}
}
void BasicHashtableImpl::copyBuckets(const void* __restrict__ fromBuckets,
void* __restrict__ toBuckets, size_t count) const {
for (size_t i = 0; i < count; i++) {
const Bucket& fromBucket = bucketAt(fromBuckets, i);
Bucket& toBucket = bucketAt(toBuckets, i);
toBucket.cookie = fromBucket.cookie;
if (fromBucket.cookie & Bucket::PRESENT) {
initializeBucketEntry(toBucket, fromBucket.entry);
}
}
}
// Table of 31-bit primes where each prime is no less than twice as large
// as the previous one. Generated by "primes.py".
static size_t PRIMES[] = {
5,
11,
23,
47,
97,
197,
397,
797,
1597,
3203,
6421,
12853,
25717,
51437,
102877,
205759,
411527,
823117,
1646237,
3292489,
6584983,
13169977,
26339969,
52679969,
105359939,
210719881,
421439783,
842879579,
1685759167,
0,
};
void BasicHashtableImpl::determineCapacity(size_t minimumCapacity, float loadFactor,
size_t* __restrict__ outBucketCount, size_t* __restrict__ outCapacity) {
LOG_ALWAYS_FATAL_IF(loadFactor <= 0.0f || loadFactor > 1.0f,
"Invalid load factor %0.3f. Must be in the range (0, 1].", loadFactor);
size_t count = ceilf(minimumCapacity / loadFactor) + 1;
size_t i = 0;
while (count > PRIMES[i] && i < NELEM(PRIMES)) {
i++;
}
count = PRIMES[i];
LOG_ALWAYS_FATAL_IF(!count, "Could not determine required number of buckets for "
"hashtable with minimum capacity %u and load factor %0.3f.",
uint32_t(minimumCapacity), loadFactor);
*outBucketCount = count;
*outCapacity = ceilf((count - 1) * loadFactor);
}
}; // namespace android
|