Orderly defines a sorted array type that implements optimized forms of insert(_:)
, index(of:)
, contains(_:)
, and more by taking advantage of binary search. This type is also useful for maintaining queues where constant-time min()
or max()
lookup is important.
var queue: SortedArray = [18, 1, 2, 20, 6].sorted()
queue.insert(3)
queue.insert(12)
print(queue) // -> [1, 2, 3, 6, 12, 18, 20]
In addition to SortedArray
, Orderly also provides a LazyMapSortedArray
type for sorting on arbitrary properties or transforms and a DescriptorSortedArray
type for sorting by an arbitrary predicate.
let x: LazyMapSortedArray = arr.sorted(on: { $0.foo })
let y: DescriptorSortedArray = arr.sortedy(by: { $0.foo < $1.foo || $0.bar < $0.bar })
This package is implemented using Generate Your Boilerplate to codegen the 3 array types. If you'd like to contribute to this package, please modify the gyb file in the Templates directory and then run the compile script from that directory.