#! @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::sort(&mut this): void { sort_elements::>(this._dat, this._len, &natural_order:: {}); } #! 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::sort_by(&mut this, before: def(&T, &T): bool): void { sort_elements::>(this._dat, this._len, &given_order:: { 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::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::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 { def before(&this, first: &T, second: &T): bool; } # The order `<` gives. struct natural_order : sort_order {} def natural_order::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 : sort_order { var before_of: def(&T, &T): bool; } def given_order::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(data: *mut T, count: u64, order: &O): void where O: sort_order { return if count < 2; var at: u64 = 0; while at < count { insert_sorted::(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::(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::(from, low, middle, high, to, order); low = high; } val swap := from; from = to; to = swap; width *= 2; } if from != data { move_elements::(data, from, count); } dispose::(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(data: *mut T, count: u64, order: &O): void where O: sort_order { 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::(nothing {}, 1); for i in 1..count { val j := insertion_place::(data, i, order); continue if j == i; move_elements::(held, data + i, 1); var k := i; while k > j { move_elements::(data + k, data + k - 1, 1); k--; } move_elements::(data + j, held, 1); } dispose::(nothing {}, held, 1); return; } for i in 1..count { val j := insertion_place::(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(data: *mut T, at: u64, order: &O): u64 where O: sort_order { 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(from: *mut T, low: u64, middle: u64, high: u64, to: *mut T, order: &O): void where O: sort_order { var left := low; var right := middle; var out := low; while left < middle && right < high { if order.before(&from[right], &from[left]) { move_elements::(to + out, from + right, 1); right++; } else { move_elements::(to + out, from + left, 1); left++; } out++; } if left < middle { move_elements::(to + out, from + left, middle - left); out += middle - left; } if right < high { move_elements::(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(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::(count)); }