op_sort_meat.hpp
Go to the documentation of this file.
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 
16 
17 
18 
19 template<typename eT>
21  {
22  public:
23 
25  bool
26  operator() (eT a, eT b) const
27  {
28  return (a < b);
29  }
30  };
31 
32 
33 
34 template<typename eT>
36  {
37  public:
38 
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  {
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  {
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
156  {
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 
arma_inline arma_warn_unused eT * memptr()
returns a pointer to array of eTs used by the matrix
Definition: Mat_meat.hpp:4024
A lightweight array for POD types. If the amount of memory requested is small, the stack is used...
arma_warn_unused bool is_finite() const
returns true if all of the elements are finite
Definition: Mat_meat.hpp:3872
const uword n_cols
number of columns in the matrix (read-only)
Definition: Mat_bones.hpp:30
arma_aligned uword aux_uword_b
storage of auxiliary data, uword format
Definition: Op_bones.hpp:48
const uword n_elem
number of elements in the matrix (read-only)
Definition: Mat_bones.hpp:31
const uword n_rows
number of rows in the matrix (read-only)
Definition: Mat_bones.hpp:29
#define arma_extra_debug_print
Definition: debug.hpp:1118
arma_aligned const T1 & m
storage of reference to the operand (eg. a matrix)
Definition: Op_bones.hpp:45
arma_inline arma_warn_unused eT * colptr(const uword in_col)
returns a pointer to array of eTs for a specified column; no bounds check
Definition: Mat_meat.hpp:4000
u32 uword
Definition: typedef.hpp:85
#define arma_debug_check
Definition: debug.hpp:1084
const Mat< eT > M
Definition: unwrap.hpp:32
arma_inline arma_warn_unused eT & at(const uword i)
linear element accessor (treats the matrix as a vector); no bounds check.
Definition: Mat_meat.hpp:3692
void copy_size(const Mat< eT2 > &m)
change the matrix (without preserving data) to have the same dimensions as the given matrix ...
Definition: Mat_meat.hpp:4303
arma_inline const eOp< T1, eop_abs > abs(const Base< typename T1::elem_type, T1 > &X, const typename arma_not_cx< typename T1::elem_type >::result *junk=0)
Definition: fn_elem.hpp:317
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
static void apply(Mat< typename T1::elem_type > &out, const Op< T1, op_sort > &in)
arma_inline const Op< T1, op_sort > sort(const Base< typename T1::elem_type, T1 > &X, const uword sort_type=0, const uword dim=0)
Definition: fn_sort.hpp:21
arma_inline eT * memptr()
Dense matrix class.
#define arma_inline
static void direct_sort(eT *X, const uword N, const uword sort_type=0)
static void copy_row(eT *X, const Mat< eT > &A, const uword row)
arma_aligned uword aux_uword_a
storage of auxiliary data, uword format
Definition: Op_bones.hpp:47
arma_inline bool operator()(eT a, eT b) const


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:58