mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-18 17:31:29 +00:00
Filler implementation for std Set
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user