YetAnotherCoupler 3.5.2
Loading...
Searching...
No Matches
utils_core.h
Go to the documentation of this file.
1// Copyright (c) 2024 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef UTILS_CORE_H
6#define UTILS_CORE_H
7
8#include "utils_common.h"
9
17void yac_quicksort_index ( int * a, size_t n, int * idx);
18void yac_quicksort_index_yac_int_size_t ( yac_int * a, size_t n, size_t * idx);
20void yac_quicksort_index_size_t_yac_int ( size_t * a, size_t n, yac_int * idx);
21void yac_quicksort_index_yac_int_uint64_t ( yac_int * a, size_t n, uint64_t * idx);
22void yac_quicksort_index_yac_int_int ( yac_int * a, size_t n, int * idx);
23void yac_quicksort_index_size_t_size_t ( size_t * a, size_t n, size_t * idx);
24void yac_quicksort_index_uint64_t_size_t ( uint64_t * a, size_t n, size_t * idx);
25void yac_quicksort_index_int_size_t ( int * a, size_t n, size_t * idx);
26void yac_quicksort_index_size_t_int ( size_t * a, size_t n, int * idx);
27void yac_quicksort_index_size_t_void_p ( size_t * a, size_t n, void * * idx);
28void yac_quicksort_index_int_yac_int ( int * a, size_t n, yac_int * idx);
29void yac_quicksort_index_int_double ( int * a, size_t n, double * idx);
31 size_t * a, size_t n, size_t * b, double * c );
33 yac_int * a, size_t n, yac_int * b, double * c );
35 yac_int * a, size_t n, yac_int * b, size_t * c );
37 int * a, size_t n, size_t * b, size_t * c );
39 int * a, size_t n, size_t * b, yac_int * c );
40
46void yac_mergesort(void* base, size_t num, size_t size,
47 int (*compar)(const void*,const void*));
48
54static inline void yac_remove_duplicates_uint(unsigned * array, size_t * n) {
55
56 size_t const N = *n;
57 size_t pos = 0;
58
59 if (N == 0) return;
60
61 unsigned prev = array[0];
62
63 for (size_t i = 1; i < N; ++i) {
64
65 if (array[i] == prev) continue;
66
67 prev = array[i];
68 ++pos;
69
70 if (pos != i) array[pos] = array[i];
71 }
72
73 *n = pos + 1;
74}
75
81static inline void yac_remove_duplicates_double(double * array, size_t * n) {
82
83 size_t const N = *n;
84 size_t pos = 0;
85
86 if (N == 0) return;
87
88 double prev = array[0];
89
90 for (size_t i = 1; i < N; ++i) {
91
92 if (array[i] == prev) continue;
93
94 prev = array[i];
95 ++pos;
96
97 if (pos != i) array[pos] = array[i];
98 }
99
100 *n = pos + 1;
101}
102
108static inline void yac_remove_duplicates_size_t(size_t * array, size_t * n) {
109
110 size_t const N = *n;
111 size_t pos = 0;
112
113 if (N == 0) return;
114
115 size_t prev = array[0];
116
117 for (size_t i = 1; i < N; ++i) {
118
119 if (array[i] == prev) continue;
120
121 prev = array[i];
122 ++pos;
123
124 if (pos != i) array[pos] = array[i];
125 }
126
127 *n = pos + 1;
128}
129
136 size_t (*array)[2], size_t * n) {
137
138 size_t const N = *n;
139 size_t pos = 0;
140
141 if (N == 0) return;
142
143 size_t prev[2] = {array[0][0],
144 array[0][1]};
145
146 for (size_t i = 1; i < N; ++i) {
147
148 if ((array[i][0] == prev[0]) &&
149 (array[i][1] == prev[1]))continue;
150
151 prev[0] = array[i][0];
152 prev[1] = array[i][1];
153 ++pos;
154
155 if (pos != i) {
156 array[pos][0] = array[i][0];
157 array[pos][1] = array[i][1];
158 }
159 }
160
161 *n = pos + 1;
162}
163
170 size_t (*array)[3], size_t * n) {
171
172 size_t const N = *n;
173 size_t pos = 0;
174
175 if (N == 0) return;
176
177 size_t prev[3] = {array[0][0],
178 array[0][1],
179 array[0][2]};
180
181 for (size_t i = 1; i < N; ++i) {
182
183 if ((array[i][0] == prev[0]) &&
184 (array[i][1] == prev[1]) &&
185 (array[i][2] == prev[2]))continue;
186
187 prev[0] = array[i][0];
188 prev[1] = array[i][1];
189 prev[2] = array[i][2];
190 ++pos;
191
192 if (pos != i) {
193 array[pos][0] = array[i][0];
194 array[pos][1] = array[i][1];
195 array[pos][2] = array[i][2];
196 }
197 }
198
199 *n = pos + 1;
200}
201
208 yac_int * array, size_t * n) {
209
210 size_t const N = *n;
211 size_t pos = 0;
212
213 if (N == 0) return;
214
215 yac_int prev = array[0];
216
217 for (size_t i = 1; i < N; ++i) {
218
219 if (array[i] == prev) continue;
220
221 prev = array[i];
222 ++pos;
223
224 if (pos != i) array[pos] = array[i];
225 }
226
227 *n = pos + 1;
228}
229
230#define ASSERT(c) \
231if (!(c)) {\
232 fprintf(stderr, "### Assertion violation: %s in %s:%d\n",\
233 #c, __FILE__, __LINE__);\
234 abort ();\
235}
236
237#define COPY_DATA(data, count) \
238 (memcpy( \
239 xmalloc((size_t)(count) * sizeof(*(data))), \
240 (data), (size_t)(count) * sizeof(*(data))))
241
242#endif // UTILS_CORE_H
243
void yac_mergesort(void *base, size_t num, size_t size, int(*compar)(const void *, const void *))
Definition mergesort.c:134
void yac_quicksort_index_yac_int_size_t(yac_int *a, size_t n, size_t *idx)
void yac_quicksort_index_size_t_size_t_double(size_t *a, size_t n, size_t *b, double *c)
void yac_quicksort_index_int_yac_int(int *a, size_t n, yac_int *idx)
static void yac_remove_duplicates_size_t_3(size_t(*array)[3], size_t *n)
Definition utils_core.h:169
void yac_quicksort_index_int_double(int *a, size_t n, double *idx)
void yac_quicksort_index_yac_int_yac_int_size_t(yac_int *a, size_t n, yac_int *b, size_t *c)
void yac_quicksort_index_size_t_yac_int(size_t *a, size_t n, yac_int *idx)
void yac_quicksort_index_size_t_void_p(size_t *a, size_t n, void **idx)
static void yac_remove_duplicates_uint(unsigned *array, size_t *n)
Definition utils_core.h:54
void yac_quicksort_index_yac_int_int(yac_int *a, size_t n, int *idx)
void yac_quicksort_index_int_size_t(int *a, size_t n, size_t *idx)
void yac_quicksort_index_int_size_t_size_t(int *a, size_t n, size_t *b, size_t *c)
static void yac_remove_duplicates_size_t(size_t *array, size_t *n)
Definition utils_core.h:108
void yac_quicksort_index_size_t_int(size_t *a, size_t n, int *idx)
void yac_quicksort_index_uint64_t_size_t(uint64_t *a, size_t n, size_t *idx)
static void yac_remove_duplicates_double(double *array, size_t *n)
Definition utils_core.h:81
void yac_quicksort_index_size_t_size_t(size_t *a, size_t n, size_t *idx)
void yac_quicksort_index(int *a, size_t n, int *idx)
void yac_quicksort_index_yac_int_yac_int(yac_int *a, size_t n, yac_int *idx)
void yac_quicksort_index_yac_int_uint64_t(yac_int *a, size_t n, uint64_t *idx)
void yac_quicksort_index_int_size_t_yac_int(int *a, size_t n, size_t *b, yac_int *c)
void yac_quicksort_index_yac_int_yac_int_double(yac_int *a, size_t n, yac_int *b, double *c)
static void yac_remove_duplicates_size_t_2(size_t(*array)[2], size_t *n)
Definition utils_core.h:135
static void yac_remove_duplicates_yac_int(yac_int *array, size_t *n)
Definition utils_core.h:207
Xt_int yac_int
Definition yac_types.h:15