refactor, indexof, map/set overloads

This commit is contained in:
dcode
2019-06-17 16:17:27 +02:00
parent 8571df939f
commit f8f2565f60
23 changed files with 1091 additions and 645 deletions

View File

@ -882,7 +882,9 @@ declare namespace v8x16 {
}
/** Macro type evaluating to the underlying native WebAssembly type. */
declare type native<T> = T;
/** Special type evaluating the value type of a collection. */
/** Special type evaluating the indexed access index type. */
declare type indexof<T extends unknown[]> = keyof T;
/** Special type evaluating the indexed access value type. */
declare type valueof<T extends unknown[]> = T[0];
/** Pseudo-class representing the backing class of integer types. */

View File

@ -1,6 +1,7 @@
/// <reference path="./rt/index.d.ts" />
import { HASH } from "./util/hash";
import { E_KEYNOTFOUND } from "util/error";
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
@ -96,11 +97,14 @@ export class Map<K,V> {
return this.find(key, HASH<K>(key)) !== null;
}
@operator("[]")
get(key: K): V {
var entry = this.find(key, HASH<K>(key));
return entry ? entry.value : <V>unreachable();
if (!entry) throw new Error(E_KEYNOTFOUND); // cannot represent `undefined`
return entry.value;
}
@operator("[]=")
set(key: K, value: V): void {
var hashCode = HASH<K>(key);
var entry = this.find(key, hashCode); // unmanaged!

View File

@ -88,6 +88,7 @@ export class Set<T> {
return null;
}
@operator("[]")
has(key: T): bool {
return this.find(key, HASH<T>(key)) !== null;
}
@ -117,6 +118,12 @@ export class Set<T> {
}
}
@operator("[]=")
private __set(key: T, value: bool): void {
if (value) this.add(key);
else this.delete(key);
}
delete(key: T): bool {
var entry = this.find(key, HASH<T>(key)); // unmanaged!
if (!entry) return false;

View File

@ -20,3 +20,7 @@ export const E_HOLEYARRAY: string = "Element type must be nullable if array is h
// @ts-ignore: decorator
@lazy @inline
export const E_NOTIMPLEMENTED: string = "Not implemented";
// @ts-ignore: decorator
@lazy @inline
export const E_KEYNOTFOUND: string = "Key does not exist";

View File

@ -28,7 +28,9 @@ declare type usize = number;
declare type f32 = number;
declare type f64 = number;
/** Special type evaluating the value type of a collection. */
/** Special type evaluating the indexed access index type. */
declare type indexof<T extends unknown[]> = keyof T;
/** Special type evaluating the indexed access value type. */
declare type valueof<T extends unknown[]> = T[0];
// Compiler hints