func swapBucketValues()

in Sources/OrderedCollections/OrderedSet/OrderedSet+Partial MutableCollection.swift [171:190]


  func swapBucketValues(
    for left: Bucket, withCurrentValue leftValue: Int,
    and right: Bucket, withCurrentValue rightValue: Int
  ) {
    var it = bucketIterator(startingAt: left)
    it.advance(until: leftValue)
    assert(it.isOccupied)
    it.currentValue = rightValue

    it = bucketIterator(startingAt: right)
    it.advance(until: rightValue)
    assert(it.isOccupied)
    // Note: this second update may mistake the bucket for `right` with the
    // bucket for `left` whose value we just updated. The second update will
    // restore the original hash table contents in this case. This is okay!
    // When this happens, the lookup chains for both elements include each
    // other, so leaving the hash table unchanged still leaves us with a
    // working hash table.
    it.currentValue = leftValue
  }