mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-18 17:31:29 +00:00
refactor, indexof, map/set overloads
This commit is contained in:
4
std/assembly/index.d.ts
vendored
4
std/assembly/index.d.ts
vendored
@ -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. */
|
||||
|
@ -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!
|
||||
|
@ -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;
|
||||
|
@ -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";
|
||||
|
4
std/portable/index.d.ts
vendored
4
std/portable/index.d.ts
vendored
@ -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
|
||||
|
Reference in New Issue
Block a user