diff --git a/examples/ListDLU.nct b/examples/ListDLU.nct index d23ae88..0fdf457 100644 --- a/examples/ListDLU.nct +++ b/examples/ListDLU.nct @@ -11,7 +11,7 @@ record ListDLU[T, S; growth] { } 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; S sz = (*this).size; (*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) { u32 newcap = (*this).capacity + growth; (*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; diff --git a/examples/MapSOaLDhS.nct b/examples/MapSOaLDhS.nct index f42af7f..d21a34f 100644 --- a/examples/MapSOaLDhS.nct +++ b/examples/MapSOaLDhS.nct @@ -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] { K key; V value; } -record MapSOaLDhS[K, V, S; capacity, sentinel] { - S(K*) hash; +record MapSIOaLpDhFt[K, V, S; capacity] { + S(K*)* hash; 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) -> { - S index = ((*this).hash)(value); - index = index & (capacity - 1); +MapSIOaLpDhFt_add: [K, V, S; capacity]u1(MapSIOaLpDhFt[K, V, S; capacity]* this, K* key, V* value) -> { + S start = this.hash(key); + start = start & (capacity - 1); + + S index = start; loop { - KVPair[K, V]* pair = &((*this).data[index]); - if(((*pair).key == *key) || ((*pair).value == sentinel)) { - (*pair).key = *key; - (*pair).value = *value; + KVPair[K, V]* pair = &this.data[index]; + if(pair.key == *key || this.occupied[index] == 0) { + pair.key = *key; + pair.value = *value; + this.occupied[index] = 1; + return 1; + } + index = (index + 1) & (capacity - 1); + if(index == start) { + return 0; } - index = index + 1; } - return 1; }; -@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); + + 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; + } + } +}; -MapSOaLDhS[ugpr, ugpr, ugpr; 128, 0] map; +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; \ No newline at end of file