Skip to content

Instantly share code, notes, and snippets.

@BenjaminPoulain
Created July 20, 2012 22:49
Show Gist options
  • Save BenjaminPoulain/3153736 to your computer and use it in GitHub Desktop.
Save BenjaminPoulain/3153736 to your computer and use it in GitHub Desktop.
diff --git a/Source/WTF/wtf/HashTable.h b/Source/WTF/wtf/HashTable.h
index ccf62dc..3aad1e1 100644
--- a/Source/WTF/wtf/HashTable.h
+++ b/Source/WTF/wtf/HashTable.h
@@ -405,7 +405,7 @@ namespace WTF {
void removeAndInvalidate(ValueType*);
void remove(ValueType*);
- bool shouldExpand() const { return (m_keyCount + m_deletedCount) * m_maxLoad >= m_tableSize; }
+ bool shouldExpand() const { return (m_keyCount + m_deletedCount) * m_maxLoad > m_tableSize; }
bool mustRehashInPlace() const { return m_keyCount * m_minLoad < m_tableSize * 2; }
bool shouldShrink() const { return m_keyCount * m_minLoad < m_tableSize && m_tableSize > KeyTraits::minimumTableSize; }
void expand();
@@ -455,6 +455,30 @@ namespace WTF {
#endif
};
+ // The following computes the upper power of two capacity to hold the size parameter.
+ // This is done at compile time to initialize the HashTraits.
+ template<unsigned size> struct OneifyLowBits;
+ template<>
+ struct OneifyLowBits<0> {
+ static const unsigned value = 0;
+ };
+ template<unsigned size>
+ struct OneifyLowBits {
+ static const unsigned value = size | OneifyLowBits<(size >> 1)>::value;
+ };
+ template<unsigned number>
+ struct UpperPowerOfTwoBound {
+ static const unsigned value = (OneifyLowBits<number - 1>::value + 1) << 1;
+ };
+
+ template<unsigned size>
+ struct HashTableCapacityForSize {
+ static const unsigned value = UpperPowerOfTwoBound<size>::value;
+ COMPILE_ASSERT(size > 0, HashTableNonZeroMinimumCapacity);
+ COMPILE_ASSERT(!static_cast<int>(value >> 31), HashTableNoCapacityOverflow);
+ COMPILE_ASSERT(value >= (2 * size), HashTableCapacityHoldsContentSize);
+ };
+
template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits>
inline HashTable<Key, Value, Extractor, HashFunctions, Traits, KeyTraits>::HashTable()
: m_table(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment