Exemplo n.º 1
0
        QByteArray decode(const QString& encoded, bool * ok)
        {
            QByteArray result;
            int size = encoded.size(), capacity = size;

            if(size % 8)
            {
                * ok = false;
                return result;
            }

            while(size > 0 && encoded[size -1] == pad())
            {
                size --;
            }

            switch(capacity - size)
            {
                case 0:
                    capacity = determineCapacity(size, 0, 0);
                    break;
                case 1:
                    capacity = determineCapacity(size, 7, 4);
                    break;
                case 3:
                    capacity = determineCapacity(size, 5, 3);
                    break;
                case 4:
                    capacity = determineCapacity(size, 4, 2);
                    break;
                case 6:
                    capacity = determineCapacity(size, 2, 1);
                    break;
                default:
                    * ok = false;
                    return result;
            }

            result.resize(capacity);

            char * output = result.data();
            const QChar * input = encoded.constData();

            for(int k = 0, i = 0; k < size; k += 8, i += 5)
            {
                if(!decode(input, k, size, i, capacity, output))
                {
                    * ok = false;
                    return QByteArray();
                }
            }

            * ok = true;
            return result;
        }
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;
}