Use macro style for more internal helpers; Update dist files

This commit is contained in:
dcodeIO
2018-12-08 23:38:49 +01:00
parent b585703eae
commit 3ed83ef3ae
37 changed files with 544 additions and 543 deletions

View File

@ -14,10 +14,9 @@ import {
} from "./internal/string";
import {
defaultComparator,
insertionSort,
weakHeapSort
} from "./internal/array";
COMPARATOR,
SORT
} from "./internal/sort";
import {
itoa,
@ -403,7 +402,7 @@ export class Array<T> {
return this;
}
sort(comparator: (a: T, b: T) => i32 = defaultComparator<T>()): this {
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
// TODO remove this when flow will allow trackcing null
assert(comparator); // The comparison function must be a function
@ -419,19 +418,8 @@ export class Array<T> {
}
return this;
}
if (isReference<T>()) {
// TODO replace this to faster stable sort (TimSort) when it implemented
insertionSort<T>(buffer, 0, length, comparator);
return this;
} else {
if (length < 256) {
insertionSort<T>(buffer, 0, length, comparator);
} else {
weakHeapSort<T>(buffer, 0, length, comparator);
}
return this;
}
SORT<T>(buffer, 0, length, comparator);
return this;
}
join(separator: string = ","): string {

View File

@ -1,4 +1,7 @@
import { AL_MASK, MAX_SIZE_32 } from "./allocator";
import {
AL_MASK,
MAX_SIZE_32
} from "./allocator";
/** Size of an ArrayBuffer header. */
export const HEADER_SIZE: usize = (offsetof<ArrayBuffer>() + AL_MASK) & ~AL_MASK;

View File

@ -1,10 +1,10 @@
import {
HEADER_SIZE as STRING_HEADER_SIZE
HEADER_SIZE
} from "./string";
/** Computes the 32-bit hash of a value of any type. */
@inline
export function hash<T>(key: T): u32 {
export function HASH<T>(key: T): u32 {
// branch-level tree-shaking makes this a `(return (call ...))`
if (isString(key)) {
return hashStr(key);
@ -66,7 +66,7 @@ function hash64(key: u64): u32 {
function hashStr(key: string): u32 {
var v = FNV_OFFSET;
for (let i: usize = 0, k: usize = key.length << 1; i < k; ++i) {
v = (v ^ <u32>load<u8>(changetype<usize>(key) + i, STRING_HEADER_SIZE)) * FNV_PRIME;
v = (v ^ <u32>load<u8>(changetype<usize>(key) + i, HEADER_SIZE)) * FNV_PRIME;
}
return v;
}

View File

@ -4,12 +4,12 @@ import {
} from "./arraybuffer";
import {
compareUnsafe,
compareUnsafe
} from "./string";
/** Obtains the default comparator for the specified type. */
/** Obtains the default comparator for the specified value type. */
@inline
export function defaultComparator<T>(): (a: T, b: T) => i32 {
export function COMPARATOR<T>(): (a: T, b: T) => i32 {
if (isInteger<T>()) {
if (isSigned<T>() && sizeof<T>() <= 4) {
return (a: T, b: T): i32 => (<i32>(a - b));
@ -44,8 +44,27 @@ export function defaultComparator<T>(): (a: T, b: T) => i32 {
}
}
@inline
export function SORT<T>(
buffer: ArrayBuffer,
byteOffset: i32,
length: i32,
comparator: (a: T, b: T) => i32
): void {
if (isReference<T>()) {
// TODO replace this to faster stable sort (TimSort) when it implemented
insertionSort<T>(buffer, byteOffset, length, comparator);
} else {
if (length < 256) {
insertionSort<T>(buffer, byteOffset, length, comparator);
} else {
weakHeapSort<T>(buffer, byteOffset, length, comparator);
}
}
}
/** Sorts an Array with the 'Insertion Sort' algorithm. */
export function insertionSort<T>(
function insertionSort<T>(
buffer: ArrayBuffer,
byteOffset: i32,
length: i32,
@ -65,7 +84,7 @@ export function insertionSort<T>(
}
/** Sorts an Array with the 'Weak Heap Sort' algorithm. */
export function weakHeapSort<T>(
function weakHeapSort<T>(
buffer: ArrayBuffer,
byteOffset: i32,
length: i32,

View File

@ -7,9 +7,8 @@ import {
} from "./arraybuffer";
import {
insertionSort,
weakHeapSort
} from "./array";
SORT as SORT_IMPL
} from "./sort";
/** Typed array base class. Not a global object. */
export abstract class TypedArray<T> {
@ -106,18 +105,8 @@ export function SORT<TArray extends TypedArray<T>, T>(
}
return array;
}
if (isReference<T>()) {
// TODO replace this to faster stable sort (TimSort) when it implemented
insertionSort<T>(buffer, byteOffset, length, comparator);
return array;
} else {
if (length < 256) {
insertionSort<T>(buffer, byteOffset, length, comparator);
} else {
weakHeapSort<T>(buffer, byteOffset, length, comparator);
}
return array;
}
SORT_IMPL<T>(buffer, byteOffset, length, comparator);
return array;
}
@inline

View File

@ -3,7 +3,7 @@ import {
} from "./internal/arraybuffer";
import {
hash
HASH
} from "./internal/hash";
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
@ -80,16 +80,16 @@ export class Map<K,V> {
}
has(key: K): bool {
return this.find(key, hash<K>(key)) !== null;
return this.find(key, HASH<K>(key)) !== null;
}
get(key: K): V {
var entry = this.find(key, hash<K>(key));
var entry = this.find(key, HASH<K>(key));
return entry ? entry.value : <V>unreachable();
}
set(key: K, value: V): void {
var hashCode = hash<K>(key);
var hashCode = HASH<K>(key);
var entry = this.find(key, hashCode);
if (entry) {
entry.value = value;
@ -120,7 +120,7 @@ export class Map<K,V> {
}
delete(key: K): bool {
var entry = this.find(key, hash<K>(key));
var entry = this.find(key, HASH<K>(key));
if (!entry) return false;
entry.taggedNext |= EMPTY;
--this.entriesCount;
@ -149,7 +149,7 @@ export class Map<K,V> {
let newEntry = changetype<MapEntry<K,V>>(newPtr);
newEntry.key = oldEntry.key;
newEntry.value = oldEntry.value;
let newBucketIndex = hash<K>(oldEntry.key) & newBucketsMask;
let newBucketIndex = HASH<K>(oldEntry.key) & newBucketsMask;
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
newEntry.taggedNext = load<usize>(newBucketPtrBase, HEADER_SIZE_AB);
store<usize>(newBucketPtrBase, newPtr, HEADER_SIZE_AB);

View File

@ -3,7 +3,7 @@ import {
} from "./internal/arraybuffer";
import {
hash
HASH
} from "./internal/hash";
// A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht
@ -78,11 +78,11 @@ export class Set<K> {
}
has(key: K): bool {
return this.find(key, hash(key)) !== null;
return this.find(key, HASH(key)) !== null;
}
add(key: K): void {
var hashCode = hash(key);
var hashCode = HASH(key);
var entry = this.find(key, hashCode);
if (!entry) {
// check if rehashing is necessary
@ -109,7 +109,7 @@ export class Set<K> {
}
delete(key: K): bool {
var entry = this.find(key, hash<K>(key));
var entry = this.find(key, HASH<K>(key));
if (!entry) return false;
entry.taggedNext |= EMPTY;
--this.entriesCount;
@ -137,7 +137,7 @@ export class Set<K> {
if (!(oldEntry.taggedNext & EMPTY)) {
let newEntry = changetype<SetEntry<K>>(newPtr);
newEntry.key = oldEntry.key;
let newBucketIndex = hash<K>(oldEntry.key) & newBucketsMask;
let newBucketIndex = HASH<K>(oldEntry.key) & newBucketsMask;
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
newEntry.taggedNext = load<usize>(newBucketPtrBase, HEADER_SIZE_AB);
store<usize>(newBucketPtrBase, newPtr, HEADER_SIZE_AB);

View File

@ -9,8 +9,8 @@ import {
} from "./internal/typedarray";
import {
defaultComparator
} from "./internal/array";
COMPARATOR
} from "./internal/sort";
export class Int8Array extends TypedArray<i8> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
@ -19,7 +19,7 @@ export class Int8Array extends TypedArray<i8> {
return FILL<Int8Array, i8>(this, value, start, end);
}
sort(comparator: (a: i8, b: i8) => i32 = defaultComparator<i8>()): Int8Array {
sort(comparator: (a: i8, b: i8) => i32 = COMPARATOR<i8>()): Int8Array {
return SORT<Int8Array, i8>(this, comparator);
}
@ -53,7 +53,7 @@ export class Uint8Array extends TypedArray<u8> {
return FILL<Uint8Array, u8>(this, value, start, end);
}
sort(comparator: (a: u8, b: u8) => i32 = defaultComparator<u8>()): Uint8Array {
sort(comparator: (a: u8, b: u8) => i32 = COMPARATOR<u8>()): Uint8Array {
return SORT<Uint8Array, u8>(this, comparator);
}
@ -97,7 +97,7 @@ export class Uint8ClampedArray extends Uint8Array {
return changetype<Uint8ClampedArray>(super.fill(value, start, end)); // safe because '.fill' reuses 'this'
}
sort(comparator: (a: u8, b: u8) => i32 = defaultComparator<u8>()): Uint8ClampedArray {
sort(comparator: (a: u8, b: u8) => i32 = COMPARATOR<u8>()): Uint8ClampedArray {
return changetype<Uint8ClampedArray>(super.sort(comparator)); // safe because '.sort' reuses 'this'
}
@ -117,7 +117,7 @@ export class Int16Array extends TypedArray<i16> {
return FILL<Int16Array, i16>(this, value, start, end);
}
sort(comparator: (a: i16, b: i16) => i32 = defaultComparator<i16>()): Int16Array {
sort(comparator: (a: i16, b: i16) => i32 = COMPARATOR<i16>()): Int16Array {
return SORT<Int16Array, i16>(this, comparator);
}
@ -151,7 +151,7 @@ export class Uint16Array extends TypedArray<u16> {
return FILL<Uint16Array, u16>(this, value, start, end);
}
sort(comparator: (a: u16, b: u16) => i32 = defaultComparator<u16>()): Uint16Array {
sort(comparator: (a: u16, b: u16) => i32 = COMPARATOR<u16>()): Uint16Array {
return SORT<Uint16Array, u16>(this, comparator);
}
@ -185,7 +185,7 @@ export class Int32Array extends TypedArray<i32> {
return FILL<Int32Array, i32>(this, value, start, end);
}
sort(comparator: (a: i32, b: i32) => i32 = defaultComparator<i32>()): Int32Array {
sort(comparator: (a: i32, b: i32) => i32 = COMPARATOR<i32>()): Int32Array {
return SORT<Int32Array, i32>(this, comparator);
}
@ -219,7 +219,7 @@ export class Uint32Array extends TypedArray<u32> {
return FILL<Uint32Array, u32>(this, value, start, end);
}
sort(comparator: (a: u32, b: u32) => i32 = defaultComparator<u32>()): Uint32Array {
sort(comparator: (a: u32, b: u32) => i32 = COMPARATOR<u32>()): Uint32Array {
return SORT<Uint32Array, u32>(this, comparator);
}
@ -253,7 +253,7 @@ export class Int64Array extends TypedArray<i64> {
return FILL<Int64Array, i64>(this, value, start, end);
}
sort(comparator: (a: i64, b: i64) => i32 = defaultComparator<i64>()): Int64Array {
sort(comparator: (a: i64, b: i64) => i32 = COMPARATOR<i64>()): Int64Array {
return SORT<Int64Array, i64>(this, comparator);
}
@ -287,7 +287,7 @@ export class Uint64Array extends TypedArray<u64> {
return FILL<Uint64Array, u64>(this, value, start, end);
}
sort(comparator: (a: u64, b: u64) => i32 = defaultComparator<u64>()): Uint64Array {
sort(comparator: (a: u64, b: u64) => i32 = COMPARATOR<u64>()): Uint64Array {
return SORT<Uint64Array, u64>(this, comparator);
}
@ -321,7 +321,7 @@ export class Float32Array extends TypedArray<f32> {
return FILL<Float32Array, f32>(this, value, start, end);
}
sort(comparator: (a: f32, b: f32) => i32 = defaultComparator<f32>()): Float32Array {
sort(comparator: (a: f32, b: f32) => i32 = COMPARATOR<f32>()): Float32Array {
return SORT<Float32Array, f32>(this, comparator);
}
@ -355,7 +355,7 @@ export class Float64Array extends TypedArray<f64> {
return FILL<Float64Array, f64>(this, value, start, end);
}
sort(comparator: (a: f64, b: f64) => i32 = defaultComparator<f64>()): Float64Array {
sort(comparator: (a: f64, b: f64) => i32 = COMPARATOR<f64>()): Float64Array {
return SORT<Float64Array, f64>(this, comparator);
}