44 #ifndef TPETRA_DETAILS_SHORTSORT_HPP
45 #define TPETRA_DETAILS_SHORTSORT_HPP
55 #include "TpetraCore_config.h"
56 #include "Kokkos_Macros.hpp"
57 #include <type_traits>
63 #ifdef TPETRA_DETAILS_SWAP_KEYSANDVALUES
64 # error "The TPETRA_DETAILS_SWAP_KEYSANDVALUES macro is already defined."
65 #endif // TPETRA_DETAILS_SWAP_KEYSANDVALUES
87 #define TPETRA_DETAILS_SWAP_KEYSANDVALUES( i, j ) \
88 if (keys[i] > keys[j]) { \
89 const KeyType tmpKey (keys[i]); \
92 const ValueType tmpVal (values[i]); \
93 values[i] = values[j]; \
98 #ifdef TPETRA_DETAILS_SWAP_KEYS
99 # error "The TPETRA_DETAILS_SWAP_KEYS macro is already defined."
100 #endif // TPETRA_DETAILS_SWAP_KEYSANDVALUES
119 #define TPETRA_DETAILS_SWAP_KEYS( i, j ) \
120 if (keys[i] > keys[j]) { \
121 const KeyType tmpKey (keys[i]); \
136 template<
class KeyType,
class ValueType>
152 template<
class KeyType>
172 template<
class KeyType,
class ValueType>
194 template<
class KeyType>
220 template<
class KeyType,
class ValueType>
244 template<
class KeyType>
272 template<
class KeyType,
class ValueType>
310 template<
class KeyType>
347 template<
class KeyType,
class ValueType,
class IndexType>
353 static_assert (std::is_integral<IndexType>::value,
354 "IndexType must be a signed integer type.");
355 static_assert (std::is_signed<IndexType>::value,
356 "IndexType must be a signed integer type. "
357 "This implementation does a count-down loop, "
358 "and may thus loop forever "
359 "if one attempts to use it with unsigned IndexType.");
360 constexpr IndexType
ZERO = 0;
361 IndexType midpoint = n /
static_cast<IndexType
> (2);
363 while (midpoint > ZERO) {
365 const IndexType theMax = n - midpoint;
366 for (IndexType j = 0; j < theMax; ++j) {
368 for (IndexType k = j; k >= 0; k -= midpoint) {
369 if (keys[k + midpoint] >= keys[k]) {
372 const KeyType tmpKey = keys[k + midpoint];
373 keys[k + midpoint] = keys[k];
375 const ValueType tmpVal = values[k + midpoint];
376 values[k + midpoint] = values[k];
380 midpoint = midpoint / 2;
388 template<
class KeyType,
class IndexType>
392 static_assert (std::is_integral<IndexType>::value,
393 "IndexType must be a signed integer type.");
394 static_assert (std::is_signed<IndexType>::value,
395 "IndexType must be a signed integer type. "
396 "This implementation does a count-down loop, "
397 "and may thus loop forever "
398 "if one attempts to use it with unsigned IndexType.");
399 constexpr IndexType
ZERO = 0;
400 IndexType midpoint = n /
static_cast<IndexType
> (2);
402 while (midpoint > ZERO) {
404 const IndexType theMax = n - midpoint;
405 for (
int j = 0; j < theMax; ++j) {
407 for (
int k = j; k >= 0; k -= midpoint) {
408 if (keys[k + midpoint] >= keys[k]) {
411 const KeyType tmpKey = keys[k + midpoint];
412 keys[k + midpoint] = keys[k];
416 midpoint = midpoint / 2;
420 template<
typename KeyType,
typename ValueType,
typename IndexType>
422 shellSortKeysAndValues2(KeyType* keys, ValueType* values, IndexType n)
424 IndexType ngaps = 10;
426 IndexType gaps[] = {3548, 1577, 701, 301, 132, 57, 23, 10, 4, 1};
427 for(IndexType gapIndex = 0; gapIndex < ngaps; gapIndex++)
429 auto gap = gaps[gapIndex];
433 for(IndexType gapOffset = 0; gapOffset < gap; gapOffset++)
435 for(IndexType i = gap + gapOffset; i < n; i += gap)
438 if(keys[i - gap] > keys[i])
440 IndexType finalPos = i - gap;
441 while(finalPos - gap >= 0 && keys[finalPos - gap] > keys[i])
446 auto tempKey = keys[i];
447 auto tempVal = values[i];
448 for(IndexType j = i - gap; j >= finalPos; j -= gap)
450 keys[j + gap] = keys[j];
451 values[j + gap] = values[j];
453 keys[finalPos] = tempKey;
454 values[finalPos] = tempVal;
465 #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.