10 #ifndef TPETRA_DETAILS_SHORTSORT_HPP
11 #define TPETRA_DETAILS_SHORTSORT_HPP
21 #include "TpetraCore_config.h"
22 #include "Kokkos_Macros.hpp"
23 #include <type_traits>
29 #ifdef TPETRA_DETAILS_SWAP_KEYSANDVALUES
30 # error "The TPETRA_DETAILS_SWAP_KEYSANDVALUES macro is already defined."
31 #endif // TPETRA_DETAILS_SWAP_KEYSANDVALUES
53 #define TPETRA_DETAILS_SWAP_KEYSANDVALUES( i, j ) \
54 if (keys[i] > keys[j]) { \
55 const KeyType tmpKey (keys[i]); \
58 const ValueType tmpVal (values[i]); \
59 values[i] = values[j]; \
64 #ifdef TPETRA_DETAILS_SWAP_KEYS
65 # error "The TPETRA_DETAILS_SWAP_KEYS macro is already defined."
66 #endif // TPETRA_DETAILS_SWAP_KEYSANDVALUES
85 #define TPETRA_DETAILS_SWAP_KEYS( i, j ) \
86 if (keys[i] > keys[j]) { \
87 const KeyType tmpKey (keys[i]); \
102 template<
class KeyType,
class ValueType>
118 template<
class KeyType>
138 template<
class KeyType,
class ValueType>
160 template<
class KeyType>
186 template<
class KeyType,
class ValueType>
210 template<
class KeyType>
238 template<
class KeyType,
class ValueType>
276 template<
class KeyType>
313 template<
class KeyType,
class ValueType,
class IndexType>
319 static_assert (std::is_integral<IndexType>::value,
320 "IndexType must be a signed integer type.");
321 static_assert (std::is_signed<IndexType>::value,
322 "IndexType must be a signed integer type. "
323 "This implementation does a count-down loop, "
324 "and may thus loop forever "
325 "if one attempts to use it with unsigned IndexType.");
326 constexpr IndexType
ZERO = 0;
327 IndexType midpoint = n /
static_cast<IndexType
> (2);
329 while (midpoint > ZERO) {
331 const IndexType theMax = n - midpoint;
332 for (IndexType j = 0; j < theMax; ++j) {
334 for (IndexType k = j; k >= 0; k -= midpoint) {
335 if (keys[k + midpoint] >= keys[k]) {
338 const KeyType tmpKey = keys[k + midpoint];
339 keys[k + midpoint] = keys[k];
341 const ValueType tmpVal = values[k + midpoint];
342 values[k + midpoint] = values[k];
346 midpoint = midpoint / 2;
354 template<
class KeyType,
class IndexType>
358 static_assert (std::is_integral<IndexType>::value,
359 "IndexType must be a signed integer type.");
360 static_assert (std::is_signed<IndexType>::value,
361 "IndexType must be a signed integer type. "
362 "This implementation does a count-down loop, "
363 "and may thus loop forever "
364 "if one attempts to use it with unsigned IndexType.");
365 constexpr IndexType
ZERO = 0;
366 IndexType midpoint = n /
static_cast<IndexType
> (2);
368 while (midpoint > ZERO) {
370 const IndexType theMax = n - midpoint;
371 for (
int j = 0; j < theMax; ++j) {
373 for (
int k = j; k >= 0; k -= midpoint) {
374 if (keys[k + midpoint] >= keys[k]) {
377 const KeyType tmpKey = keys[k + midpoint];
378 keys[k + midpoint] = keys[k];
382 midpoint = midpoint / 2;
386 template<
typename KeyType,
typename ValueType,
typename IndexType>
388 shellSortKeysAndValues2(KeyType* keys, ValueType* values, IndexType n)
390 IndexType ngaps = 10;
392 IndexType gaps[] = {3548, 1577, 701, 301, 132, 57, 23, 10, 4, 1};
393 for(IndexType gapIndex = 0; gapIndex < ngaps; gapIndex++)
395 auto gap = gaps[gapIndex];
399 for(IndexType gapOffset = 0; gapOffset < gap; gapOffset++)
401 for(IndexType i = gap + gapOffset; i < n; i += gap)
404 if(keys[i - gap] > keys[i])
406 IndexType finalPos = i - gap;
407 while(finalPos - gap >= 0 && keys[finalPos - gap] > keys[i])
412 auto tempKey = keys[i];
413 auto tempVal = values[i];
414 for(IndexType j = i - gap; j >= finalPos; j -= gap)
416 keys[j + gap] = keys[j];
417 values[j + gap] = values[j];
419 keys[finalPos] = tempKey;
420 values[finalPos] = tempVal;
431 #endif // TPETRA_DETAILS_SHORTSORT_HPP
KOKKOS_FUNCTION void shortSortKeys_8(KeyType keys[8])
Sort length-8 array of keys.
KOKKOS_FUNCTION void shortSortKeys_3(KeyType keys[3])
Sort length-3 array of keys.
KOKKOS_FUNCTION void shortSortKeysAndValues_4(KeyType keys[4], ValueType values[4])
Sort keys and values jointly, by keys, for arrays of length 4.
KOKKOS_FUNCTION void shortSortKeys_4(KeyType keys[4])
Sort length-4 array of keys.
KOKKOS_FUNCTION void shortSortKeysAndValues_2(KeyType keys[2], ValueType values[2])
Sort keys and values jointly, by keys, for arrays of length 2.
KOKKOS_FUNCTION void shellSortKeys(KeyType keys[], const IndexType n)
Shellsort (yes, it's one word) the input array keys.
KOKKOS_FUNCTION void shortSortKeys_2(KeyType keys[2])
Sort length-2 array of keys.
KOKKOS_FUNCTION void shortSortKeysAndValues_3(KeyType keys[3], ValueType values[3])
Sort keys and values jointly, by keys, for arrays of length 3.
KOKKOS_FUNCTION void shellSortKeysAndValues(KeyType keys[], ValueType values[], const IndexType n)
Shellsort (yes, it's one word) the input array keys, and apply the resulting permutation to the input...
#define TPETRA_DETAILS_SWAP_KEYS(i, j)
Macro that swaps the i and j entries of keys, if keys[i] > keysj.
Replace old values with zero.
KOKKOS_FUNCTION void shortSortKeysAndValues_8(KeyType keys[8], ValueType values[8])
Sort keys and values jointly, by keys, for arrays of length 8.
#define TPETRA_DETAILS_SWAP_KEYSANDVALUES(i, j)
Macro that swaps the i and j entries of keys and values, if keys[i] > keysj.