Simple bucket array

This commit is contained in:
Krzosa Karol
2022-06-24 11:55:43 +02:00
parent 37750908aa
commit ee2410cb32

View File

@@ -169,10 +169,91 @@ want to export all the symbols, we can namespace them optionally.
#include "typechecking.cpp"
#include "c_language_codegen.cpp"
#include "bytecode_interpreter.cpp"
#include "bytecode_codegen.cpp"
// #include "bytecode_interpreter.cpp"
// #include "bytecode_codegen.cpp"
template<class T, S64 bucket_size>
struct Simple_Bucket_Array{
struct Bucket{ Bucket *next; T data[bucket_size]; S64 len; };
Bucket *first;
Bucket *last ;
void grow(Allocator *allocator){
if(!first){
Bucket *bucket = exp_alloc_type(allocator, Bucket);
bucket->next = 0; bucket->len = 0;
first = last = bucket;
}
if(last->len >= bucket_size){
Bucket *bucket = exp_alloc_type(allocator, Bucket);
bucket->next = 0; bucket->len = 0;
last = last->next = bucket;
}
}
void add(Allocator *allocator, T item){
grow(allocator);
last->data[last->len++] = item;
}
struct Iter{
Simple_Bucket_Array *ref;
Bucket *bucket;
T *it;
S64 iter_i;
S64 i;
force_inline void next(){
if(iter_i >= bucket->len){
if(bucket->next){
bucket = bucket->next;
iter_i = 0;
}
else{
it = 0;
return;
}
}
it = bucket->data + iter_i++;
i += 1;
}
force_inline B32 should_continue(){ return it != 0; }
};
force_inline Iter iter(){
if(!this->first || this->first->len == 0) return {};
return {this, this->first, this->first->data, 1};
}
};
function void
test_bucket_arrays(){
Scratch scratch;
Simple_Bucket_Array<int, 4> a = {};
for(int i = 0; i < 32; i++)
a.add(scratch, i);
For_It(a){
assert(*it.it == it.i);
}
for(auto i = a.iter(); i.should_continue(); i.next()){
assert(*i.it == i.i);
}
int total_i = 0;
for(auto bucket = a.first; bucket; bucket=bucket->next){
for(int i = 0; i < bucket->len; i++){
assert(bucket->data[i] == total_i++);
}
}
}
int main(int argument_count, char **arguments){
#if OS_WINDOWS
// Set output mode to handle virtual terminal sequences
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -199,6 +280,7 @@ int main(int argument_count, char **arguments){
test_array();
test_string_builder();
test_intern_table();
test_bucket_arrays();
emit_line_directives = true;
String program_name = "vm.kl"_s;
@@ -232,8 +314,8 @@ int main(int argument_count, char **arguments){
arena_clear(&pctx->stage_arena);
compile_to_bc();
__debugbreak();
// compile_to_bc();
// __debugbreak();
String result = get_compilation_result();
assert(os_write_file("program.c"_s, result));
{