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