YAC 3.18.0
Yet Another Coupler
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 <string.h>
9#include <stdint.h>
10
11#include "utils_common.h"
12
13void yac_quicksort_index ( int * a, size_t n, int * idx);
14void yac_quicksort_index_yac_int_size_t ( yac_int * a, size_t n, size_t * idx);
16void yac_quicksort_index_size_t_yac_int ( size_t * a, size_t n, yac_int * idx);
17void yac_quicksort_index_yac_int_uint64_t ( yac_int * a, size_t n, uint64_t * idx);
18void yac_quicksort_index_yac_int_int ( yac_int * a, size_t n, int * idx);
19void yac_quicksort_index_size_t_size_t ( size_t * a, size_t n, size_t * idx);
20void yac_quicksort_index_uint64_t_size_t ( uint64_t * a, size_t n, size_t * idx);
21void yac_quicksort_index_int_size_t ( int * a, size_t n, size_t * idx);
22void yac_quicksort_index_size_t_int ( size_t * a, size_t n, int * idx);
23void yac_quicksort_index_size_t_void_p ( size_t * a, size_t n, void * * idx);
24void yac_quicksort_index_int_yac_int ( int * a, size_t n, yac_int * idx);
25void yac_quicksort_index_int_double ( int * a, size_t n, double * idx);
27 size_t * a, size_t n, size_t * b, double * c );
29 yac_int * a, size_t n, yac_int * b, double * c );
31 yac_int * a, size_t n, yac_int * b, size_t * c );
33 int * a, size_t n, size_t * b, size_t * c );
35 int * a, size_t n, size_t * b, yac_int * c );
36
37void yac_mergesort(void* base, size_t num, size_t size,
38 int (*compar)(const void*,const void*));
39
45static inline void yac_remove_duplicates_uint(unsigned * array, size_t * n) {
46
47 size_t const N = *n;
48 size_t pos = 0;
49
50 if (N == 0) return;
51
52 unsigned prev = array[0];
53
54 for (size_t i = 1; i < N; ++i) {
55
56 if (array[i] == prev) continue;
57
58 prev = array[i];
59 ++pos;
60
61 if (pos != i) array[pos] = array[i];
62 }
63
64 *n = pos + 1;
65}
66
72static inline void yac_remove_duplicates_double(double * array, size_t * n) {
73
74 size_t const N = *n;
75 size_t pos = 0;
76
77 if (N == 0) return;
78
79 double prev = array[0];
80
81 for (size_t i = 1; i < N; ++i) {
82
83 // use memcmp to handle NaNs correctly
84 if (!memcmp(&array[i], &prev, sizeof(prev))) continue;
85
86 prev = array[i];
87 ++pos;
88
89 if (pos != i) array[pos] = array[i];
90 }
91
92 *n = pos + 1;
93}
94
100static inline void yac_remove_duplicates_size_t(size_t * array, size_t * n) {
101
102 size_t const N = *n;
103 size_t pos = 0;
104
105 if (N == 0) return;
106
107 size_t prev = array[0];
108
109 for (size_t i = 1; i < N; ++i) {
110
111 if (array[i] == prev) continue;
112
113 prev = array[i];
114 ++pos;
115
116 if (pos != i) array[pos] = array[i];
117 }
118
119 *n = pos + 1;
120}
121
128 size_t (*array)[2], size_t * n) {
129
130 size_t const N = *n;
131 size_t pos = 0;
132
133 if (N == 0) return;
134
135 size_t prev[2] = {array[0][0],
136 array[0][1]};
137
138 for (size_t i = 1; i < N; ++i) {
139
140 if ((array[i][0] == prev[0]) &&
141 (array[i][1] == prev[1]))continue;
142
143 prev[0] = array[i][0];
144 prev[1] = array[i][1];
145 ++pos;
146
147 if (pos != i) {
148 array[pos][0] = array[i][0];
149 array[pos][1] = array[i][1];
150 }
151 }
152
153 *n = pos + 1;
154}
155
162 size_t (*array)[3], size_t * n) {
163
164 size_t const N = *n;
165 size_t pos = 0;
166
167 if (N == 0) return;
168
169 size_t prev[3] = {array[0][0],
170 array[0][1],
171 array[0][2]};
172
173 for (size_t i = 1; i < N; ++i) {
174
175 if ((array[i][0] == prev[0]) &&
176 (array[i][1] == prev[1]) &&
177 (array[i][2] == prev[2]))continue;
178
179 prev[0] = array[i][0];
180 prev[1] = array[i][1];
181 prev[2] = array[i][2];
182 ++pos;
183
184 if (pos != i) {
185 array[pos][0] = array[i][0];
186 array[pos][1] = array[i][1];
187 array[pos][2] = array[i][2];
188 }
189 }
190
191 *n = pos + 1;
192}
193
200 yac_int * array, size_t * n) {
201
202 size_t const N = *n;
203 size_t pos = 0;
204
205 if (N == 0) return;
206
207 yac_int prev = array[0];
208
209 for (size_t i = 1; i < N; ++i) {
210
211 if (array[i] == prev) continue;
212
213 prev = array[i];
214 ++pos;
215
216 if (pos != i) array[pos] = array[i];
217 }
218
219 *n = pos + 1;
220}
221
222// Sorts the provided arrays based on a flag-array (containing the
223// values "0" and "!= 0"). After the sort, all array elements whose associated
224// flag value is "0" are the front of the array.
225//
226// This sort is:
227// * not stable
228// * has a time complexity of O(n)
229static inline void yac_flag_sort_size_t(
230 size_t * array_size_t, int * flag, size_t false_count) {
231
232 // The number of "true" elements in the 0...false_count-1 range of the
233 // array is identical to the number of "false" elements in the
234 // false_count...false_count+true_count-1. We just have to find matching
235 // pairs and swap them.
236 for (size_t i = 0, j = false_count; i < false_count; ++i) {
237 // if there is a wrongfully placed "true" element
238 if (flag[i]) {
239 // find a wrongfully place "false" element
240 for (; flag[j]; ++j);
241 // swap elements
242 size_t temp_size_t = array_size_t[i];
243 array_size_t[i] = array_size_t[j];
244 array_size_t[j] = temp_size_t;
245 // set to next element in "true" list
246 ++j;
247 }
248 }
249}
250
261char * yac_string_dup(char const * string);
262
263#define ASSERT(c) \
264if (!(c)) {\
265 fprintf(stderr, "### Assertion violation: %s in %s:%d\n",\
266 #c, __FILE__, __LINE__);\
267 abort ();\
268}
269
270#define COPY_DATA(data, count) \
271 (memcpy( \
272 xmalloc((size_t)(count) * sizeof(*(data))), \
273 (data), (size_t)(count) * sizeof(*(data))))
274
275#endif // UTILS_CORE_H
276
#define N
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:161
static void yac_flag_sort_size_t(size_t *array_size_t, int *flag, size_t false_count)
Definition utils_core.h:229
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:45
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)
char * yac_string_dup(char const *string)
Definition utils_core.c:36
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:100
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:72
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:127
static void yac_remove_duplicates_yac_int(yac_int *array, size_t *n)
Definition utils_core.h:199
YAC_INT yac_int
Definition yac_types.h:15