From cb5111de82e7486b902a09b7b7e2d5b79abcab96 Mon Sep 17 00:00:00 2001 From: lucielle Date: Tue, 5 May 2026 01:21:11 -0500 Subject: [PATCH] add array functionality --- Makefile | 4 +++- include/lsort.h | 2 +- include/types.h | 29 +++++++++++++++++++++++++++++ test/test.c | 35 +++++++++++++++++++++++++++++------ types.c | 21 +++++++++++++++++++++ 5 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 include/types.h create mode 100644 types.c diff --git a/Makefile b/Makefile index 776fa50..93c2361 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ CC = gcc SHELL := /bin/bash INCLUDE := include -CFLAGS = -Wall -I$(INCLUDE) +CFLAGS = -Wall -I$(INCLUDE) -ggdb BUILD_DIR := build @@ -18,8 +18,10 @@ INCLUDE_INSTALL_PATH := /usr/local/include/lsort LIB_INSTALL_PATH := /usr/local/lib STATIC_BUILD_FILES := $(STATIC_BUILD_DIR)/lsort.o \ + $(STATIC_BUILD_DIR)/types.o DYNAMIC_BUILD_FILES := $(DYNAMIC_BUILD_DIR)/lsort.o \ + $(STATIC_BUILD_DIR)/types.o TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \ diff --git a/include/lsort.h b/include/lsort.h index 0943fc8..cc625a2 100644 --- a/include/lsort.h +++ b/include/lsort.h @@ -1,6 +1,6 @@ #ifndef LSORT_H #define LSORT_H -int add(int x, int y); + #endif \ No newline at end of file diff --git a/include/types.h b/include/types.h new file mode 100644 index 0000000..759fb37 --- /dev/null +++ b/include/types.h @@ -0,0 +1,29 @@ +#ifndef LSORT_TYPES_H +#define LSORT_TYPES_H + +#include +#include "string.h" + +typedef struct { + const char *buf; + size_t len; +} lsort_string; + +typedef struct { + size_t count; + size_t capacity; + int* items; +} lsort_int_array; + +lsort_int_array* lsort_create_int_array(); +void lsort_int_array_append(lsort_int_array* array, int number); +int lsort_int_array_swap(lsort_int_array* array, int indexA, int indexB); + + +// struct lsort_int_link { +// int value; +// lsort_int_array* next; +// }; + + +#endif \ No newline at end of file diff --git a/test/test.c b/test/test.c index a0c0483..29cb4b4 100644 --- a/test/test.c +++ b/test/test.c @@ -1,11 +1,34 @@ -#include "../include/lsort.h" -#include "stdio.h" +#include "../include/types.h" -int main(void) { - int x = 5; - int y = 2; +int test_int_array_append(lsort_int_array* array) { + + lsort_int_array_append(array, 25); + lsort_int_array_append(array, 64); + lsort_int_array_append(array, 34); + lsort_int_array_append(array, 74); + lsort_int_array_append(array, 99); + lsort_int_array_append(array, 23); - printf("%d + %d = %d\n", x, y, add(x, y)); + if (array->items[5] != 23) return 1; + + return 0; +} + +int test_int_array_swap(lsort_int_array* array) { + lsort_int_array_swap(array, 0, 5); + lsort_int_array_swap(array, 4, 2); + lsort_int_array_swap(array, 2, 3); + + if (array->items[0] != 23 || array->items[5] != 25 || array->items[4] != 34 || array->items[2] != 74 || array->items[3] != 99) return 1; + + return 0; +} + +int main(void) { + lsort_int_array array = {0}; + + if (test_int_array_append(&array)) return 1; + if (test_int_array_swap(&array)) return 2; return 0; } diff --git a/types.c b/types.c new file mode 100644 index 0000000..13a4148 --- /dev/null +++ b/types.c @@ -0,0 +1,21 @@ +#include "include/types.h" +#include + +void lsort_int_array_append(lsort_int_array* array, int item) { + if (array->count >= array->capacity) { + if (array->capacity == 0) array->capacity = 8; + else array->capacity *= 2; + + array->items = realloc(array->items, array->capacity * sizeof(*array->items)); + } + array->items[array->count++] = item; +} + +int lsort_int_array_swap(lsort_int_array* array, int indexA, int indexB) { + if (indexA > array->count - 1 || indexB > array->count - 1) return 1; + int temp = array->items[indexA]; + array->items[indexA] = array->items[indexB]; + array->items[indexB] = temp; + + return 0; +} \ No newline at end of file