Skip to content

fix: new sorting algorithm #1904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
80c4ae5
init
MaxGraey Jun 14, 2021
9cb9fe2
fix fixtures
MaxGraey Jun 14, 2021
3a38451
use ints in mergeRuns
MaxGraey Jun 14, 2021
abd6c8e
cleanups
MaxGraey Jun 14, 2021
cd5d7f9
use usize for offsets
MaxGraey Jun 15, 2021
a1f4d4c
more
MaxGraey Jun 16, 2021
b620730
Merge branch 'main' into new-sort
MaxGraey Jun 24, 2021
a320a19
lazy -> inline
MaxGraey Jul 2, 2021
cfd9326
join temp arrays into one
MaxGraey Jul 2, 2021
4e2c505
add TODO
MaxGraey Jul 2, 2021
ff7fc64
refactor
MaxGraey Jul 3, 2021
ecfa6fd
cleanups
MaxGraey Jul 3, 2021
fa15710
refactor
MaxGraey Jul 3, 2021
605371b
Merge branch 'main' into new-sort
MaxGraey Jul 3, 2021
82641f9
Merge branch 'main' into new-sort
MaxGraey Jul 4, 2021
5fa6c30
refactor
MaxGraey Jul 4, 2021
fa63754
minor opt
MaxGraey Jul 5, 2021
a474907
simplify main sorting routines
MaxGraey Jul 7, 2021
fed4e12
more
MaxGraey Jul 7, 2021
db577b9
upd fixtures
MaxGraey Jul 7, 2021
27331c7
refactor
MaxGraey Jul 7, 2021
0a9beed
upd fixture again
MaxGraey Jul 7, 2021
270926e
fix stability for len <= 3
MaxGraey Jul 7, 2021
9ca8fe5
clean
MaxGraey Jul 7, 2021
6c6b6dc
remove redundant store
MaxGraey Jul 9, 2021
57abed6
minor refactorings
MaxGraey Jul 10, 2021
ff8c1c9
more
MaxGraey Jul 10, 2021
3880b06
update fixtures
MaxGraey Jul 10, 2021
2cefcd8
add lazy decorator
MaxGraey Jul 10, 2021
e9f6614
add simple stability check
MaxGraey Jul 11, 2021
028f47d
this is slightly faster and clearer
MaxGraey Jul 11, 2021
d9d0b5c
Merge branch 'main' into new-sort
MaxGraey Jul 12, 2021
9cf14d4
upd fixtures
MaxGraey Jul 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions std/assembly/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,19 +440,7 @@ export class Array<T> {
}

sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
var length = this.length_;
if (length <= 1) return this;
var base = this.dataStart;
if (length == 2) {
let a: T = load<T>(base, sizeof<T>()); // a = arr[1]
let b: T = load<T>(base); // b = arr[0]
if (comparator(a, b) < 0) {
store<T>(base, b, sizeof<T>()); // arr[1] = b;
store<T>(base, a); // arr[0] = a;
}
return this;
}
SORT<T>(base, length, comparator);
SORT<T>(this.dataStart, this.length_, comparator);
return this;
}

Expand Down
59 changes: 24 additions & 35 deletions std/assembly/typedarray.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { COMPARATOR, SORT as SORT_IMPL } from "./util/sort";
import { COMPARATOR, SORT } from "./util/sort";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_NOTIMPLEMENTED } from "./util/error";
import { joinIntegerArray, joinFloatArray } from "./util/string";
import { idof } from "./builtins";
Expand Down Expand Up @@ -65,7 +65,8 @@ export class Int8Array extends ArrayBufferView {
}

sort(comparator: (a: i8, b: i8) => i32 = COMPARATOR<i8>()): Int8Array {
return SORT<Int8Array, i8>(this, comparator);
SORT<i8>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int8Array {
Expand Down Expand Up @@ -200,7 +201,8 @@ export class Uint8Array extends ArrayBufferView {
}

sort(comparator: (a: u8, b: u8) => i32 = COMPARATOR<u8>()): Uint8Array {
return SORT<Uint8Array, u8>(this, comparator);
SORT<u8>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8Array {
Expand Down Expand Up @@ -334,8 +336,9 @@ export class Uint8ClampedArray extends ArrayBufferView {
return FILL<Uint8ClampedArray, u8>(this, value, start, end);
}

sort(fn: (a: u8, b: u8) => i32 = COMPARATOR<u8>()): Uint8ClampedArray {
return SORT<Uint8ClampedArray, u8>(this, fn);
sort(comparator: (a: u8, b: u8) => i32 = COMPARATOR<u8>()): Uint8ClampedArray {
SORT<u8>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8ClampedArray {
Expand Down Expand Up @@ -470,7 +473,8 @@ export class Int16Array extends ArrayBufferView {
}

sort(comparator: (a: i16, b: i16) => i32 = COMPARATOR<i16>()): Int16Array {
return SORT<Int16Array, i16>(this, comparator);
SORT<i16>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int16Array {
Expand Down Expand Up @@ -605,7 +609,8 @@ export class Uint16Array extends ArrayBufferView {
}

sort(comparator: (a: u16, b: u16) => i32 = COMPARATOR<u16>()): Uint16Array {
return SORT<Uint16Array, u16>(this, comparator);
SORT<u16>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint16Array {
Expand Down Expand Up @@ -740,7 +745,8 @@ export class Int32Array extends ArrayBufferView {
}

sort(comparator: (a: i32, b: i32) => i32 = COMPARATOR<i32>()): Int32Array {
return SORT<Int32Array, i32>(this, comparator);
SORT<i32>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int32Array {
Expand Down Expand Up @@ -875,7 +881,8 @@ export class Uint32Array extends ArrayBufferView {
}

sort(comparator: (a: u32, b: u32) => i32 = COMPARATOR<u32>()): Uint32Array {
return SORT<Uint32Array, u32>(this, comparator);
SORT<u32>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint32Array {
Expand Down Expand Up @@ -1010,7 +1017,8 @@ export class Int64Array extends ArrayBufferView {
}

sort(comparator: (a: i64, b: i64) => i32 = COMPARATOR<i64>()): Int64Array {
return SORT<Int64Array, i64>(this, comparator);
SORT<i64>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int64Array {
Expand Down Expand Up @@ -1145,7 +1153,8 @@ export class Uint64Array extends ArrayBufferView {
}

sort(comparator: (a: u64, b: u64) => i32 = COMPARATOR<u64>()): Uint64Array {
return SORT<Uint64Array, u64>(this, comparator);
SORT<u64>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint64Array {
Expand Down Expand Up @@ -1280,7 +1289,8 @@ export class Float32Array extends ArrayBufferView {
}

sort(comparator: (a: f32, b: f32) => i32 = COMPARATOR<f32>()): Float32Array {
return SORT<Float32Array, f32>(this, comparator);
SORT<f32>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float32Array {
Expand Down Expand Up @@ -1415,7 +1425,8 @@ export class Float64Array extends ArrayBufferView {
}

sort(comparator: (a: f64, b: f64) => i32 = COMPARATOR<f64>()): Float64Array {
return SORT<Float64Array, f64>(this, comparator);
SORT<f64>(this.dataStart, this.length, comparator);
return this;
}

slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float64Array {
Expand Down Expand Up @@ -1511,28 +1522,6 @@ function FILL<TArray extends ArrayBufferView, T extends number>(
return array;
}

// @ts-ignore: decorator
@inline
function SORT<TArray extends ArrayBufferView, T>(
array: TArray,
comparator: (a: T, b: T) => i32
): TArray {
var len = array.length;
if (len <= 1) return array;
var base = array.dataStart;
if (len == 2) {
let a: T = load<T>(base, sizeof<T>()); // a = arr[1]
let b: T = load<T>(base); // b = arr[0]
if (comparator(a, b) < 0) {
store<T>(base, b, sizeof<T>()); // arr[1] = b
store<T>(base, a); // arr[0] = a
}
return array;
}
SORT_IMPL<T>(base, len, comparator);
return array;
}

// @ts-ignore: decorator
@inline
function SLICE<TArray extends ArrayBufferView, T>(
Expand Down
Loading