Since the subscript is read-only, this could also be a function:
extension UnsafeMutableBufferPointer {
func pointer(offset: Int) -> UnsafeMutablePointer<Element> {
return baseAddress! + offset
}
}
extension Collection {
func stablyPartitioned(by belongsInFirstPartition: (Element) -> Bool) -> [Element] {
return Array<Element>(unsafeUninitializedCapacity: count) { buffer in
var low = 0
var high = buffer.count
for element in self {
if belongsInFirstPartition(element) {
buffer.pointer(offset: low).initialize(to: element)
low += 1
} else {
high -= 1
buffer.pointer(offset: high).initialize(to: element)
}
}
buffer[high...].reverse()
return buffer.count
}
}
}