9.2
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-2026 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/*----------------------------------------------------------------------------*/
68/*----------------------------------------------------------------------------*/
69
70template <typename T, typename U>
71void
73 cs_lnum_t r,
74 T a[],
75 U b[])
76{
77 cs_lnum_t size = r - l;
78
79 if (size == 0)
80 return;
81
82 /* Compute stride */
84 for (h = 1; h <= size/9; h = 3*h+1) ;
85
86 /* Sort array */
87 for ( ; h > 0; h /= 3) {
88
89 for (cs_lnum_t i = l+h; i < r; i++) {
90
91 T va = a[i];
92 U vb = b[i];
93
94 cs_lnum_t j = i;
95 while ( (j >= l+h) && (va < a[j-h]) ) {
96 a[j] = a[j-h];
97 b[j] = b[j-h];
98 j -= h;
99 }
100 a[j] = va;
101 b[j] = vb;
102
103 } /* Loop on array elements */
104
105 } /* End of loop on stride */
106}
107
108/*----------------------------------------------------------------------------
109 * Sort an array "a" between its left bound "l" and its right bound "r"
110 * thanks to a shell sort (Knuth algorithm).
111 * Index location of the sorted array are stored in loc. a is unchanged.
112 *
113 * parameters:
114 * l <-- left bound
115 * r <-- right bound
116 * a <-> array to sort (not modified)
117 * loc <-> position by increasing order (size = r-l)
118 *---------------------------------------------------------------------------*/
119
120void
122 cs_lnum_t r,
123 const cs_lnum_t a[],
124 cs_lnum_t loc[]);
125
126/*----------------------------------------------------------------------------
127 * Sort an array "a" between its left bound "l" and its right bound "r"
128 * thanks to a shell sort (Knuth algorithm).
129 *
130 * parameters:
131 * l <-- left bound
132 * r <-- right bound
133 * a <-> array to sort
134 *---------------------------------------------------------------------------*/
135
136void
138 cs_lnum_t r,
139 cs_lnum_t a[]);
140
141/*----------------------------------------------------------------------------
142 * Sort an array of integers "a" between its left bound "l" and its
143 * right bound "r" using a shell sort (Knuth algorithm).
144 *
145 * parameters:
146 * l <-- left bound
147 * r <-- right bound
148 * a <-> array to sort
149 *---------------------------------------------------------------------------*/
150
151void
153 cs_lnum_t r,
154 int a[]);
155
156/*----------------------------------------------------------------------------
157 * Sort a global array "a" between its left bound "l" and its right bound "r"
158 * thanks to a shell sort (Knuth algorithm).
159 *
160 * parameters:
161 * l <-- left bound
162 * r <-- right bound
163 * a <-> array to sort
164 *---------------------------------------------------------------------------*/
165
166void
168 cs_lnum_t r,
169 cs_gnum_t a[]);
170
171/*----------------------------------------------------------------------------
172 * Sort an array "a" and apply the sort to its associated array "b" (local
173 * numbering)
174 * Sort is realized thanks to a shell sort (Knuth algorithm).
175 *
176 * parameters:
177 * l --> left bound
178 * r --> right bound
179 * a <-> array to sort
180 * b <-> associated array
181 *---------------------------------------------------------------------------*/
182
183void
185 cs_lnum_t r,
186 cs_lnum_t a[],
187 cs_lnum_t b[]);
188
189/*----------------------------------------------------------------------------
190 * Sort an array "a" and apply the sort to its associated array "b" (local
191 * numbering)
192 * Sort is realized thanks to a shell sort (Knuth algorithm).
193 *
194 * parameters:
195 * l --> left bound
196 * r --> right bound
197 * a <-> array to sort
198 * b <-> associated array
199 *---------------------------------------------------------------------------*/
200
201void
203 int r,
204 int a[],
205 double b[]);
206
207/*----------------------------------------------------------------------------
208 * Sort an array "a" and apply the sort to its associated array "b" (local
209 * numbering)
210 * Sort is realized thanks to a shell sort (Knuth algorithm).
211 *
212 * parameters:
213 * l --> left bound
214 * r --> right bound
215 * a <-> array to sort
216 * b <-> associated array
217 *---------------------------------------------------------------------------*/
218
219void
221 int r,
222 cs_lnum_t a[],
223 short int b[]);
224
225/*----------------------------------------------------------------------------
226 * Sort an array "a" and apply the sort to its associated array "b" (local
227 * numbering)
228 * Sort is realized thanks to a shell sort (Knuth algorithm).
229 *
230 * parameters:
231 * l --> left bound
232 * r --> right bound
233 * a <-> array to sort
234 * b <-> associated array
235 *---------------------------------------------------------------------------*/
236
237void
239 cs_lnum_t r,
240 cs_gnum_t a[],
241 cs_gnum_t b[]);
242
243/*----------------------------------------------------------------------------
244 * Order an array of local numbers.
245 *
246 * parameters:
247 * number <-> array of numbers to sort
248 * n_elts <-- number of elements considered
249 *----------------------------------------------------------------------------*/
250
251void
252cs_sort_lnum(cs_lnum_t number[],
253 size_t n_elts);
254
255/*----------------------------------------------------------------------------
256 * Sort rows of an indexed structure.
257 *
258 * parameters:
259 * n_elts <-- number of indexed elements
260 * elt_idx <-- element index (size: n_elts+1)
261 * elts <-> indexed values
262 *
263 * returns:
264 * true if no values were encountered multiple times in a given row
265 *---------------------------------------------------------------------------*/
266
267bool
269 const cs_lnum_t elt_idx[],
270 cs_lnum_t elts[]);
271
272/*----------------------------------------------------------------------------
273 * Sort rows of an indexed structure of global ids
274 *
275 * parameters:
276 * n_elts <-- number of indexed elements
277 * elt_idx <-- element index (size: n_elts+1)
278 * elts <-> indexed values
279 *
280 * returns:
281 * true if no values were encountered multiple times in a given row
282 *---------------------------------------------------------------------------*/
283
284bool
286 const cs_lnum_t elt_idx[],
287 cs_gnum_t elts[]);
288
289/*----------------------------------------------------------------------------
290 * Sort an array of global number and remove duplicate entries.
291 *
292 * The calling code may choose to reallocate the array to the new, compacted
293 * size; this is not done automatically, to avoid the overhead of memory
294 * management in cases where it is not useful (i.e. when the array is
295 * discarded soon after use).
296 *
297 * parameters:
298 * n_elts <-- initial number of elements
299 * elts <-> elements to sort
300 *
301 * returns:
302 * number of compacted elements
303 *---------------------------------------------------------------------------*/
304
307 cs_gnum_t elts[]);
308
309/*----------------------------------------------------------------------------
310 * Sort an array of global number couples and remove duplicate entries.
311 *
312 * Lexicographical ordering is considered.
313 *
314 * The calling code may choose to reallocate the array to the new, compacted
315 * size; this is not done automatically, to avoid the overhead of memory
316 * management in cases where it is not useful (i.e. when the array is
317 * discarded soon after use).
318 *
319 * parameters:
320 * n_elts <-- initial number of elements
321 * elts <-> elements to sort (size: n_elts*2, interleaved)
322 *
323 * returns:
324 * number of compacted elements
325 *---------------------------------------------------------------------------*/
326
329 cs_gnum_t elts[]);
330
331/*---------------------------------------------------------------------------*/
332
333#endif /* CS_SORT_H */
unsigned cs_gnum_t
global mesh entity number
Definition: cs_defs.h:317
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:325
@ h
Definition: cs_field_pointer.h:90
void cs_sort_dcoupled_shell(int l, int r, int a[], double b[])
Definition: cs_sort.cpp:470
void cs_sort_int_shell(cs_lnum_t l, cs_lnum_t r, int a[])
Definition: cs_sort.cpp:343
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:72
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:571
void cs_sort_shell(cs_lnum_t l, cs_lnum_t r, cs_lnum_t a[])
Definition: cs_sort.cpp:308
void cs_sort_sicoupled_shell(int l, int r, cs_lnum_t a[], short int b[])
Definition: cs_sort.cpp:521
cs_lnum_t cs_sort_and_compact_gnum_2(cs_lnum_t n_elts, cs_gnum_t elts[])
Definition: cs_sort.cpp:858
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:266
bool cs_sort_indexed_gnum(cs_lnum_t n_elts, const cs_lnum_t elt_idx[], cs_gnum_t elts[])
Definition: cs_sort.cpp:723
void cs_sort_lnum(cs_lnum_t number[], size_t n_elts)
Definition: cs_sort.cpp:621
void cs_sort_gnum_shell(cs_lnum_t l, cs_lnum_t r, cs_gnum_t a[])
Definition: cs_sort.cpp:378
cs_lnum_t cs_sort_and_compact_gnum(cs_lnum_t n_elts, cs_gnum_t elts[])
Definition: cs_sort.cpp:762
bool cs_sort_indexed(cs_lnum_t n_elts, const cs_lnum_t elt_idx[], cs_lnum_t elts[])
Definition: cs_sort.cpp:686