Update examples with QoL parsing additions
This commit is contained in:
parent
2f67a6f109
commit
b21ce51435
@ -11,7 +11,7 @@ record ListDLU[T, S; growth] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ListDLU_remove: [T, S; growth]u0(ListDLU[T, S; growth]* this, S index) -> {
|
ListDLU_remove: [T, S; growth]u0(ListDLU[T, S; growth]* this, S index) -> {
|
||||||
T* data0 = &((*((*this).data))[index]);
|
T* data0 = &(*(*this).data)[index];
|
||||||
T* data1 = data0 + @sizeof T;
|
T* data1 = data0 + @sizeof T;
|
||||||
S sz = (*this).size;
|
S sz = (*this).size;
|
||||||
(*this).size = (*this).size - 1;
|
(*this).size = (*this).size - 1;
|
||||||
@ -33,7 +33,7 @@ ListDLU_add: [T, S; growth]u0(ListDLU[T, S; growth]* this, Alloc *alloc, T value
|
|||||||
if((*this).size == (*this).capacity) {
|
if((*this).size == (*this).capacity) {
|
||||||
u32 newcap = (*this).capacity + growth;
|
u32 newcap = (*this).capacity + growth;
|
||||||
(*this).capacity = newcap;
|
(*this).capacity = newcap;
|
||||||
(*this).data = ((*alloc).realloc)((*alloc).userdata, (*this).data, newcap * @sizeof T);
|
(*this).data = (*alloc).realloc((*alloc).userdata, (*this).data, newcap * @sizeof T);
|
||||||
}
|
}
|
||||||
|
|
||||||
(*((*this).data))[(*this).size] = value;
|
(*((*this).data))[(*this).size] = value;
|
||||||
|
@ -1,31 +1,77 @@
|
|||||||
/*
|
/*
|
||||||
* SOaLDhS: Statically-allocated, Open-addressing, Linear probing, Dynamic hash, sentinels
|
* SIOaLpDhFt: Statically-allocated, Interleaved KV values, Open-addressing, Linear probing, Dynamic hash, Flag tombstones
|
||||||
*/
|
*/
|
||||||
|
|
||||||
record KVPair[K, V] {
|
record KVPair[K, V] {
|
||||||
K key;
|
K key;
|
||||||
V value;
|
V value;
|
||||||
}
|
}
|
||||||
|
|
||||||
record MapSOaLDhS[K, V, S; capacity, sentinel] {
|
record MapSIOaLpDhFt[K, V, S; capacity] {
|
||||||
S(K*) hash;
|
S(K*)* hash;
|
||||||
KVPair[capacity] data;
|
KVPair[capacity] data;
|
||||||
|
u8[capacity] occupied;
|
||||||
}
|
}
|
||||||
|
|
||||||
MapSOaLDhS_add: [K, V, S; capacity, sentinel]u1(MapSOaLDhS[K, V, S; capacity, sentinel]* this, K* key, V* value) -> {
|
MapSIOaLpDhFt_add: [K, V, S; capacity]u1(MapSIOaLpDhFt[K, V, S; capacity]* this, K* key, V* value) -> {
|
||||||
S index = ((*this).hash)(value);
|
S start = this.hash(key);
|
||||||
index = index & (capacity - 1);
|
start = start & (capacity - 1);
|
||||||
|
|
||||||
|
S index = start;
|
||||||
loop {
|
loop {
|
||||||
KVPair[K, V]* pair = &((*this).data[index]);
|
KVPair[K, V]* pair = &this.data[index];
|
||||||
if(((*pair).key == *key) || ((*pair).value == sentinel)) {
|
if(pair.key == *key || this.occupied[index] == 0) {
|
||||||
(*pair).key = *key;
|
pair.key = *key;
|
||||||
(*pair).value = *value;
|
pair.value = *value;
|
||||||
}
|
this.occupied[index] = 1;
|
||||||
index = index + 1;
|
|
||||||
}
|
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
index = (index + 1) & (capacity - 1);
|
||||||
|
if(index == start) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@instantiate MapSOaLDhS_add[ugpr, ugpr, ugpr; 128, 0];
|
MapSIOaLpDhFt_get: [K, V, S; capacity]V*(MapSIOaLpDhFt[K, V, S; capacity]* this, K* key) -> {
|
||||||
|
S start = this.hash(key);
|
||||||
|
start = start & (capacity - 1);
|
||||||
|
|
||||||
MapSOaLDhS[ugpr, ugpr, ugpr; 128, 0] map;
|
S index = start;
|
||||||
|
loop {
|
||||||
|
KVPair[K, V]* pair = &this.data[index];
|
||||||
|
if(pair.key == *key) {
|
||||||
|
if(this.occupied[index] == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return &pair.value;
|
||||||
|
}
|
||||||
|
index = (index + 1) & (capacity - 1);
|
||||||
|
if(index == start) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
zero_hash: ugpr(ugpr* val) -> {
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
@instantiate MapSIOaLpDhFt_add[ugpr, ugpr, ugpr; 32];
|
||||||
|
@instantiate MapSIOaLpDhFt_get[ugpr, ugpr, ugpr; 32];
|
||||||
|
|
||||||
|
main: u0() -> {
|
||||||
|
map.hash = &zero_hash;
|
||||||
|
|
||||||
|
MapSIOaLpDhFt_get[ugpr, ugpr, ugpr; 32](&map, &test_key);
|
||||||
|
MapSIOaLpDhFt_add[ugpr, ugpr, ugpr; 32](&map, &test_key, &test_value);
|
||||||
|
MapSIOaLpDhFt_get[ugpr, ugpr, ugpr; 32](&map, &test_key);
|
||||||
|
};
|
||||||
|
|
||||||
|
loop {}
|
||||||
|
|
||||||
|
@section(".data");
|
||||||
|
MapSIOaLpDhFt[ugpr, ugpr, ugpr; 32] map:;
|
||||||
|
|
||||||
|
ugpr test_value: 10;
|
||||||
|
ugpr test_key: 5;
|
Loading…
Reference in New Issue
Block a user