summaryrefslogtreecommitdiff
path: root/src/stdlib/_qsort.h
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-08-15 11:44:23 -0400
committerJakob Kaivo <jkk@ung.org>2020-08-15 11:44:23 -0400
commit933040452f1091b86192c08ad343cd5ae50d14d4 (patch)
tree9f53f5ea23c35a6d14835ae0019b064d91b74634 /src/stdlib/_qsort.h
parent05347c1760befb18a2a2396233bcff8c7d3e283e (diff)
do swaps byte-by-byte to remove the need for allocating buffers
Diffstat (limited to 'src/stdlib/_qsort.h')
-rw-r--r--src/stdlib/_qsort.h17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/stdlib/_qsort.h b/src/stdlib/_qsort.h
index 298e28a1..28d273cd 100644
--- a/src/stdlib/_qsort.h
+++ b/src/stdlib/_qsort.h
@@ -1,19 +1,22 @@
#include <stdlib.h>
-#include <string.h>
struct __qs {
char *base;
size_t size;
int (*compar)(const void *, const void *);
- char buf[64];
- char *swap;
};
-static void __swap(struct __qs *a, size_t i, size_t j)
+static void __swap(struct __qs *a, size_t b, size_t c)
{
- memcpy(a->swap, a->base + (a->size * i), a->size);
- memcpy(a->base + (a->size * i), a->base + (a->size * j), a->size);
- memcpy(a->base + (a->size * j), a->swap, a->size);
+ char *x = a->base + (a->size * b);
+ char *y = a->base + (a->size * c);
+ size_t i;
+
+ for (i = 0; i < a->size; i++) {
+ char tmp = x[i];
+ x[i] = y[i];
+ y[i] = tmp;
+ }
}
static size_t __partition(struct __qs *a, size_t lo, size_t hi)