Add dbuf_sort
This commit is contained in:
parent
73eb83484a
commit
ad97de47a6
@ -16,6 +16,11 @@ typedef enum guf_obj_cpy_opt {
|
|||||||
GUF_CPY_MOVE = 2,
|
GUF_CPY_MOVE = 2,
|
||||||
} guf_obj_cpy_opt;
|
} guf_obj_cpy_opt;
|
||||||
|
|
||||||
|
typedef enum guf_sort_opt {
|
||||||
|
GUF_SORT_ASCENDING = 0,
|
||||||
|
GUF_SORT_DESCENDING = 1
|
||||||
|
} guf_sort_opt;
|
||||||
|
|
||||||
// bool guf_alloc_init(void);
|
// bool guf_alloc_init(void);
|
||||||
// void *guf_malloc(size_t size, const char *name);
|
// void *guf_malloc(size_t size, const char *name);
|
||||||
// void *guf_calloc(size_t count, size_t size, const char *name);
|
// void *guf_calloc(size_t count, size_t size, const char *name);
|
||||||
|
|||||||
@ -299,9 +299,24 @@ void GUFCAT(GUF_CNT_NAME, _free)(GUF_CNT_NAME *dbuf)
|
|||||||
dbuf->capacity = dbuf->size = 0;
|
dbuf->capacity = dbuf->size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// void guf_dbuf_sort(guf_dbuf *dbuf, void (*cmp)(const void *a, const void *b));
|
void GUFCAT(GUF_CNT_NAME, _sort)(GUF_CNT_NAME *dbuf, guf_sort_opt sort_opt)
|
||||||
// void guf_dbuf_ascending(guf_dbuf *dbuf);
|
{
|
||||||
// void guf_dbuf_descending(guf_dbuf *dbuf);
|
GUF_ASSERT_RELEASE(GUFCAT(GUF_CNT_NAME, _valid_and_maybe_empty)(dbuf));
|
||||||
|
|
||||||
|
if (dbuf->size == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sort_opt == GUF_SORT_ASCENDING) {
|
||||||
|
GUF_ASSERT_RELEASE(GUF_CNT_T_OPS.cmp_void);
|
||||||
|
qsort(dbuf->data, dbuf->size, sizeof(GUF_CNT_T), GUF_CNT_T_OPS.cmp_void);
|
||||||
|
} else if (sort_opt == GUF_SORT_DESCENDING) {
|
||||||
|
GUF_ASSERT_RELEASE(GUF_CNT_T_OPS.cmp_void_inv);
|
||||||
|
qsort(dbuf->data, dbuf->size, sizeof(GUF_CNT_T), GUF_CNT_T_OPS.cmp_void_inv);
|
||||||
|
} else {
|
||||||
|
GUF_ASSERT_RELEASE(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
typedef struct GUFCAT(GUF_CNT_NAME, _iter) {
|
typedef struct GUFCAT(GUF_CNT_NAME, _iter) {
|
||||||
GUF_CNT_T *cur, *end, *begin;
|
GUF_CNT_T *cur, *end, *begin;
|
||||||
|
|||||||
@ -71,14 +71,16 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
dbuf_heap_cstr mut_strings = dbuf_heap_cstr_new();
|
dbuf_heap_cstr mut_strings = dbuf_heap_cstr_new();
|
||||||
dbuf_heap_cstr_push_val_cpy(&mut_strings, "First mut!");
|
dbuf_heap_cstr_push_val_cpy(&mut_strings, "1");
|
||||||
dbuf_heap_cstr_push_val_cpy(&mut_strings, "Second mut!");
|
dbuf_heap_cstr_push_val_cpy(&mut_strings, "2");
|
||||||
|
|
||||||
char *move_me_pls = calloc(128, sizeof(char));
|
char *move_me_pls = calloc(128, sizeof(char));
|
||||||
strcpy(move_me_pls, "Third mut");
|
strcpy(move_me_pls, "3");
|
||||||
dbuf_heap_cstr_push(&mut_strings, &move_me_pls, GUF_CPY_MOVE);
|
dbuf_heap_cstr_push(&mut_strings, &move_me_pls, GUF_CPY_MOVE);
|
||||||
GUF_ASSERT_RELEASE(move_me_pls == NULL);
|
GUF_ASSERT_RELEASE(move_me_pls == NULL);
|
||||||
|
|
||||||
|
dbuf_heap_cstr_sort(&mut_strings, GUF_SORT_DESCENDING);
|
||||||
|
|
||||||
GUF_FOREACH(&mut_strings, dbuf_heap_cstr, it) {
|
GUF_FOREACH(&mut_strings, dbuf_heap_cstr, it) {
|
||||||
printf("str: %s\n", *it.cur);
|
printf("str: %s\n", *it.cur);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user