comparison armadillo-2.4.4/include/armadillo_bits/fn_sort_index.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) 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
14 //! \addtogroup fn_sort_index
15 //! @{
16
17
18
19
20 template<typename T1, typename T2>
21 struct arma_sort_index_packet_ascend
22 {
23 T1 val;
24 T2 index;
25 };
26
27
28
29 template<typename T1, typename T2>
30 struct arma_sort_index_packet_descend
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 {
63 arma_extra_debug_sigprint();
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 {
88 arma_extra_debug_sigprint();
89
90 typedef typename T1::elem_type eT;
91
92 arma_type_check(( is_complex<eT>::value == true ));
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
125 //! @}