Remove unneeded semicolons from the core library

This commit is contained in:
gingerBill
2021-08-31 22:21:13 +01:00
parent b176af2742
commit 251da264ed
187 changed files with 27227 additions and 27227 deletions
+37 -37
View File
@@ -4,27 +4,27 @@ import "core:intrinsics"
import "core:runtime"
import "core:mem"
_ :: intrinsics;
_ :: runtime;
_ :: mem;
_ :: intrinsics
_ :: runtime
_ :: mem
map_keys :: proc(m: $M/map[$K]$V, allocator := context.allocator) -> (keys: []K) {
keys = make(type_of(keys), len(m), allocator);
i := 0;
keys = make(type_of(keys), len(m), allocator)
i := 0
for key in m {
keys[i] = key;
i += 1;
keys[i] = key
i += 1
}
return;
return
}
map_values :: proc(m: $M/map[$K]$V, allocator := context.allocator) -> (values: []V) {
values = make(type_of(values), len(m), allocator);
i := 0;
values = make(type_of(values), len(m), allocator)
i := 0
for _, value in m {
values[i] = value;
i += 1;
values[i] = value
i += 1
}
return;
return
}
Map_Entry :: struct($Key, $Value: typeid) {
@@ -40,42 +40,42 @@ Map_Entry_Info :: struct($Key, $Value: typeid) {
map_entries :: proc(m: $M/map[$K]$V, allocator := context.allocator) -> (entries: []Map_Entry(K, V)) {
entries = make(type_of(entries), len(m), allocator);
i := 0;
entries = make(type_of(entries), len(m), allocator)
i := 0
for key, value in m {
entries[i].key = key;
entries[i].value = value;
i += 1;
entries[i].key = key
entries[i].value = value
i += 1
}
return;
return
}
map_entry_infos :: proc(m: $M/map[$K]$V, allocator := context.allocator) -> (entries: []Map_Entry_Info(K, V)) #no_bounds_check {
m := m;
rm := (^mem.Raw_Map)(&m);
m := m
rm := (^mem.Raw_Map)(&m)
info := runtime.type_info_base(type_info_of(M)).variant.(runtime.Type_Info_Map);
gs := runtime.type_info_base(info.generated_struct).variant.(runtime.Type_Info_Struct);
ed := runtime.type_info_base(gs.types[1]).variant.(runtime.Type_Info_Dynamic_Array);
entry_type := ed.elem.variant.(runtime.Type_Info_Struct);
key_offset := entry_type.offsets[2];
value_offset := entry_type.offsets[3];
entry_size := uintptr(ed.elem_size);
info := runtime.type_info_base(type_info_of(M)).variant.(runtime.Type_Info_Map)
gs := runtime.type_info_base(info.generated_struct).variant.(runtime.Type_Info_Struct)
ed := runtime.type_info_base(gs.types[1]).variant.(runtime.Type_Info_Dynamic_Array)
entry_type := ed.elem.variant.(runtime.Type_Info_Struct)
key_offset := entry_type.offsets[2]
value_offset := entry_type.offsets[3]
entry_size := uintptr(ed.elem_size)
entries = make(type_of(entries), rm.entries.len);
entries = make(type_of(entries), rm.entries.len)
data := uintptr(rm.entries.data);
data := uintptr(rm.entries.data)
for i in 0..<rm.entries.len {
header := (^runtime.Map_Entry_Header)(data);
header := (^runtime.Map_Entry_Header)(data)
hash := header.hash;
key := (^K)(data + key_offset)^;
value := (^V)(data + value_offset)^;
hash := header.hash
key := (^K)(data + key_offset)^
value := (^V)(data + value_offset)^
entries[i] = {hash, key, value};
entries[i] = {hash, key, value}
data += entry_size;
data += entry_size
}
return;
return
}
+28 -28
View File
@@ -3,69 +3,69 @@ package slice
import "core:mem"
ptr_add :: proc(p: $P/^$T, x: int) -> ^T {
return (^T)(uintptr(p) + size_of(T)*x);
return (^T)(uintptr(p) + size_of(T)*x)
}
ptr_sub :: proc(p: $P/^$T, x: int) -> ^T {
return #force_inline ptr_add(p, -x);
return #force_inline ptr_add(p, -x)
}
ptr_swap_non_overlapping :: proc(x, y: rawptr, len: int) {
if len <= 0 {
return;
return
}
if x == y { // Ignore pointers that are the same
return;
return
}
Block :: distinct [4]u64;
BLOCK_SIZE :: size_of(Block);
Block :: distinct [4]u64
BLOCK_SIZE :: size_of(Block)
i := 0;
t := &Block{};
i := 0
t := &Block{}
for ; i + BLOCK_SIZE <= len; i += BLOCK_SIZE {
a := rawptr(uintptr(x) + uintptr(i));
b := rawptr(uintptr(y) + uintptr(i));
a := rawptr(uintptr(x) + uintptr(i))
b := rawptr(uintptr(y) + uintptr(i))
mem.copy(t, a, BLOCK_SIZE);
mem.copy(a, b, BLOCK_SIZE);
mem.copy(b, t, BLOCK_SIZE);
mem.copy(t, a, BLOCK_SIZE)
mem.copy(a, b, BLOCK_SIZE)
mem.copy(b, t, BLOCK_SIZE)
}
if i < len {
rem := len - i;
rem := len - i
a := rawptr(uintptr(x) + uintptr(i));
b := rawptr(uintptr(y) + uintptr(i));
a := rawptr(uintptr(x) + uintptr(i))
b := rawptr(uintptr(y) + uintptr(i))
mem.copy(t, a, rem);
mem.copy(a, b, rem);
mem.copy(b, t, rem);
mem.copy(t, a, rem)
mem.copy(a, b, rem)
mem.copy(b, t, rem)
}
}
ptr_rotate :: proc(left: int, mid: ^$T, right: int) {
when size_of(T) != 0 {
left, mid, right := left, mid, right;
left, mid, right := left, mid, right
// TODO(bill): Optimization with a buffer for smaller ranges
if left >= right {
for {
ptr_swap_non_overlapping(ptr_sub(mid, right), mid, right);
mid = ptr_sub(mid, right);
ptr_swap_non_overlapping(ptr_sub(mid, right), mid, right)
mid = ptr_sub(mid, right)
left -= right;
left -= right
if left < right {
break;
break
}
}
} else {
ptr_swap_non_overlapping(ptr_sub(mid, left), mid, left);
mid = ptr_add(mid, left);
ptr_swap_non_overlapping(ptr_sub(mid, left), mid, left)
mid = ptr_add(mid, left)
right -= left;
right -= left
if right < left {
break;
break
}
}
}
+105 -105
View File
@@ -5,325 +5,325 @@ import "core:builtin"
import "core:math/bits"
import "core:mem"
_ :: intrinsics;
_ :: builtin;
_ :: bits;
_ :: mem;
_ :: intrinsics
_ :: builtin
_ :: bits
_ :: mem
swap :: proc(array: $T/[]$E, a, b: int, loc := #caller_location) {
when size_of(E) > 8 {
ptr_swap_non_overlapping(&array[a], &array[b], size_of(E));
ptr_swap_non_overlapping(&array[a], &array[b], size_of(E))
} else {
array[a], array[b] = array[b], array[a];
array[a], array[b] = array[b], array[a]
}
}
reverse :: proc(array: $T/[]$E) {
n := len(array)/2;
n := len(array)/2
for i in 0..<n {
a, b := i, len(array)-i-1;
array[a], array[b] = array[b], array[a];
a, b := i, len(array)-i-1
array[a], array[b] = array[b], array[a]
}
}
contains :: proc(array: $T/[]$E, value: E) -> bool where intrinsics.type_is_comparable(E) {
_, found := linear_search(array, value);
return found;
_, found := linear_search(array, value)
return found
}
linear_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
where intrinsics.type_is_comparable(T) #no_bounds_check {
for x, i in array {
if x == key {
return i, true;
return i, true
}
}
return -1, false;
return -1, false
}
linear_search_proc :: proc(array: $A/[]$T, f: proc(T) -> bool) -> (index: int, found: bool) #no_bounds_check {
for x, i in array {
if f(x) {
return i, true;
return i, true
}
}
return -1, false;
return -1, false
}
binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
where intrinsics.type_is_ordered(T) #no_bounds_check {
n := len(array);
n := len(array)
switch n {
case 0:
return -1, false;
return -1, false
case 1:
if array[0] == key {
return 0, true;
return 0, true
}
return -1, false;
return -1, false
}
lo, hi := 0, n-1;
lo, hi := 0, n-1
for array[hi] != array[lo] && key >= array[lo] && key <= array[hi] {
when intrinsics.type_is_ordered_numeric(T) {
// NOTE(bill): This is technically interpolation search
m := lo + int((key - array[lo]) * T(hi - lo) / (array[hi] - array[lo]));
m := lo + int((key - array[lo]) * T(hi - lo) / (array[hi] - array[lo]))
} else {
m := lo + (hi - lo)/2;
m := lo + (hi - lo)/2
}
switch {
case array[m] < key:
lo = m + 1;
lo = m + 1
case key < array[m]:
hi = m - 1;
hi = m - 1
case:
return m, true;
return m, true
}
}
if key == array[lo] {
return lo, true;
return lo, true
}
return -1, false;
return -1, false
}
equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_comparable(E) {
if len(a) != len(b) {
return false;
return false
}
when intrinsics.type_is_simple_compare(E) {
return mem.compare_ptrs(raw_data(a), raw_data(b), len(a)*size_of(E)) == 0;
return mem.compare_ptrs(raw_data(a), raw_data(b), len(a)*size_of(E)) == 0
} else {
for i in 0..<len(a) {
if a[i] != b[i] {
return false;
return false
}
}
return true;
return true
}
}
simple_equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_simple_compare(E) {
if len(a) != len(b) {
return false;
return false
}
return mem.compare_ptrs(raw_data(a), raw_data(b), len(a)*size_of(E)) == 0;
return mem.compare_ptrs(raw_data(a), raw_data(b), len(a)*size_of(E)) == 0
}
has_prefix :: proc(array: $T/[]$E, needle: E) -> bool where intrinsics.type_is_comparable(E) {
n := len(needle);
n := len(needle)
if len(array) >= n {
return equal(array[:n], needle);
return equal(array[:n], needle)
}
return false;
return false
}
has_suffix :: proc(array: $T/[]$E, needle: E) -> bool where intrinsics.type_is_comparable(E) {
array := array;
m, n := len(array), len(needle);
array := array
m, n := len(array), len(needle)
if m >= n {
return equal(array[m-n:], needle);
return equal(array[m-n:], needle)
}
return false;
return false
}
fill :: proc(array: $T/[]$E, value: E) {
for _, i in array {
array[i] = value;
array[i] = value
}
}
rotate_left :: proc(array: $T/[]$E, mid: int) {
n := len(array);
m := mid %% n;
k := n - m;
p := raw_data(array);
ptr_rotate(mid, ptr_add(p, mid), k);
n := len(array)
m := mid %% n
k := n - m
p := raw_data(array)
ptr_rotate(mid, ptr_add(p, mid), k)
}
rotate_right :: proc(array: $T/[]$E, k: int) {
rotate_left(array, -k);
rotate_left(array, -k)
}
swap_with_slice :: proc(a, b: $T/[]$E, loc := #caller_location) {
assert(len(a) == len(b), "miss matching slice lengths", loc);
assert(len(a) == len(b), "miss matching slice lengths", loc)
ptr_swap_non_overlapping(raw_data(a), raw_data(b), len(a)*size_of(E));
ptr_swap_non_overlapping(raw_data(a), raw_data(b), len(a)*size_of(E))
}
concatenate :: proc(a: []$T/[]$E, allocator := context.allocator) -> (res: T) {
if len(a) == 0 {
return;
return
}
n := 0;
n := 0
for s in a {
n += len(s);
n += len(s)
}
res = make(T, n, allocator);
i := 0;
res = make(T, n, allocator)
i := 0
for s in a {
i += copy(res[i:], s);
i += copy(res[i:], s)
}
return;
return
}
// copies slice into a new dynamic array
clone :: proc(a: $T/[]$E, allocator := context.allocator) -> []E {
d := make([]E, len(a), allocator);
copy(d[:], a);
return d;
d := make([]E, len(a), allocator)
copy(d[:], a)
return d
}
// copies slice into a new dynamic array
to_dynamic :: proc(a: $T/[]$E, allocator := context.allocator) -> [dynamic]E {
d := make([dynamic]E, len(a), allocator);
copy(d[:], a);
return d;
d := make([dynamic]E, len(a), allocator)
copy(d[:], a)
return d
}
// Converts slice into a dynamic array without cloning or allocating memory
into_dynamic :: proc(a: $T/[]$E) -> [dynamic]E {
s := transmute(mem.Raw_Slice)a;
s := transmute(mem.Raw_Slice)a
d := mem.Raw_Dynamic_Array{
data = s.data,
len = 0,
cap = s.len,
allocator = mem.nil_allocator(),
};
return transmute([dynamic]E)d;
}
return transmute([dynamic]E)d
}
length :: proc(a: $T/[]$E) -> int {
return len(a);
return len(a)
}
is_empty :: proc(a: $T/[]$E) -> bool {
return len(a) == 0;
return len(a) == 0
}
split_at :: proc(array: $T/[]$E, index: int) -> (a, b: T) {
return array[:index], array[index:];
return array[:index], array[index:]
}
split_first :: proc(array: $T/[]$E) -> (first: E, rest: T) {
return array[0], array[1:];
return array[0], array[1:]
}
split_last :: proc(array: $T/[]$E) -> (rest: T, last: E) {
n := len(array)-1;
return array[:n], array[n];
n := len(array)-1
return array[:n], array[n]
}
first :: proc(array: $T/[]$E) -> E {
return array[0];
return array[0]
}
last :: proc(array: $T/[]$E) -> E {
return array[len(array)-1];
return array[len(array)-1]
}
first_ptr :: proc(array: $T/[]$E) -> ^E {
if len(array) != 0 {
return &array[0];
return &array[0]
}
return nil;
return nil
}
last_ptr :: proc(array: $T/[]$E) -> ^E {
if len(array) != 0 {
return &array[len(array)-1];
return &array[len(array)-1]
}
return nil;
return nil
}
get :: proc(array: $T/[]$E, index: int) -> (value: E, ok: bool) {
if 0 <= index && index < len(array) {
value = array[index];
ok = true;
value = array[index]
ok = true
}
return;
return
}
get_ptr :: proc(array: $T/[]$E, index: int) -> (value: ^E, ok: bool) {
if 0 <= index && index < len(array) {
value = &array[index];
ok = true;
value = &array[index]
ok = true
}
return;
return
}
as_ptr :: proc(array: $T/[]$E) -> ^E {
return raw_data(array);
return raw_data(array)
}
mapper :: proc(s: $S/[]$U, f: proc(U) -> $V, allocator := context.allocator) -> []V {
r := make([]V, len(s), allocator);
r := make([]V, len(s), allocator)
for v, i in s {
r[i] = f(v);
r[i] = f(v)
}
return r;
return r
}
reduce :: proc(s: $S/[]$U, initializer: $V, f: proc(V, U) -> V) -> V {
r := initializer;
r := initializer
for v in s {
r = f(r, v);
r = f(r, v)
}
return r;
return r
}
filter :: proc(s: $S/[]$U, f: proc(U) -> bool, allocator := context.allocator) -> S {
r := make([dynamic]U, 0, 0, allocator);
r := make([dynamic]U, 0, 0, allocator)
for v in s {
if f(v) {
append(&r, v);
append(&r, v)
}
}
return r[:];
return r[:]
}
min :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
if len(s) != 0 {
res = s[0];
ok = true;
res = s[0]
ok = true
for v in s[1:] {
res = builtin.min(res, v);
res = builtin.min(res, v)
}
}
return;
return
}
max :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
if len(s) != 0 {
res = s[0];
ok = true;
res = s[0]
ok = true
for v in s[1:] {
res = builtin.max(res, v);
res = builtin.max(res, v)
}
}
return;
return
}
dot_product :: proc(a, b: $S/[]$T) -> T
where intrinsics.type_is_numeric(T) {
if len(a) != len(b) {
panic("slice.dot_product: slices of unequal length");
panic("slice.dot_product: slices of unequal length")
}
r: T;
r: T
#no_bounds_check for _, i in a {
r += a[i] * b[i];
r += a[i] * b[i]
}
return r;
return r
}
+209 -209
View File
@@ -1,9 +1,9 @@
package slice
import "core:intrinsics"
_ :: intrinsics;
_ :: intrinsics
ORD :: intrinsics.type_is_ordered;
ORD :: intrinsics.type_is_ordered
Ordering :: enum {
Less = -1,
@@ -14,23 +14,23 @@ Ordering :: enum {
cmp :: proc(a, b: $E) -> Ordering where ORD(E) {
switch {
case a < b:
return .Less;
return .Less
case a > b:
return .Greater;
return .Greater
}
return .Equal;
return .Equal
}
cmp_proc :: proc($E: typeid) -> (proc(E, E) -> Ordering) where ORD(E) {
return proc(a, b: E) -> Ordering {
switch {
case a < b:
return .Less;
return .Less
case a > b:
return .Greater;
return .Greater
}
return .Equal;
};
return .Equal
}
}
// sort sorts a slice
@@ -38,7 +38,7 @@ cmp_proc :: proc($E: typeid) -> (proc(E, E) -> Ordering) where ORD(E) {
sort :: proc(data: $T/[]$E) where ORD(E) {
when size_of(E) != 0 {
if n := len(data); n > 1 {
_quick_sort(data, 0, n, _max_depth(n));
_quick_sort(data, 0, n, _max_depth(n))
}
}
}
@@ -48,7 +48,7 @@ sort :: proc(data: $T/[]$E) where ORD(E) {
sort_by :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) {
when size_of(E) != 0 {
if n := len(data); n > 1 {
_quick_sort_less(data, 0, n, _max_depth(n), less);
_quick_sort_less(data, 0, n, _max_depth(n), less)
}
}
}
@@ -56,7 +56,7 @@ sort_by :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) {
sort_by_cmp :: proc(data: $T/[]$E, cmp: proc(i, j: E) -> Ordering) {
when size_of(E) != 0 {
if n := len(data); n > 1 {
_quick_sort_cmp(data, 0, n, _max_depth(n), cmp);
_quick_sort_cmp(data, 0, n, _max_depth(n), cmp)
}
}
}
@@ -64,124 +64,124 @@ sort_by_cmp :: proc(data: $T/[]$E, cmp: proc(i, j: E) -> Ordering) {
is_sorted :: proc(array: $T/[]$E) -> bool where ORD(E) {
for i := len(array)-1; i > 0; i -= 1 {
if array[i] < array[i-1] {
return false;
return false
}
}
return true;
return true
}
is_sorted_by :: proc(array: $T/[]$E, less: proc(i, j: E) -> bool) -> bool {
for i := len(array)-1; i > 0; i -= 1 {
if less(array[i], array[i-1]) {
return false;
return false
}
}
return true;
return true
}
is_sorted_cmp :: proc(array: $T/[]$E, cmp: proc(i, j: E) -> Ordering) -> bool {
for i := len(array)-1; i > 0; i -= 1 {
if cmp(array[i], array[i-1]) == .Equal {
return false;
return false
}
}
return true;
return true
}
reverse_sort :: proc(data: $T/[]$E) where ORD(E) {
sort_by(data, proc(i, j: E) -> bool {
return j < i;
});
return j < i
})
}
reverse_sort_by :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) where ORD(E) {
context._internal = rawptr(less);
context._internal = rawptr(less)
sort_by(data, proc(i, j: E) -> bool {
k := (proc(i, j: E) -> bool)(context._internal);
return k(j, i);
});
k := (proc(i, j: E) -> bool)(context._internal)
return k(j, i)
})
}
reverse_sort_by_cmp :: proc(data: $T/[]$E, cmp: proc(i, j: E) -> Ordering) where ORD(E) {
context._internal = rawptr(cmp);
context._internal = rawptr(cmp)
sort_by_cmp(data, proc(i, j: E) -> Ordering {
k := (proc(i, j: E) -> Ordering)(context._internal);
return k(j, i);
});
k := (proc(i, j: E) -> Ordering)(context._internal)
return k(j, i)
})
}
// TODO(bill): Should `sort_by_key` exist or is `sort_by` more than enough?
sort_by_key :: proc(data: $T/[]$E, key: proc(E) -> $K) where ORD(K) {
context._internal = rawptr(key);
context._internal = rawptr(key)
sort_by(data, proc(i, j: E) -> bool {
k := (proc(E) -> K)(context._internal);
return k(i) < k(j);
});
k := (proc(E) -> K)(context._internal)
return k(i) < k(j)
})
}
reverse_sort_by_key :: proc(data: $T/[]$E, key: proc(E) -> $K) where ORD(K) {
context._internal = rawptr(key);
context._internal = rawptr(key)
sort_by(data, proc(i, j: E) -> bool {
k := (proc(E) -> K)(context._internal);
return k(j) < k(i);
});
k := (proc(E) -> K)(context._internal)
return k(j) < k(i)
})
}
is_sorted_by_key :: proc(array: $T/[]$E, key: proc(E) -> $K) -> bool where ORD(K) {
for i := len(array)-1; i > 0; i -= 1 {
if key(array[i]) < key(array[i-1]) {
return false;
return false
}
}
return true;
return true
}
@(private)
_max_depth :: proc(n: int) -> int { // 2*ceil(log2(n+1))
depth: int;
depth: int
for i := n; i > 0; i >>= 1 {
depth += 1;
depth += 1
}
return depth * 2;
return depth * 2
}
@(private)
_quick_sort :: proc(data: $T/[]$E, a, b, max_depth: int) where ORD(E) {
median3 :: proc(data: T, m1, m0, m2: int) {
if data[m1] < data[m0] {
swap(data, m1, m0);
swap(data, m1, m0)
}
if data[m2] < data[m1] {
swap(data, m2, m1);
swap(data, m2, m1)
if data[m1] < data[m0] {
swap(data, m1, m0);
swap(data, m1, m0)
}
}
}
do_pivot :: proc(data: T, lo, hi: int) -> (midlo, midhi: int) {
m := int(uint(lo+hi)>>1);
m := int(uint(lo+hi)>>1)
if hi-lo > 40 {
s := (hi-lo)/8;
median3(data, lo, lo+s, lo+s*2);
median3(data, m, m-s, m+s);
median3(data, hi-1, hi-1-s, hi-1-s*2);
s := (hi-lo)/8
median3(data, lo, lo+s, lo+s*2)
median3(data, m, m-s, m+s)
median3(data, hi-1, hi-1-s, hi-1-s*2)
}
median3(data, lo, m, hi-1);
median3(data, lo, m, hi-1)
pivot := lo;
a, c := lo+1, hi-1;
pivot := lo
a, c := lo+1, hi-1
for ; a < c && data[a] < data[pivot]; a += 1 {
}
b := a;
b := a
for {
for ; b < c && !(data[pivot] < data[b]); b += 1 { // data[b] <= pivot
@@ -189,33 +189,33 @@ _quick_sort :: proc(data: $T/[]$E, a, b, max_depth: int) where ORD(E) {
for ; b < c && data[pivot] < data[c-1]; c -=1 { // data[c-1] > pivot
}
if b >= c {
break;
break
}
swap(data, b, c-1);
b += 1;
c -= 1;
swap(data, b, c-1)
b += 1
c -= 1
}
protect := hi-c < 5;
protect := hi-c < 5
if !protect && hi-c < (hi-lo)/4 {
dups := 0;
dups := 0
if !(data[pivot] < data[hi-1]) {
swap(data, c, hi-1);
c += 1;
dups += 1;
swap(data, c, hi-1)
c += 1
dups += 1
}
if !(data[b-1] < data[pivot]) {
b -= 1;
dups += 1;
b -= 1
dups += 1
}
if !(data[m] < data[pivot]) {
swap(data, m, b-1);
b -= 1;
dups += 1;
swap(data, m, b-1)
b -= 1
dups += 1
}
protect = dups > 1;
protect = dups > 1
}
if protect {
for {
@@ -224,43 +224,43 @@ _quick_sort :: proc(data: $T/[]$E, a, b, max_depth: int) where ORD(E) {
for ; a < b && data[a] < data[pivot]; a += 1 {
}
if a >= b {
break;
break
}
swap(data, a, b-1);
a += 1;
b -= 1;
swap(data, a, b-1)
a += 1
b -= 1
}
}
swap(data, pivot, b-1);
return b-1, c;
swap(data, pivot, b-1)
return b-1, c
}
a, b, max_depth := a, b, max_depth;
a, b, max_depth := a, b, max_depth
if b-a > 12 { // only use shell sort for lengths <= 12
if max_depth == 0 {
_heap_sort(data, a, b);
return;
_heap_sort(data, a, b)
return
}
max_depth -= 1;
mlo, mhi := do_pivot(data, a, b);
max_depth -= 1
mlo, mhi := do_pivot(data, a, b)
if mlo-a < b-mhi {
_quick_sort(data, a, mlo, max_depth);
a = mhi;
_quick_sort(data, a, mlo, max_depth)
a = mhi
} else {
_quick_sort(data, mhi, b, max_depth);
b = mlo;
_quick_sort(data, mhi, b, max_depth)
b = mlo
}
}
if b-a > 1 {
// Shell short with gap 6
for i in a+6..<b {
if data[i] < data[i-6] {
swap(data, i, i-6);
swap(data, i, i-6)
}
}
_insertion_sort(data, a, b);
_insertion_sort(data, a, b)
}
}
@@ -268,7 +268,7 @@ _quick_sort :: proc(data: $T/[]$E, a, b, max_depth: int) where ORD(E) {
_insertion_sort :: proc(data: $T/[]$E, a, b: int) where ORD(E) {
for i in a+1..<b {
for j := i; j > a && data[j] < data[j-1]; j -= 1 {
swap(data, j, j-1);
swap(data, j, j-1)
}
}
}
@@ -276,33 +276,33 @@ _insertion_sort :: proc(data: $T/[]$E, a, b: int) where ORD(E) {
@(private)
_heap_sort :: proc(data: $T/[]$E, a, b: int) where ORD(E) {
sift_down :: proc(data: T, lo, hi, first: int) {
root := lo;
root := lo
for {
child := 2*root + 1;
child := 2*root + 1
if child >= hi {
break;
break
}
if child+1 < hi && data[first+child] < data[first+child+1] {
child += 1;
child += 1
}
if !(data[first+root] < data[first+child]) {
return;
return
}
swap(data, first+root, first+child);
root = child;
swap(data, first+root, first+child)
root = child
}
}
first, lo, hi := a, 0, b-a;
first, lo, hi := a, 0, b-a
for i := (hi-1)/2; i >= 0; i -= 1 {
sift_down(data, i, hi, first);
sift_down(data, i, hi, first)
}
for i := hi-1; i >= 0; i -= 1 {
swap(data, first, first+i);
sift_down(data, lo, i, first);
swap(data, first, first+i)
sift_down(data, lo, i, first)
}
}
@@ -315,32 +315,32 @@ _heap_sort :: proc(data: $T/[]$E, a, b: int) where ORD(E) {
_quick_sort_less :: proc(data: $T/[]$E, a, b, max_depth: int, less: proc(i, j: E) -> bool) {
median3 :: proc(data: T, m1, m0, m2: int, less: proc(i, j: E) -> bool) {
if less(data[m1], data[m0]) {
swap(data, m1, m0);
swap(data, m1, m0)
}
if less(data[m2], data[m1]) {
swap(data, m2, m1);
swap(data, m2, m1)
if less(data[m1], data[m0]) {
swap(data, m1, m0);
swap(data, m1, m0)
}
}
}
do_pivot :: proc(data: T, lo, hi: int, less: proc(i, j: E) -> bool) -> (midlo, midhi: int) {
m := int(uint(lo+hi)>>1);
m := int(uint(lo+hi)>>1)
if hi-lo > 40 {
s := (hi-lo)/8;
median3(data, lo, lo+s, lo+s*2, less);
median3(data, m, m-s, m+s, less);
median3(data, hi-1, hi-1-s, hi-1-s*2, less);
s := (hi-lo)/8
median3(data, lo, lo+s, lo+s*2, less)
median3(data, m, m-s, m+s, less)
median3(data, hi-1, hi-1-s, hi-1-s*2, less)
}
median3(data, lo, m, hi-1, less);
median3(data, lo, m, hi-1, less)
pivot := lo;
a, c := lo+1, hi-1;
pivot := lo
a, c := lo+1, hi-1
for ; a < c && less(data[a], data[pivot]); a += 1 {
}
b := a;
b := a
for {
for ; b < c && !less(data[pivot], data[b]); b += 1 { // data[b] <= pivot
@@ -348,33 +348,33 @@ _quick_sort_less :: proc(data: $T/[]$E, a, b, max_depth: int, less: proc(i, j: E
for ; b < c && less(data[pivot], data[c-1]); c -=1 { // data[c-1] > pivot
}
if b >= c {
break;
break
}
swap(data, b, c-1);
b += 1;
c -= 1;
swap(data, b, c-1)
b += 1
c -= 1
}
protect := hi-c < 5;
protect := hi-c < 5
if !protect && hi-c < (hi-lo)/4 {
dups := 0;
dups := 0
if !less(data[pivot], data[hi-1]) {
swap(data, c, hi-1);
c += 1;
dups += 1;
swap(data, c, hi-1)
c += 1
dups += 1
}
if !less(data[b-1], data[pivot]) {
b -= 1;
dups += 1;
b -= 1
dups += 1
}
if !less(data[m], data[pivot]) {
swap(data, m, b-1);
b -= 1;
dups += 1;
swap(data, m, b-1)
b -= 1
dups += 1
}
protect = dups > 1;
protect = dups > 1
}
if protect {
for {
@@ -383,43 +383,43 @@ _quick_sort_less :: proc(data: $T/[]$E, a, b, max_depth: int, less: proc(i, j: E
for ; a < b && less(data[a], data[pivot]); a += 1 {
}
if a >= b {
break;
break
}
swap(data, a, b-1);
a += 1;
b -= 1;
swap(data, a, b-1)
a += 1
b -= 1
}
}
swap(data, pivot, b-1);
return b-1, c;
swap(data, pivot, b-1)
return b-1, c
}
a, b, max_depth := a, b, max_depth;
a, b, max_depth := a, b, max_depth
if b-a > 12 { // only use shell sort for lengths <= 12
if max_depth == 0 {
_heap_sort_less(data, a, b, less);
return;
_heap_sort_less(data, a, b, less)
return
}
max_depth -= 1;
mlo, mhi := do_pivot(data, a, b, less);
max_depth -= 1
mlo, mhi := do_pivot(data, a, b, less)
if mlo-a < b-mhi {
_quick_sort_less(data, a, mlo, max_depth, less);
a = mhi;
_quick_sort_less(data, a, mlo, max_depth, less)
a = mhi
} else {
_quick_sort_less(data, mhi, b, max_depth, less);
b = mlo;
_quick_sort_less(data, mhi, b, max_depth, less)
b = mlo
}
}
if b-a > 1 {
// Shell short with gap 6
for i in a+6..<b {
if less(data[i], data[i-6]) {
swap(data, i, i-6);
swap(data, i, i-6)
}
}
_insertion_sort_less(data, a, b, less);
_insertion_sort_less(data, a, b, less)
}
}
@@ -427,7 +427,7 @@ _quick_sort_less :: proc(data: $T/[]$E, a, b, max_depth: int, less: proc(i, j: E
_insertion_sort_less :: proc(data: $T/[]$E, a, b: int, less: proc(i, j: E) -> bool) {
for i in a+1..<b {
for j := i; j > a && less(data[j], data[j-1]); j -= 1 {
swap(data, j, j-1);
swap(data, j, j-1)
}
}
}
@@ -435,33 +435,33 @@ _insertion_sort_less :: proc(data: $T/[]$E, a, b: int, less: proc(i, j: E) -> bo
@(private)
_heap_sort_less :: proc(data: $T/[]$E, a, b: int, less: proc(i, j: E) -> bool) {
sift_down :: proc(data: T, lo, hi, first: int, less: proc(i, j: E) -> bool) {
root := lo;
root := lo
for {
child := 2*root + 1;
child := 2*root + 1
if child >= hi {
break;
break
}
if child+1 < hi && less(data[first+child], data[first+child+1]) {
child += 1;
child += 1
}
if !less(data[first+root], data[first+child]) {
return;
return
}
swap(data, first+root, first+child);
root = child;
swap(data, first+root, first+child)
root = child
}
}
first, lo, hi := a, 0, b-a;
first, lo, hi := a, 0, b-a
for i := (hi-1)/2; i >= 0; i -= 1 {
sift_down(data, i, hi, first, less);
sift_down(data, i, hi, first, less)
}
for i := hi-1; i >= 0; i -= 1 {
swap(data, first, first+i);
sift_down(data, lo, i, first, less);
swap(data, first, first+i)
sift_down(data, lo, i, first, less)
}
}
@@ -474,32 +474,32 @@ _heap_sort_less :: proc(data: $T/[]$E, a, b: int, less: proc(i, j: E) -> bool) {
_quick_sort_cmp :: proc(data: $T/[]$E, a, b, max_depth: int, cmp: proc(i, j: E) -> Ordering) {
median3 :: proc(data: T, m1, m0, m2: int, cmp: proc(i, j: E) -> Ordering) {
if cmp(data[m1], data[m0]) == .Less {
swap(data, m1, m0);
swap(data, m1, m0)
}
if cmp(data[m2], data[m1]) == .Less {
swap(data, m2, m1);
swap(data, m2, m1)
if cmp(data[m1], data[m0]) == .Less {
swap(data, m1, m0);
swap(data, m1, m0)
}
}
}
do_pivot :: proc(data: T, lo, hi: int, cmp: proc(i, j: E) -> Ordering) -> (midlo, midhi: int) {
m := int(uint(lo+hi)>>1);
m := int(uint(lo+hi)>>1)
if hi-lo > 40 {
s := (hi-lo)/8;
median3(data, lo, lo+s, lo+s*2, cmp);
median3(data, m, m-s, m+s, cmp);
median3(data, hi-1, hi-1-s, hi-1-s*2, cmp);
s := (hi-lo)/8
median3(data, lo, lo+s, lo+s*2, cmp)
median3(data, m, m-s, m+s, cmp)
median3(data, hi-1, hi-1-s, hi-1-s*2, cmp)
}
median3(data, lo, m, hi-1, cmp);
median3(data, lo, m, hi-1, cmp)
pivot := lo;
a, c := lo+1, hi-1;
pivot := lo
a, c := lo+1, hi-1
for ; a < c && cmp(data[a], data[pivot]) == .Less; a += 1 {
}
b := a;
b := a
for {
for ; b < c && cmp(data[pivot], data[b]) >= .Equal; b += 1 { // data[b] <= pivot
@@ -507,33 +507,33 @@ _quick_sort_cmp :: proc(data: $T/[]$E, a, b, max_depth: int, cmp: proc(i, j: E)
for ; b < c && cmp(data[pivot], data[c-1]) == .Less; c -=1 { // data[c-1] > pivot
}
if b >= c {
break;
break
}
swap(data, b, c-1);
b += 1;
c -= 1;
swap(data, b, c-1)
b += 1
c -= 1
}
protect := hi-c < 5;
protect := hi-c < 5
if !protect && hi-c < (hi-lo)/4 {
dups := 0;
dups := 0
if cmp(data[pivot], data[hi-1]) != .Less {
swap(data, c, hi-1);
c += 1;
dups += 1;
swap(data, c, hi-1)
c += 1
dups += 1
}
if cmp(data[b-1], data[pivot]) != .Less {
b -= 1;
dups += 1;
b -= 1
dups += 1
}
if cmp(data[m], data[pivot]) != .Less {
swap(data, m, b-1);
b -= 1;
dups += 1;
swap(data, m, b-1)
b -= 1
dups += 1
}
protect = dups > 1;
protect = dups > 1
}
if protect {
for {
@@ -542,43 +542,43 @@ _quick_sort_cmp :: proc(data: $T/[]$E, a, b, max_depth: int, cmp: proc(i, j: E)
for ; a < b && cmp(data[a], data[pivot]) == .Less; a += 1 {
}
if a >= b {
break;
break
}
swap(data, a, b-1);
a += 1;
b -= 1;
swap(data, a, b-1)
a += 1
b -= 1
}
}
swap(data, pivot, b-1);
return b-1, c;
swap(data, pivot, b-1)
return b-1, c
}
a, b, max_depth := a, b, max_depth;
a, b, max_depth := a, b, max_depth
if b-a > 12 { // only use shell sort for lengths <= 12
if max_depth == 0 {
_heap_sort_cmp(data, a, b, cmp);
return;
_heap_sort_cmp(data, a, b, cmp)
return
}
max_depth -= 1;
mlo, mhi := do_pivot(data, a, b, cmp);
max_depth -= 1
mlo, mhi := do_pivot(data, a, b, cmp)
if mlo-a < b-mhi {
_quick_sort_cmp(data, a, mlo, max_depth, cmp);
a = mhi;
_quick_sort_cmp(data, a, mlo, max_depth, cmp)
a = mhi
} else {
_quick_sort_cmp(data, mhi, b, max_depth, cmp);
b = mlo;
_quick_sort_cmp(data, mhi, b, max_depth, cmp)
b = mlo
}
}
if b-a > 1 {
// Shell short with gap 6
for i in a+6..<b {
if cmp(data[i], data[i-6]) == .Less {
swap(data, i, i-6);
swap(data, i, i-6)
}
}
_insertion_sort_cmp(data, a, b, cmp);
_insertion_sort_cmp(data, a, b, cmp)
}
}
@@ -586,7 +586,7 @@ _quick_sort_cmp :: proc(data: $T/[]$E, a, b, max_depth: int, cmp: proc(i, j: E)
_insertion_sort_cmp :: proc(data: $T/[]$E, a, b: int, cmp: proc(i, j: E) -> Ordering) {
for i in a+1..<b {
for j := i; j > a && cmp(data[j], data[j-1]) == .Less; j -= 1 {
swap(data, j, j-1);
swap(data, j, j-1)
}
}
}
@@ -594,33 +594,33 @@ _insertion_sort_cmp :: proc(data: $T/[]$E, a, b: int, cmp: proc(i, j: E) -> Orde
@(private)
_heap_sort_cmp :: proc(data: $T/[]$E, a, b: int, cmp: proc(i, j: E) -> Ordering) {
sift_down :: proc(data: T, lo, hi, first: int, cmp: proc(i, j: E) -> Ordering) {
root := lo;
root := lo
for {
child := 2*root + 1;
child := 2*root + 1
if child >= hi {
break;
break
}
if child+1 < hi && cmp(data[first+child], data[first+child+1]) == .Less {
child += 1;
child += 1
}
if cmp(data[first+root], data[first+child]) >= .Equal {
return;
return
}
swap(data, first+root, first+child);
root = child;
swap(data, first+root, first+child)
root = child
}
}
first, lo, hi := a, 0, b-a;
first, lo, hi := a, 0, b-a
for i := (hi-1)/2; i >= 0; i -= 1 {
sift_down(data, i, hi, first, cmp);
sift_down(data, i, hi, first, cmp)
}
for i := hi-1; i >= 0; i -= 1 {
swap(data, first, first+i);
sift_down(data, lo, i, first, cmp);
swap(data, first, first+i)
sift_down(data, lo, i, first, cmp)
}
}