fn_sort_index.hpp
Go to the documentation of this file.
1 // Copyright (C) 2009-2010 NICTA (www.nicta.com.au)
2 // Copyright (C) 2009-2010 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 
20 template<typename T1, typename T2>
22  {
23  T1 val;
24  T2 index;
25  };
26 
27 
28 
29 template<typename T1, typename T2>
31  {
32  T1 val;
33  T2 index;
34  };
35 
36 
37 
38 template<typename T1, typename T2>
39 inline
40 bool
41 operator< (const arma_sort_index_packet_ascend<T1,T2>& A, const arma_sort_index_packet_ascend<T1,T2>& B)
42  {
43  return A.val < B.val;
44  }
45 
46 
47 
48 template<typename T1, typename T2>
49 inline
50 bool
51 operator< (const arma_sort_index_packet_descend<T1,T2>& A, const arma_sort_index_packet_descend<T1,T2>& B)
52  {
53  return A.val > B.val;
54  }
55 
56 
57 
58 template<typename umat_elem_type, typename packet_type, typename eT>
59 void
60 inline
61 sort_index_helper(umat_elem_type* out_mem, std::vector<packet_type>& packet_vec, const eT* in_mem)
62  {
64 
65  const uword n_elem = packet_vec.size();
66 
67  for(uword i=0; i<n_elem; ++i)
68  {
69  packet_vec[i].val = in_mem[i];
70  packet_vec[i].index = i;
71  }
72 
73  std::sort( packet_vec.begin(), packet_vec.end() );
74 
75  for(uword i=0; i<n_elem; ++i)
76  {
77  out_mem[i] = packet_vec[i].index;
78  }
79  }
80 
81 
82 
83 template<typename T1>
84 inline
85 umat
86 sort_index(const Base<typename T1::elem_type,T1>& X, const uword sort_type = 0)
87  {
89 
90  typedef typename T1::elem_type eT;
91 
93 
94  const unwrap<T1> tmp(X.get_ref());
95  const Mat<eT>& A = tmp.M;
96 
97  if(A.is_empty() == true)
98  {
99  return umat();
100  }
101 
102  arma_debug_check( (A.is_vec() == false), "sort_index(): currently only handles vectors");
103 
104  typedef typename umat::elem_type out_elem_type;
105 
106  umat out(A.n_rows, A.n_cols);
107 
108  if(sort_type == 0)
109  {
110  std::vector< arma_sort_index_packet_ascend<eT,out_elem_type> > packet_vec(A.n_elem);
111 
112  sort_index_helper(out.memptr(), packet_vec, A.mem);
113  }
114  else
115  {
116  std::vector< arma_sort_index_packet_descend<eT,out_elem_type> > packet_vec(A.n_elem);
117 
118  sort_index_helper(out.memptr(), packet_vec, A.mem);
119  }
120 
121  return out;
122  }
123 
124 
arma_inline arma_warn_unused eT * memptr()
returns a pointer to array of eTs used by the matrix
Definition: Mat_meat.hpp:4024
arma_inline const derived & get_ref() const
Definition: Base_meat.hpp:22
Mat< uword > umat
Definition: typedef.hpp:127
umat sort_index(const Base< typename T1::elem_type, T1 > &X, const uword sort_type=0)
u32 uword
Definition: typedef.hpp:85
#define arma_type_check(condition)
void sort_index_helper(umat_elem_type *out_mem, std::vector< packet_type > &packet_vec, const eT *in_mem)
eT elem_type
the type of elements stored in the matrix
Definition: Mat_bones.hpp:26
#define arma_debug_check
Definition: debug.hpp:1084
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
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
Dense matrix class.


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