IOSS  2.0
Ioss_Sort.h
Go to the documentation of this file.
1 // Copyright(C) 1999-2020 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6 
7 #ifndef IOSS_Ioss_Sort_h
8 #define IOSS_Ioss_Sort_h
9 
10 #include <cstddef>
11 #include <vector>
12 
13 // This is used instead of the std::sort since we were having issues
14 // with the std::sort on some compiler versions with certain options
15 // enabled (-fopenmp). If this shows up as a hotspot in performance
16 // measurements, then we can use std::sort on most platforms and just
17 // use this version where there are compiler issues.
18 
19 // Using Explicit Template Instantiation with the types:
20 //
21 // std::vector<int>, std::vector<int64_t>, std::vector<std::pair<int64_t,int64_t>>
22 //
23 // Update in Ioss_Sort.C if other types are needed.
24 
25 namespace {
26  const int QSORT_CUTOFF = 12;
27 
28  template <typename INT> void SWAP(INT *V, size_t I, size_t J) { std::swap(V[I], V[J]); }
29 
30  template <typename INT> void order3(INT v[], size_t left, size_t center, size_t right)
31  {
32  if (v[left] > v[center]) {
33  SWAP(v, left, center);
34  }
35  if (v[left] > v[right]) {
36  SWAP(v, left, right);
37  }
38  if (v[center] > v[right]) {
39  SWAP(v, center, right);
40  }
41  }
42 
43  template <typename INT> size_t median3(INT v[], size_t left, size_t right)
44  {
45  size_t center = (left + right) / 2;
46  size_t pl = left;
47  size_t pm = center;
48  size_t pr = right;
49 
50  if (right - left > 40) {
51  size_t s = (right - left) / 8;
52  order3(v, left, left + s, left + 2 * s);
53  order3(v, center - s, center, center + s);
54  order3(v, right - 2 * s, right - s, right);
55 
56  // Now set up to get median of the 3 medians...
57  pl = left + s;
58  pm = center;
59  pr = right - s;
60  }
61  order3(v, pl, pm, pr);
62 
63  SWAP(v, center, right - 1);
64  return right - 1;
65  }
66 
67  template <typename INT> void qsort_int(INT v[], size_t left, size_t right)
68  {
69  if (left + QSORT_CUTOFF < right) {
70  size_t pivot = median3(v, left, right);
71  size_t i = left;
72  size_t j = right - 1;
73 
74  for (;;) {
75  while (v[++i] < v[pivot]) {
76  ;
77  }
78  while (v[--j] > v[pivot]) {
79  ;
80  }
81  if (i < j) {
82  SWAP(v, i, j);
83  }
84  else {
85  break;
86  }
87  }
88 
89  SWAP(v, i, right - 1);
90  qsort_int(v, left, i - 1);
91  qsort_int(v, i + 1, right);
92  }
93  }
94 
95  template <typename INT> void isort_int(INT v[], size_t N)
96  {
97  size_t j;
98  size_t ndx = 0;
99 
100  if (N <= 1) {
101  return;
102  }
103  INT small = v[0];
104  for (size_t i = 1; i < N; i++) {
105  if (v[i] < small) {
106  small = v[i];
107  ndx = i;
108  }
109  }
110  /* Put smallest value in slot 0 */
111  SWAP(v, 0, ndx);
112 
113  for (size_t i = 1; i < N; i++) {
114  INT tmp = v[i];
115  for (j = i; tmp < v[j - 1]; j--) {
116  v[j] = v[j - 1];
117  }
118  v[j] = tmp;
119  }
120  }
121 } // namespace
122 
123 namespace Ioss {
124  template <typename INT> void qsort(std::vector<INT> &v)
125  {
126  if (v.size() <= 1) {
127  return;
128  }
129  qsort_int(v.data(), 0, v.size() - 1);
130  isort_int(v.data(), v.size());
131  }
132 } // namespace Ioss
133 
134 #endif
Ioss::qsort
void qsort(std::vector< INT > &v)
Definition: Ioss_Sort.h:124
Ioss
The main namespace for the Ioss library.
Definition: Ioad_DatabaseIO.C:40
INT
int INT
Definition: Ioss_StructuredBlock.h:27
anonymous_namespace{Ioss_Sort.h}::qsort_int
void qsort_int(INT v[], size_t left, size_t right)
Definition: Ioss_Sort.h:67
anonymous_namespace{Ioss_Sort.h}::median3
size_t median3(INT v[], size_t left, size_t right)
Definition: Ioss_Sort.h:43
anonymous_namespace{Ioss_Sort.h}::isort_int
void isort_int(INT v[], size_t N)
Definition: Ioss_Sort.h:95
anonymous_namespace{Ioss_Sort.h}::order3
void order3(INT v[], size_t left, size_t center, size_t right)
Definition: Ioss_Sort.h:30
anonymous_namespace{Ioss_Sort.h}::SWAP
void SWAP(INT *V, size_t I, size_t J)
Definition: Ioss_Sort.h:28
anonymous_namespace{Ioss_Sort.h}::QSORT_CUTOFF
const int QSORT_CUTOFF
Definition: Ioss_Sort.h:26