Filler implementation for std Set

This commit is contained in:
dcodeIO
2018-01-15 00:08:06 +01:00
parent 49d29fc9f2
commit f2ba4b4a76
13 changed files with 7637 additions and 3 deletions

8
std/assembly.d.ts vendored
View File

@ -271,6 +271,14 @@ interface Number {}
interface Object {}
interface RegExp {}
declare class Set<T> {
readonly size: i32;
has(value: T): bool;
add(value: T): void;
delete(value: T): bool;
clear(): void;
}
// Internal decorators
/** Annotates an element as a program global. */

View File

@ -214,7 +214,7 @@ export function set_memory(dest: usize, c: u8, n: usize): void {
// based on musl's implementation of memset
// becomes obsolete once https://github.com/WebAssembly/bulk-memory-operations lands
// fill head and tail wwith minimal branching
// fill head and tail with minimal branching
if (!n)
return;
store<u8>(dest, c);

View File

@ -1,3 +1,60 @@
// const prime1: u32 = 73;
// const prime2: u32 = 5009;
export class Set<T> {
// TODO
private __memory: usize;
private __capacity: u32;
private __size: u32;
constructor() {
this.__memory = 0;
this.__capacity = this.__size = 0;
}
get size(): i32 {
return this.__size;
}
// FIXME: not a proper set implementation, just a filler
has(value: T): bool {
for (var index: usize = 0, limit: usize = this.__size; index < limit; ++index)
if (load<T>(this.__memory + index * sizeof<T>()) == value)
return true;
return false;
}
add(value: T): Set<T> {
if (this.__size >= this.__capacity) {
var newCapacity = max(this.__capacity << 1, 8);
var newMemory = allocate_memory(<usize>newCapacity * sizeof<T>());
if (this.__memory) {
move_memory(newMemory, this.__memory, <usize>this.__capacity * sizeof<T>());
free_memory(this.__memory);
}
this.__capacity = newCapacity;
this.__memory = newMemory;
}
store<T>(this.__memory + <usize>this.__size * sizeof<T>(), value);
++this.__size;
return this;
}
delete(value: T): bool {
for (var index: usize = 0, limit: usize = this.__size; index < limit; ++index)
if (load<T>(this.__memory + index * sizeof<T>()) == value) {
if (index + 1 < this.__size)
move_memory(this.__memory + index * sizeof<T>(), this.__memory + (index + 1) * sizeof<T>(), this.__size - index - 1);
--this.__size;
return true;
}
return false;
}
clear(): void {
this.__size = 0;
}
// TODO: think about iterators
}

4
std/portable.d.ts vendored
View File

@ -214,14 +214,16 @@ declare class Symbol {
declare class Set<T> {
constructor(entries?: T[]);
add(value: T): void;
has(value: T): bool;
add(value: T): void;
delete(value: T): bool;
clear(): void;
[Symbol.iterator](): Iterator<T>;
}
declare class Map<K,V> {
constructor(entries?: [K, V][]);
readonly size: i32;
set(key: K, value: V): void;
has(key: K): bool;
get(key: K): V | null;