if that's what's necessary

This commit is contained in:
dcode
2019-03-14 06:09:49 +01:00
parent a5e14a0eaa
commit 84ddd97761
25 changed files with 1845 additions and 769 deletions

View File

@ -1,15 +1,19 @@
/** Number of alignment bits. */
// @ts-ignore: decorator
@inline export const AL_BITS: u32 = 3;
@inline
export const AL_BITS: u32 = 3;
/** Number of possible alignment values. */
// @ts-ignore: decorator
@inline export const AL_SIZE: usize = 1 << <usize>AL_BITS;
@inline
export const AL_SIZE: usize = 1 << <usize>AL_BITS;
/** Mask to obtain just the alignment bits. */
// @ts-ignore: decorator
@inline export const AL_MASK: usize = AL_SIZE - 1;
@inline
export const AL_MASK: usize = AL_SIZE - 1;
/** Maximum 32-bit allocation size. */
// @ts-ignore: decorator
@inline export const MAX_SIZE_32: usize = 1 << 30; // 1GB
@inline
export const MAX_SIZE_32: usize = 1 << 30; // 1GB

View File

@ -1,35 +1,32 @@
// @ts-ignore: decorator
@inline export function HASH<T>(key: T): u32 {
@inline
export function HASH<T>(key: T): u32 {
if (isString(key)) {
// @ts-ignore: type
return hashStr(key);
return hashStr(changetype<string>(key));
} else if (isReference<T>()) {
if (sizeof<T>() == 4) return hash32(changetype<u32>(key));
if (sizeof<T>() == 8) return hash64(changetype<u64>(key));
} else if (isFloat<T>()) {
// @ts-ignore: type
if (sizeof<T>() == 4) return hash32(reinterpret<u32>(key));
// @ts-ignore: type
if (sizeof<T>() == 8) return hash64(reinterpret<u64>(key));
if (sizeof<T>() == 4) return hash32(reinterpret<u32>(f32(key)));
if (sizeof<T>() == 8) return hash64(reinterpret<u64>(f64(key)));
} else {
// @ts-ignore: type
if (sizeof<T>() == 1) return hash8 (<u32>key);
// @ts-ignore: type
if (sizeof<T>() == 2) return hash16(<u32>key);
// @ts-ignore: type
if (sizeof<T>() == 4) return hash32(<u32>key);
// @ts-ignore: type
if (sizeof<T>() == 8) return hash64(<u64>key);
if (sizeof<T>() == 1) return hash8 (u32(key));
if (sizeof<T>() == 2) return hash16(u32(key));
if (sizeof<T>() == 4) return hash32(u32(key));
if (sizeof<T>() == 8) return hash64(u64(key));
}
unreachable();
return unreachable();
}
// FNV-1a 32-bit as a starting point, see: http://isthe.com/chongo/tech/comp/fnv/
// @ts-ignore: decorator
@inline const FNV_OFFSET: u32 = 2166136261;
@inline
const FNV_OFFSET: u32 = 2166136261;
// @ts-ignore: decorator
@inline const FNV_PRIME: u32 = 16777619;
@inline
const FNV_PRIME: u32 = 16777619;
function hash8(key: u32): u32 {
return (FNV_OFFSET ^ key) * FNV_PRIME;

View File

@ -2,10 +2,12 @@ import { runtime } from "../runtime";
import { CharCode } from "./string";
// @ts-ignore: decorator
@inline export const MAX_DOUBLE_LENGTH = 28;
@inline
export const MAX_DOUBLE_LENGTH = 28;
// @ts-ignore: decorator
@lazy @inline const POWERS10: u32[] = [
@lazy @inline
const POWERS10: u32[] = [
1,
10,
100,
@ -33,7 +35,8 @@ import { CharCode } from "./string";
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99"
*/
// @ts-ignore: decorator
@lazy @inline const DIGITS: u32[] = [
@lazy @inline
const DIGITS: u32[] = [
0x00300030, 0x00310030, 0x00320030, 0x00330030, 0x00340030,
0x00350030, 0x00360030, 0x00370030, 0x00380030, 0x00390030,
0x00300031, 0x00310031, 0x00320031, 0x00330031, 0x00340031,
@ -57,7 +60,8 @@ import { CharCode } from "./string";
];
// @ts-ignore: decorator
@lazy @inline const EXP_POWERS: i16[] = [
@lazy @inline
const EXP_POWERS: i16[] = [
-1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980,
-954, -927, -901, -874, -847, -821, -794, -768, -741, -715,
-688, -661, -635, -608, -582, -555, -529, -502, -475, -449,
@ -71,7 +75,8 @@ import { CharCode } from "./string";
// 1e-348, 1e-340, ..., 1e340
// @ts-ignore: decorator
@lazy @inline const FRC_POWERS: u64[] = [
@lazy @inline
const FRC_POWERS: u64[] = [
0xFA8FD5A0081C0288, 0xBAAEE17FA23EBF76, 0x8B16FB203055AC76, 0xCF42894A5DCE35EA,
0x9A6BB0AA55653B2D, 0xE61ACF033D1A45DF, 0xAB70FE17C79AC6CA, 0xFF77B1FCBEBCDC4F,
0xBE5691EF416BD60C, 0x8DD01FAD907FFC3C, 0xD3515C2831559A83, 0x9D71AC8FADA6C9B5,
@ -105,7 +110,7 @@ export function decimalCount32(value: u32): u32 {
let lutbuf = <ArrayBuffer>POWERS10.buffer_;
let power = LOAD<u32>(lutbuf, t);
t -= <u32>(value < power);
t -= u32(value < power);
return t + 1;
} else {
if (value < 100000) {
@ -135,7 +140,7 @@ export function decimalCount64(value: u64): u32 {
let lutbuf = <ArrayBuffer>POWERS10.buffer_;
let power = LOAD<u32,u64>(lutbuf, t - 10);
t -= <u32>(value < 10000000000 * power);
t -= u32(value < 10000000000 * power);
return t + 1;
} else {
if (value < 1000000000000000) {

View File

@ -1,60 +1,50 @@
import { compareImpl } from "./string";
// @ts-ignore: decorator
@inline export function COMPARATOR<T>(): (a: T, b: T) => i32 {
@inline
export function COMPARATOR<T>(): (a: T, b: T) => i32 {
if (isInteger<T>()) {
if (isSigned<T>() && sizeof<T>() <= 4) {
// @ts-ignore: type
return (a: T, b: T): i32 => (<i32>(a - b));
return (a: T, b: T): i32 => (i32(a) - i32(b));
} else {
// @ts-ignore: type
return (a: T, b: T): i32 => (<i32>(a > b) - <i32>(a < b));
return (a: T, b: T): i32 => (i32(a > b) - i32(a < b));
}
} else if (isFloat<T>()) {
if (sizeof<T>() == 4) {
return (a: T, b: T): i32 => {
// @ts-ignore: type
var ia = reinterpret<i32>(a);
// @ts-ignore: type
var ib = reinterpret<i32>(b);
var ia = reinterpret<i32>(f32(a));
var ib = reinterpret<i32>(f32(b));
ia ^= (ia >> 31) >>> 1;
ib ^= (ib >> 31) >>> 1;
// @ts-ignore: type
return <i32>(ia > ib) - <i32>(ia < ib);
return i32(ia > ib) - i32(ia < ib);
};
} else {
return (a: T, b: T): i32 => {
// @ts-ignore: type
var ia = reinterpret<i64>(a);
// @ts-ignore: type
var ib = reinterpret<i64>(b);
var ia = reinterpret<i64>(f64(a));
var ib = reinterpret<i64>(f64(b));
ia ^= (ia >> 63) >>> 1;
ib ^= (ib >> 63) >>> 1;
// @ts-ignore: type
return <i32>(ia > ib) - <i32>(ia < ib);
return i32(ia > ib) - i32(ia < ib);
};
}
} else if (isString<T>()) {
return (a: T, b: T): i32 => {
if (a === b || a === null || b === null) return 0;
// @ts-ignore: type
var alen = (<string>a).length;
// @ts-ignore: type
var blen = (<string>b).length;
var alen = changetype<string>(a).length;
var blen = changetype<string>(b).length;
if (!alen && !blen) return 0;
if (!alen) return -1;
if (!blen) return 1;
// @ts-ignore: type
return compareImpl(<string>a, 0, <string>b, 0, <usize>min(alen, blen));
return compareImpl(changetype<string>(a), 0, changetype<string>(b), 0, <usize>min(alen, blen));
};
} else {
// @ts-ignore: type
return (a: T, b: T): i32 => (<i32>(a > b) - <i32>(a < b));
return (a: T, b: T): i32 => (i32(a > b) - i32(a < b));
}
}
// @ts-ignore: decorator
@inline export function SORT<T>(
@inline
export function SORT<T>(
dataStart: usize,
length: i32,
comparator: (a: T, b: T) => i32

View File

@ -9,7 +9,8 @@ export function compareImpl(str1: string, index1: usize, str2: string, index2: u
}
// @ts-ignore: decorator
@inline export const enum CharCode {
@inline
export const enum CharCode {
PLUS = 0x2B,
MINUS = 0x2D,
DOT = 0x2E,
@ -58,7 +59,7 @@ export function isWhiteSpaceOrLineTerminator(c: u16): bool {
/** Parses a string to an integer (usually), using the specified radix. */
export function parse<T>(str: string, radix: i32 = 0): T {
var len: i32 = str.length;
// @ts-ignore: type
// @ts-ignore: cast
if (!len) return <T>NaN;
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
@ -67,13 +68,13 @@ export function parse<T>(str: string, radix: i32 = 0): T {
// determine sign
var sign: T;
if (code == CharCode.MINUS) {
// @ts-ignore: type
// @ts-ignore: cast
if (!--len) return <T>NaN;
code = <i32>load<u16>(ptr += 2);
// @ts-ignore: type
sign = -1;
} else if (code == CharCode.PLUS) {
// @ts-ignore: type
// @ts-ignore: cast
if (!--len) return <T>NaN;
code = <i32>load<u16>(ptr += 2);
// @ts-ignore: type