From 705be6072037ce3c0fa90538c2a5307c65c31a71 Mon Sep 17 00:00:00 2001 From: Shanti Chellaram Date: Sun, 11 Feb 2024 20:14:09 +0900 Subject: [PATCH] Initial commit --- README.md | 45 ++++++++++++++++++++++ dynarray/dynarray.h | 94 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 README.md create mode 100644 dynarray/dynarray.h diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c04629 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ + + +Learning Goals: +* Able to hit the ground running in modern software development tooling + * Git + * Editor + * Compilation + +* Has a collection of portfolio projects. +* Runs their own game server. +* Knows where to go +* Able to confidently start / write a program in C + +Homeworks and assignments + +Lesson 1: + +The Industrial Revolution, Charles Babbage, and Ada Lovelace. + +The industrial revolution was about breaking down complex tasks into simple +pieces so more could be done faster / in parallel. The value of " + + + +Ideas: + +calculator project + +end state: algebraic solver? + +expression evaluator + +AST generator + +typecasting? + +pointers + +integer parsing from string + +basic math + +string -> integer conversion + +test cases diff --git a/dynarray/dynarray.h b/dynarray/dynarray.h new file mode 100644 index 0000000..09f6c9d --- /dev/null +++ b/dynarray/dynarray.h @@ -0,0 +1,94 @@ +/** + * Note to implementer: + * You probably want to use these functions from the C standard library. + * malloc() / calloc() + * free(); + * realloc() + * memcpy() + */ + +/* Error definitions. */ + +enum dynarray_error_t { + ERR_OK = 0, /** No error. */ + ERR_NULL = 1, /** Error: an argument provided was null. */ + ERR_BADARG = 2, /** Error: a non-pointer argument provided was invalid. */ + ERR_NO_ALLOC = 3, /** Error: a memory allocation failed internally. */ + ERR_BOUNDS = 4 /** Error: attempted to access an index of this array that does not exist. */ +}; + +/** + * Create a dynamic array, with the provided initial capacity and growth factor. + * + * @param size_t element_size the sizeof any given element + * @param size_t capacity the initial size of this array; this many elements + * can be added without needing to grow the array + * @param double growth_factor - how much bigger to grow the array when you attempt to add a new + * + * @return a pointer to the created array or NULL if it failed to allocate + **/ +struct dynarray_t *dynarray_create(size_t capacity, size_t element_size, double growth_factor); + +/** + * Delete a dynarray. Does nothing if array is NULL + */ +void dynarray_destroy(struct dynarray_t *array); + +/** + * Adds an element to the end of this array. + * + * @param array the dynarray + * @param p_in a pointer to the element to add + * + * Returns ERR_OK (0) on success + * Returns ERR_NULL if array or p_in is NULL + * Returns ERR_NO_ALLOC if append required a reallocation that failed + */ +int dynarray_append(struct dynarray_t *, void *p_in); + +/** + * Returns the number of elements stored in the dynarray. + * + * @returns 0 if array is NULL + */ +size_t dynarray_size(struct dynarray_t *); + +/** + * Returns the number of elements that *could* be stored in this dynarray + * without triggering a reallocation. + * + * Returns the capacity of the dynarray, or 0 if the array is NULL + */ +size_t dynarray_capacity(struct dynarray_t *array); + +/** + * Stores a copy of the element in the location pointed at by + * + * Returns ERR_OK (0) on success + * Returns ERR_NULL if array or p_out is NULL + * Returns ERR_BOUNDS if the provided index is not between 0 and size() - 1 + */ +int dynarray_get(struct dynarray_t *array, size_t index, void *p_out); + +/** + * Sets the element at index 0 to the value pointed at by p_in. + * + * Returns ERR_OK (0) on success + * Returns ERR_NULL if array or p_in is NULL + * Returns ERR_BOUNDS if the provided index is not between 0 and size() - 1 + */ +int dynarray_set(struct dynarray_t *array, size_t index, void *p_in); + +/** + * Removes the element at the provided index. Elements after this index are + * shifted down by one. Copies the removed element to p_in, if it is provided. + * + * @param array the dynarray + * @param index the index of the element to remove + * @param p_in if not NULL, the removed element will be stored here + * + * Returns ERR_OK (0) on success + * Returns ERR_NULL if array is null + * Returns ERR_BOUNDS if the provided index is not between 0 and size() - 1 + */ +int dynarray_remove(struct dynarray_t *, size_t index, void *p_out);