Browse Source

moving computers, updating repo

Iver 1 month ago
parent
commit
c46e2344f8
10 changed files with 134 additions and 28 deletions
  1. 5 8
      Makefile
  2. BIN
      builds/libkiyomitree.so
  3. BIN
      builds/main.bin
  4. 4 0
      changelog.txt
  5. 5 0
      readme.md
  6. 18 0
      src/headers/KT_functions.h
  7. 0 0
      src/headers/KT_types.h
  8. 2 0
      src/headers/kiyomitree.h
  9. 50 0
      src/launch program/main.c
  10. 50 20
      src/main/main.c

+ 5 - 8
Makefile

@@ -1,16 +1,13 @@
 COMPILER=gcc
 COMPILER=gcc
 FLAGS_ALL=-g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter
 FLAGS_ALL=-g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter
-FLAGS_EXAMPLE=-Lbuilds/ -lNAMEHERE -lLIBNAMEHERE -Wl,-rpath=builds/ -lm
-FLAGS_LIB=-fPIC -shared -lc -lm 
+FLAGS_EXAMPLE=-Lbuilds/ -lkiyomitree -Wl,-rpath=builds/ -lm
+FLAGS_LIB=-fPIC -shared -lc 
 
 
-main.bin: NAMEHERE.so LIBNAMEHERE.so
+main.bin: libkiyomitree.so
 	$(COMPILER) $(FLAGS_ALL) src/launch\ program/main.c -o builds/main.bin $(FLAGS_EXAMPLE) 
 	$(COMPILER) $(FLAGS_ALL) src/launch\ program/main.c -o builds/main.bin $(FLAGS_EXAMPLE) 
 
 
-NAMEHERE.so:
-	$(COMPILER) $(FLAGS_ALL) src/main/main.c -o builds/librasteriver.so $(FLAGS_LIB) 
-
-LIBNAMEHERE.so:
-	cp src/libraries/libLIBNAMEHERE.so builds/
+libkiyomitree.so:
+	$(COMPILER) $(FLAGS_ALL) src/main/main.c -o builds/libkiyomitree.so $(FLAGS_LIB) 
 
 
 clean:
 clean:
 	rm builds/*
 	rm builds/*

BIN
builds/libkiyomitree.so


BIN
builds/main.bin


+ 4 - 0
changelog.txt

@@ -0,0 +1,4 @@
+-made file tree
+-made functions
+-added example program
+-updated makefile

+ 5 - 0
readme.md

@@ -1,5 +1,10 @@
 # KiyomiTree, A Dynamic Array Manager
 # KiyomiTree, A Dynamic Array Manager
 
 
+## How to Use?
+ 
+ * Easy, just use KT_new_array to get the pointer and then use the array how you would any other array!
+ * DO NOT REALLOC, OR FREE THIS ARRAY WITHOUT USING KT_FREE
+
 ## Etymology
 ## Etymology
 
 
  * Based off the name OrangeTree from my other repo because they do similar things
  * Based off the name OrangeTree from my other repo because they do similar things

+ 18 - 0
src/headers/KT_functions.h

@@ -0,0 +1,18 @@
+#ifndef KT_FUNCTIONS_H
+#define KT_FUNCTIONS_H
+
+#include <stdlib.h>
+
+void** KT_new_array(size_t element_count);
+
+void KT_append(void*** array, void* pointer);
+
+void KT_prepend(void*** array, void* pointer);
+
+void KT_insert_left(void*** array, void* pointer, size_t index);
+
+void KT_insert_right(void*** array, void* pointer, size_t index);
+
+void KT_drop(void*** array, size_t index);
+
+#endif

+ 0 - 0
src/headers/KT_types.h


+ 2 - 0
src/headers/kiyomitree.h

@@ -5,4 +5,6 @@
 #include <stdlib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
 
 
+#include "KT_functions.h"
+
 #endif
 #endif

+ 50 - 0
src/launch program/main.c

@@ -1,3 +1,53 @@
+#include "../headers/kiyomitree.h"
+
+#include <stdio.h>
+
+void print_elements(void** array){
+    size_t current_elements = *(size_t*)(array[-1]);
+    
+    for (size_t i = 0; i < current_elements; i++){
+        printf((char*)array[i]);
+    }
+
+    printf("\n");
+}
+
 int main(){
 int main(){
+    char* a = "Hello, ";
+    char* b = "World!";
+    char* c = "Dynamic Arrays ";
+    char* d = "\b!";
+    char* e = "Dylan the dynamic array lover says: ";
+
+    void** array = KT_new_array(0);
+
+    // add A to array
+    KT_append(&array, a);
+
+    print_elements(array);
+
+    // add B to end of array
+    KT_append(&array, b);
+
+    print_elements(array);
+
+    // add C to the left of B
+    KT_insert_left(&array, c, 1);
+
+    print_elements(array);
+
+    // remove B from array
+    KT_drop(&array, 2);
+    
+    // add D to end of array
+    KT_append(&array, d);
+
+    print_elements(array);
+
+    // add E to the beginning of the linked list that contains A
+    KT_prepend(&array, e);
+
+    print_elements(array);
+
     return 0;
     return 0;
 }
 }

+ 50 - 20
src/main/main.c

@@ -15,47 +15,77 @@ void** KT_new_array(size_t element_count){
     return array + 2;
     return array + 2;
 }
 }
 
 
-void check_size(void** array){
-    size_t* current_allocated_elements = array[-2];
-    size_t* current_existing_elements = array[-1];
+#include <stdio.h>
+
+void check_size(void*** array){
+    size_t* current_allocated_elements = (size_t*)((*array)[-2]);
+    size_t* current_existing_elements = (size_t*)((*array)[-1]);
     
     
-    if (*current_existing_elements >= *current_allocated_elements)
-        array = (void**)(realloc(array - 2, sizeof(void*) * (*current_allocated_elements * 2 + 2)) + 2);
-}
+    if (*current_existing_elements >= *current_allocated_elements){
+        *current_allocated_elements += *current_allocated_elements ? *current_allocated_elements : 1;
+        
+        void* temp = realloc(*array - 2, sizeof(void*) * (*current_allocated_elements + 2));
+
+        if (!temp)
+            printf("realloc failed\n");
+
+        *array = (void**)(temp) + 2;
+    
+        printf("%ld elements now in array \n", *((size_t*)(*array)[-1]));
+    }
+}   
+
+void KT_append(void*** array, void* pointer){    
+    size_t* existing_elements = (size_t*)(*array)[-1];
+
+    (*existing_elements)++;
 
 
-void KT_append(void** array, void* pointer){    
     check_size(array);
     check_size(array);
 
 
-    array[(*(size_t*)array[-1])++] = pointer;
+    (*array)[*existing_elements - 1] = pointer;
 }
 }
 
 
-void KT_prepend(void** array, void* pointer){    
-    (*(size_t*)array[-1])++;
+void KT_prepend(void*** array, void* pointer){    
+    size_t* existing_elements = (size_t*)(*array)[-1];
+
+    (*existing_elements)++;
 
 
     check_size(array);
     check_size(array);
 
 
-    memcpy(array + 1, array, *(size_t*)array[-1] - 1);
+    memmove(*array + 1, *array, sizeof(void*) * (*existing_elements - 1));
 
 
-    array[0] = pointer;
+    (*array)[0] = pointer;
 }
 }
 
 
-void KT_insert_left(void** array, void* pointer, size_t index){
-    (*(size_t*)array[-1])++;
+void KT_insert_left(void*** array, void* pointer, size_t index){
+    size_t* existing_elements = (size_t*)(*array)[-1];
+
+    (*existing_elements)++;
 
 
     check_size(array);
     check_size(array);
 
 
-    memcpy(array + index + 1, array, *(size_t*)array[-1] - index - 1);
+    memmove(*array + index + 1, *array + index, sizeof(void*) * (*existing_elements - index - 1));
 
 
-    array[index] = pointer;
+    (*array)[index] = pointer;
 }
 }
 
 
 
 
-void KT_insert_right(void** array, void* pointer, size_t index){
-    (*(size_t*)array[-1])++;
+void KT_insert_right(void*** array, void* pointer, size_t index){
+    size_t* existing_elements = (size_t*)(*array)[-1];
+
+    (*existing_elements)++;
 
 
     check_size(array);
     check_size(array);
 
 
-    memcpy(array + index + 2, array, *(size_t*)array[-1] - index - 2);
+    memmove(*array + index + 2, *array, sizeof(void*) * (*existing_elements - index - 2));
+
+    (*array)[index + 1] = pointer;
+}
+
+void KT_drop(void*** array, size_t index){
+    size_t* existing_elements = (size_t*)(*array)[-1];
+
+    (*existing_elements)--;
 
 
-    array[index + 1] = pointer;
+    memmove(*array + index, *array + index + 1, sizeof(void*) * (*existing_elements - index - 1));
 }
 }