Skip to content

Commit

Permalink
Merge pull request #195 from lorentey/fix-bitset-shrinking-issue
Browse files Browse the repository at this point in the history
[BitSet] Fix invariant violation in member subscript
  • Loading branch information
lorentey authored Oct 9, 2022
2 parents 92210c5 + 1b71d2c commit d05cba0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 3 additions & 1 deletion Sources/BitCollections/BitSet/BitSet+Extras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ extension BitSet {
} else if member > _capacity {
return
}
_update { handle in handle[member: member] = newValue }
_updateThenShrink { handle, shrink in
shrink = handle.update(member, to: newValue)
}
}
}

Expand Down
12 changes: 5 additions & 7 deletions Sources/BitCollections/BitSet/BitSet._UnsafeHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,11 @@ extension BitSet._UnsafeHandle {
return _mutableWords[index.word].remove(index.bit)
}

subscript(member member: UInt) -> Bool {
get { contains(member) }
set {
ensureMutable()
let (w, b) = _BitPosition(member).split
_mutableWords[w].update(b, to: newValue)
}
internal mutating func update(_ member: UInt, to newValue: Bool) -> Bool {
ensureMutable()
let (w, b) = _BitPosition(member).split
_mutableWords[w].update(b, to: newValue)
return w == _words.count &- 1
}
}

Expand Down

0 comments on commit d05cba0

Please sign in to comment.