Partitioning and Rotating
Partition a collection according to a unary predicate, rotate a collection around a particular index, or find the index where a collection is already partitioned.
Overview
A stable partition maintains the relative order of elements within both partitions.
// partition(by:) - unstable ordering
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let p1 = numbers.partition(by: { $0.isMultiple(of: 20) })
// p1 == 4
// numbers == [10, 70, 30, 50, 40, 60, 20, 80]
// ^ start of second partition
// stablePartition(by:) - maintains relative ordering
numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let p2 = numbers.stablePartition(by: { $0.isMultiple(of: 20) })
// p2 == 4
// numbers == [10, 30, 50, 70, 20, 40, 60, 80]
// ^ start of second partition
Use the rotate
method to shift the elements of a collection to start at a new position, moving the displaced elements to the end:
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let p = numbers.rotate(toStartAt: 2)
// numbers == [30, 40, 50, 60, 70, 80, 10, 20]
// p == 6 -- numbers[p] == 10
Stable Partition
func stablePartition(by: (Element) throws -> Bool
) rethrows -> Index Moves all elements satisfying the given predicate into a suffix of this collection, preserving the relative order of the elements in both partitions, and returns the start of the resulting suffix.
func stablePartition(subrange: Range<Index>, by: (Element) throws -> Bool
) rethrows -> Index Moves all elements satisfying the given predicate into a suffix of the given range, preserving the relative order of the elements in both partitions, and returns the start of the resulting suffix.
func partitioned(by: (Element) throws -> Bool
) rethrows -> (falseElements: [Element], trueElements: [Element]) Returns two arrays containing, in order, the elements of the sequence that do and don’t satisfy the given predicate.
func partitioned(by: (Element) throws -> Bool
) rethrows -> (falseElements: [Element], trueElements: [Element]) Returns two arrays containing, in order, the elements of the collection that do and don’t satisfy the given predicate.
Partition of Subranges
func partition(subrange: Range<Index>, by: (Element) throws -> Bool
) rethrows -> Index Moves all elements satisfying
isSuffixElement
into a suffix of the collection, returning the start position of the resulting suffix.func partition(subrange: Range<Index>, by: (Element) throws -> Bool
) rethrows -> Index Moves all elements satisfying
isSuffixElement
into a suffix of the collection, returning the start position of the resulting suffix.
Finding a Partition Index
func partitioningIndex(where: (Element) throws -> Bool
) rethrows -> Index Returns the start index of the partition of a collection that matches the given predicate.
Rotation
func rotate(toStartAt: Index
) -> Index Rotates the elements of this collection so that the element at the specified index becomes the start of the collection.
func rotate(toStartAt: Index
) -> Index Rotates the elements of this collection so that the element at the specified index becomes the start of the collection.
func rotate(subrange: Range<Index>, toStartAt: Index
) -> Index Rotates the elements within the given subrange so that the element at the specified index becomes the start of the subrange.
func rotate(subrange: Range<Index>, toStartAt: Index
) -> Index Rotates the elements within the given subrange so that the element at the specified index becomes the start of the subrange.
Reversing
func reverse(subrange: Range<Index>
) Reverses the elements within the given subrange.