Chromium Code Reviews| Index: src/core/SkTDynamicHash.h |
| diff --git a/src/core/SkTDynamicHash.h b/src/core/SkTDynamicHash.h |
| index 21aca07b907174d3d7e3071e64b0b7857be60aa5..e7b57edfa62d7359414ecc50a0e01fc045c3c472 100644 |
| --- a/src/core/SkTDynamicHash.h |
| +++ b/src/core/SkTDynamicHash.h |
| @@ -34,10 +34,9 @@ public: |
| int index = this->firstIndex(key); |
| for (int round = 0; round < fCapacity; round++) { |
| T* candidate = fArray[index]; |
| - if (candidate == Empty()) { |
| + if (candidate == NULL) { |
|
reed1
2013/08/07 18:10:32
nit: NULL == candidate
|
| return NULL; |
| - } |
| - if (candidate != Deleted() && Equal(*candidate, key)) { |
| + } else if(Equal(*candidate, key)) { |
| return candidate; |
| } |
| index = this->nextIndex(index, round); |
| @@ -54,12 +53,11 @@ public: |
| int index = this->firstIndex(key); |
| for (int round = 0; round < fCapacity; round++) { |
| T* candidate = fArray[index]; |
| - if (candidate == Empty() || candidate == Deleted()) { |
| + if (candidate == NULL) { |
|
reed1
2013/08/07 18:10:32
see prev. nit
|
| fArray[index] = newEntry; |
| fCount++; |
| return; |
| - } |
| - if (Equal(*candidate, key)) { |
| + } else if (Equal(*candidate, key)) { |
| fArray[index] = newEntry; |
| return; |
| } |
| @@ -84,7 +82,7 @@ protected: |
| int index = this->firstIndex(key); |
| for (int round = 0; round < fCapacity; round++) { |
| const T* candidate = fArray[index]; |
| - if (candidate == Empty() || candidate == Deleted() || Equal(*candidate, key)) { |
| + if (candidate == NULL || Equal(*candidate, key)) { |
|
reed1
2013/08/07 18:10:32
again with the nits
|
| return round; |
| } |
| index = this->nextIndex(index, round); |
| @@ -94,13 +92,9 @@ protected: |
| } |
| private: |
| - // We have two special values to indicate an empty or deleted entry. |
| - static const T* Empty() { return reinterpret_cast<const T*>(0); } // i.e. NULL |
| - static const T* Deleted() { return reinterpret_cast<const T*>(1); } // Also an invalid pointer. |
| - |
| static T** AllocArray(int capacity) { |
| T** array = (T**)sk_malloc_throw(sizeof(T*) * capacity); |
| - sk_bzero(array, sizeof(T*) * capacity); // All cells == Empty(). |
| + sk_bzero(array, sizeof(T*) * capacity); // All cells == NULL. |
| return array; |
| } |
| @@ -108,11 +102,20 @@ private: |
| int index = this->firstIndex(key); |
| for (int round = 0; round < fCapacity; round++) { |
| const T* candidate = fArray[index]; |
| - if (candidate == Empty()) { |
| + if (candidate == NULL) { |
|
reed1
2013/08/07 18:10:32
not again?
|
| return; |
| - } |
| - if (candidate != Deleted() && Equal(*candidate, key)) { |
| - fArray[index] = const_cast<T*>(Deleted()); |
| + } else if (Equal(*candidate, key)) { |
| + // To delete an entry we find the last non-empty entry in this collision chain, move |
| + // it up here, then clear out that last entry. This way there's never an empty slot |
| + // in the middle of a collision chain. |
| + int last = index; |
| + while (fArray[nextIndex(last, round)] != NULL) { |
| + last = nextIndex(last, round); |
| + round++; |
| + } |
| + // Note how this works even if index == last. |
| + fArray[index] = fArray[last]; |
| + fArray[last] = NULL; |
| fCount--; |
| return; |
| } |
| @@ -136,7 +139,7 @@ private: |
| for (int i = 0; i < oldCapacity; i++) { |
| T* entry = oldArray[i]; |
| - if (entry != Empty() && entry != Deleted()) { |
| + if (entry != NULL) { |
| this->add(entry); |
| } |
| } |
| @@ -162,7 +165,7 @@ private: |
| return (index + round + 1) & this->hashMask(); |
| } |
| - int fCount; // Number of non-empty, non-deleted entries in fArray. |
| + int fCount; // Number of non-NULL entries in fArray. |
| int fCapacity; // Number of entries in fArray. Always a power of 2. |
| T** fArray; |
| }; |