vnl_index_sort.h
Go to the documentation of this file.
1 #ifndef vnl_index_sort_h_
2 #define vnl_index_sort_h_
3 //:
4 // \file
5 // \author Michael R. Bowers
6 //
7 
8 #include <algorithm>
9 #include <utility>
10 #include <vector>
11 #include <vnl/vnl_vector.h>
12 #ifdef _MSC_VER
13 # include <vcl_msvc_warnings.h>
14 #endif
15 #include "vnl/vnl_export.h"
16 
17 template <class TValue, class TIndex>
19 {
20  public:
21 
22  //: typedefs for vector sorting
25 
26  //: typedefs for matrix sorting
29 
30  //: matrix sort along rows or columns?
32 
33  //: just sort indices
35  const SortVectorType& values,
36  SortVectorIndexType& indices)
37  {
38  sortIndices(values, indices);
39  }
40 
41  //: sort indices and values
43  const SortVectorType& values,
44  SortVectorType& sorted_values,
45  SortVectorIndexType& indices)
46  {
47  vector_sort(values, indices);
48 
49  // gets values from sorted indices
50  reindexValues(values, indices, sorted_values);
51  }
52 
53  //: sort indices, return sorted values in place
55  SortVectorType& values,
56  SortVectorIndexType& indices)
57  {
58  vector_sort(values, indices);
59  SortVectorType tmpValues(values);
60 
61  // gets values and indices from sorted indices
62  reindexValues(tmpValues, indices, values);
63  }
64 
65  //: matrix sort
66  // specify along rows or columns
68  DirectionType direction,
69  const SortMatrixType& values,
70  SortMatrixType& sorted_values,
71  SortMatrixIndexType& indices)
72  {
73  sorted_values.set_size(values.rows(), values.cols());
74  indices.set_size(values.rows(), values.cols());
75 
76  SortVectorType valVect;
77  SortVectorType sortedValVect;
78  SortVectorIndexType indVect;
79  for (unsigned int vIx = 0;
80  vIx < (direction == ByRow ? values.rows() : values.cols()); vIx++)
81  {
82  getVector(values, direction, vIx, valVect);
83  vector_sort(valVect, sortedValVect, indVect);
84  putVector(sortedValVect, direction, vIx, sorted_values);
85  putVector(indVect, direction, vIx, indices);
86  }
87  }
88 
89  private:
90  //: Implementation class - Do Not Use.
91  // Author - Ian Scott
92  template <class T, class I>
94  {
95  const T *data;
96  bool operator () (const I &a, const I &b)
97  {
98  return data[a] < data[b];
99  }
100  };
101 
102  //: sort the indices of a vector
103  // Author - Ian Scott
105  {
107  c.data = v.data_block();
108  s.set_size(v.size());
109 
110  for (TIndex ix = 0; ix < (TIndex) v.size(); ix++) s[ix] = ix;
111 
112  std::sort(s.begin(), s.end(), c);
113  }
114 
115  //: reorder values from sorted indices
117  const SortVectorType& values,
118  const SortVectorIndexType& indices,
119  SortVectorType& sorted_values)
120  {
121  sorted_values.set_size(values.size());
122  for (TIndex ix = 0; ix < (TIndex) values.size(); ix++)
123  sorted_values[ix] = values[indices[ix]];
124  }
125 
126  //: get specified vector from matrix depending on direction
127  template<class T>
128  void getVector(
129  const vnl_matrix<T>& fromMat,
130  DirectionType direction,
131  int whichVect,
132  vnl_vector<T>& toVect)
133  {
134  switch (direction)
135  {
136  case ByRow:
137  toVect = fromMat.get_row(whichVect);
138  break;
139  case ByColumn:
140  toVect = fromMat.get_column(whichVect);
141  break;
142  default:
143  toVect.clear();
144  break;
145  }
146  }
147 
148  //: put specified vector to matrix depending on direction
149  template<class T>
150  void putVector(
151  const vnl_vector<T>& fromVect,
152  DirectionType direction,
153  int whichVect,
154  vnl_matrix<T>& toMat)
155  {
156  switch (direction)
157  {
158  case ByRow:
159  toMat.set_row(whichVect, fromVect);
160  break;
161  case ByColumn:
162  toMat.set_column(whichVect, fromVect);
163  break;
164  default:
165  break;
166  }
167  }
168 };
169 
170 #endif
unsigned int cols() const
Return the number of columns.
Definition: vnl_matrix.h:183
void vector_sort_in_place(SortVectorType &values, SortVectorIndexType &indices)
sort indices, return sorted values in place.
Implementation class - Do Not Use.
void matrix_sort(DirectionType direction, const SortMatrixType &values, SortMatrixType &sorted_values, SortMatrixIndexType &indices)
matrix sort.
void sortIndices(const SortVectorType &v, SortVectorIndexType &s)
sort the indices of a vector.
bool set_size(size_t n)
Resize to n elements.
Definition: vnl_vector.hxx:250
void vector_sort(const SortVectorType &values, SortVectorIndexType &indices)
just sort indices.
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
vnl_matrix< TValue > SortMatrixType
typedefs for matrix sorting.
vnl_vector< TValue > SortVectorType
typedefs for vector sorting.
enum vnl_index_sort::DirectionType Direction
iterator end()
Iterator pointing to element beyond end of data.
Definition: vnl_vector.h:246
#define v
Definition: vnl_vector.h:42
vnl_matrix & set_column(unsigned i, T const *v)
Set the elements of the i'th column to v[i] (No bounds checking).
void putVector(const vnl_vector< T > &fromVect, DirectionType direction, int whichVect, vnl_matrix< T > &toMat)
put specified vector to matrix depending on direction.
void vector_sort(const SortVectorType &values, SortVectorType &sorted_values, SortVectorIndexType &indices)
sort indices and values.
void getVector(const vnl_matrix< T > &fromMat, DirectionType direction, int whichVect, vnl_vector< T > &toVect)
get specified vector from matrix depending on direction.
iterator begin()
Iterator pointing to start of data.
Definition: vnl_vector.h:243
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
void clear()
Make the vector as if it had been default-constructed.
Definition: vnl_vector.hxx:240
bool operator()(const I &a, const I &b)
vnl_vector< TIndex > SortVectorIndexType
Mathematical vector class, templated by type of element.
Definition: vnl_fwd.h:16
vnl_vector< T > get_column(unsigned c) const
Get a vector equal to the given column.
Definition: vnl_matrix.hxx:977
vnl_vector< T > get_row(unsigned r) const
Get a vector equal to the given row.
Definition: vnl_matrix.hxx:962
vnl_matrix & set_row(unsigned i, T const *v)
Set the elements of the i'th row to v[i] (No bounds checking).
DirectionType
matrix sort along rows or columns?.
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
void reindexValues(const SortVectorType &values, const SortVectorIndexType &indices, SortVectorType &sorted_values)
reorder values from sorted indices.
bool set_size(unsigned r, unsigned c)
Resize to r rows by c columns. Old data lost.
Definition: vnl_matrix.hxx:390
vnl_matrix< TIndex > SortMatrixIndexType