summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-08-16 12:15:30 -0400
committerJakob Kaivo <jkk@ung.org>2020-08-16 12:15:30 -0400
commit75f2a7c3d2bed3c1c5add63325c8052ab6749afe (patch)
tree5333dc83ba6e59cfc539c345227d558ce5defe7c
parent8d4af013972f64dbabb7176706c18a94a63792f2 (diff)
merge _qsort.h directly into qsort.c
-rw-r--r--mk/qsort.d1
-rw-r--r--src/stdlib/_qsort.h33
-rw-r--r--src/stdlib/qsort.c33
3 files changed, 32 insertions, 35 deletions
diff --git a/mk/qsort.d b/mk/qsort.d
index cdec9ffe..3eb0785f 100644
--- a/mk/qsort.d
+++ b/mk/qsort.d
@@ -3,7 +3,6 @@ libc.a(qsort.o): $(OBJDIR)/qsort.o
@$(AR) $(ARFLAGS) $@ $(OBJDIR)/$%
$(OBJDIR)/qsort.o: src/stdlib/qsort.c
-$(OBJDIR)/qsort.o: src/stdlib/_qsort.h
$(OBJDIR)/qsort.o:
@echo " [CC] $@"
@mkdir -p $(@D)
diff --git a/src/stdlib/_qsort.h b/src/stdlib/_qsort.h
deleted file mode 100644
index 475932a7..00000000
--- a/src/stdlib/_qsort.h
+++ /dev/null
@@ -1,33 +0,0 @@
-#include <stdlib.h>
-
-static void __swap(char *base, size_t size, size_t b, size_t c)
-{
- char *x = base + (size * b);
- char *y = base + (size * c);
- size_t i;
-
- for (i = 0; i < size; i++) {
- char tmp = x[i];
- x[i] = y[i];
- y[i] = tmp;
- }
-}
-
-static void __qsort(char *base, size_t size, size_t lo, size_t hi, int (*compar)(const void *, const void *))
-{
- size_t i = lo, j = 0;
- if (lo >= hi) {
- return;
- }
-
- for (j = lo; j < hi; j++) {
- if (compar(base + (size * j), base + (size * hi)) < 0) {
- __swap(base, size, i, j);
- i++;
- }
- }
- __swap(base, size, i, hi);
-
- __qsort(base, size, lo, i - 1, compar);
- __qsort(base, size, i + 1, hi, compar);
-}
diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c
index b8e21b9c..0c7d8e21 100644
--- a/src/stdlib/qsort.c
+++ b/src/stdlib/qsort.c
@@ -1,8 +1,39 @@
#include <stdlib.h>
-#include "_qsort.h"
/** sort an array **/
+static void __swap(char *base, size_t size, size_t b, size_t c)
+{
+ char *x = base + (size * b);
+ char *y = base + (size * c);
+ size_t i;
+
+ for (i = 0; i < size; i++) {
+ char tmp = x[i];
+ x[i] = y[i];
+ y[i] = tmp;
+ }
+}
+
+static void __qsort(char *base, size_t size, size_t lo, size_t hi, int (*compar)(const void *, const void *))
+{
+ size_t i = lo, j = 0;
+ if (lo >= hi) {
+ return;
+ }
+
+ for (j = lo; j < hi; j++) {
+ if (compar(base + (size * j), base + (size * hi)) < 0) {
+ __swap(base, size, i, j);
+ i++;
+ }
+ }
+ __swap(base, size, i, hi);
+
+ __qsort(base, size, lo, i - 1, compar);
+ __qsort(base, size, i + 1, hi, compar);
+}
+
void qsort(void * base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
{
__qsort(base, size, 0, nmemb - 1, compar);