commit 55d07f107f3ad65c585424f9b959d9df5f9d911f Author: Randy Jordan Date: Sat Jul 19 13:53:45 2025 -0500 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8aa2645 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) [year] [fullname] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..95a2a59 --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +# Compiler Flags +CC := gcc +CFLAGS := -g -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer + +# Directory variables +LIBDIR := lib +OBJ := obj +INC := include +SRC := src +TEST := tests + +# Filepath Pattern Matching +LIB := $(LIBDIR)/lib.a +SRCS := $(wildcard $(SRC)/*.c) +OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS)) +TESTS := $(wildcard $(TEST)/*.c) +TESTBINS := $(patsubst $(TEST)/%.c, $(TEST)/bin/%, $(TESTS)) + +# Commands must be labeled PHONY +.PHONY: all release clean test + +# Compiler Release Flags +release: CFLAGS := -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer -O2 -DNDEBUG +release: clean $(LIB) + +# Target for compilation. +all: $(LIB) + +# Target / Dependencies +$(LIB): $(OBJS) | $(LIBDIR) + $(RM) $(LIB) + ar -cvrs $@ $^ + +$(OBJ)/%.o: $(SRC)/%.c $(SRC)/%.h | $(OBJ) + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJ)/%.o: $(SRC)/%.c | $(OBJ) + $(CC) $(CFLAGS) -c $< -o $@ + +$(TEST)/bin/%: $(TEST)/%.c $(LIB) | $(TEST)/bin + $(CC) $(CFLAGS) $< $(LIB) -o $@ + +# Make directories if none. +$(LIBDIR): + mkdir $@ + +$(INC): + mkdir $@ + +$(OBJ): + mkdir $@ + +$(TEST)/bin: + mkdir $@ + +# Run the tests in the bin folder and track results +test: $(LIB) $(TEST)/bin $(TESTBINS) + @SUCCESS_COUNT=0; FAILURE_COUNT=0; \ + for test in $(TESTBINS); do \ + ./$$test; \ + EXIT_CODE=$$?; \ + TEST_NAME=$(notdir $$test); \ + if [ $$EXIT_CODE -eq 0 ]; then \ + echo "\033[0;32m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (SUCCESS)\033[0m"; \ + SUCCESS_COUNT=$$((SUCCESS_COUNT + 1)); \ + else \ + echo "\033[0;31m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (FAILURE)\033[0m"; \ + FAILURE_COUNT=$$((FAILURE_COUNT + 1)); \ + fi; \ + done; \ + echo "\n\nTests completed"; \ + echo "SUCCESS: $$SUCCESS_COUNT"; \ + echo "FAILURE: $$FAILURE_COUNT"; + +clean: + $(RM) -r $(LIBDIR) $(OBJ) $(TEST)/bin/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..f63040d --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# Vec + +## Description +Dynamic Array (Vector) implementation written in C. + +## Table of Contents + +- [Description](#description) +- [Features](#features) +- [Usage](#usage) +- [Credits / Resources](#credits--resources) +- [License](#license) + +## Features / TODOS +- [x] 0(1) Access +- [x] Automatic Re-sizing +- [x] Contiguous memory +- [ ] Add cleanup func for deletion + +## Usage +`make` to build
+`make test` to build and run tests
+ +## Credits / Resources + +## License +This project is licensed under MIT - see the [LICENSE](LICENSE) file for details. diff --git a/include/except.h b/include/except.h new file mode 100644 index 0000000..3ccbf3a --- /dev/null +++ b/include/except.h @@ -0,0 +1,80 @@ +#ifndef EXCEPT_INCLUDED +#define EXCEPT_INCLUDED + +#include + +struct Exception { + const char *reason; +}; +typedef struct Exception Exception; + +struct ExceptFrame { + struct ExceptFrame *prev; // Exception Stack + jmp_buf env; // Enviroment Buffer + const char *file; // Exception File + int line; // Exception Line + const Exception *exception; // Exception Reason +}; +typedef struct ExceptFrame ExceptFrame; + +// Exception States +enum { EXCEPT_ENTERED=0, EXCEPT_RAISED, EXCEPT_HANDLED, EXCEPT_FINALIZED}; + +void except_raise(const Exception *e, const char *file,int line); // Raise exceptions + +// External declarations +extern ExceptFrame *except_stack; // Global exception stack +extern const Exception assert_failed; // Forward declaration for assert. +extern void asserted(int e); + +#ifdef NDEBUG +#define ASSERTED(e) ((void)0) +#else +#define ASSERTED(e) ((void)((e)||(RAISE(assert_failed),0))) +#endif + +// Raise an Exception. +#define RAISE(e) except_raise(&(e), __FILE__, __LINE__) + +// Reraise the currect exception. +#define RERAISE except_raise(except_frame.exception, \ + except_frame.file, except_frame.line) + +// Switch to the previous exception frame and return. +#define RETURN switch (except_stack = except_stack->prev,0) default: return + +// Start a try block. +#define TRY do { \ + volatile int except_flag; \ + ExceptFrame except_frame; \ + except_frame.prev = except_stack; \ + except_stack = &except_frame; \ + except_flag = setjmp(except_frame.env); \ + if (except_flag == EXCEPT_ENTERED) { + +// Handle specific example. +#define EXCEPT(e) \ + if (except_flag == EXCEPT_ENTERED) except_stack = except_stack->prev; \ + } else if (except_frame.exception == &(e)) { \ + except_flag = EXCEPT_HANDLED; + +// Catch all other exceptions. +#define ELSE \ + if (except_flag == EXCEPT_ENTERED) except_stack = except_stack->prev; \ + } else { \ + except_flag = EXCEPT_HANDLED; + +// Execute finalization code. +#define FINALLY \ + if (except_flag == EXCEPT_ENTERED) except_stack = except_stack->prev; \ + } { \ + if (except_flag == EXCEPT_ENTERED) \ + except_flag = EXCEPT_FINALIZED; + +// End Try block. +#define END_TRY \ + if (except_flag == EXCEPT_ENTERED) except_stack = except_stack->prev; \ + } if (except_flag == EXCEPT_RAISED) RERAISE; \ +} while (0) + +#endif diff --git a/include/vec.h b/include/vec.h new file mode 100644 index 0000000..68f0e19 --- /dev/null +++ b/include/vec.h @@ -0,0 +1,26 @@ +#ifndef VEC_INCLUDED +#define VEC_INCLUDED + +struct Vec { + void **items; + int cap; + int len; +}; +typedef struct Vec Vec; + +typedef enum { + SOFTFAIL = 0x1, +} VecFlags; + +#define VEC_INIT_SZ 1 + +Vec *vec_init(int flags); +int vec_realloc(Vec *v,int cap); +void vec_free(Vec *v); +int vec_set(Vec *v,int index, void *item); +void *vec_read(Vec *v,int index); +int vec_push(Vec *v, void *item); +void *vec_pop(Vec *v); +void *vec_delete(Vec *v, int index); + +#endif diff --git a/src/except.c b/src/except.c new file mode 100644 index 0000000..6d3c652 --- /dev/null +++ b/src/except.c @@ -0,0 +1,30 @@ +#include "../include/except.h" +#include +#include + +ExceptFrame *except_stack = NULL; // Global exception stack. +const Exception assert_failed = { "Assertion Failure!" }; // If ASSERT fails. + +void except_raise(const Exception *e, const char *file,int line) { + // An exception was raised, grab the exception stack. + ExceptFrame *p = except_stack; + asserted(e != NULL); // Ensure exception pointer is not NULL + if (p == NULL) { // Uncaught Exception + const char *msg = e->reason ? e->reason : "Uncaught Exception!"; + fprintf(stderr,"\033[31m%s | Address: 0x%p | Raised at %s@%d \033[0m" ,msg,(void *)e,file,line); + fflush(stderr); + abort(); + } + // Set the exception details to the current frame. + p->exception = e; // Exception reason + p->file = file; + p->line = line; + // Move to the previous frame in the stack. + except_stack = except_stack->prev; + // Jump to the saved context environment. + longjmp(p->env, EXCEPT_RAISED); +} +void asserted(int e) { + ASSERTED(e); + (void)e; +} diff --git a/src/vec.c b/src/vec.c new file mode 100644 index 0000000..11fe6f6 --- /dev/null +++ b/src/vec.c @@ -0,0 +1,108 @@ +#include "../include/except.h" +#include "../include/vec.h" + +#include +#include +#include + +const Exception out_of_memory = { "Out Of Memory" }; // OOM Exception. + +Vec *vec_init(int flags){ + Vec *v = malloc(sizeof(*v)); + + if(v == NULL){ + if(!(flags & SOFTFAIL)) RAISE(out_of_memory); + return NULL; + } + + v->items = malloc( sizeof(void *) * VEC_INIT_SZ ); + if(v->items == NULL){ + if(!(flags & SOFTFAIL)) RAISE(out_of_memory); + return NULL; + } + v->len = 0; + v->cap = VEC_INIT_SZ; + return v; +} +int vec_realloc(Vec *v, int cap){ + ASSERTED(v); + ASSERTED(cap < INT_MAX); + Vec *old = v; + size_t sz = sizeof(void *) * cap; + void **items = realloc(v->items, sz); + if(items){ + v->items = items; + v->cap = cap; + return v->cap; + } + v = old; + return -1; +} +void vec_free(Vec *v){ + free(v->items); + v->len = 0; + v->cap = 0; + free(v); + v = NULL; +} +int vec_set(Vec *v,int index,void *item){ + ASSERTED(v); + ASSERTED(index < INT_MAX); + if(index >= 0 && index < v->len){ + v->items[index] = item; + return index; + } + return -1; +} +void *vec_read(Vec *v,int index){ + ASSERTED(v); + ASSERTED(index < INT_MAX); + void *res = NULL; + if(index >= 0 && index < v->len){ + res = v->items[index]; + } + return res; +} +int vec_push(Vec *v, void *item){ + ASSERTED(v); + ASSERTED(item); + if(v->len < v->cap){ + v->items[v->len++] = item; + return v->len; + } + // We are at capacity, resize + int status = vec_realloc(v, v->cap * 2); + if(status > 0){ // If the realloc succeded, add item. + v->items[v->len++] = item; + return v->len; + } + return -1; +} +void *vec_pop(Vec *v){ + ASSERTED(v); + void *res = v->items[--v->len]; + v->items[v->len] = NULL; + return res; +} +void *vec_delete(Vec *v, int index){ + ASSERTED(v); + ASSERTED(index < INT_MAX); + int i = 0; + if(index > v->len) return NULL; + void *res = v->items[index]; + // Shift elements to the left to fill the gap + for (i = index; i < v->len - 1; ++i){ + v->items[i] = v->items[i + 1]; + } + // Set the last element to NULL + v->items[v->len - 1] = NULL; + // Update the total count + v->len--; + // Resize the vector if needed + if (v->cap > VEC_INIT_SZ && v->len <= v->cap / 4){ + vec_realloc(v, v->cap / 2); + } + return res; +} + + diff --git a/tests/01_vec_init_free.c b/tests/01_vec_init_free.c new file mode 100644 index 0000000..31d6a49 --- /dev/null +++ b/tests/01_vec_init_free.c @@ -0,0 +1,21 @@ +#define DEBUG +#include "../include/except.h" +#include "../include/vec.h" + +#include +#include +#include + +int main(void){ + + Vec *v = NULL; + v = vec_init(SOFTFAIL); + + ASSERTED(v->items != NULL); + ASSERTED(v->cap == VEC_INIT_SZ); + ASSERTED(v->len == 0); + + vec_free(v); + return EXIT_SUCCESS; + +} diff --git a/tests/02_vec_push.c b/tests/02_vec_push.c new file mode 100644 index 0000000..1172acc --- /dev/null +++ b/tests/02_vec_push.c @@ -0,0 +1,39 @@ +#define DEBUG + +#include "../include/except.h" +#include "../include/vec.h" + +#include +#include +#include +int main(void){ + + Vec *v = NULL; + v = vec_init(SOFTFAIL); + ASSERTED(v->items != NULL); + ASSERTED(v->cap == VEC_INIT_SZ); + ASSERTED(v->len == 0); + char *str = "Hello"; + char *str2 = "World"; + char *str3 = "Goodbye"; + + int p1 = vec_push(v, (void *)str); + + ASSERTED(p1 == 1); + ASSERTED(v->len == 1); + + int p2 = vec_push(v, (void *)str2); + ASSERTED(p2 == 2); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 2); + + int p3 = vec_push(v,str3); + + ASSERTED(p3 == 3); + ASSERTED(v->len == 3); + ASSERTED(v->cap == 4); + + vec_free(v); + return EXIT_SUCCESS; + +} diff --git a/tests/03_vec_pop.c b/tests/03_vec_pop.c new file mode 100644 index 0000000..88a93d3 --- /dev/null +++ b/tests/03_vec_pop.c @@ -0,0 +1,45 @@ +#define DEBUG + +#include "../include/except.h" +#include "../include/vec.h" + +#include +#include +#include +int main(void){ + + Vec *v = NULL; + v = vec_init(SOFTFAIL); + ASSERTED(v->items != NULL); + ASSERTED(v->cap == VEC_INIT_SZ); + ASSERTED(v->len == 0); + char *str = "Hello"; + char *str2 = "World"; + char *str3 = "Goodbye"; + + int p1 = vec_push(v, (void *)str); + + ASSERTED(p1 == 1); + ASSERTED(v->len == 1); + + int p2 = vec_push(v, (void *)str2); + ASSERTED(p2 == 2); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 2); + + int p3 = vec_push(v,str3); + + ASSERTED(p3 == 3); + ASSERTED(v->len == 3); + ASSERTED(v->cap == 4); + + char *str4 = (char *)vec_pop(v); + ASSERTED(strcmp(str4,str3) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + + vec_free(v); + return EXIT_SUCCESS; + +} diff --git a/tests/04_vec_read.c b/tests/04_vec_read.c new file mode 100644 index 0000000..845cdab --- /dev/null +++ b/tests/04_vec_read.c @@ -0,0 +1,50 @@ +#define DEBUG + +#include "../include/except.h" +#include "../include/vec.h" + +#include +#include +#include +int main(void){ + + Vec *v = NULL; + v = vec_init(SOFTFAIL); + ASSERTED(v->items != NULL); + ASSERTED(v->cap == VEC_INIT_SZ); + ASSERTED(v->len == 0); + char *str = "Hello"; + char *str2 = "World"; + char *str3 = "Goodbye"; + + int p1 = vec_push(v, (void *)str); + + ASSERTED(p1 == 1); + ASSERTED(v->len == 1); + + int p2 = vec_push(v, (void *)str2); + ASSERTED(p2 == 2); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 2); + + int p3 = vec_push(v,str3); + + ASSERTED(p3 == 3); + ASSERTED(v->len == 3); + ASSERTED(v->cap == 4); + + char *str4 = (char *)vec_pop(v); + ASSERTED(strcmp(str4,str3) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + + char *ptr = (char *)vec_read(v,1); + ASSERTED(strcmp(str2,ptr) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + vec_free(v); + return EXIT_SUCCESS; + +} diff --git a/tests/05_vec_set.c b/tests/05_vec_set.c new file mode 100644 index 0000000..c12858e --- /dev/null +++ b/tests/05_vec_set.c @@ -0,0 +1,56 @@ +#define DEBUG + +#include "../include/except.h" +#include "../include/vec.h" + +#include +#include +#include +int main(void){ + + Vec *v = NULL; + v = vec_init(SOFTFAIL); + ASSERTED(v->items != NULL); + ASSERTED(v->cap == VEC_INIT_SZ); + ASSERTED(v->len == 0); + char *str = "Hello"; + char *str2 = "World"; + char *str3 = "Goodbye"; + + int p1 = vec_push(v, (void *)str); + + ASSERTED(p1 == 1); + ASSERTED(v->len == 1); + + int p2 = vec_push(v, (void *)str2); + ASSERTED(p2 == 2); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 2); + + int p3 = vec_push(v,str3); + + ASSERTED(p3 == 3); + ASSERTED(v->len == 3); + ASSERTED(v->cap == 4); + + char *str4 = (char *)vec_pop(v); + ASSERTED(strcmp(str4,str3) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + + char *ptr = (char *)vec_read(v,1); + ASSERTED(strcmp(str2,ptr) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + vec_set(v,1,(void *)ptr); + ASSERTED(strcmp( (char *)v->items[1],(char *)ptr ) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + + vec_free(v); + return EXIT_SUCCESS; + +} diff --git a/tests/06_vec_realloc.c b/tests/06_vec_realloc.c new file mode 100644 index 0000000..ac78286 --- /dev/null +++ b/tests/06_vec_realloc.c @@ -0,0 +1,58 @@ +#define DEBUG + +#include "../include/except.h" +#include "../include/vec.h" + +#include +#include +#include +int main(void){ + + Vec *v = NULL; + v = vec_init(SOFTFAIL); + ASSERTED(v->items != NULL); + ASSERTED(v->cap == VEC_INIT_SZ); + ASSERTED(v->len == 0); + char *str = "Hello"; + char *str2 = "World"; + char *str3 = "Goodbye"; + + int p1 = vec_push(v, (void *)str); + + ASSERTED(p1 == 1); + ASSERTED(v->len == 1); + + int p2 = vec_push(v, (void *)str2); + ASSERTED(p2 == 2); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 2); + + int p3 = vec_push(v,str3); + + ASSERTED(p3 == 3); + ASSERTED(v->len == 3); + ASSERTED(v->cap == 4); + + char *str4 = (char *)vec_pop(v); + ASSERTED(strcmp(str4,str3) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + + char *ptr = (char *)vec_read(v,1); + ASSERTED(strcmp(str2,ptr) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + vec_set(v,1,(void *)ptr); + ASSERTED(strcmp( (char *)v->items[1],(char *)ptr ) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + vec_realloc(v,100); + ASSERTED(v->cap == 100); + + vec_free(v); + return EXIT_SUCCESS; + +} diff --git a/tests/07_vec_delete.c b/tests/07_vec_delete.c new file mode 100644 index 0000000..31a5b17 --- /dev/null +++ b/tests/07_vec_delete.c @@ -0,0 +1,63 @@ +#define DEBUG + +#include "../include/except.h" +#include "../include/vec.h" + +#include +#include +#include +int main(void){ + + Vec *v = NULL; + v = vec_init(SOFTFAIL); + ASSERTED(v->items != NULL); + ASSERTED(v->cap == VEC_INIT_SZ); + ASSERTED(v->len == 0); + char *str = "Hello"; + char *str2 = "World"; + char *str3 = "Goodbye"; + + int p1 = vec_push(v, (void *)str); + + ASSERTED(p1 == 1); + ASSERTED(v->len == 1); + + int p2 = vec_push(v, (void *)str2); + ASSERTED(p2 == 2); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 2); + + int p3 = vec_push(v,str3); + + ASSERTED(p3 == 3); + ASSERTED(v->len == 3); + ASSERTED(v->cap == 4); + + char *str4 = (char *)vec_pop(v); + ASSERTED(strcmp(str4,str3) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + + char *ptr = (char *)vec_read(v,1); + ASSERTED(strcmp(str2,ptr) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + vec_set(v,1,(void *)ptr); + ASSERTED(strcmp( (char *)v->items[1],(char *)ptr ) == 0); + ASSERTED(v->len == 2); + ASSERTED(v->cap == 4); + + vec_realloc(v,100); + ASSERTED(v->cap == 100); + + vec_delete(v,1); + ASSERTED(v->len == 1); + ASSERTED(v->cap == 100/2); + + + vec_free(v); + return EXIT_SUCCESS; + +}