Mercurial > hg > segmenter-vamp-plugin
comparison armadillo-3.900.4/include/armadillo_bits/op_sort_meat.hpp @ 49:1ec0e2823891
Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author | Chris Cannam |
---|---|
date | Thu, 13 Jun 2013 10:25:24 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
48:69251e11a913 | 49:1ec0e2823891 |
---|---|
1 // Copyright (C) 2008-2012 NICTA (www.nicta.com.au) | |
2 // Copyright (C) 2008-2012 Conrad Sanderson | |
3 // | |
4 // This Source Code Form is subject to the terms of the Mozilla Public | |
5 // License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 | |
8 | |
9 //! \addtogroup op_sort | |
10 //! @{ | |
11 | |
12 | |
13 | |
14 template<typename eT> | |
15 class arma_ascend_sort_helper | |
16 { | |
17 public: | |
18 | |
19 arma_inline | |
20 bool | |
21 operator() (eT a, eT b) const | |
22 { | |
23 return (a < b); | |
24 } | |
25 }; | |
26 | |
27 | |
28 | |
29 template<typename eT> | |
30 class arma_descend_sort_helper | |
31 { | |
32 public: | |
33 | |
34 arma_inline | |
35 bool | |
36 operator() (eT a, eT b) const | |
37 { | |
38 return (a > b); | |
39 } | |
40 }; | |
41 | |
42 | |
43 | |
44 template<typename T> | |
45 class arma_ascend_sort_helper< std::complex<T> > | |
46 { | |
47 public: | |
48 | |
49 typedef typename std::complex<T> eT; | |
50 | |
51 inline | |
52 bool | |
53 operator() (const eT& a, const eT& b) const | |
54 { | |
55 return (std::abs(a) < std::abs(b)); | |
56 } | |
57 }; | |
58 | |
59 | |
60 | |
61 template<typename T> | |
62 class arma_descend_sort_helper< std::complex<T> > | |
63 { | |
64 public: | |
65 | |
66 typedef typename std::complex<T> eT; | |
67 | |
68 inline | |
69 bool | |
70 operator() (const eT& a, const eT& b) const | |
71 { | |
72 return (std::abs(a) > std::abs(b)); | |
73 } | |
74 }; | |
75 | |
76 | |
77 | |
78 template<typename eT> | |
79 inline | |
80 void | |
81 op_sort::direct_sort(eT* X, const uword n_elem, const uword sort_type) | |
82 { | |
83 arma_extra_debug_sigprint(); | |
84 | |
85 if(sort_type == 0) | |
86 { | |
87 arma_ascend_sort_helper<eT> comparator; | |
88 | |
89 std::sort(&X[0], &X[n_elem], comparator); | |
90 } | |
91 else | |
92 { | |
93 arma_descend_sort_helper<eT> comparator; | |
94 | |
95 std::sort(&X[0], &X[n_elem], comparator); | |
96 } | |
97 } | |
98 | |
99 | |
100 | |
101 template<typename eT> | |
102 inline | |
103 void | |
104 op_sort::direct_sort_ascending(eT* X, const uword n_elem) | |
105 { | |
106 arma_extra_debug_sigprint(); | |
107 | |
108 arma_ascend_sort_helper<eT> comparator; | |
109 | |
110 std::sort(&X[0], &X[n_elem], comparator); | |
111 } | |
112 | |
113 | |
114 | |
115 template<typename eT> | |
116 inline | |
117 void | |
118 op_sort::copy_row(eT* X, const Mat<eT>& A, const uword row) | |
119 { | |
120 const uword N = A.n_cols; | |
121 | |
122 uword i,j; | |
123 | |
124 for(i=0, j=1; j<N; i+=2, j+=2) | |
125 { | |
126 X[i] = A.at(row,i); | |
127 X[j] = A.at(row,j); | |
128 } | |
129 | |
130 if(i < N) | |
131 { | |
132 X[i] = A.at(row,i); | |
133 } | |
134 } | |
135 | |
136 | |
137 | |
138 template<typename eT> | |
139 inline | |
140 void | |
141 op_sort::copy_row(Mat<eT>& A, const eT* X, const uword row) | |
142 { | |
143 const uword N = A.n_cols; | |
144 | |
145 uword i,j; | |
146 | |
147 for(i=0, j=1; j<N; i+=2, j+=2) | |
148 { | |
149 A.at(row,i) = X[i]; | |
150 A.at(row,j) = X[j]; | |
151 } | |
152 | |
153 if(i < N) | |
154 { | |
155 A.at(row,i) = X[i]; | |
156 } | |
157 } | |
158 | |
159 | |
160 | |
161 template<typename T1> | |
162 inline | |
163 void | |
164 op_sort::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_sort>& in) | |
165 { | |
166 arma_extra_debug_sigprint(); | |
167 | |
168 typedef typename T1::elem_type eT; | |
169 | |
170 const unwrap_check<T1> tmp(in.m, out); | |
171 const Mat<eT>& X = tmp.M; | |
172 | |
173 const uword sort_type = in.aux_uword_a; | |
174 const uword dim = in.aux_uword_b; | |
175 | |
176 arma_debug_check( (sort_type > 1), "sort(): incorrect usage. sort_type must be 0 or 1"); | |
177 arma_debug_check( (dim > 1), "sort(): incorrect usage. dim must be 0 or 1" ); | |
178 arma_debug_check( (X.is_finite() == false), "sort(): given object has non-finite elements" ); | |
179 | |
180 if( (X.n_rows * X.n_cols) <= 1 ) | |
181 { | |
182 out = X; | |
183 return; | |
184 } | |
185 | |
186 | |
187 if(dim == 0) // sort the contents of each column | |
188 { | |
189 arma_extra_debug_print("op_sort::apply(), dim = 0"); | |
190 | |
191 out = X; | |
192 | |
193 const uword n_rows = out.n_rows; | |
194 const uword n_cols = out.n_cols; | |
195 | |
196 for(uword col=0; col < n_cols; ++col) | |
197 { | |
198 op_sort::direct_sort( out.colptr(col), n_rows, sort_type ); | |
199 } | |
200 } | |
201 else | |
202 if(dim == 1) // sort the contents of each row | |
203 { | |
204 if(X.n_rows == 1) // a row vector | |
205 { | |
206 arma_extra_debug_print("op_sort::apply(), dim = 1, vector specific"); | |
207 | |
208 out = X; | |
209 op_sort::direct_sort(out.memptr(), out.n_elem, sort_type); | |
210 } | |
211 else // not a row vector | |
212 { | |
213 arma_extra_debug_print("op_sort::apply(), dim = 1, generic"); | |
214 | |
215 out.copy_size(X); | |
216 | |
217 const uword n_rows = out.n_rows; | |
218 const uword n_cols = out.n_cols; | |
219 | |
220 podarray<eT> tmp_array(n_cols); | |
221 | |
222 for(uword row=0; row < n_rows; ++row) | |
223 { | |
224 op_sort::copy_row(tmp_array.memptr(), X, row); | |
225 | |
226 op_sort::direct_sort( tmp_array.memptr(), n_cols, sort_type ); | |
227 | |
228 op_sort::copy_row(out, tmp_array.memptr(), row); | |
229 } | |
230 } | |
231 } | |
232 | |
233 } | |
234 | |
235 | |
236 //! @} |