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.

Partitioning.md

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

Partition of Subranges

Finding a Partition Index

Rotation

Reversing