comparison armadillo-2.4.4/include/armadillo_bits/op_sort_meat.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12
13
14 //! \addtogroup op_sort
15 //! @{
16
17
18
19 template<typename eT>
20 class arma_ascend_sort_helper
21 {
22 public:
23
24 arma_inline
25 bool
26 operator() (eT a, eT b) const
27 {
28 return (a < b);
29 }
30 };
31
32
33
34 template<typename eT>
35 class arma_descend_sort_helper
36 {
37 public:
38
39 arma_inline
40 bool
41 operator() (eT a, eT b) const
42 {
43 return (a > b);
44 }
45 };
46
47
48
49 template<typename T>
50 class arma_ascend_sort_helper< std::complex<T> >
51 {
52 public:
53
54 typedef typename std::complex<T> eT;
55
56 inline
57 bool
58 operator() (const eT& a, const eT& b) const
59 {
60 return (std::abs(a) < std::abs(b));
61 }
62 };
63
64
65
66 template<typename T>
67 class arma_descend_sort_helper< std::complex<T> >
68 {
69 public:
70
71 typedef typename std::complex<T> eT;
72
73 inline
74 bool
75 operator() (const eT& a, const eT& b) const
76 {
77 return (std::abs(a) > std::abs(b));
78 }
79 };
80
81
82
83 template<typename eT>
84 inline
85 void
86 op_sort::direct_sort(eT* X, const uword n_elem, const uword sort_type)
87 {
88 arma_extra_debug_sigprint();
89
90 if(sort_type == 0)
91 {
92 arma_ascend_sort_helper<eT> comparator;
93
94 std::sort(&X[0], &X[n_elem], comparator);
95 }
96 else
97 {
98 arma_descend_sort_helper<eT> comparator;
99
100 std::sort(&X[0], &X[n_elem], comparator);
101 }
102 }
103
104
105
106 template<typename eT>
107 inline
108 void
109 op_sort::copy_row(eT* X, const Mat<eT>& A, const uword row)
110 {
111 const uword N = A.n_cols;
112
113 uword i,j;
114
115 for(i=0, j=1; j<N; i+=2, j+=2)
116 {
117 X[i] = A.at(row,i);
118 X[j] = A.at(row,j);
119 }
120
121 if(i < N)
122 {
123 X[i] = A.at(row,i);
124 }
125 }
126
127
128
129 template<typename eT>
130 inline
131 void
132 op_sort::copy_row(Mat<eT>& A, const eT* X, const uword row)
133 {
134 const uword N = A.n_cols;
135
136 uword i,j;
137
138 for(i=0, j=1; j<N; i+=2, j+=2)
139 {
140 A.at(row,i) = X[i];
141 A.at(row,j) = X[j];
142 }
143
144 if(i < N)
145 {
146 A.at(row,i) = X[i];
147 }
148 }
149
150
151
152 template<typename T1>
153 inline
154 void
155 op_sort::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_sort>& in)
156 {
157 arma_extra_debug_sigprint();
158
159 typedef typename T1::elem_type eT;
160
161 const unwrap<T1> tmp(in.m);
162 const Mat<eT>& X = tmp.M;
163
164 const uword sort_type = in.aux_uword_a;
165 const uword dim = in.aux_uword_b;
166
167 arma_debug_check( (sort_type > 1), "sort(): incorrect usage. sort_type must be 0 or 1");
168 arma_debug_check( (dim > 1), "sort(): incorrect usage. dim must be 0 or 1" );
169 arma_debug_check( (X.is_finite() == false), "sort(): given object has non-finite elements" );
170
171 if( (X.n_rows * X.n_cols) <= 1 )
172 {
173 out = X;
174 return;
175 }
176
177
178 if(dim == 0) // sort the contents of each column
179 {
180 arma_extra_debug_print("op_sort::apply(), dim = 0");
181
182 out = X;
183
184 const uword n_rows = out.n_rows;
185 const uword n_cols = out.n_cols;
186
187 for(uword col=0; col < n_cols; ++col)
188 {
189 op_sort::direct_sort( out.colptr(col), n_rows, sort_type );
190 }
191 }
192 else
193 if(dim == 1) // sort the contents of each row
194 {
195 if(X.n_rows == 1) // a row vector
196 {
197 arma_extra_debug_print("op_sort::apply(), dim = 1, vector specific");
198
199 out = X;
200 op_sort::direct_sort(out.memptr(), out.n_elem, sort_type);
201 }
202 else // not a row vector
203 {
204 arma_extra_debug_print("op_sort::apply(), dim = 1, generic");
205
206 out.copy_size(X);
207
208 const uword n_rows = out.n_rows;
209 const uword n_cols = out.n_cols;
210
211 podarray<eT> tmp_array(n_cols);
212
213 for(uword row=0; row < n_rows; ++row)
214 {
215 op_sort::copy_row(tmp_array.memptr(), X, row);
216
217 op_sort::direct_sort( tmp_array.memptr(), n_cols, sort_type );
218
219 op_sort::copy_row(out, tmp_array.memptr(), row);
220 }
221 }
222 }
223
224 }
225
226
227 //! @}