diff options
| author | realtradam <[email protected]> | 2022-08-01 13:54:20 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2022-08-01 13:54:20 -0400 |
| commit | 541bd6a040566da752999c8826c6a637a8bb7d7c (patch) | |
| tree | 07996c8fcb83ff3356695e4e1eade83860b36c22 | |
| parent | 4b2ec7c721eae2b95e57ee09ba5234abdb9755dc (diff) | |
| download | arraylist-master.tar.gz arraylist-master.zip | |
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | Readme.mdown | 16 | ||||
| -rw-r--r-- | arraylist.h | 20 | ||||
| -rw-r--r-- | src/arraylist.h (renamed from arraylist.c) | 40 | ||||
| -rw-r--r-- | test/test.c (renamed from test.c) | 2 |
5 files changed, 50 insertions, 31 deletions
@@ -1,3 +1,2 @@ build: - gcc -c arraylist.c -std=c99 -o arraylist.o -Wall - gcc test.c arraylist.o -std=c99 -o apptest -Wall + gcc test/test.c -Isrc -std=c99 -o apptest -Wall diff --git a/Readme.mdown b/Readme.mdown index f5019b3..fbde033 100644 --- a/Readme.mdown +++ b/Readme.mdown @@ -1,3 +1,17 @@ # Arraylist -Generic arraylist implementation in C. WIP. +Generic arraylist single header implementation in C. + +## How to use +Define it in your main file like so: +``` +#define ARRAYLIST_IMPLEMENTATION +#include "arraylist.h" +#undef ARRAYLIST_IMPLEMENTATION +``` +and for any other files you would also like to use it in simply do +``` +#include "arraylist.h" +``` +--- +Define `ARRAYLIST_DEFAULT_SIZE` to a value of your choice to change the default initialized size(number of elements) of an arraylist. diff --git a/arraylist.h b/arraylist.h deleted file mode 100644 index 7a1f08e..0000000 --- a/arraylist.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef ARRAYLIST_H -#define ARRAYLIST_H - -#include <stdbool.h> - -typedef struct array_list_type Arraylist; - -Arraylist* al_create(int type_size); -void al_free(Arraylist* al); -void* al_access(Arraylist* al, int position); -bool al_push(Arraylist* al, void* item); -void al_delete_last(Arraylist* al); -bool al_delete_at(Arraylist *al, int position); -bool al_allocate_at_least(Arraylist* al, int size); -int al_len(Arraylist* al); - -// give 2 indexes, moves source to overwrite the target -bool al_overwrite_and_delete(Arraylist* al, int source, int target); - -#endif diff --git a/arraylist.c b/src/arraylist.h index 048299f..b7b33ad 100644 --- a/arraylist.c +++ b/src/arraylist.h @@ -1,16 +1,38 @@ +#ifndef ARRAYLIST_H +#define ARRAYLIST_H #include <stdlib.h> #include <stdio.h> #include <string.h> -#include "arraylist.h" +#include <stdbool.h> #define PUBLIC /* nothing */ #define PRIVATE static // size doubles whenever it is exceeded +#ifndef ARRAYLIST_DEFAULT_SIZE #define ARRAYLIST_DEFAULT_SIZE 4 +#endif + //#define ARRAYLIST_DEBUG + +typedef struct array_list_type Arraylist; + +Arraylist* al_create(int type_size); +void al_free(Arraylist* al); +void* al_access(Arraylist* al, int position); +bool al_push(Arraylist* al, void* item); +void al_delete_last(Arraylist* al); +bool al_delete_at(Arraylist *al, int position); +bool al_allocate_at_least(Arraylist* al, int size); +int al_len(Arraylist* al); + +// give 2 indexes, moves source to overwrite the target +bool al_overwrite_and_delete(Arraylist* al, int source, int target); + +#ifdef ARRAYLIST_IMPLEMENTATION + struct array_list_type { void* data; // the arraylist data int element_size; @@ -44,8 +66,8 @@ bool al_delete_at(Arraylist *al, int position) { } else if((position >= 0) && (position < al->num_elements)) { memcpy( - al->data + (position * al->element_size), - al->data + ((position + 1) * al->element_size), + (char*)al->data + (position * al->element_size), + (char*)al->data + ((position + 1) * al->element_size), al->element_size * (al->num_elements - position) ); al->num_elements -= 1; @@ -79,7 +101,7 @@ bool al_allocate_at_least(Arraylist* al, int size) { Arraylist* al_create(int type_size) { - Arraylist* al = malloc(sizeof(Arraylist)); + Arraylist* al = (Arraylist*)malloc(sizeof(Arraylist)); al->data = malloc(sizeof(type_size) * ARRAYLIST_DEFAULT_SIZE); al->element_size = type_size; @@ -105,7 +127,7 @@ bool al_push(Arraylist* al, void* item) { #endif } memcpy( - al->data + (al->num_elements * al->element_size), + (char*)al->data + (al->num_elements * al->element_size), item, al->element_size ); @@ -116,7 +138,7 @@ bool al_push(Arraylist* al, void* item) { void* al_access(Arraylist* al, int position) { if((position >= al->num_elements) || (position < 0)) return NULL; - return al->data + (al->element_size * position); + return (char*)al->data + (al->element_size * position); } bool al_overwrite_and_delete(Arraylist* al, int source, int target) { @@ -126,11 +148,13 @@ bool al_overwrite_and_delete(Arraylist* al, int source, int target) { return false; } memcpy( - al->data + (target * al->element_size), - al->data + (source * al->element_size), + (char*)al->data + (target * al->element_size), + (char*)al->data + (source * al->element_size), al->element_size ); return al_delete_at(al, source); } +#endif +#endif @@ -1,6 +1,8 @@ #include <stdio.h> +#define ARRAYLIST_IMPLEMENTATION #include "arraylist.h" +#undef ARRAYLIST_IMPLEMENTATION bool test_match(Arraylist* al, int answer_key[], int answer_key_size) { if(al_len(al) != answer_key_size) { |
