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 {
type* data;
u32 length;
u32 next_index;
u32 count;
void alloc(u32 initial_capacity = 16) {
data = (type*)malloc(initial_capacity * sizeof(type));
next_index = 0;
count = 0;
length = initial_capacity;
}
@@ -21,16 +21,16 @@ struct Array_List {
}
void clear() {
next_index = 0;
count = 0;
}
Array_List<type> clone() {
Array_List<type> ret;
ret.length = length;
ret.next_index = next_index;
ret.count = count;
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];
}
return ret;
@@ -41,24 +41,24 @@ struct Array_List {
}
type* end() {
return data+(next_index);
return data+(count);
}
void remove_index(u32 index) {
data[index] = data[--next_index];
data[index] = data[--count];
}
void append(type element) {
if (next_index == length) {
if (count == length) {
length *= 2;
data = (type*)realloc(data, length * sizeof(type));
}
data[next_index] = element;
next_index++;
data[count] = element;
count++;
}
void reserve(u32 count) {
if (next_index+count >= (u32)length) {
void reserve(u32 amount) {
if (count+amount >= (u32)length) {
length *= 2;
data = (type*)realloc(data, length * sizeof(type));
}
@@ -112,9 +112,9 @@ struct Array_List {
void sort(s32 left=-1, s32 right=-1) {
if (left == -1) {
if (next_index == 0)
if (count == 0)
return;
sort(0, next_index - 1);
sort(0, count - 1);
return;
} else if (left == right) {
return;
@@ -130,7 +130,7 @@ struct Array_List {
u32 sorted_find(type elem, s32 left=-1, s32 right=-1) {
if (left == -1) {
return sorted_find(elem, 0, next_index - 1);
return sorted_find(elem, 0, count - 1);
} else if (left == right) {
if ((size_t)data[left] == (size_t)elem)
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,
u32 c_file_line, String type, const char* format, ...) -> void {
error = new Error;
error->type = type;
error = (Error*) malloc(sizeof(Error));
va_list args;
va_start(args, format);
error->message.length = print_va_args_to_string(&(error->message.data), format, &args);
va_end(args);
error->type = type;
print("\n%{color<}%{->Str} error:%{>color} %{->Str}\n",
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) {
// FIXME(Felix): Why can I not call Array_List::append here??? Hallo?
if (next_index == length) {
if (count == length) {
length *= 2;
data = (Lambda<void()>*)realloc(data, length * sizeof(Lambda<void()>));
}
data[next_index] = f;
next_index++;
data[count] = f;
count++;
}
void operator()() {
while(next_index --> 0) {
while(count --> 0) {
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) {
--color_stack.next_index;
if (color_stack.next_index == 0) {
--color_stack.count;
if (color_stack.count == 0) {
return print_to_file(f, "%s", console_normal);
} 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]);
}
}
+2 -12
View File
@@ -49,18 +49,7 @@ s32 main(s32 argc, char* argv[]) {
// test_printer();
init_printer();
// create_generic_error("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");
create_error(__FUNCTION__, __FILE__, __LINE__,
make_heap_string("generic"),
"nothing to lex was found:\n"
create_generic_error("nothing to lex was found:\n"
" in %{color<}%{->char}%{>color}\n"
" at %{color<}%{->char}%{>color}\n"
"bottom text\n",
@@ -68,5 +57,6 @@ s32 main(s32 argc, char* argv[]) {
"some file name",
console_cyan,
"yesssssss");
return 0;
}