Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_Sort.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef _ZOLTAN2_SORT_HPP_
11 #define _ZOLTAN2_SORT_HPP_
12 
13 #include <Teuchos_Array.hpp>
14 #include <algorithm>
15 
16 
21 
22 // TODO: This is a generic utility class; should move this source file.
23 // We could perhaps use Sort2 from Tpetra, but that uses a custom sort not std::sort
24 
25 namespace Zoltan2{
26 
27 namespace Details {
28 
29  // These helper functions can't be class members, so put in Details namespace.
30  template<class KeyType, class ValueType>
31  bool SortInc (const std::pair<KeyType, ValueType>& a, const std::pair<KeyType, ValueType>& b)
32  {
33  return a.second < b.second;
34  }
35 
36  template<class KeyType, class ValueType>
37  bool SortDec (const std::pair<KeyType, ValueType>& a, const std::pair<KeyType, ValueType>& b)
38  {
39  return a.second > b.second;
40  }
41 
42 } // namespace Details
43 
44 template <typename key_t, typename value_t>
45 class SortPairs
46 {
47  public:
49  {
50  }
51 
52  public:
53  void sort(Teuchos::Array<std::pair<key_t,value_t> > &listofPairs, bool inc=true)
54  {
55  // Sort in increasing (default) or decreasing order of value
56  if (inc)
57  std::stable_sort (listofPairs.begin(), listofPairs.end(), Details::SortInc<key_t, value_t>);
58  else
59  std::stable_sort (listofPairs.begin(), listofPairs.end(), Details::SortDec<key_t, value_t>);
60  }
61 
62 };
63 }
64 #endif
bool SortDec(const std::pair< KeyType, ValueType > &a, const std::pair< KeyType, ValueType > &b)
void sort(Teuchos::Array< std::pair< key_t, value_t > > &listofPairs, bool inc=true)
bool SortInc(const std::pair< KeyType, ValueType > &a, const std::pair< KeyType, ValueType > &b)