add qsort to arr
This commit is contained in:
@@ -199,16 +199,17 @@ bool dyn_array_contains_func(void *this, uint8_t *value);
|
|||||||
// Comparison function: returns true if a equals b, false otherwise
|
// Comparison function: returns true if a equals b, false otherwise
|
||||||
typedef bool (*dyn_array_eq_fn)(const void *a, const void *b);
|
typedef bool (*dyn_array_eq_fn)(const void *a, const void *b);
|
||||||
|
|
||||||
bool dyn_array_contains_cmp_func(void *this, uint8_t *value, dyn_array_eq_fn eq);
|
bool dyn_array_contains_eq_func(void *this, uint8_t *value, dyn_array_eq_fn eq);
|
||||||
|
|
||||||
#define arr_contains_cmp(THIS, EQ_FN, ...)\
|
#define arr_contains_cmp(THIS, EQ_FN, ...)\
|
||||||
(\
|
(\
|
||||||
STATIC_ASSERT(sizeof(*(THIS)) == sizeof(*(__VA_ARGS__))),\
|
STATIC_ASSERT(sizeof(*(THIS)) == sizeof(*(__VA_ARGS__))),\
|
||||||
dyn_array_contains_cmp_func((THIS), (uint8_t*)(__VA_ARGS__), (EQ_FN))\
|
dyn_array_contains_eq_func((THIS), (uint8_t*)(__VA_ARGS__), (EQ_FN))\
|
||||||
)
|
)
|
||||||
|
|
||||||
// Comparison function for sorting: returns -1 if a < b, 0 if a == b, 1 if a > b
|
// Comparison function for sorting: returns -1 if a < b, 0 if a == b, 1 if a > b
|
||||||
typedef int (*dyn_array_cmp_fn)(const void *a, const void *b);
|
typedef int (*dyn_array_cmp_fn)(const void *a, const void *b);
|
||||||
|
void arr_qsort(void *this, dyn_array_cmp_fn cmp_fn);
|
||||||
|
|
||||||
// CLI /////////////////////////////////////////////////////////////////////////
|
// CLI /////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|||||||
+6
-1
@@ -1,5 +1,6 @@
|
|||||||
#include "cig.h"
|
#include "cig.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void *dyn_array_create_func(dyn_array_create_func_args_t args) {
|
void *dyn_array_create_func(dyn_array_create_func_args_t args) {
|
||||||
dyn_array_header_t *header = allocator_alloc_func(
|
dyn_array_header_t *header = allocator_alloc_func(
|
||||||
@@ -83,7 +84,7 @@ bool dyn_array_contains_func(void *this, uint8_t *value) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool dyn_array_contains_cmp_func(void *this, uint8_t *value, dyn_array_eq_fn eq) {
|
bool dyn_array_contains_eq_func(void *this, uint8_t *value, dyn_array_eq_fn eq) {
|
||||||
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
||||||
size_t itemsize = header->itemsize;
|
size_t itemsize = header->itemsize;
|
||||||
|
|
||||||
@@ -113,3 +114,7 @@ void dyn_array_bounds_check_func(void *this, size_t index, const char *file, int
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void arr_qsort(void *this, dyn_array_cmp_fn cmp_fn) {
|
||||||
|
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
||||||
|
qsort(this, header->n_items, header->itemsize, cmp_fn);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user