-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh_merge.h
167 lines (148 loc) · 4.37 KB
/
sh_merge.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* ISC License
*
* Copyright (c) 2021, Ioulianos Kakoulidis
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
* BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/**
* @file sh_merge.h
* @brief Merge two sorted arrays.
* @author Ioulianos Kakoulidis
*/
/*
* GNU indent program options: -orig -bad -bap -bs -cli2 -di1 -nbc -nut
*/
#ifndef SH_MERGE_H
#define SH_MERGE_H
#ifdef __cplusplus
extern "C" {
#endif
/**
*
* @brief Merge two sorted arrays.
*
* @param arr_a First array
* @param num_el_a Number of elements in array arr_a
* @param arr_b Second array
* @param num_el_b Number of elements in array arr_b
* @param el_size Element size (in bytes)
* @param compare Compare function
*
* @return Resulting array or NULL
*
*/
void *merge(const void *arr_a, size_t num_el_a,
const void *arr_b, size_t num_el_b,
size_t el_size, int (*compare) (const void *,
const void *));
/**
*
* @brief Merge two sorted arrays.
* Reallocates dst array memory. Merge elements in reverse order.
*
* @param dst Destination array
* @param num_el_dst Number of elements in array dst
* @param src Source array
* @param num_el_src Number of elements in array src
* @param el_size Element size (in bytes)
* @param compare Compare function
*
* @return Resulting array or NULL
*
*/
void *merge_into(void *dst, size_t num_el_dst,
const void *src, size_t num_el_src,
size_t el_size, int (*compare) (const void *,
const void *));
#ifdef __cplusplus
}
#endif
#ifdef SH_MERGE_IMPLEMENTATION
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define ASSERT_MERGE_ARGS() \
assert(el_size != 0); \
assert(compare != NULL);
void *
merge(const void *arr_a, size_t num_el_a,
const void *arr_b, size_t num_el_b,
size_t el_size, int (*compare) (const void *, const void *))
{
char *ac = (char *) arr_a;
char *bc = (char *) arr_b;
char *ah = ac + (num_el_a * el_size);
char *bh = bc + (num_el_b * el_size);
char *r = NULL;
char *rc = NULL;
ASSERT_MERGE_ARGS();
r = (char *) malloc((num_el_a + num_el_b) * el_size);
if (!r || !((num_el_a + num_el_b) * el_size)) {
return NULL;
}
rc = r;
while ((ac < ah) && (bc < bh)) {
if ((*compare) (ac, bc) > 0) {
memcpy(rc, bc, el_size);
bc += el_size;
} else {
memcpy(rc, ac, el_size);
ac += el_size;
}
rc += el_size;
}
if (ac < ah) {
memcpy(rc, ac, ah - ac);
} else if (bc < bh) {
memcpy(rc, bc, bh - bc);
}
return r;
}
void *
merge_into(void *dst, size_t num_el_dst,
const void *src, size_t num_el_src,
size_t el_size, int (*compare) (const void *, const void *))
{
char *dl;
char *sl = (char *) src;
char *dc;
char *sc = sl + (num_el_src * el_size);
char *r = NULL;
char *rc = NULL;
ASSERT_MERGE_ARGS();
r = (char *) realloc(dst, (num_el_dst + num_el_src) * el_size);
if (!r || !((num_el_dst + num_el_src) * el_size)) {
return NULL;
}
rc = r + (num_el_dst + num_el_src) * el_size;
dl = (char *) r;
dc = dl + (num_el_dst * el_size);
while ((dl < dc) && (sl < sc)) {
if ((*compare) (dc - el_size, sc - el_size) > 0) {
memcpy(rc - el_size, dc - el_size, el_size);
dc -= el_size;
} else {
memcpy(rc - el_size, sc - el_size, el_size);
sc -= el_size;
}
rc -= el_size;
}
if (sl < sc) {
memcpy(r, sl, sc - sl);
}
return r;
}
#endif
#endif