summaryrefslogtreecommitdiff
path: root/include/al/array_sort.h
blob: b6d74684b03868df74c3b93f44950b1eb6908b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef _AL_ARRAY_SORT_H
#define _AL_ARRAY_SORT_H

#include "lib.h"

#define AL_INSERSION_SORT(data, type, count, cmp)  \
AL_MACRO_WRAP                                      \
({                                                 \
    s32 i = 1;                                     \
    while (i < (s32)count) {                       \
        s32 j = i - 1;                             \
        type x = data[i];                          \
        while (j >= 0 && cmp(&data[j], &x) > 0) {  \
            data[j + 1] = data[j];                 \
            j--;                                   \
        }                                          \
        data[j + 1] = x;                           \
        i++;                                       \
    }                                              \
})

#ifdef AL_USE_STDLIB
#define AL_STDLIB_QSORT(data, type, count, cmp) qsort(data, count, sizeof(type), cmp)
#endif

#endif // _AL_ARRAY_SORT_H