1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
4 // Copyright 2008-2016 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17
18
19
20 //! \addtogroup op_unique
21 //! @{
22
23
24
25 template<typename T1>
26 inline
27 bool
apply_helper(Mat<typename T1::elem_type> & out,const Proxy<T1> & P,const bool P_is_row)28 op_unique::apply_helper(Mat<typename T1::elem_type>& out, const Proxy<T1>& P, const bool P_is_row)
29 {
30 arma_extra_debug_sigprint();
31
32 typedef typename T1::elem_type eT;
33
34 const uword n_elem = P.get_n_elem();
35
36 if(n_elem == 0)
37 {
38 if(P_is_row)
39 {
40 out.set_size(1,0);
41 }
42 else
43 {
44 out.set_size(0,1);
45 }
46
47 return true;
48 }
49
50 if(n_elem == 1)
51 {
52 const eT tmp = (Proxy<T1>::use_at) ? P.at(0,0) : P[0];
53
54 out.set_size(1, 1);
55
56 out[0] = tmp;
57
58 return true;
59 }
60
61 Mat<eT> X(n_elem, 1, arma_nozeros_indicator());
62
63 eT* X_mem = X.memptr();
64
65 if(Proxy<T1>::use_at == false)
66 {
67 typename Proxy<T1>::ea_type Pea = P.get_ea();
68
69 for(uword i=0; i<n_elem; ++i)
70 {
71 const eT val = Pea[i];
72
73 if(arma_isnan(val)) { out.soft_reset(); return false; }
74
75 X_mem[i] = val;
76 }
77 }
78 else
79 {
80 const uword n_rows = P.get_n_rows();
81 const uword n_cols = P.get_n_cols();
82
83 for(uword col=0; col < n_cols; ++col)
84 for(uword row=0; row < n_rows; ++row)
85 {
86 const eT val = P.at(row,col);
87
88 if(arma_isnan(val)) { out.soft_reset(); return false; }
89
90 (*X_mem) = val; X_mem++;
91 }
92
93 X_mem = X.memptr();
94 }
95
96 arma_unique_comparator<eT> comparator;
97
98 std::sort( X.begin(), X.end(), comparator );
99
100 uword N_unique = 1;
101
102 for(uword i=1; i < n_elem; ++i)
103 {
104 const eT a = X_mem[i-1];
105 const eT b = X_mem[i ];
106
107 const eT diff = a - b;
108
109 if(diff != eT(0)) { ++N_unique; }
110 }
111
112 if(P_is_row)
113 {
114 out.set_size(1, N_unique);
115 }
116 else
117 {
118 out.set_size(N_unique, 1);
119 }
120
121 eT* out_mem = out.memptr();
122
123 if(n_elem > 0) { (*out_mem) = X_mem[0]; out_mem++; }
124
125 for(uword i=1; i < n_elem; ++i)
126 {
127 const eT a = X_mem[i-1];
128 const eT b = X_mem[i ];
129
130 const eT diff = a - b;
131
132 if(diff != eT(0)) { (*out_mem) = b; out_mem++; }
133 }
134
135 return true;
136 }
137
138
139
140 template<typename T1>
141 inline
142 void
apply(Mat<typename T1::elem_type> & out,const Op<T1,op_unique> & in)143 op_unique::apply(Mat<typename T1::elem_type>& out, const Op<T1, op_unique>& in)
144 {
145 arma_extra_debug_sigprint();
146
147 const Proxy<T1> P(in.m);
148
149 const bool all_non_nan = op_unique::apply_helper(out, P, false);
150
151 arma_debug_check( (all_non_nan == false), "unique(): detected NaN" );
152 }
153
154
155
156 template<typename T1>
157 inline
158 void
apply(Mat<typename T1::elem_type> & out,const Op<T1,op_unique_vec> & in)159 op_unique_vec::apply(Mat<typename T1::elem_type>& out, const Op<T1, op_unique_vec>& in)
160 {
161 arma_extra_debug_sigprint();
162
163 const Proxy<T1> P(in.m);
164
165 const bool P_is_row = (T1::is_xvec) ? bool(P.get_n_rows() == 1) : bool(T1::is_row);
166
167 const bool all_non_nan = op_unique::apply_helper(out, P, P_is_row);
168
169 arma_debug_check( (all_non_nan == false), "unique(): detected NaN" );
170 }
171
172
173
174 //! @}
175