Welcome to little lamb

Code » limb » commit 7b5d7d6

genalloc.h: Add genalloc_qsort*() macros

author Olivier Brunel
2023-07-07 12:36:27 UTC
committer Olivier Brunel
2023-07-24 10:16:43 UTC
parent a31d6b87987b24f1ad41e873acd7e11c10c4e7f1

genalloc.h: Add genalloc_qsort*() macros

src/doc/genalloc.h.0.md +13 -0
src/doc/genalloc.h/genalloc_qsort_full.3.md +41 -0
src/liblimb/include/limb/genalloc.h +8 -1

diff --git a/src/doc/genalloc.h.0.md b/src/doc/genalloc.h.0.md
index 8b6b4a4..0c37bbc 100644
--- a/src/doc/genalloc.h.0.md
+++ b/src/doc/genalloc.h.0.md
@@ -98,6 +98,19 @@ The following functions/macros are defined :
 :: Insert the content of a *genalloc* into another one at a specific offset.
 
 
+: [genalloc_qsort_full](3)
+:: Sort a section of a *genalloc* using [qsort](3).
+
+: [genalloc_qsort_r_full](3)
+:: Similar to [genalloc_qsort_full](3) but using [qsort_r](3).
+
+: [genalloc_qsort](3)
+:: Sort a full *genalloc* using [qsort](3).
+
+: [genalloc_qsort_r](3)
+:: Similar to [genalloc_qsort](3) but using [qsort_r](3).
+
+
 : [genalloc_remove](3)
 :: Remove data from a *genalloc*.
 
diff --git a/src/doc/genalloc.h/genalloc_qsort_full.3.md b/src/doc/genalloc.h/genalloc_qsort_full.3.md
new file mode 100644
index 0000000..3cdfe9d
--- /dev/null
+++ b/src/doc/genalloc.h/genalloc_qsort_full.3.md
@@ -0,0 +1,41 @@
+% limb manual
+% genalloc_qsort_full(3)
+
+# NAME
+
+genalloc_qsort_full, genalloc_qsort_r_full, genalloc_qsort, genalloc_qsort_r -
+sort a genalloc using qsort/qsort_r
+
+# SYNOPSIS
+
+    #include <limb/genalloc.h>
+
+```pre hl
+int (*cmp_fn) (const void *<em>p1</em>, const void *<em>p2</em>);
+int (*cmp_r_fn) (const void *<em>p1</em>, const void *<em>p2</em>, void *<em>ctx</em>);
+
+void genalloc_qsort_full(<em>type</em>, genalloc *<em>ga</em>, size_t <em>from</em>, size_t <em>len</em>, cmp_fn <em>cmp</em>)
+void genalloc_qsort_r_full(<em>type</em>, genalloc *<em>ga</em>, size_t <em>from</em>, size_t <em>len</em>, cmp_r_fn <em>cmp_r</em>, void *<em>ctx</em>)
+
+void genalloc_qsort(<em>type</em>, genalloc *<em>ga</em>,  cmp_fn <em>cmp</em>)
+void genalloc_qsort_r(<em>type</em>, genalloc *<em>ga</em>,  cmp_r_fn <em>cmp_r</em>, void *<em>ctx</em>)
+```
+
+# DESCRIPTION
+
+The `genalloc_qsort_full`() macro sorts `len` elements of the *genalloc* pointed
+by `ga`, starting at element `from` (counting from 0), using [qsort](3) with
+comparison function pointed to by `cmp`.
+
+The `genalloc_qsort_r_full`() macro is similar to `genalloc_qsort_full`() but
+using [qsort_r](3) with comparison function pointed to by `cmp_r` which will
+receive `ctx` as its third argument.
+
+For more information about comparison functions, refer to [qsort](3) and
+[qsort_r](3).
+
+The `genalloc_qsort`() macro is similar to `genalloc_qsort_full`() but to sort
+the full array.
+
+The `genalloc_qsort_r`() macro is similar to `genalloc_qsort_r_full`() but to
+sort the full array.
diff --git a/src/liblimb/include/limb/genalloc.h b/src/liblimb/include/limb/genalloc.h
index 517a1fe..9d1d0ec 100644
--- a/src/liblimb/include/limb/genalloc.h
+++ b/src/liblimb/include/limb/genalloc.h
@@ -4,8 +4,15 @@
 #ifndef LIMB_GENALLOC_H
 #define LIMB_GENALLOC_H
 
+#include <stdlib.h> /* qsort*() */
 #include <skalibs/genalloc.h>
 
-#define genalloc_remove(type, ga, offset, n)   stralloc_remove((ga), (offset) * sizeof(type), (n) * sizeof(type))
+#define genalloc_remove(type, ga, offset, n)    stralloc_remove((ga), (offset) * sizeof(type), (n) * sizeof(type))
+
+#define genalloc_qsort_full(type, ga, from, len, cmp)           qsort((void *) (&genalloc_s(type, (ga))[from]), len, sizeof(type), cmp)
+#define genalloc_qsort_r_full(type, ga, from, len, cmp, ctx)    qsort_r((void *) (&genalloc_s(type, (ga))[from]), len, sizeof(type), cmp, ctx)
+
+#define genalloc_qsort(type, ga, cmp)           genalloc_qsort_full(type, (ga), 0, genalloc_len(type, (ga)), cmp)
+#define genalloc_qsort_r(type, ga, cmp, ctx)    genalloc_qsort_r_full(type, (ga), 0, genalloc_len(type, (ga)), cmp, ctx)
 
 #endif /* LIMB_GENALLOC_H */