Browse Source

can break from OT_iterate

Iver 1 month ago
parent
commit
db97367bfd
6 changed files with 22 additions and 9 deletions
  1. BIN
      builds/liborangetree.so
  2. BIN
      builds/main.bin
  3. 1 1
      changelog.txt
  4. 6 3
      src/headers/OT_functions.h
  5. 3 1
      src/launch program/main.c
  6. 12 4
      src/main/main.c

BIN
builds/liborangetree.so


BIN
builds/main.bin


+ 1 - 1
changelog.txt

@@ -1 +1 @@
--added safeguard for if void operand
+-if user_function in OT_iterate returns -1, loop breaks

+ 6 - 3
src/headers/OT_functions.h

@@ -11,11 +11,13 @@ void* OT_get_first(void* operand);
 
 
 // - Removes an element from the linked list
 // - Removes an element from the linked list
 // - Takes in specific element of the list
 // - Takes in specific element of the list
-void OT_drop(void* operand);
+// - Returns -1 if the element was the last in the linked list
+int OT_drop(void* operand);
 
 
 // - Removes an element from the linked list and frees it
 // - Removes an element from the linked list and frees it
 // - Takes in specific element of the list
 // - Takes in specific element of the list
-void OT_erase(void* operand);
+// - Returns -1 if the element was the last in the linked list
+int OT_erase(void* operand);
 
 
 // - Adds an element to the end of a linked list
 // - Adds an element to the end of a linked list
 // - Takes in any element of the list and an element to add to the list
 // - Takes in any element of the list and an element to add to the list
@@ -39,7 +41,8 @@ void OT_free(void* operand);
 
 
 // - Runs `(*user_function)` for every element in the linked list
 // - Runs `(*user_function)` for every element in the linked list
 // - Takes in any element of the list, a pointer that will be passed into the `user_function`, and `user_function`
 // - Takes in any element of the list, a pointer that will be passed into the `user_function`, and `user_function`
-// - `user_function` must be in the form `void func(void* operand, void* user_pointer)`
+// - `user_function` must be in the form `int func(void* operand, void* user_pointer)`
+// - if 'user_function' returns -1, the loop will break
 void OT_iterate(void* operand, void* user_pointer, void (*user_function)(void*, void*));
 void OT_iterate(void* operand, void* user_pointer, void (*user_function)(void*, void*));
 
 
 #endif
 #endif

+ 3 - 1
src/launch program/main.c

@@ -7,10 +7,12 @@ typedef struct {
     char* string;
     char* string;
 } blahblahblah;
 } blahblahblah;
 
 
-void iteration_function(void* operand, void* user_pointer){
+int iteration_function(void* operand, void* user_pointer){
     blahblahblah* oper = (blahblahblah*)(operand);
     blahblahblah* oper = (blahblahblah*)(operand);
     
     
     printf(oper->string);
     printf(oper->string);
+
+    return 0;
 }
 }
 
 
 int main(){
 int main(){

+ 12 - 4
src/main/main.c

@@ -18,7 +18,7 @@ void* OT_get_first(void* operand){
     return (void*)cur_oper;
     return (void*)cur_oper;
 }
 }
 
 
-void OT_drop(void* operand){
+int OT_drop(void* operand){
     struct OT_operand* oper = (struct OT_operand*)operand;
     struct OT_operand* oper = (struct OT_operand*)operand;
     
     
     struct OT_operand* prev = (struct OT_operand*)oper->OT_prev;
     struct OT_operand* prev = (struct OT_operand*)oper->OT_prev;
@@ -26,9 +26,13 @@ void OT_drop(void* operand){
 
 
     if (prev) prev->OT_next = (void*)next;
     if (prev) prev->OT_next = (void*)next;
     if (next) next->OT_prev = (void*)prev;
     if (next) next->OT_prev = (void*)prev;
+
+    if (!prev && !next) return -1;
+    
+    return 0;
 }
 }
 
 
-void OT_erase(void* operand){
+int OT_erase(void* operand){
     struct OT_operand* oper = (struct OT_operand*)operand;
     struct OT_operand* oper = (struct OT_operand*)operand;
     
     
     struct OT_operand* prev = (struct OT_operand*)oper->OT_prev;
     struct OT_operand* prev = (struct OT_operand*)oper->OT_prev;
@@ -38,6 +42,10 @@ void OT_erase(void* operand){
     if (next) next->OT_prev = (void*)prev;
     if (next) next->OT_prev = (void*)prev;
 
 
     free(operand);
     free(operand);
+
+    if (!prev && !next) return -1;
+    
+    return 0;
 }
 }
 
 
 void OT_append(void* operand, void* appendee){
 void OT_append(void* operand, void* appendee){
@@ -120,13 +128,13 @@ void OT_free(void* operand){
     }
     }
 }
 }
 
 
-void OT_iterate(void* operand, void* user_pointer, void (*user_function)(void*, void*)){
+void OT_iterate(void* operand, void* user_pointer, int (*user_function)(void*, void*)){
     struct OT_operand* cur_oper = (struct OT_operand*)OT_get_first(operand);
     struct OT_operand* cur_oper = (struct OT_operand*)OT_get_first(operand);
 
 
     while (cur_oper){
     while (cur_oper){
         struct OT_operand* next = (struct OT_operand*)(cur_oper->OT_next);
         struct OT_operand* next = (struct OT_operand*)(cur_oper->OT_next);
         
         
-        (*user_function)(cur_oper, user_pointer);
+        if ((*user_function)(cur_oper, user_pointer) == -1) break;
 
 
         cur_oper = next;
         cur_oper = next;
     }
     }