aboutsummaryrefslogtreecommitdiff
path: root/include/util/array.h
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-06-26 16:45:52 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-07-08 19:17:35 +0200
commit2087cc237cd0fe1ed29ebf891648bacb46f4833b (patch)
treeede4ba232bea36d8fc670ce360514927a2400bc7 /include/util/array.h
parentaed35c78c6a51a4b4c37ebda5643d2246842fb74 (diff)
Cleanup: move libutil headers to sub directory
Move all the libutil stuff from the toplevel include/ to a util/ sub directory and fix up the includes that make use of them. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'include/util/array.h')
-rw-r--r--include/util/array.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/include/util/array.h b/include/util/array.h
new file mode 100644
index 0000000..cbac7c2
--- /dev/null
+++ b/include/util/array.h
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * array.h
+ *
+ * Copyright (C) 2021 David Oberhollenzer <goliath@infraroot.at>
+ */
+#ifndef ARRAY_H
+#define ARRAY_H
+
+#include "sqfs/predef.h"
+#include "sqfs/error.h"
+
+#include <stddef.h>
+#include <string.h>
+#include <stdlib.h>
+
+typedef struct {
+ /* sizeof a single element */
+ size_t size;
+
+ /* total number of elements available */
+ size_t count;
+
+ /* actually used number of elements available */
+ size_t used;
+
+ void *data;
+} array_t;
+
+static SQFS_INLINE void *array_get(array_t *array, size_t index)
+{
+ if (index >= array->used)
+ return NULL;
+
+ return (char *)array->data + array->size * index;
+}
+
+static SQFS_INLINE int array_set(array_t *array, size_t index, const void *data)
+{
+ if (index >= array->used)
+ return SQFS_ERROR_OUT_OF_BOUNDS;
+
+ memcpy((char *)array->data + array->size * index, data, array->size);
+ return 0;
+}
+
+static SQFS_INLINE void array_sort_range(array_t *array, size_t start,
+ size_t count,
+ int (*compare_fun)(const void *a,
+ const void *b))
+{
+ if (start < array->used) {
+ if (count > (array->used - start))
+ count = array->used - start;
+
+ qsort((char *)array->data + array->size * start, count,
+ array->size, compare_fun);
+ }
+}
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+SQFS_INTERNAL int array_init(array_t *array, size_t size, size_t capacity);
+
+SQFS_INTERNAL int array_init_copy(array_t *array, const array_t *src);
+
+SQFS_INTERNAL void array_cleanup(array_t *array);
+
+SQFS_INTERNAL int array_append(array_t *array, const void *data);
+
+SQFS_INTERNAL int array_set_capacity(array_t *array, size_t capacity);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ARRAY_H */