author | Alan Dipert
<alan@dipert.org> 2024-04-13 03:59:31 UTC |
committer | Alan Dipert
<alan@dipert.org> 2024-04-13 03:59:31 UTC |
parent | f37401197f1ec1208c474deceb80e2ddd855d0ed |
lines.c | +14 | -71 |
lines.h | +1 | -2 |
main.c | +5 | -3 |
diff --git a/lines.c b/lines.c index d06ad4f..279e395 100644 --- a/lines.c +++ b/lines.c @@ -7,79 +7,22 @@ #include "lines.h" -struct node_tag **lines_make() { - struct node_tag** lines = vector_create(); - return lines; -} - -void lines_add(struct node_tag **lines, struct node_tag *line) { - vector_add(&lines, line); +void lines_insert(struct node_tag ***lines, struct node_tag *line) { + size_t low = 0, high = vector_size(*lines); + while (low < high) { + size_t mid = low + (high - low) / 2; + if ((*lines)[mid]->data.line.linum < line->data.line.linum) { + low = mid + 1; + } else { + high = mid; + } + } + vector_insert(lines, low, line); } void lines_print(struct node_tag **lines) { printf("Size = %zu\n", vector_size(lines)); + for (size_t i = 0; i < vector_size(lines); i++) { + printf("Line %g\n", lines[i]->data.line.linum); + } } - -/* #include <stdio.h> */ -/* #include <stdlib.h> */ - -/* typedef struct { */ -/* double* array; */ -/* size_t used; */ -/* size_t size; */ -/* } Vector; */ - -/* void initVector(Vector *v, size_t initialSize) { */ -/* v->array = (double *)malloc(initialSize * sizeof(double)); */ -/* v->used = 0; */ -/* v->size = initialSize; */ -/* } */ - -/* void insertSorted(Vector *v, double element) { */ -/* // Check for reallocation needs */ -/* if (v->used == v->size) { */ -/* v->size *= 2; */ -/* v->array = (double *)realloc(v->array, v->size * sizeof(double)); */ -/* } */ - -/* // Binary search to find the correct insertion point */ -/* size_t low = 0, high = v->used; */ -/* while (low < high) { */ -/* size_t mid = low + (high - low) / 2; */ -/* if (v->array[mid] < element) { */ -/* low = mid + 1; */ -/* } else { */ -/* high = mid; */ -/* } */ -/* } */ - -/* // Insert element at the correct position */ -/* for (size_t i = v->used; i > low; --i) { */ -/* v->array[i] = v->array[i - 1]; */ -/* } */ -/* v->array[low] = element; */ -/* v->used++; */ -/* } */ - -/* void freeVector(Vector *v) { */ -/* free(v->array); */ -/* v->array = NULL; */ -/* v->used = v->size = 0; */ -/* } */ - -/* int main() { */ -/* Vector v; */ -/* initVector(&v, 5); */ - -/* insertSorted(&v, 3.0); */ -/* insertSorted(&v, 1.0); */ -/* insertSorted(&v, 4.0); */ -/* insertSorted(&v, 2.0); */ - -/* for (size_t i = 0; i < v.used; i++) { */ -/* printf("%f ", v.array[i]); */ -/* } */ - -/* freeVector(&v); */ -/* return 0; */ -/* } */ diff --git a/lines.h b/lines.h index c1be860..43f3ac0 100644 --- a/lines.h +++ b/lines.h @@ -8,7 +8,6 @@ #include "parse.h" #include "vec.h" -struct node_tag **lines_make(); -void lines_add(struct node_tag **lines, struct node_tag *line); +void lines_insert(struct node_tag ***lines, struct node_tag *line); void lines_print(struct node_tag **lines); diff --git a/main.c b/main.c index 844cfd0..6d8e995 100644 --- a/main.c +++ b/main.c @@ -7,15 +7,17 @@ #include "lines.h" #include <stdio.h> +#include <stdbool.h> void read_loop() { + true; struct node_tag *line; - struct node_tag **lines = lines_make(); - while (1) { + struct node_tag **lines = vector_create(); + while (true) { if ((line = read_line_stdin("🦄 "))) { printf("It parsed 👍\n"); printf("linum: %g\n", line->data.line.linum); - lines_add(lines, line); + lines_insert(&lines, line); lines_print(lines); } }