Local Variables 22
This commit is contained in:
parent
4f2211eb72
commit
72a41e81e6
18 changed files with 1335 additions and 115 deletions
|
|
@ -10,7 +10,7 @@ import clox.memory;
|
|||
enum TABLE_MAX_LOAD = 0.75;
|
||||
|
||||
struct Table{
|
||||
uint count, capacity;
|
||||
size_t count, capacity;
|
||||
Entry* entries;
|
||||
struct Entry{
|
||||
Obj.String* key;
|
||||
|
|
@ -25,7 +25,7 @@ struct Table{
|
|||
FREE_ARRAY!Entry(entries, capacity);
|
||||
initialise();
|
||||
}
|
||||
private void adjustCapacity(uint cap){
|
||||
private void adjustCapacity(size_t cap){
|
||||
Entry* ent = ALLOCATE!Entry(cap);
|
||||
for(int i = 0; i < cap; i++){
|
||||
ent[i].key = null;
|
||||
|
|
@ -46,7 +46,7 @@ struct Table{
|
|||
entries = ent;
|
||||
capacity = cap;
|
||||
}
|
||||
private Entry* findEntry(Entry* entries, int capacity, in Obj.String* key){
|
||||
private Entry* findEntry(Entry* entries, size_t capacity, in Obj.String* key){
|
||||
uint index = key.hash % capacity;
|
||||
Entry* tombstone = null;
|
||||
while(true){
|
||||
|
|
@ -66,7 +66,7 @@ struct Table{
|
|||
}
|
||||
bool set(Obj.String* key, Value value){
|
||||
if(count + 1 > capacity * TABLE_MAX_LOAD){
|
||||
uint cap = GROW_CAPACITY(capacity);
|
||||
size_t cap = GROW_CAPACITY(capacity);
|
||||
adjustCapacity(cap);
|
||||
}
|
||||
Entry* entry = findEntry(entries, capacity, key);
|
||||
|
|
@ -121,12 +121,13 @@ struct Table{
|
|||
}
|
||||
|
||||
unittest{
|
||||
import clox.vm : vm;
|
||||
Table tbl;
|
||||
tbl.initialise();
|
||||
scope(exit)
|
||||
scope(exit){
|
||||
tbl.free();
|
||||
scope(exit)
|
||||
freeObjects();
|
||||
vm.free();
|
||||
}
|
||||
|
||||
assert(tbl.count == 0);
|
||||
|
||||
|
|
@ -146,7 +147,7 @@ unittest{
|
|||
assert(tbl.count == 1);
|
||||
|
||||
foreach(i; 0..25){
|
||||
Obj.String* str2 = Obj.String.copy("hello");
|
||||
Obj.String* str2 = Obj.String.copy("hi 2");
|
||||
tbl.set(str2, Value.from(i));
|
||||
assert(tbl.get(str2, val));
|
||||
assert(val.asNumber == i);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue