summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-08-24 14:07:14 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2019-08-24 14:07:14 -0400
commit32ab8c03cc4382401f4b0a5948354c6d14d08650 (patch)
treefb067f6e1f3235cc18c3f61fa12ec1355fd79aff /util
parentde61270e8ed6c152be89c9b10f1354a82712f2df (diff)
improved functions; added header file
Diffstat (limited to 'util')
-rw-r--r--util/arr.c30
1 files changed, 18 insertions, 12 deletions
diff --git a/util/arr.c b/util/arr.c
index 6cbf9eb..f8da701 100644
--- a/util/arr.c
+++ b/util/arr.c
@@ -6,20 +6,20 @@ typedef struct {
size_t item_sz;
} Array;
-void arr_create(Array *arr, size_t item_sz) {
+static void arr_create(Array *arr, size_t item_sz) {
arr->len = arr->cap = 0;
arr->item_sz = item_sz;
arr->data = NULL;
arr->last = NULL;
}
-void arr_reserve(Array *arr, size_t n) {
+static void arr_reserve(Array *arr, size_t n) {
arr->cap = n;
arr->data = realloc(arr->data, arr->item_sz * arr->cap);
arr->last = (void*)((char*)arr->data + arr->item_sz * (arr->len - 1));
}
-void *arr_add(Array *arr) {
+static void *arr_add(Array *arr) {
if (arr->len >= arr->cap) {
arr_reserve(arr, (arr->cap + 2) * 2);
}
@@ -29,20 +29,26 @@ void *arr_add(Array *arr) {
return item;
}
-void arr_remove_last(Array *arr) {
- arr->len--;
- /* OPTIM (memory): Shorten array. */
+static void arr_clear(Array *arr) {
+ free(arr->data);
+ arr->len = arr->cap = 0;
+ arr->data = NULL;
+ arr->last = NULL;
}
-void arr_free(Array *arr) {
- free(arr->data);
+static void arr_remove_last(Array *arr) {
+ /* OPTIM (memory): Shorten array. */
+ arr->len--;
+ if (arr->len) {
+ arr->last = (char*)arr->last - arr->item_sz;
+ } else {
+ arr_clear(arr);
+ }
+
}
-void arr_clear(Array *arr) {
+static void arr_free(Array *arr) {
free(arr->data);
- arr->len = arr->cap = 0;
- arr->data = NULL;
- arr->last = NULL;
}
#define arr_foreach(arr, type, var) for (type *var = (arr)->data; var; var == (arr)->last ? var = NULL : var++)