diff --git a/Sources/BasicContainers/HashTable/_HTable+Insert.swift b/Sources/BasicContainers/HashTable/_HTable+Insert.swift index 57a12d6a..2829a2c1 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Insert.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Insert.swift @@ -18,7 +18,10 @@ extension _HTable { swapper: (Bucket) -> Void, ) -> Bucket { assert(isSmall) - // FIXME: Scramble order by swapping things in some way + if _count > 0 { + // Scramble order by swapping the new item in the middle of the table. + swapper(Bucket(offset: _count / 2)) + } let r = Bucket(offset: _count) _count &+= 1 _maxProbeLength = _count diff --git a/Tests/BasicContainersTests/RigidSetTests.swift b/Tests/BasicContainersTests/RigidSetTests.swift index becef804..c41a26ed 100644 --- a/Tests/BasicContainersTests/RigidSetTests.swift +++ b/Tests/BasicContainersTests/RigidSetTests.swift @@ -279,9 +279,8 @@ class RigidSetTests: CollectionTestCase { } } } - + func test_probeLengths() { - // FIXME: This isn't really testing anything; figure out how to handle this let c1 = 500 let scale = _HTable.minimumScale(forCapacity: c1) let c2 = _HTable.maximumCapacity(forScale: scale) @@ -289,9 +288,9 @@ class RigidSetTests: CollectionTestCase { for i in 0 ..< c2 { set.insert(i) } - set._dump(bitmap: true, chains: true) + expectConsistentSet(set) } - + #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW func test_borrowing_iterator() { withEvery("capacity", in: [0, 1, 2, 3, 4, 10, 100, 1000]) { capacity in @@ -300,7 +299,7 @@ class RigidSetTests: CollectionTestCase { var s = RigidSet>(capacity: capacity) withEvery("i", in: 0 ..< capacity) { i in s.insert(tracker.instance(for: i)) - + var expected = Set(0 ... i) var it = s.makeBorrowingIterator() while true { @@ -320,6 +319,22 @@ class RigidSetTests: CollectionTestCase { } #endif + func test_iteration_order_for_small_sets() { + let c = _HTable.maximumUnhashedCount + var set = RigidSet(capacity: c) + for i in 0 ..< c { + set.insert(i) + } + var items: [Int] = [] + var i = set.startIndex + while i != set.endIndex { + items.append(set[i]) + i = set.index(after: i) + } + // The order of items is deterministic, but it should appear somewhat random + expectEqualElements(items, [1, 3, 5, 6, 0, 4, 2]) + } + func test_iteration_indexAfter() { withEvery("capacity", in: [0, 1, 2, 10, 100, 200]) { capacity in withLifetimeTracking { tracker in