Changeset 237522 in webkit
- Timestamp:
- Oct 28, 2018 11:18:57 AM (6 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r237486 r237522 1 2018-10-28 Geoffrey Garen <ggaren@apple.com> 2 3 HashMap should support selecting a random entry 4 https://bugs.webkit.org/show_bug.cgi?id=190814 5 6 Reviewed by Ryosuke Niwa. 7 8 * wtf/HashTable.h: 9 (WTF::HashTable::random): 10 (WTF::HashTable::random const): Merge the empty and deleted checks, and 11 use more idiomatic addressing. 12 1 13 2018-10-26 Commit Queue <commit-queue@webkit.org> 2 14 -
trunk/Source/WTF/wtf/HashTable.h
r237476 r237522 387 387 388 388 while (1) { 389 ValueType* entry = m_table + (weakRandomUint32() & m_tableSizeMask); 390 if (isEmptyBucket(*entry) || isDeletedBucket(*entry)) 391 continue; 392 return makeKnownGoodIterator(entry); 389 auto& bucket = m_table[weakRandomUint32() & m_tableSizeMask]; 390 if (!isEmptyOrDeletedBucket(bucket)) 391 return makeKnownGoodIterator(&bucket); 393 392 }; 394 393 } -
trunk/Tools/ChangeLog
r237490 r237522 1 2018-10-28 Geoffrey Garen <ggaren@apple.com> 2 3 HashMap should support selecting a random entry 4 https://bugs.webkit.org/show_bug.cgi?id=190814 5 6 Reviewed by Ryosuke Niwa. 7 8 * TestWebKitAPI/Tests/WTF/HashMap.cpp: Renamed IsRandom to 9 IsEvenlyDistributed to reflect the fact that we're only testing the 10 distribution. Added a test case that covers more table densities and 11 the remove() operation. 12 1 13 2018-10-27 Charlie Turner <cturner@igalia.com> 2 14 -
trunk/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp
r237476 r237522 1014 1014 } 1015 1015 1016 TEST(WTF_HashMap, Random_IsRandom) 1017 { 1018 HashMap<unsigned, unsigned> map; 1016 TEST(WTF_HashMap, Random_IsEvenlyDistributed) 1017 { 1018 HashMap<unsigned, unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> map; 1019 map.add(0, 0); 1019 1020 map.add(1, 1); 1020 map.add(2, 2); 1021 1021 1022 unsigned zeros = 0; 1022 1023 unsigned ones = 0; 1023 unsigned twos = 0;1024 1024 1025 1025 for (unsigned i = 0; i < 1000; ++i) { 1026 1026 auto it = map.random(); 1027 if (it->value == 1) 1027 if (!it->value) 1028 ++zeros; 1029 else { 1030 ASSERT_EQ(it->value, 1u); 1028 1031 ++ones; 1029 else {1030 ASSERT_EQ(it->value, 2u);1031 ++twos;1032 1032 } 1033 1033 } 1034 1034 1035 ASSERT_EQ(ones + twos, 1000u); 1035 ASSERT_EQ(zeros + ones, 1000u); 1036 ASSERT_LE(zeros, 600u); 1036 1037 ASSERT_LE(ones, 600u); 1037 ASSERT_LE(twos, 600u); 1038 } 1039 1040 TEST(WTF_HashMap, Random_IsEvenlyDistributedAfterRemove) 1041 { 1042 for (size_t tableSize = 2; tableSize <= 2 * 6; ++tableSize) { // Our hash tables shrink at a load factor of 1 / 6. 1043 HashMap<unsigned, unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> map; 1044 for (size_t i = 0; i < tableSize; ++i) 1045 map.add(i, i); 1046 for (size_t i = 2; i < tableSize; ++i) 1047 map.remove(i); 1048 1049 unsigned zeros = 0; 1050 unsigned ones = 0; 1051 1052 for (unsigned i = 0; i < 1000; ++i) { 1053 auto it = map.random(); 1054 if (!it->value) 1055 ++zeros; 1056 else { 1057 ASSERT_EQ(it->value, 1u); 1058 ++ones; 1059 } 1060 } 1061 1062 ASSERT_EQ(zeros + ones, 1000u); 1063 ASSERT_LE(zeros, 600u); 1064 ASSERT_LE(ones, 600u); 1065 } 1038 1066 } 1039 1067
Note: See TracChangeset
for help on using the changeset viewer.