LOOS  v2.3.2
sorting.hpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2009, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 
24 #if !defined(LOOS_SORTING_HPP)
25 #define LOOS_SORTING_HPP
26 
27 
28 #include <vector>
29 #include <algorithm>
30 #include <stdexcept>
31 
32 #include <loos_defs.hpp>
33 
34 namespace loos {
35 
37  template<typename T>
38  class AscendingSort {
39  public:
40  AscendingSort(const T& A) : A_(A) { }
41  bool operator()(const uint i, const uint j) const {
42  return(A_[i] < A_[j]);
43  }
44 
45  private:
46  const T& A_;
47  };
48 
50  template<typename T>
52  public:
53  DescendingSort(const T& A) : A_(A) { }
54  bool operator()(const uint i, const uint j) const {
55  return(A_[i] > A_[j]);
56  }
57 
58  private:
59  const T& A_;
60  };
61 
62 
65 
70  template<typename T, class SortPredicate>
71  std::vector<uint> sortedIndex(const T& A) {
72  std::vector<uint> indices(A.size());
73 
74  for (uint i = 0; i<A.size(); ++i)
75  indices[i] = i;
76 
77  std::sort(indices.begin(), indices.end(), SortPredicate(A));
78  return(indices);
79  }
80 
82  template<typename T>
83  std::vector<uint> sortedIndex(const T& A) {
84  return(sortedIndex<T, AscendingSort<T> >(A));
85  }
86 
87 }
88 
89 
90 
91 #endif
Policy class for sorting in descending sequence.
Definition: sorting.hpp:51
std::vector< uint > sortedIndex(const T &A)
Definition: sorting.hpp:71
Namespace for most things not already encapsulated within a class.
Policy class for sorting in ascending sequence.
Definition: sorting.hpp:38