summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2022-08-01 13:54:20 -0400
committerrealtradam <[email protected]>2022-08-01 13:54:20 -0400
commit541bd6a040566da752999c8826c6a637a8bb7d7c (patch)
tree07996c8fcb83ff3356695e4e1eade83860b36c22
parent4b2ec7c721eae2b95e57ee09ba5234abdb9755dc (diff)
downloadarraylist-541bd6a040566da752999c8826c6a637a8bb7d7c.tar.gz
arraylist-541bd6a040566da752999c8826c6a637a8bb7d7c.zip
change to single headerHEADmaster
-rw-r--r--Makefile3
-rw-r--r--Readme.mdown16
-rw-r--r--arraylist.h20
-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
diff --git a/Makefile b/Makefile
index 587a191..aa97c8a 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/test.c b/test/test.c
index 6216586..9399409 100644
--- a/test.c
+++ b/test/test.c
@@ -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) {