9.1
general documentation
cs_sort.h
Go to the documentation of this file.
1#ifndef __CS_SORT_H__
2#define __CS_SORT_H__
3
4/*============================================================================
5 * Functions related to in-place sorting of arrays.
6 *===========================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2025 EDF S.A.
12
13 This program is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free Software
15 Foundation; either version 2 of the License, or (at your option) any later
16 version.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25 Street, Fifth Floor, Boston, MA 02110-1301, USA.
26*/
27
28/*----------------------------------------------------------------------------*/
29
30#include "base/cs_defs.h"
31
32/*----------------------------------------------------------------------------
33 * Local headers
34 *---------------------------------------------------------------------------*/
35
36#include "base/cs_base.h"
37
38/*=============================================================================
39 * Macro definitions
40 *===========================================================================*/
41
42/*============================================================================
43 * Type definitions
44 *===========================================================================*/
45
46/*=============================================================================
47 * Static global variables
48 *===========================================================================*/
49
50/*=============================================================================
51 * Public function prototypes
52 *===========================================================================*/
53
54#if defined(__cplusplus)
55
56/*----------------------------------------------------------------------------*/
70/*----------------------------------------------------------------------------*/
71
72template <typename T, typename U>
73void
75 cs_lnum_t r,
76 T a[],
77 U b[])
78{
79 cs_lnum_t size = r - l;
80
81 if (size == 0)
82 return;
83
84 /* Compute stride */
86 for (h = 1; h <= size/9; h = 3*h+1) ;
87
88 /* Sort array */
89 for ( ; h > 0; h /= 3) {
90
91 for (cs_lnum_t i = l+h; i < r; i++) {
92
93 T va = a[i];
94 U vb = b[i];
95
96 cs_lnum_t j = i;
97 while ( (j >= l+h) && (va < a[j-h]) ) {
98 a[j] = a[j-h];
99 b[j] = b[j-h];
100 j -= h;
101 }
102 a[j] = va;
103 b[j] = vb;
104
105 } /* Loop on array elements */
106
107 } /* End of loop on stride */
108}
109
110#endif // (__cplusplus)
111
112/*---------------------------------------------------------------------------*/
113
115
116/*----------------------------------------------------------------------------
117 * Sort an array "a" between its left bound "l" and its right bound "r"
118 * thanks to a shell sort (Knuth algorithm).
119 * Index location of the sorted array are stored in loc. a is unchanged.
120 *
121 * parameters:
122 * l <-- left bound
123 * r <-- right bound
124 * a <-> array to sort (not modified)
125 * loc <-> position by increasing order (size = r-l)
126 *---------------------------------------------------------------------------*/
127
128void
130 cs_lnum_t r,
131 const cs_lnum_t a[],
132 cs_lnum_t loc[]);
133
134/*----------------------------------------------------------------------------
135 * Sort an array "a" between its left bound "l" and its right bound "r"
136 * thanks to a shell sort (Knuth algorithm).
137 *
138 * parameters:
139 * l <-- left bound
140 * r <-- right bound
141 * a <-> array to sort
142 *---------------------------------------------------------------------------*/
143
144void
146 cs_lnum_t r,
147 cs_lnum_t a[]);
148
149/*----------------------------------------------------------------------------
150 * Sort an array of integers "a" between its left bound "l" and its
151 * right bound "r" using a shell sort (Knuth algorithm).
152 *
153 * parameters:
154 * l <-- left bound
155 * r <-- right bound
156 * a <-> array to sort
157 *---------------------------------------------------------------------------*/
158
159void
161 cs_lnum_t r,
162 int a[]);
163
164/*----------------------------------------------------------------------------
165 * Sort a global array "a" between its left bound "l" and its right bound "r"
166 * thanks to a shell sort (Knuth algorithm).
167 *
168 * parameters:
169 * l <-- left bound
170 * r <-- right bound
171 * a <-> array to sort
172 *---------------------------------------------------------------------------*/
173
174void
176 cs_lnum_t r,
177 cs_gnum_t a[]);
178
179/*----------------------------------------------------------------------------
180 * Sort an array "a" and apply the sort to its associated array "b" (local
181 * numbering)
182 * Sort is realized thanks to a shell sort (Knuth algorithm).
183 *
184 * parameters:
185 * l --> left bound
186 * r --> right bound
187 * a <-> array to sort
188 * b <-> associated array
189 *---------------------------------------------------------------------------*/
190
191void
193 cs_lnum_t r,
194 cs_lnum_t a[],
195 cs_lnum_t b[]);
196
197/*----------------------------------------------------------------------------
198 * Sort an array "a" and apply the sort to its associated array "b" (local
199 * numbering)
200 * Sort is realized thanks to a shell sort (Knuth algorithm).
201 *
202 * parameters:
203 * l --> left bound
204 * r --> right bound
205 * a <-> array to sort
206 * b <-> associated array
207 *---------------------------------------------------------------------------*/
208
209void
211 int r,
212 int a[],
213 double b[]);
214
215/*----------------------------------------------------------------------------
216 * Sort an array "a" and apply the sort to its associated array "b" (local
217 * numbering)
218 * Sort is realized thanks to a shell sort (Knuth algorithm).
219 *
220 * parameters:
221 * l --> left bound
222 * r --> right bound
223 * a <-> array to sort
224 * b <-> associated array
225 *---------------------------------------------------------------------------*/
226
227void
229 int r,
230 cs_lnum_t a[],
231 short int b[]);
232
233/*----------------------------------------------------------------------------
234 * Sort an array "a" and apply the sort to its associated array "b" (local
235 * numbering)
236 * Sort is realized thanks to a shell sort (Knuth algorithm).
237 *
238 * parameters:
239 * l --> left bound
240 * r --> right bound
241 * a <-> array to sort
242 * b <-> associated array
243 *---------------------------------------------------------------------------*/
244
245void
247 cs_lnum_t r,
248 cs_gnum_t a[],
249 cs_gnum_t b[]);
250
251/*----------------------------------------------------------------------------
252 * Order an array of local numbers.
253 *
254 * parameters:
255 * number <-> array of numbers to sort
256 * n_elts <-- number of elements considered
257 *----------------------------------------------------------------------------*/
258
259void
260cs_sort_lnum(cs_lnum_t number[],
261 size_t n_elts);
262
263/*----------------------------------------------------------------------------
264 * Sort rows of an indexed structure.
265 *
266 * parameters:
267 * n_elts <-- number of indexed elements
268 * elt_idx <-- element index (size: n_elts+1)
269 * elts <-> indexed values
270 *
271 * returns:
272 * true if no values were encountered multiple times in a given row
273 *---------------------------------------------------------------------------*/
274
275bool
277 const cs_lnum_t elt_idx[],
278 cs_lnum_t elts[]);
279
280/*----------------------------------------------------------------------------
281 * Sort rows of an indexed structure of global ids
282 *
283 * parameters:
284 * n_elts <-- number of indexed elements
285 * elt_idx <-- element index (size: n_elts+1)
286 * elts <-> indexed values
287 *
288 * returns:
289 * true if no values were encountered multiple times in a given row
290 *---------------------------------------------------------------------------*/
291
292bool
294 const cs_lnum_t elt_idx[],
295 cs_gnum_t elts[]);
296
297/*----------------------------------------------------------------------------
298 * Sort an array of global number and remove duplicate entries.
299 *
300 * The calling code may choose to reallocate the array to the new, compacted
301 * size; this is not done automatically, to avoid the overhead of memory
302 * management in cases where it is not useful (i.e. when the array is
303 * discarded soon after use).
304 *
305 * parameters:
306 * n_elts <-- initial number of elements
307 * elts <-> elements to sort
308 *
309 * returns:
310 * number of compacted elements
311 *---------------------------------------------------------------------------*/
312
315 cs_gnum_t elts[]);
316
317/*----------------------------------------------------------------------------
318 * Sort an array of global number couples and remove duplicate entries.
319 *
320 * Lexicographical ordering is considered.
321 *
322 * The calling code may choose to reallocate the array to the new, compacted
323 * size; this is not done automatically, to avoid the overhead of memory
324 * management in cases where it is not useful (i.e. when the array is
325 * discarded soon after use).
326 *
327 * parameters:
328 * n_elts <-- initial number of elements
329 * elts <-> elements to sort (size: n_elts*2, interleaved)
330 *
331 * returns:
332 * number of compacted elements
333 *---------------------------------------------------------------------------*/
334
337 cs_gnum_t elts[]);
338
339/*---------------------------------------------------------------------------*/
340
342
343#endif /* __CS_SORT_H__ */
#define BEGIN_C_DECLS
Definition: cs_defs.h:554
unsigned cs_gnum_t
global mesh entity number
Definition: cs_defs.h:342
#define END_C_DECLS
Definition: cs_defs.h:555
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:350
@ h
Definition: cs_field_pointer.h:94
void cs_sort_dcoupled_shell(int l, int r, int a[], double b[])
Definition: cs_sort.cpp:472
void cs_sort_int_shell(cs_lnum_t l, cs_lnum_t r, int a[])
Definition: cs_sort.cpp:345
void cs_sort_coupled_shell(cs_lnum_t l, cs_lnum_t r, T a[], U b[])
Sort an array "a" and apply the sort to its associated array "b".
Definition: cs_sort.h:74
void cs_sort_coupled_gnum_shell(cs_lnum_t l, cs_lnum_t r, cs_gnum_t a[], cs_gnum_t b[])
Definition: cs_sort.cpp:573
void cs_sort_shell(cs_lnum_t l, cs_lnum_t r, cs_lnum_t a[])
Definition: cs_sort.cpp:310
void cs_sort_sicoupled_shell(int l, int r, cs_lnum_t a[], short int b[])
Definition: cs_sort.cpp:523
cs_lnum_t cs_sort_and_compact_gnum_2(cs_lnum_t n_elts, cs_gnum_t elts[])
Definition: cs_sort.cpp:860
void cs_sort_shell_inplace(cs_lnum_t l, cs_lnum_t r, const cs_lnum_t a[], cs_lnum_t loc[])
Definition: cs_sort.cpp:268
bool cs_sort_indexed_gnum(cs_lnum_t n_elts, const cs_lnum_t elt_idx[], cs_gnum_t elts[])
Definition: cs_sort.cpp:725
void cs_sort_lnum(cs_lnum_t number[], size_t n_elts)
Definition: cs_sort.cpp:623
void cs_sort_gnum_shell(cs_lnum_t l, cs_lnum_t r, cs_gnum_t a[])
Definition: cs_sort.cpp:380
cs_lnum_t cs_sort_and_compact_gnum(cs_lnum_t n_elts, cs_gnum_t elts[])
Definition: cs_sort.cpp:764
bool cs_sort_indexed(cs_lnum_t n_elts, const cs_lnum_t elt_idx[], cs_lnum_t elts[])
Definition: cs_sort.cpp:688