diff --git a/Sources/BitCollections/BitArray/BitArray+Extras.swift b/Sources/BitCollections/BitArray/BitArray+Extras.swift index d0ecd69a..6bf4c0b1 100644 --- a/Sources/BitCollections/BitArray/BitArray+Extras.swift +++ b/Sources/BitCollections/BitArray/BitArray+Extras.swift @@ -28,3 +28,24 @@ extension BitArray { } } } + +extension BitArray { + public mutating func insert( + repeating value: Bool, + count: Int, + at index: Int + ) { + precondition(count >= 0, "Can't insert a negative number of values") + precondition(index >= 0 && index <= count, "Index out of bounds") + guard count > 0 else { return } + _extend(by: count, with: false) + _copy(from: index ..< self.count - count, to: index + count) + fill(in: index ..< index + count, with: value) + } + + public mutating func append(repeating value: Bool, count: Int) { + precondition(count >= 0, "Can't append a negative number of values") + guard count > 0 else { return } + _extend(by: count, with: value) + } +} diff --git a/Sources/BitCollections/BitArray/BitArray+Fill.swift b/Sources/BitCollections/BitArray/BitArray+Fill.swift index fd96dd56..6c8223f6 100644 --- a/Sources/BitCollections/BitArray/BitArray+Fill.swift +++ b/Sources/BitCollections/BitArray/BitArray+Fill.swift @@ -31,4 +31,11 @@ extension BitArray { } } } + + public mutating func fill( + in range: R, + with value: Bool = true + ) where R.Bound == Int { + fill(in: range.relative(to: self), with: value) + } }