Files
sakurac/core/containers/sort.sakura
T
2026-12-06 13:52:57 -06:00

200 lines
6.7 KiB
Plaintext

#! @file sort.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the sorts of slices and vectors: `sort`, by the order `<` gives, and
#! `sort_by`, by a function saying whether one element comes before another.
#! Both are stable, keeping elements neither comes before in the order they were
#! in, so what is sorted comes out the same however many times it is sorted.
module core;
#! Puts the elements in order, least first, by the order `<` gives.
#! @param this The slice to sort, whose elements are `ordered`.
#! @remarks The sort is stable: elements equal to each other keep the order they
#! were in. Runs of a few are sorted by insertion and then merged, which
#! takes memory for as many elements again while it sorts. Elements
#! are moved byte for byte, so nothing is closed or copied.
pub def slice<T>::sort(&mut this): void {
sort_elements::<T, natural_order<T>>(this._dat, this._len, &natural_order::<T> {});
}
#! Puts the elements in an order a function gives.
#! @param this The slice to sort.
#! @param before Whether one element comes before another, which holds of no
#! element and itself, as `<` does.
#! @remarks The sort is stable, as `sort` is: elements neither comes before keep
#! the order they were in.
pub def slice<T>::sort_by(&mut this, before: def(&T, &T): bool): void {
sort_elements::<T, given_order<T>>(this._dat, this._len, &given_order::<T> { before_of = before });
}
#! Puts the elements in order, least first, by the order `<` gives.
#! @param this The vector to sort, whose elements are `ordered`.
#! @remarks The sort is stable, as a slice's `sort` is.
pub def vector<T>::sort(&mut this): void {
var whole := this.slice;
whole.sort();
}
#! Puts the elements in an order a function gives.
#! @param this The vector to sort.
#! @param before Whether one element comes before another, which holds of no
#! element and itself, as `<` does.
#! @remarks The sort is stable, as a slice's `sort_by` is.
pub def vector<T>::sort_by(&mut this, before: def(&T, &T): bool): void {
var whole := this.slice;
whole.sort_by(before);
}
# What a sort orders elements by: whether one comes before another.
trait sort_order<T> {
def before(&this, first: &T, second: &T): bool;
}
# The order `<` gives.
struct natural_order<T> : sort_order<T> {}
def natural_order<T>::before(&this, first: &T, second: &T): bool {
if const !(T is ordered) {
compile_error("`sort` puts elements in the order `<` gives, so they are `ordered`. `sort_by` takes an order of its own.");
}
return first < second;
}
# The order a function gives.
struct given_order<T> : sort_order<T> {
var before_of: def(&T, &T): bool;
}
def given_order<T>::before(&this, first: &T, second: &T): bool {
return this.before_of(first, second);
}
# How many elements a run sorted by insertion holds, before runs are merged.
const sort_run: u64 = 16;
# Sorts elements in place, stably: runs of a few by insertion, then each pair of
# runs merged, back and forth between the elements and as many slots again.
def sort_elements<T, O>(data: *mut T, count: u64, order: &O): void where O: sort_order<T> {
return if count < 2;
var at: u64 = 0;
while at < count {
insert_sorted::<T, O>(data + at, count - at < sort_run ? count - at : sort_run, order);
at += sort_run;
}
return if count <= sort_run;
# The spare slots hold the elements a while, so they are cleared where the
# elements close, as `acquire` clears them.
val spare := acquire::<T>(nothing {}, count);
var from := data;
var to := spare;
var width := sort_run;
while width < count {
var low: u64 = 0;
while low < count {
val middle := low + width < count ? low + width : count;
val high := low + 2 * width < count ? low + 2 * width : count;
merge_runs::<T, O>(from, low, middle, high, to, order);
low = high;
}
val swap := from;
from = to;
to = swap;
width *= 2;
}
if from != data {
move_elements::<T>(data, from, count);
}
dispose::<T>(nothing {}, spare, count);
}
# Sorts a few elements in place by insertion, which keeps equal ones in order.
# Where each goes is found while it is still in its place, so the order is only
# asked about elements in the block: one held aside in a local would be put on
# the heap each time, where the order is a function's.
def insert_sorted<T, O>(data: *mut T, count: u64, order: &O): void where O: sort_order<T> {
if const T is closeable {
# What closes is held aside byte for byte, in a slot of its own, so nothing is
# closed or copied as it moves.
val held := acquire::<T>(nothing {}, 1);
for i in 1..count {
val j := insertion_place::<T, O>(data, i, order);
continue if j == i;
move_elements::<T>(held, data + i, 1);
var k := i;
while k > j {
move_elements::<T>(data + k, data + k - 1, 1);
k--;
}
move_elements::<T>(data + j, held, 1);
}
dispose::<T>(nothing {}, held, 1);
return;
}
for i in 1..count {
val j := insertion_place::<T, O>(data, i, order);
continue if j == i;
val held: T = data[i];
var k := i;
while k > j {
data[k] = data[k - 1];
k--;
}
data[j] = held;
}
}
# Where an element goes among those before it, which are in order: after the
# last it does not come before.
def insertion_place<T, O>(data: *mut T, at: u64, order: &O): u64 where O: sort_order<T> {
var j := at;
while j > 0 && order.before(&data[at], &data[j - 1]) {
j--;
}
return j;
}
# Merges two runs next to each other, each in order, into the same places of
# another block, taking from the first where neither comes before the other.
def merge_runs<T, O>(from: *mut T, low: u64, middle: u64, high: u64, to: *mut T, order: &O): void where O: sort_order<T> {
var left := low;
var right := middle;
var out := low;
while left < middle && right < high {
if order.before(&from[right], &from[left]) {
move_elements::<T>(to + out, from + right, 1);
right++;
} else {
move_elements::<T>(to + out, from + left, 1);
left++;
}
out++;
}
if left < middle {
move_elements::<T>(to + out, from + left, middle - left);
out += middle - left;
}
if right < high {
move_elements::<T>(to + out, from + right, high - right);
}
}
# Moves elements byte for byte, which neither closes what they land on nor copies
# what they hold, as a sort only puts them in other places.
def move_elements<T>(to: *mut T, from: *mut T, count: u64): void {
if const !(T is closeable) {
for i in 0..count {
to[i] = from[i];
}
return;
}
memcpy(to as *mut u8, from as *u8, bytes_of::<T>(count));
}