Skip to content

Commit 3ed83ef

Browse files
committed
Use macro style for more internal helpers; Update dist files
1 parent b585703 commit 3ed83ef

37 files changed

+544
-543
lines changed

dist/asc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

std/assembly/array.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import {
1414
} from "./internal/string";
1515

1616
import {
17-
defaultComparator,
18-
insertionSort,
19-
weakHeapSort
20-
} from "./internal/array";
17+
COMPARATOR,
18+
SORT
19+
} from "./internal/sort";
2120

2221
import {
2322
itoa,
@@ -403,7 +402,7 @@ export class Array<T> {
403402
return this;
404403
}
405404

406-
sort(comparator: (a: T, b: T) => i32 = defaultComparator<T>()): this {
405+
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
407406
// TODO remove this when flow will allow trackcing null
408407
assert(comparator); // The comparison function must be a function
409408

@@ -419,19 +418,8 @@ export class Array<T> {
419418
}
420419
return this;
421420
}
422-
423-
if (isReference<T>()) {
424-
// TODO replace this to faster stable sort (TimSort) when it implemented
425-
insertionSort<T>(buffer, 0, length, comparator);
426-
return this;
427-
} else {
428-
if (length < 256) {
429-
insertionSort<T>(buffer, 0, length, comparator);
430-
} else {
431-
weakHeapSort<T>(buffer, 0, length, comparator);
432-
}
433-
return this;
434-
}
421+
SORT<T>(buffer, 0, length, comparator);
422+
return this;
435423
}
436424

437425
join(separator: string = ","): string {

std/assembly/internal/arraybuffer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { AL_MASK, MAX_SIZE_32 } from "./allocator";
1+
import {
2+
AL_MASK,
3+
MAX_SIZE_32
4+
} from "./allocator";
25

36
/** Size of an ArrayBuffer header. */
47
export const HEADER_SIZE: usize = (offsetof<ArrayBuffer>() + AL_MASK) & ~AL_MASK;

std/assembly/internal/hash.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
2-
HEADER_SIZE as STRING_HEADER_SIZE
2+
HEADER_SIZE
33
} from "./string";
44

55
/** Computes the 32-bit hash of a value of any type. */
66
@inline
7-
export function hash<T>(key: T): u32 {
7+
export function HASH<T>(key: T): u32 {
88
// branch-level tree-shaking makes this a `(return (call ...))`
99
if (isString(key)) {
1010
return hashStr(key);
@@ -66,7 +66,7 @@ function hash64(key: u64): u32 {
6666
function hashStr(key: string): u32 {
6767
var v = FNV_OFFSET;
6868
for (let i: usize = 0, k: usize = key.length << 1; i < k; ++i) {
69-
v = (v ^ <u32>load<u8>(changetype<usize>(key) + i, STRING_HEADER_SIZE)) * FNV_PRIME;
69+
v = (v ^ <u32>load<u8>(changetype<usize>(key) + i, HEADER_SIZE)) * FNV_PRIME;
7070
}
7171
return v;
7272
}
Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import {
44
} from "./arraybuffer";
55

66
import {
7-
compareUnsafe,
7+
compareUnsafe
88
} from "./string";
99

10-
/** Obtains the default comparator for the specified type. */
10+
/** Obtains the default comparator for the specified value type. */
1111
@inline
12-
export function defaultComparator<T>(): (a: T, b: T) => i32 {
12+
export function COMPARATOR<T>(): (a: T, b: T) => i32 {
1313
if (isInteger<T>()) {
1414
if (isSigned<T>() && sizeof<T>() <= 4) {
1515
return (a: T, b: T): i32 => (<i32>(a - b));
@@ -44,8 +44,27 @@ export function defaultComparator<T>(): (a: T, b: T) => i32 {
4444
}
4545
}
4646

47+
@inline
48+
export function SORT<T>(
49+
buffer: ArrayBuffer,
50+
byteOffset: i32,
51+
length: i32,
52+
comparator: (a: T, b: T) => i32
53+
): void {
54+
if (isReference<T>()) {
55+
// TODO replace this to faster stable sort (TimSort) when it implemented
56+
insertionSort<T>(buffer, byteOffset, length, comparator);
57+
} else {
58+
if (length < 256) {
59+
insertionSort<T>(buffer, byteOffset, length, comparator);
60+
} else {
61+
weakHeapSort<T>(buffer, byteOffset, length, comparator);
62+
}
63+
}
64+
}
65+
4766
/** Sorts an Array with the 'Insertion Sort' algorithm. */
48-
export function insertionSort<T>(
67+
function insertionSort<T>(
4968
buffer: ArrayBuffer,
5069
byteOffset: i32,
5170
length: i32,
@@ -65,7 +84,7 @@ export function insertionSort<T>(
6584
}
6685

6786
/** Sorts an Array with the 'Weak Heap Sort' algorithm. */
68-
export function weakHeapSort<T>(
87+
function weakHeapSort<T>(
6988
buffer: ArrayBuffer,
7089
byteOffset: i32,
7190
length: i32,

std/assembly/internal/typedarray.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import {
77
} from "./arraybuffer";
88

99
import {
10-
insertionSort,
11-
weakHeapSort
12-
} from "./array";
10+
SORT as SORT_IMPL
11+
} from "./sort";
1312

1413
/** Typed array base class. Not a global object. */
1514
export abstract class TypedArray<T> {
@@ -106,18 +105,8 @@ export function SORT<TArray extends TypedArray<T>, T>(
106105
}
107106
return array;
108107
}
109-
if (isReference<T>()) {
110-
// TODO replace this to faster stable sort (TimSort) when it implemented
111-
insertionSort<T>(buffer, byteOffset, length, comparator);
112-
return array;
113-
} else {
114-
if (length < 256) {
115-
insertionSort<T>(buffer, byteOffset, length, comparator);
116-
} else {
117-
weakHeapSort<T>(buffer, byteOffset, length, comparator);
118-
}
119-
return array;
120-
}
108+
SORT_IMPL<T>(buffer, byteOffset, length, comparator);
109+
return array;
121110
}
122111

123112
@inline

std/assembly/map.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
} from "./internal/arraybuffer";
44

55
import {
6-
hash
6+
HASH
77
} from "./internal/hash";
88

99
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
@@ -80,16 +80,16 @@ export class Map<K,V> {
8080
}
8181

8282
has(key: K): bool {
83-
return this.find(key, hash<K>(key)) !== null;
83+
return this.find(key, HASH<K>(key)) !== null;
8484
}
8585

8686
get(key: K): V {
87-
var entry = this.find(key, hash<K>(key));
87+
var entry = this.find(key, HASH<K>(key));
8888
return entry ? entry.value : <V>unreachable();
8989
}
9090

9191
set(key: K, value: V): void {
92-
var hashCode = hash<K>(key);
92+
var hashCode = HASH<K>(key);
9393
var entry = this.find(key, hashCode);
9494
if (entry) {
9595
entry.value = value;
@@ -120,7 +120,7 @@ export class Map<K,V> {
120120
}
121121

122122
delete(key: K): bool {
123-
var entry = this.find(key, hash<K>(key));
123+
var entry = this.find(key, HASH<K>(key));
124124
if (!entry) return false;
125125
entry.taggedNext |= EMPTY;
126126
--this.entriesCount;
@@ -149,7 +149,7 @@ export class Map<K,V> {
149149
let newEntry = changetype<MapEntry<K,V>>(newPtr);
150150
newEntry.key = oldEntry.key;
151151
newEntry.value = oldEntry.value;
152-
let newBucketIndex = hash<K>(oldEntry.key) & newBucketsMask;
152+
let newBucketIndex = HASH<K>(oldEntry.key) & newBucketsMask;
153153
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
154154
newEntry.taggedNext = load<usize>(newBucketPtrBase, HEADER_SIZE_AB);
155155
store<usize>(newBucketPtrBase, newPtr, HEADER_SIZE_AB);

0 commit comments

Comments
 (0)