diff --git a/build/lib/mazeforge/core/loader.py b/build/lib/mazeforge/core/loader.py index 7bde5d6..51ac4da 100644 --- a/build/lib/mazeforge/core/loader.py +++ b/build/lib/mazeforge/core/loader.py @@ -53,27 +53,8 @@ def configure_library(lib): mazeforge_util.init.argtypes = [] mazeforge_util.init.restype = None mazeforge_util.init() - - -mazeforge_util.c_print.argtypes = [] -mazeforge_util.c_print.restype = None - -""" -0: - - -1: - │ - - -2: - -── - -3: - │ -──┘ -""" +mazeforge_util.test.argtypes = [] +mazeforge_util.test.restype = None # print_maze mazeforge_util.print_maze.argtypes = [ diff --git a/build/lib/mazeforge/core/platform/linux/aarch64/libmaze.so b/build/lib/mazeforge/core/platform/linux/aarch64/libmaze.so index 38ae644..078e840 100755 Binary files a/build/lib/mazeforge/core/platform/linux/aarch64/libmaze.so and b/build/lib/mazeforge/core/platform/linux/aarch64/libmaze.so differ diff --git a/build/lib/mazeforge/core/platform/linux/arm/libmaze.so b/build/lib/mazeforge/core/platform/linux/arm/libmaze.so index c0f19b1..44121ff 100755 Binary files a/build/lib/mazeforge/core/platform/linux/arm/libmaze.so and b/build/lib/mazeforge/core/platform/linux/arm/libmaze.so differ diff --git a/build/lib/mazeforge/core/platform/linux/i686/libmaze.so b/build/lib/mazeforge/core/platform/linux/i686/libmaze.so index 6b49005..8b2fe24 100755 Binary files a/build/lib/mazeforge/core/platform/linux/i686/libmaze.so and b/build/lib/mazeforge/core/platform/linux/i686/libmaze.so differ diff --git a/build/lib/mazeforge/core/platform/linux/x86_64/libmaze.so b/build/lib/mazeforge/core/platform/linux/x86_64/libmaze.so index d6dcdf7..7799204 100755 Binary files a/build/lib/mazeforge/core/platform/linux/x86_64/libmaze.so and b/build/lib/mazeforge/core/platform/linux/x86_64/libmaze.so differ diff --git a/build/lib/mazeforge/core/platform/macos/arm/libmaze.dylib b/build/lib/mazeforge/core/platform/macos/arm/libmaze.dylib index 9c06e4d..7f17a06 100755 Binary files a/build/lib/mazeforge/core/platform/macos/arm/libmaze.dylib and b/build/lib/mazeforge/core/platform/macos/arm/libmaze.dylib differ diff --git a/build/lib/mazeforge/core/platform/macos/x86_64/libmaze.dylib b/build/lib/mazeforge/core/platform/macos/x86_64/libmaze.dylib index 334d8f4..364b6a0 100755 Binary files a/build/lib/mazeforge/core/platform/macos/x86_64/libmaze.dylib and b/build/lib/mazeforge/core/platform/macos/x86_64/libmaze.dylib differ diff --git a/build/lib/mazeforge/core/platform/windows/i686/libmaze.dll b/build/lib/mazeforge/core/platform/windows/i686/libmaze.dll index acd27cb..d6887a3 100755 Binary files a/build/lib/mazeforge/core/platform/windows/i686/libmaze.dll and b/build/lib/mazeforge/core/platform/windows/i686/libmaze.dll differ diff --git a/build/lib/mazeforge/core/platform/windows/x86_64/libmaze.dll b/build/lib/mazeforge/core/platform/windows/x86_64/libmaze.dll index 6d4de12..2810c1b 100755 Binary files a/build/lib/mazeforge/core/platform/windows/x86_64/libmaze.dll and b/build/lib/mazeforge/core/platform/windows/x86_64/libmaze.dll differ diff --git a/src/mazeforge/core/loader.py b/src/mazeforge/core/loader.py index 7bde5d6..51ac4da 100644 --- a/src/mazeforge/core/loader.py +++ b/src/mazeforge/core/loader.py @@ -53,27 +53,8 @@ def configure_library(lib): mazeforge_util.init.argtypes = [] mazeforge_util.init.restype = None mazeforge_util.init() - - -mazeforge_util.c_print.argtypes = [] -mazeforge_util.c_print.restype = None - -""" -0: - - -1: - │ - - -2: - -── - -3: - │ -──┘ -""" +mazeforge_util.test.argtypes = [] +mazeforge_util.test.restype = None # print_maze mazeforge_util.print_maze.argtypes = [ diff --git a/src/mazeforge/core/platform/linux/aarch64/libmaze.so b/src/mazeforge/core/platform/linux/aarch64/libmaze.so index 38ae644..078e840 100755 Binary files a/src/mazeforge/core/platform/linux/aarch64/libmaze.so and b/src/mazeforge/core/platform/linux/aarch64/libmaze.so differ diff --git a/src/mazeforge/core/platform/linux/arm/libmaze.so b/src/mazeforge/core/platform/linux/arm/libmaze.so index c0f19b1..44121ff 100755 Binary files a/src/mazeforge/core/platform/linux/arm/libmaze.so and b/src/mazeforge/core/platform/linux/arm/libmaze.so differ diff --git a/src/mazeforge/core/platform/linux/i686/libmaze.so b/src/mazeforge/core/platform/linux/i686/libmaze.so index 6b49005..8b2fe24 100755 Binary files a/src/mazeforge/core/platform/linux/i686/libmaze.so and b/src/mazeforge/core/platform/linux/i686/libmaze.so differ diff --git a/src/mazeforge/core/platform/linux/x86_64/libmaze.so b/src/mazeforge/core/platform/linux/x86_64/libmaze.so index d6dcdf7..7799204 100755 Binary files a/src/mazeforge/core/platform/linux/x86_64/libmaze.so and b/src/mazeforge/core/platform/linux/x86_64/libmaze.so differ diff --git a/src/mazeforge/core/platform/macos/arm/libmaze.dylib b/src/mazeforge/core/platform/macos/arm/libmaze.dylib index 9c06e4d..7f17a06 100755 Binary files a/src/mazeforge/core/platform/macos/arm/libmaze.dylib and b/src/mazeforge/core/platform/macos/arm/libmaze.dylib differ diff --git a/src/mazeforge/core/platform/macos/x86_64/libmaze.dylib b/src/mazeforge/core/platform/macos/x86_64/libmaze.dylib index 334d8f4..364b6a0 100755 Binary files a/src/mazeforge/core/platform/macos/x86_64/libmaze.dylib and b/src/mazeforge/core/platform/macos/x86_64/libmaze.dylib differ diff --git a/src/mazeforge/core/platform/windows/i686/libmaze.dll b/src/mazeforge/core/platform/windows/i686/libmaze.dll index acd27cb..d6887a3 100755 Binary files a/src/mazeforge/core/platform/windows/i686/libmaze.dll and b/src/mazeforge/core/platform/windows/i686/libmaze.dll differ diff --git a/src/mazeforge/core/platform/windows/x86_64/libmaze.dll b/src/mazeforge/core/platform/windows/x86_64/libmaze.dll index 6d4de12..2810c1b 100755 Binary files a/src/mazeforge/core/platform/windows/x86_64/libmaze.dll and b/src/mazeforge/core/platform/windows/x86_64/libmaze.dll differ diff --git a/src/mazeforge/core/scripts/bst.c b/src/mazeforge/core/scripts/bst.c index 7e6e513..e4ffc32 100755 --- a/src/mazeforge/core/scripts/bst.c +++ b/src/mazeforge/core/scripts/bst.c @@ -6,74 +6,104 @@ Node* create_node(int value) { Node* node = (Node*)malloc(sizeof(Node)); + node->left = node->right = NULL; node->value = value; - node->exists = true; - node->lower = node->higher = NULL; + node->size = 0; return node; } - // Public // Create and returns an empty binary search tree BinaryTree* bst_create() { BinaryTree* tree = (BinaryTree*)malloc(sizeof(BinaryTree)); - if (tree == NULL) exit(EXIT_FAILURE); + if (tree == NULL) + exit(EXIT_FAILURE); tree->root = NULL; return tree; } - void recursive_delete(Node* node) { - if (node == NULL) return; - recursive_delete(node->lower); - recursive_delete(node->higher); + if (node == NULL) + return; + recursive_delete(node->left); + recursive_delete(node->right); free(node); } // Public // Delete and deallocate binary search tree void bst_delete(BinaryTree* tree) { - if (tree->root != NULL) recursive_delete(tree->root); + if (tree->root != NULL) + recursive_delete(tree->root); free(tree); } +void recursive_print_values(const Node* node) { + if (node == NULL) + return; + recursive_print_values(node->left); + printf("%d\n", node->value); + recursive_print_values(node->right); +} -void recursive_print(const Node* node) { - if (node == NULL) return; - recursive_print(node->lower); - if (node->exists == true) printf("%d\n", node->value); - recursive_print(node->higher); +// Public +// Print values of a binary search tree in order +void bst_print_values(BinaryTree* tree) { + if (tree->root == NULL) + return; + recursive_print_values(tree->root); +} + +void recursive_print(Node* node, int indentation) { + if (node == NULL) + return; + printf("|"); + for (int i = 0; i < indentation; i++) { + printf(" "); + } + printf("- %d (%d children)\n", node->value, node->size); + indentation++; + recursive_print(node->left, indentation); + recursive_print(node->right, indentation); } // Public -// Print values of binary search tree in order +// Print the structure of a binary search tree void bst_print(BinaryTree* tree) { - if (tree->root == NULL) return; - recursive_print(tree->root); + printf("| Binary Search Tree Structure (%d children)\n", tree->size); + if (tree->root == NULL) + return; + recursive_print(tree->root, 0); } - bool recursive_insert(Node* node, int value) { - if (node->value == value && node->exists == false) { - node->exists = true; - return true; - } else if (node->value == value) { + if (node->value == value) { return false; } else if (node->value > value) { - if (node->lower == NULL) { + if (node->left == NULL) { Node* new_node = create_node(value); - node->lower = new_node; + node->left = new_node; + node->size++; return true; } else { - return recursive_insert(node->lower, value); + if (recursive_insert(node->left, value) == true) { + node->size++; + return true; + } + return false; } } else { - if (node->higher == NULL) { + if (node->right == NULL) { Node* new_node = create_node(value); - node->higher = new_node; + node->right = new_node; + node->size++; return true; } else { - return recursive_insert(node->higher, value); + if (recursive_insert(node->right, value) == true) { + node->size++; + return true; + } + return false; } } } @@ -92,103 +122,131 @@ void bst_insert(BinaryTree* tree, int value) { } } +bool recursive_remove(Node** link, int value) { + Node* node = *link; -bool recursive_remove(Node* node, Node* parent, int value) { - if (node->value == value) { - bool removed = node->exists; - if (parent != NULL && node->lower == NULL && node->higher == NULL) { - if (parent->lower != NULL && node->value == parent->lower->value) { - parent->lower = NULL; - } else { - parent->higher = NULL; - } - free(node); - } else { - node->exists = false; + if (node == NULL) { + return false; + } + + if (node->value > value) { + if (recursive_remove(&node->left, value) == true) { + node->size--; + return true; } - return removed; - } else if (node->value > value) { - if (node->lower == NULL) return false; - return recursive_remove(node->lower, node, value); + return false; + } else if (node->value < value) { + if (recursive_remove(&node->right, value) == true) { + node->size--; + return true; + } + return false; + } + + if (node->left == NULL) { + // Lower or both are NULL + Node* temp = node; + *link = node->right; // Update parent's reference + free(temp); + } else if (node->right == NULL) { + // Higher is NULL + Node* temp = node; + *link = node->left; // Update parent's reference + free(temp); } else { - if (node->higher == NULL) return false; - return recursive_remove(node->higher, node, value); + // None are NULL + Node* successor = node->right; + while (successor->left != NULL) { + successor = successor->left; + } + node->value = successor->value; + recursive_remove(&node->right, successor->value); + node->size = 0; + if (node->left != NULL) { + node->size += 1 + node->left->size; + } + if (node->right != NULL) { + node->size += 1 + node->right->size; + } } + return true; } // Public // Remove a value from a binary search tree void bst_remove(BinaryTree* tree, int value) { - if (tree->root == NULL) return; - if (recursive_remove(tree->root, NULL, value) == true) { + if (tree->root == NULL) + return; + if (recursive_remove(&tree->root, value) == true) { tree->size--; } } - bool recursive_contains(Node* node, int value) { if (node->value == value) { - return node->exists == true; + return true; } else if (node->value > value) { - if (node->lower == NULL) return false; - return recursive_contains(node->lower, value); + if (node->left == NULL) + return false; + return recursive_contains(node->left, value); } else { - if (node->higher == NULL) return false; - return recursive_contains(node->higher, value); + if (node->right == NULL) + return false; + return recursive_contains(node->right, value); } } // Public // Returns bool(value ∈ tree) bool bst_contains(BinaryTree* tree, int value) { - if (tree->root == NULL) return false; - return recursive_contains(tree->root, value); + if (tree->root == NULL) + return false; + return recursive_contains(tree->root, value); } - int recursive_get_lower(Node* node, int* index) { - if (node->lower != NULL) { - int value = recursive_get_lower(node->lower, index); // Go down left + if (node->left != NULL) { + int value = recursive_get_lower(node->left, index); // Go down left if (*index == -1) { return value; } } - if (node->exists == true) { - --*index; // Decrease index - if (*index == -1) { - return node->value; - } + + --*index; // Decrease index + if (*index == -1) { + return node->value; } - if (node->higher != NULL) { - return recursive_get_lower(node->higher, index); // Go down right + + if (node->right != NULL) { + return recursive_get_lower(node->right, index); // Go down right } return 0; } int recursive_get_higher(Node* node, int* index) { - if (node->higher != NULL) { - int value = recursive_get_higher(node->higher, index); // Go down right + if (node->right != NULL) { + int value = recursive_get_higher(node->right, index); // Go down right if (*index == -1) { return value; } } - if (node->exists == true) { - --*index; // Decrease index - if (*index == -1) { - return node->value; - } + + --*index; // Decrease index + if (*index == -1) { + return node->value; } - if (node->lower != NULL) { - return recursive_get_higher(node->lower, index); // Go down left + + if (node->left != NULL) { + return recursive_get_higher(node->left, index); // Go down left } return 0; } - // Public // Retrieve the value at an index -int bst_get(BinaryTree* tree, int index) { - if (tree->root == NULL || index >= tree->size || index < -tree->size + 1) return -1; +int bst_get2(BinaryTree* tree, int index) { + if (tree->root == NULL || index >= tree->size || index < -tree->size + 1) + return -1; if (index < 0) { index += tree->size; } @@ -200,6 +258,35 @@ int bst_get(BinaryTree* tree, int index) { } } +int recursive_get(Node* node, int index) { + if (node == NULL) { + } + if (node->size == 0) { + return node->value; + } + if (node->left != NULL) { + if (node->left->size >= index) { + return recursive_get(node->left, index); + } else if (node->left->size == index - 1) { + return node->value; + } else { + return recursive_get(node->right, index - node->left->size - 2); + } + } + if (index == 0) { + return node->value; + } + return recursive_get(node->right, index - 1); +} + +int bst_get(BinaryTree* tree, int index) { + if (tree->root == NULL || index >= tree->size || index < -tree->size + 1) + return -1; + if (index < 0) { + index += tree->size; + } + return recursive_get(tree->root, index); +} // Public // Return a random value from a binary search tree @@ -207,3 +294,46 @@ int bst_get_random(BinaryTree* tree) { int index = rand() % tree->size; return bst_get(tree, index); } + +int main() { + BinaryTree* tree = bst_create(); + bst_insert(tree, 50); + bst_insert(tree, 60); + bst_insert(tree, 20); + bst_insert(tree, 25); + bst_insert(tree, 70); + bst_insert(tree, 40); + bst_insert(tree, 22); + bst_insert(tree, 13); + bst_insert(tree, 80); + bst_print(tree); + bst_print_values(tree); + + for (int i = 0; i < tree->size; i++) { + printf("val %d\n", bst_get(tree, i)); + bst_print(tree); + } + bst_delete(tree); + return 0; + + + bst_remove(tree, 50); + bst_remove(tree, 25); + bst_print(tree); + bst_print_values(tree); + for (int i = 0; i < 10; i++) { + printf("%d\n", bst_get(tree, i)); + } + + while (tree->size != 0) { + int cell = bst_get_random(tree); + if (cell == -1) { + break; + } + bst_remove(tree, cell); + bst_print(tree); + } + + bst_delete(tree); + return 0; +} \ No newline at end of file diff --git a/src/mazeforge/core/scripts/bst.h b/src/mazeforge/core/scripts/bst.h index 1521e64..86a76fa 100755 --- a/src/mazeforge/core/scripts/bst.h +++ b/src/mazeforge/core/scripts/bst.h @@ -4,10 +4,10 @@ #include typedef struct Node { + struct Node* left; + struct Node* right; int value; - bool exists; - struct Node* lower; // left child -> lower value - struct Node* higher; // right child -> higher value + int size; } Node; typedef struct BinaryTree { diff --git a/src/mazeforge/core/scripts/generator.c b/src/mazeforge/core/scripts/generator.c index bdd127b..ce89a89 100644 --- a/src/mazeforge/core/scripts/generator.c +++ b/src/mazeforge/core/scripts/generator.c @@ -1,58 +1,100 @@ #include // implements malloc, calloc and realloc #include // implements int8_t -#include // implements printf -#include // implements time -#include "bst.h" // implements binary search trees +#include // implements printf +#include // implements time +#include "bst.h" // implements binary search trees void init() { srand(time(NULL)); } - void test() { printf("Hello World from C!\n"); } +bool get_bit(int8_t* array, int coord, int index) { + return (array[coord] >> index) & 1; +} + +void set_bit(int8_t* array, int coord, int index, bool value) { + if (value == true) { + array[coord] |= (1 << index); + } else { + array[coord] &= ~(1 << index); + } +} void printBinary(int num) { // Loop through each bit starting from the most significant bit (leftmost) for (int i = 3; i >= 0; i--) { // Check if the i-th bit is set (1) or unset (0) int bit = (num >> i) & 1; - + // Print the bit printf("%d", bit); } printf("\n"); } - -void print_maze(int8_t *array, int width, int height) { +void print_raw_maze(int8_t* array, int width, int height) { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int value = array[y + x * width]; // x/y swapped cuz working printf("%d ", value); } printf("\n"); - } -} - - -bool get_bit(int8_t *array, int coord, int index) { - return (array[coord] >> index) & 1; + } } +void print_maze(int8_t* array, int width, int height) { + printf("┌"); + for (int i = 0; i < width - 1; i++) { + printf("───┬"); + } + printf("───┐\n"); + for (int y = 0; y < height - 1; y++) { + printf("│"); + for (int x = 0; x < width - 1; x++) { + if (get_bit(array, x + y * width, 0) == true) { + printf(" │"); + } else { + printf(" "); + } + } + printf(" │\n"); + + printf("├"); + for (int x = 0; x < width - 1; x++) { + if (get_bit(array, x + y * width, 1) == true) { + printf("───┼"); + } else { + printf(" ┼"); + } + } + if (get_bit(array, width - 1 + y * width, 1) == true) { + printf("───┤\n"); + } else { + printf(" ┤\n"); + } + } + printf("│"); + for (int x = 0; x < width - 1; x++) { + if (get_bit(array, x + (height - 1) * width, 0) == true) { + printf(" │"); + } else { + printf(" "); + } + } + printf(" │\n"); -void set_bit(int8_t *array, int coord, int index, bool value) { - if (value == true) { - array[coord] |= (1 << index); - } else { - array[coord] &= ~(1 << index); + printf("└"); + for (int i = 0; i < width - 1; i++) { + printf("───┴"); } + printf("───┘\n"); } - void add_adjacent_cells(int x, int y, BinaryTree* adjacent_cells, int8_t* array, int width, int height) { int coord = x + y * width; @@ -70,8 +112,7 @@ void add_adjacent_cells(int x, int y, BinaryTree* adjacent_cells, int8_t* array, } } - -void create_connection(int8_t *array, int x, int y, int width, int height) { +void create_connection(int8_t* array, int x, int y, int width, int height) { int coord = x + y * width; int neighbours[4]; @@ -141,15 +182,13 @@ void create_connection(int8_t *array, int x, int y, int width, int height) { } } - -void generate_maze(int8_t *array, int width, int height) { +void generate_maze(int8_t* array, int width, int height) { // Fill array with -1 for (int i = 0; i < width * height; i++) { array[i] = -1; } - // calloc(number of elements, size per element) -> allocates space + fills with 0 - // malloc(number of elements * size per element) -> allocates space (maybe prefered) + // Create binary search tree BinaryTree* adjacent_cells = bst_create(); // Create starting point @@ -171,10 +210,10 @@ void generate_maze(int8_t *array, int width, int height) { // Adjust cell walls array[new_cell] = 15; create_connection(array, x, y, width, height); - printBinary(array[new_cell]); } // Clean up bst_delete(adjacent_cells); print_maze(array, width, height); + return 0; }