Simple bucket array
This commit is contained in:
90
main.cpp
90
main.cpp
@@ -169,10 +169,91 @@ want to export all the symbols, we can namespace them optionally.
|
|||||||
#include "typechecking.cpp"
|
#include "typechecking.cpp"
|
||||||
|
|
||||||
#include "c_language_codegen.cpp"
|
#include "c_language_codegen.cpp"
|
||||||
#include "bytecode_interpreter.cpp"
|
// #include "bytecode_interpreter.cpp"
|
||||||
#include "bytecode_codegen.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){
|
int main(int argument_count, char **arguments){
|
||||||
|
|
||||||
#if OS_WINDOWS
|
#if OS_WINDOWS
|
||||||
// Set output mode to handle virtual terminal sequences
|
// Set output mode to handle virtual terminal sequences
|
||||||
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
@@ -199,6 +280,7 @@ int main(int argument_count, char **arguments){
|
|||||||
test_array();
|
test_array();
|
||||||
test_string_builder();
|
test_string_builder();
|
||||||
test_intern_table();
|
test_intern_table();
|
||||||
|
test_bucket_arrays();
|
||||||
|
|
||||||
emit_line_directives = true;
|
emit_line_directives = true;
|
||||||
String program_name = "vm.kl"_s;
|
String program_name = "vm.kl"_s;
|
||||||
@@ -232,8 +314,8 @@ int main(int argument_count, char **arguments){
|
|||||||
|
|
||||||
|
|
||||||
arena_clear(&pctx->stage_arena);
|
arena_clear(&pctx->stage_arena);
|
||||||
compile_to_bc();
|
// compile_to_bc();
|
||||||
__debugbreak();
|
// __debugbreak();
|
||||||
String result = get_compilation_result();
|
String result = get_compilation_result();
|
||||||
assert(os_write_file("program.c"_s, result));
|
assert(os_write_file("program.c"_s, result));
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user