Mercurial > hg > segmenter-vamp-plugin
comparison armadillo-3.900.4/include/armadillo_bits/op_shuffle_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) 2009-2012 NICTA (www.nicta.com.au) | |
2 // Copyright (C) 2009-2012 Conrad Sanderson | |
3 // Copyright (C) 2009-2010 Dimitrios Bouzas | |
4 // | |
5 // This Source Code Form is subject to the terms of the Mozilla Public | |
6 // License, v. 2.0. If a copy of the MPL was not distributed with this | |
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
8 | |
9 | |
10 | |
11 //! \addtogroup op_shuffle | |
12 //! @{ | |
13 | |
14 | |
15 | |
16 template<typename T1> | |
17 inline | |
18 void | |
19 op_shuffle::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_shuffle>& in) | |
20 { | |
21 arma_extra_debug_sigprint(); | |
22 | |
23 typedef typename T1::elem_type eT; | |
24 | |
25 const unwrap<T1> tmp(in.m); | |
26 const Mat<eT>& X = tmp.M; | |
27 | |
28 if(X.is_empty()) { out.copy_size(X); return; } | |
29 | |
30 const uword dim = in.aux_uword_a; | |
31 const uword N = (dim == 0) ? X.n_rows : X.n_cols; | |
32 | |
33 // see "fn_sort_index.hpp" for the definition of "arma_sort_index_packet" | |
34 // and the associated comparison functor | |
35 std::vector< arma_sort_index_packet<int,uword> > packet_vec(N); | |
36 | |
37 for(uword i=0; i<N; ++i) | |
38 { | |
39 packet_vec[i].val = std::rand(); | |
40 packet_vec[i].index = i; | |
41 } | |
42 | |
43 arma_sort_index_helper_ascend comparator; | |
44 | |
45 std::sort( packet_vec.begin(), packet_vec.end(), comparator ); | |
46 | |
47 const bool is_alias = (&out == &X); | |
48 | |
49 if(X.is_vec() == false) | |
50 { | |
51 if(is_alias == false) | |
52 { | |
53 arma_extra_debug_print("op_shuffle::apply(): matrix"); | |
54 | |
55 out.copy_size(X); | |
56 | |
57 if(dim == 0) | |
58 { | |
59 for(uword i=0; i<N; ++i) { out.row(i) = X.row(packet_vec[i].index); } | |
60 } | |
61 else | |
62 { | |
63 for(uword i=0; i<N; ++i) { out.col(i) = X.col(packet_vec[i].index); } | |
64 } | |
65 } | |
66 else // in-place shuffle | |
67 { | |
68 arma_extra_debug_print("op_shuffle::apply(): in-place matrix"); | |
69 | |
70 // reuse the val member variable of packet_vec | |
71 // to indicate whether a particular row or column | |
72 // has already been shuffled | |
73 | |
74 for(uword i=0; i<N; ++i) | |
75 { | |
76 packet_vec[i].val = 0; | |
77 } | |
78 | |
79 if(dim == 0) | |
80 { | |
81 for(uword i=0; i<N; ++i) | |
82 { | |
83 if(packet_vec[i].val == 0) | |
84 { | |
85 const uword j = packet_vec[i].index; | |
86 | |
87 out.swap_rows(i, j); | |
88 | |
89 packet_vec[j].val = 1; | |
90 } | |
91 } | |
92 } | |
93 else | |
94 { | |
95 for(uword i=0; i<N; ++i) | |
96 { | |
97 if(packet_vec[i].val == 0) | |
98 { | |
99 const uword j = packet_vec[i].index; | |
100 | |
101 out.swap_cols(i, j); | |
102 | |
103 packet_vec[j].val = 1; | |
104 } | |
105 } | |
106 } | |
107 } | |
108 } | |
109 else // we're dealing with a vector | |
110 { | |
111 if(is_alias == false) | |
112 { | |
113 arma_extra_debug_print("op_shuffle::apply(): vector"); | |
114 | |
115 out.copy_size(X); | |
116 | |
117 if(dim == 0) | |
118 { | |
119 if(X.n_rows > 1) // i.e. column vector | |
120 { | |
121 for(uword i=0; i<N; ++i) { out[i] = X[ packet_vec[i].index ]; } | |
122 } | |
123 else | |
124 { | |
125 out = X; | |
126 } | |
127 } | |
128 else | |
129 { | |
130 if(X.n_cols > 1) // i.e. row vector | |
131 { | |
132 for(uword i=0; i<N; ++i) { out[i] = X[ packet_vec[i].index ]; } | |
133 } | |
134 else | |
135 { | |
136 out = X; | |
137 } | |
138 } | |
139 } | |
140 else // in-place shuffle | |
141 { | |
142 arma_extra_debug_print("op_shuffle::apply(): in-place vector"); | |
143 | |
144 // reuse the val member variable of packet_vec | |
145 // to indicate whether a particular row or column | |
146 // has already been shuffled | |
147 | |
148 for(uword i=0; i<N; ++i) | |
149 { | |
150 packet_vec[i].val = 0; | |
151 } | |
152 | |
153 if(dim == 0) | |
154 { | |
155 if(X.n_rows > 1) // i.e. column vector | |
156 { | |
157 for(uword i=0; i<N; ++i) | |
158 { | |
159 if(packet_vec[i].val == 0) | |
160 { | |
161 const uword j = packet_vec[i].index; | |
162 | |
163 std::swap(out[i], out[j]); | |
164 | |
165 packet_vec[j].val = 1; | |
166 } | |
167 } | |
168 } | |
169 } | |
170 else | |
171 { | |
172 if(X.n_cols > 1) // i.e. row vector | |
173 { | |
174 for(uword i=0; i<N; ++i) | |
175 { | |
176 if(packet_vec[i].val == 0) | |
177 { | |
178 const uword j = packet_vec[i].index; | |
179 | |
180 std::swap(out[i], out[j]); | |
181 | |
182 packet_vec[j].val = 1; | |
183 } | |
184 } | |
185 } | |
186 } | |
187 } | |
188 } | |
189 | |
190 } | |
191 | |
192 | |
193 //! @} |