Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Unified Diff: src/core/SkTDynamicHash.h

Issue 22571010: SkTDynamicHash: support remove() without needing Deleted(). (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/DynamicHashTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
};
« no previous file with comments | « no previous file | tests/DynamicHashTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698