git » unicorn-sparkle-basic.git » commit 752cfc5

add lines_find_idx, use SIZE_MAX for notfound sentinel

author Alan Dipert
2024-06-19 19:43:36 UTC
committer Alan Dipert
2024-06-19 19:43:36 UTC
parent 717a59370acff5de0bbfd27e0240f80ba0982860

add lines_find_idx, use SIZE_MAX for notfound sentinel

lines.c +14 -0
lines.h +2 -0
main.c +2 -4

diff --git a/lines.c b/lines.c
index 279e395..0ea7964 100644
--- a/lines.c
+++ b/lines.c
@@ -20,6 +20,20 @@ void lines_insert(struct node_tag ***lines, struct node_tag *line) {
   vector_insert(lines, low, line);
 }
 
+size_t lines_find_idx(struct node_tag **lines, double linum) {
+  if (vector_size(lines) == 0) return SIZE_MAX;
+  size_t low = 0, high = vector_size(lines);
+  while (low < high) {
+    size_t mid = low + (high - low) / 2;
+    if (lines[mid]->data.line.linum < linum) {
+      low = mid + 1;
+    } else {
+      high = mid;
+    }
+  }
+  return lines[low]->data.line.linum == linum ? low : SIZE_MAX;
+}
+
 void lines_print(struct node_tag **lines) {
   printf("Size = %zu\n", vector_size(lines));
   for (size_t i = 0; i < vector_size(lines); i++) {
diff --git a/lines.h b/lines.h
index bbfc832..2c7f086 100644
--- a/lines.h
+++ b/lines.h
@@ -7,6 +7,8 @@
 
 #include "parse.h"
 #include "vec.h"
+#include <stdint.h>
 
 void lines_insert(struct node_tag ***lines, struct node_tag *line);
+size_t lines_find_idx(struct node_tag **lines, double linum);
 void lines_print(struct node_tag **lines);
diff --git a/main.c b/main.c
index a4d0c2f..514a925 100644
--- a/main.c
+++ b/main.c
@@ -19,9 +19,6 @@ void eval_stmt(struct node_tag *node) {
 }
 
 void run(struct node_tag **lines) {
-  for (size_t i = 0; i < vector_size(lines); i++) {
-    eval_stmt(lines[i]);
-  }
 }
 
 void read_loop() {
@@ -32,6 +29,7 @@ void read_loop() {
       switch (line->type) {
       case NODE_COMMAND_RUN:
         printf("RUN command entered\n");
+        printf("found = %zu\n", lines_find_idx(lines, 99));
         break;
       case NODE_NUMBERED_LINE:
         printf("linum: %g\n", line->data.line.linum);
@@ -40,7 +38,7 @@ void read_loop() {
       default:
         /* die */
       }
-      lines_print(lines);
+      /* lines_print(lines); */
     }
   }
 }