|
1 | | -import { COMPARATOR, SORT as SORT_IMPL } from "./util/sort"; |
| 1 | +import { COMPARATOR, SORT } from "./util/sort"; |
2 | 2 | import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_NOTIMPLEMENTED } from "./util/error"; |
3 | 3 | import { joinIntegerArray, joinFloatArray } from "./util/string"; |
4 | 4 | import { idof } from "./builtins"; |
@@ -65,7 +65,8 @@ export class Int8Array extends ArrayBufferView { |
65 | 65 | } |
66 | 66 |
|
67 | 67 | sort(comparator: (a: i8, b: i8) => i32 = COMPARATOR<i8>()): Int8Array { |
68 | | - return SORT<Int8Array, i8>(this, comparator); |
| 68 | + SORT<i8>(this.dataStart, this.length, comparator); |
| 69 | + return this; |
69 | 70 | } |
70 | 71 |
|
71 | 72 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int8Array { |
@@ -200,7 +201,8 @@ export class Uint8Array extends ArrayBufferView { |
200 | 201 | } |
201 | 202 |
|
202 | 203 | sort(comparator: (a: u8, b: u8) => i32 = COMPARATOR<u8>()): Uint8Array { |
203 | | - return SORT<Uint8Array, u8>(this, comparator); |
| 204 | + SORT<u8>(this.dataStart, this.length, comparator); |
| 205 | + return this; |
204 | 206 | } |
205 | 207 |
|
206 | 208 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8Array { |
@@ -334,8 +336,9 @@ export class Uint8ClampedArray extends ArrayBufferView { |
334 | 336 | return FILL<Uint8ClampedArray, u8>(this, value, start, end); |
335 | 337 | } |
336 | 338 |
|
337 | | - sort(fn: (a: u8, b: u8) => i32 = COMPARATOR<u8>()): Uint8ClampedArray { |
338 | | - return SORT<Uint8ClampedArray, u8>(this, fn); |
| 339 | + sort(comparator: (a: u8, b: u8) => i32 = COMPARATOR<u8>()): Uint8ClampedArray { |
| 340 | + SORT<u8>(this.dataStart, this.length, comparator); |
| 341 | + return this; |
339 | 342 | } |
340 | 343 |
|
341 | 344 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8ClampedArray { |
@@ -470,7 +473,8 @@ export class Int16Array extends ArrayBufferView { |
470 | 473 | } |
471 | 474 |
|
472 | 475 | sort(comparator: (a: i16, b: i16) => i32 = COMPARATOR<i16>()): Int16Array { |
473 | | - return SORT<Int16Array, i16>(this, comparator); |
| 476 | + SORT<i16>(this.dataStart, this.length, comparator); |
| 477 | + return this; |
474 | 478 | } |
475 | 479 |
|
476 | 480 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int16Array { |
@@ -605,7 +609,8 @@ export class Uint16Array extends ArrayBufferView { |
605 | 609 | } |
606 | 610 |
|
607 | 611 | sort(comparator: (a: u16, b: u16) => i32 = COMPARATOR<u16>()): Uint16Array { |
608 | | - return SORT<Uint16Array, u16>(this, comparator); |
| 612 | + SORT<u16>(this.dataStart, this.length, comparator); |
| 613 | + return this; |
609 | 614 | } |
610 | 615 |
|
611 | 616 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint16Array { |
@@ -740,7 +745,8 @@ export class Int32Array extends ArrayBufferView { |
740 | 745 | } |
741 | 746 |
|
742 | 747 | sort(comparator: (a: i32, b: i32) => i32 = COMPARATOR<i32>()): Int32Array { |
743 | | - return SORT<Int32Array, i32>(this, comparator); |
| 748 | + SORT<i32>(this.dataStart, this.length, comparator); |
| 749 | + return this; |
744 | 750 | } |
745 | 751 |
|
746 | 752 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int32Array { |
@@ -875,7 +881,8 @@ export class Uint32Array extends ArrayBufferView { |
875 | 881 | } |
876 | 882 |
|
877 | 883 | sort(comparator: (a: u32, b: u32) => i32 = COMPARATOR<u32>()): Uint32Array { |
878 | | - return SORT<Uint32Array, u32>(this, comparator); |
| 884 | + SORT<u32>(this.dataStart, this.length, comparator); |
| 885 | + return this; |
879 | 886 | } |
880 | 887 |
|
881 | 888 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint32Array { |
@@ -1010,7 +1017,8 @@ export class Int64Array extends ArrayBufferView { |
1010 | 1017 | } |
1011 | 1018 |
|
1012 | 1019 | sort(comparator: (a: i64, b: i64) => i32 = COMPARATOR<i64>()): Int64Array { |
1013 | | - return SORT<Int64Array, i64>(this, comparator); |
| 1020 | + SORT<i64>(this.dataStart, this.length, comparator); |
| 1021 | + return this; |
1014 | 1022 | } |
1015 | 1023 |
|
1016 | 1024 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int64Array { |
@@ -1145,7 +1153,8 @@ export class Uint64Array extends ArrayBufferView { |
1145 | 1153 | } |
1146 | 1154 |
|
1147 | 1155 | sort(comparator: (a: u64, b: u64) => i32 = COMPARATOR<u64>()): Uint64Array { |
1148 | | - return SORT<Uint64Array, u64>(this, comparator); |
| 1156 | + SORT<u64>(this.dataStart, this.length, comparator); |
| 1157 | + return this; |
1149 | 1158 | } |
1150 | 1159 |
|
1151 | 1160 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint64Array { |
@@ -1280,7 +1289,8 @@ export class Float32Array extends ArrayBufferView { |
1280 | 1289 | } |
1281 | 1290 |
|
1282 | 1291 | sort(comparator: (a: f32, b: f32) => i32 = COMPARATOR<f32>()): Float32Array { |
1283 | | - return SORT<Float32Array, f32>(this, comparator); |
| 1292 | + SORT<f32>(this.dataStart, this.length, comparator); |
| 1293 | + return this; |
1284 | 1294 | } |
1285 | 1295 |
|
1286 | 1296 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float32Array { |
@@ -1415,7 +1425,8 @@ export class Float64Array extends ArrayBufferView { |
1415 | 1425 | } |
1416 | 1426 |
|
1417 | 1427 | sort(comparator: (a: f64, b: f64) => i32 = COMPARATOR<f64>()): Float64Array { |
1418 | | - return SORT<Float64Array, f64>(this, comparator); |
| 1428 | + SORT<f64>(this.dataStart, this.length, comparator); |
| 1429 | + return this; |
1419 | 1430 | } |
1420 | 1431 |
|
1421 | 1432 | slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float64Array { |
@@ -1511,28 +1522,6 @@ function FILL<TArray extends ArrayBufferView, T extends number>( |
1511 | 1522 | return array; |
1512 | 1523 | } |
1513 | 1524 |
|
1514 | | -// @ts-ignore: decorator |
1515 | | -@inline |
1516 | | -function SORT<TArray extends ArrayBufferView, T>( |
1517 | | - array: TArray, |
1518 | | - comparator: (a: T, b: T) => i32 |
1519 | | -): TArray { |
1520 | | - var len = array.length; |
1521 | | - if (len <= 1) return array; |
1522 | | - var base = array.dataStart; |
1523 | | - if (len == 2) { |
1524 | | - let a: T = load<T>(base, sizeof<T>()); // a = arr[1] |
1525 | | - let b: T = load<T>(base); // b = arr[0] |
1526 | | - if (comparator(a, b) < 0) { |
1527 | | - store<T>(base, b, sizeof<T>()); // arr[1] = b |
1528 | | - store<T>(base, a); // arr[0] = a |
1529 | | - } |
1530 | | - return array; |
1531 | | - } |
1532 | | - SORT_IMPL<T>(base, len, comparator); |
1533 | | - return array; |
1534 | | -} |
1535 | | - |
1536 | 1525 | // @ts-ignore: decorator |
1537 | 1526 | @inline |
1538 | 1527 | function SLICE<TArray extends ArrayBufferView, T>( |
|
0 commit comments