forked from dlasalle/domlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlsort.h
47 lines (38 loc) · 1.26 KB
/
dlsort.h
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @file dlsort.h
* @brief Sorting Key-Value pairs
* @author Dominique LaSalle <[email protected]>
* Copyright (c) 2013-2015, Dominique LaSalle
* @version 1
* @date 2013-10-08
*/
#ifndef DL_SORT_H
#define DL_SORT_H
#define __DL_MK_SORTKV_HEADERS(prefix,typek,typev) \
typev * prefix ## _countingsort_v(const typek * keys, const typev * vals, \
typev * out, typek min, typev max, size_t n);
#define __DL_MK_SORTKV_FUNCS(prefix,typek,typev) \
typev * prefix ## _countingsort_v(const typek * const keys, \
const typev * const vals, typev * const out, const typek min, \
const typek max, const size_t n) \
{ \
size_t i; \
const size_t size = (size_t)(max - min)+1; \
size_t * counts = size_calloc(size+1); \
/* avoid having to do offsets in each iteration */ \
size_t * const start = counts - ((ssize_t)min); \
for (i=0;i<n;++i) { \
++start[keys[i]]; \
} \
size ## _prefixsum_exc(counts,size+1); \
for (i=0;i<n;++i) { \
out[start[keys[i]]++] = vals[i]; \
} \
dl_free(counts); \
return out; \
}
#define DL_MK_SORTKV_HEADERS(prefix,typek,typev) \
__DL_MK_SORTKV_HEADERS(prefix,typek,typev)
#define DL_MK_SORTKV_FUNCS(prefix,typek,typev) \
__DL_MK_SORTKV_FUNCS(prefix,typek,typev)
#endif