aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorMosder <xmosder12@gmail.com>2026-08-17 11:20:55 +0200
committerMosder <xmosder12@gmail.com>2026-08-17 11:20:55 +0200
commit674fb098ddf9daa173c4e579c84275e943445e5a (patch)
tree4ef0486d88bd148abcbbc3362f84b79b4bdd9231 /include
parent59b5ab2b1ccfcec55e64befb666632d6698113ed (diff)
fix: fixed arr_prev and arr_next to not give warnings about void* arithmetic
Diffstat (limited to 'include')
-rw-r--r--include/moslib/ds/array.h6
1 files changed, 4 insertions, 2 deletions
diff --git a/include/moslib/ds/array.h b/include/moslib/ds/array.h
index ac8b9f9..65b729f 100644
--- a/include/moslib/ds/array.h
+++ b/include/moslib/ds/array.h
@@ -444,7 +444,7 @@
//
// Returns:
// pointer to the previous item or NULL if there's no more items
-#define mos_arr_prev(arr, curr) (mos_arr_len(arr) == 0 ? NULL : (!(curr) ? mos_arr_last(arr) : ((curr) == (arr) ? NULL : (curr) - 1)))
+#define mos_arr_prev(arr, curr) ((!(arr) || !(curr)) ? mos_arr_last(arr) : ((curr) == mos_arr_first(arr) ? NULL : (arr) + mos_arr_p_diff(curr, arr) - 1))
// Get the pointer to the next item in the array from the pointer to the current one
// If pointer is NULL - gets the first item from the array
@@ -458,7 +458,7 @@
//
// Returns:
// pointer to the next item or NULL if there's no more items
-#define mos_arr_next(arr, curr) (mos_arr_len(arr) == 0 ? NULL : (!(curr) ? (arr) : ((curr) == mos_arr_last(arr) ? NULL : (curr) + 1)))
+#define mos_arr_next(arr, curr) ((!(arr) || !(curr)) ? mos_arr_first(arr) : ((curr) == mos_arr_last(arr) ? NULL : (arr) + mos_arr_p_diff(curr, arr) + 1))
// Get the length of the array
//
@@ -521,6 +521,8 @@ typedef struct {
size_t cap;
} MosArrHeader;
+#define mos_arr_p_diff(p1, p2) (((char *)(p1) - (char*)(p2)) / sizeof(*(p2)))
+
// Function prototypes for macros
extern void mos_arr_append_fn(void *p_arr, size_t el_size);
extern void mos_arr_insert_fn(void *p_arr, size_t i, size_t el_size);