Updated Array_List: next_index -> count

This commit is contained in:
Felix Brendel
2020-11-05 14:37:16 +01:00
parent ae09f3b78e
commit 1a09ed780c
5 changed files with 34 additions and 44 deletions
+15 -15
View File
@@ -7,11 +7,11 @@ template <typename type>
struct Array_List { struct Array_List {
type* data; type* data;
u32 length; u32 length;
u32 next_index; u32 count;
void alloc(u32 initial_capacity = 16) { void alloc(u32 initial_capacity = 16) {
data = (type*)malloc(initial_capacity * sizeof(type)); data = (type*)malloc(initial_capacity * sizeof(type));
next_index = 0; count = 0;
length = initial_capacity; length = initial_capacity;
} }
@@ -21,16 +21,16 @@ struct Array_List {
} }
void clear() { void clear() {
next_index = 0; count = 0;
} }
Array_List<type> clone() { Array_List<type> clone() {
Array_List<type> ret; Array_List<type> ret;
ret.length = length; ret.length = length;
ret.next_index = next_index; ret.count = count;
ret.data = (type*)malloc(length * sizeof(type)); ret.data = (type*)malloc(length * sizeof(type));
for (u32 i = 0; i < next_index; ++i) { for (u32 i = 0; i < count; ++i) {
ret.data[i] = data[i]; ret.data[i] = data[i];
} }
return ret; return ret;
@@ -41,24 +41,24 @@ struct Array_List {
} }
type* end() { type* end() {
return data+(next_index); return data+(count);
} }
void remove_index(u32 index) { void remove_index(u32 index) {
data[index] = data[--next_index]; data[index] = data[--count];
} }
void append(type element) { void append(type element) {
if (next_index == length) { if (count == length) {
length *= 2; length *= 2;
data = (type*)realloc(data, length * sizeof(type)); data = (type*)realloc(data, length * sizeof(type));
} }
data[next_index] = element; data[count] = element;
next_index++; count++;
} }
void reserve(u32 count) { void reserve(u32 amount) {
if (next_index+count >= (u32)length) { if (count+amount >= (u32)length) {
length *= 2; length *= 2;
data = (type*)realloc(data, length * sizeof(type)); data = (type*)realloc(data, length * sizeof(type));
} }
@@ -112,9 +112,9 @@ struct Array_List {
void sort(s32 left=-1, s32 right=-1) { void sort(s32 left=-1, s32 right=-1) {
if (left == -1) { if (left == -1) {
if (next_index == 0) if (count == 0)
return; return;
sort(0, next_index - 1); sort(0, count - 1);
return; return;
} else if (left == right) { } else if (left == right) {
return; return;
@@ -130,7 +130,7 @@ struct Array_List {
u32 sorted_find(type elem, s32 left=-1, s32 right=-1) { u32 sorted_find(type elem, s32 left=-1, s32 right=-1) {
if (left == -1) { if (left == -1) {
return sorted_find(elem, 0, next_index - 1); return sorted_find(elem, 0, count - 1);
} else if (left == right) { } else if (left == right) {
if ((size_t)data[left] == (size_t)elem) if ((size_t)data[left] == (size_t)elem)
return left; return left;
+2 -2
View File
@@ -21,14 +21,14 @@ auto delete_error() -> void {
auto create_error(const char* c_func_name, const char* c_file_name, auto create_error(const char* c_func_name, const char* c_file_name,
u32 c_file_line, String type, const char* format, ...) -> void { u32 c_file_line, String type, const char* format, ...) -> void {
error = new Error; error = (Error*) malloc(sizeof(Error));
error->type = type;
va_list args; va_list args;
va_start(args, format); va_start(args, format);
error->message.length = print_va_args_to_string(&(error->message.data), format, &args); error->message.length = print_va_args_to_string(&(error->message.data), format, &args);
va_end(args); va_end(args);
error->type = type;
print("\n%{color<}%{->Str} error:%{>color} %{->Str}\n", print("\n%{color<}%{->Str} error:%{>color} %{->Str}\n",
console_red, &(error->type), &(error->message)); console_red, &(error->type), &(error->message));
+6 -6
View File
@@ -59,19 +59,19 @@ struct Hook : Array_List<Lambda<void()>> {
} }
void operator<<(Lambda<void()> f) { void operator<<(Lambda<void()> f) {
// FIXME(Felix): Why can I not call Array_List::append here??? Hallo? // FIXME(Felix): Why can I not call Array_List::append here??? Hallo?
if (next_index == length) { if (count == length) {
length *= 2; length *= 2;
data = (Lambda<void()>*)realloc(data, length * sizeof(Lambda<void()>)); data = (Lambda<void()>*)realloc(data, length * sizeof(Lambda<void()>));
} }
data[next_index] = f; data[count] = f;
next_index++; count++;
} }
void operator()() { void operator()() {
while(next_index --> 0) { while(count --> 0) {
fflush(stdout); fflush(stdout);
data[next_index](); data[count]();
} }
next_index = 0; count = 0;
} }
}; };
+3 -3
View File
@@ -415,11 +415,11 @@ int print_color_start(FILE* f, char* str) {
} }
int print_color_end(FILE* f) { int print_color_end(FILE* f) {
--color_stack.next_index; --color_stack.count;
if (color_stack.next_index == 0) { if (color_stack.count == 0) {
return print_to_file(f, "%s", console_normal); return print_to_file(f, "%s", console_normal);
} else { } else {
return print_to_file(f, "%s", color_stack[color_stack.next_index-1]); return print_to_file(f, "%s", color_stack[color_stack.count-1]);
} }
} }
+8 -18
View File
@@ -49,24 +49,14 @@ s32 main(s32 argc, char* argv[]) {
// test_printer(); // test_printer();
init_printer(); init_printer();
// create_generic_error("nothing to lex was found:\n" create_generic_error("nothing to lex was found:\n"
// " in %{color<}%{->char}%{>color}\n" " in %{color<}%{->char}%{>color}\n"
// " at %{color<}%{->char}%{>color}\n" " at %{color<}%{->char}%{>color}\n"
// "bottom text\n", "bottom text\n",
// console_green, console_green,
// "some file name", "some file name",
// console_cyan, console_cyan,
// "yesssssss"); "yesssssss");
create_error(__FUNCTION__, __FILE__, __LINE__,
make_heap_string("generic"),
"nothing to lex was found:\n"
" in %{color<}%{->char}%{>color}\n"
" at %{color<}%{->char}%{>color}\n"
"bottom text\n",
console_green,
"some file name",
console_cyan,
"yesssssss");
return 0; return 0;
} }