From 17343b752a208b8545dbec998cd71a465a54bde4 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 31 Jan 2023 18:01:46 -0800 Subject: [PATCH] [BitArray] fill(in:with:), insert(repeating:count:at:), append(repeating:count:) --- .../BitArray/BitArray+Extras.swift | 21 +++++++++++++++++++ .../BitArray/BitArray+Fill.swift | 7 +++++++ 2 files changed, 28 insertions(+) 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) + } }