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 //! \addtogroup op_find_unique
20 //! @{
21
22
23
24 template<typename T1>
25 inline
26 bool
apply_helper(Mat<uword> & out,const Proxy<T1> & P,const bool ascending_indices)27 op_find_unique::apply_helper(Mat<uword>& out, const Proxy<T1>& P, const bool ascending_indices)
28 {
29 arma_extra_debug_sigprint();
30
31 typedef typename T1::elem_type eT;
32
33 const uword n_elem = P.get_n_elem();
34
35 if(n_elem == 0) { out.set_size(0,1); return true; }
36 if(n_elem == 1) { out.set_size(1,1); out[0] = 0; return true; }
37
38 uvec indices(n_elem, arma_nozeros_indicator());
39
40 std::vector< arma_find_unique_packet<eT> > packet_vec(n_elem);
41
42 if(Proxy<T1>::use_at == false)
43 {
44 typename Proxy<T1>::ea_type Pea = P.get_ea();
45
46 for(uword i=0; i<n_elem; ++i)
47 {
48 const eT val = Pea[i];
49
50 if(arma_isnan(val)) { return false; }
51
52 packet_vec[i].val = val;
53 packet_vec[i].index = i;
54 }
55 }
56 else
57 {
58 const uword n_rows = P.get_n_rows();
59 const uword n_cols = P.get_n_cols();
60
61 uword i = 0;
62
63 for(uword col=0; col < n_cols; ++col)
64 for(uword row=0; row < n_rows; ++row)
65 {
66 const eT val = P.at(row,col);
67
68 if(arma_isnan(val)) { return false; }
69
70 packet_vec[i].val = val;
71 packet_vec[i].index = i;
72
73 ++i;
74 }
75 }
76
77 arma_find_unique_comparator<eT> comparator;
78
79 std::sort( packet_vec.begin(), packet_vec.end(), comparator );
80
81 uword* indices_mem = indices.memptr();
82
83 indices_mem[0] = packet_vec[0].index;
84
85 uword count = 1;
86
87 for(uword i=1; i < n_elem; ++i)
88 {
89 const eT diff = packet_vec[i-1].val - packet_vec[i].val;
90
91 if(diff != eT(0))
92 {
93 indices_mem[count] = packet_vec[i].index;
94 ++count;
95 }
96 }
97
98 out.steal_mem_col(indices,count);
99
100 if(ascending_indices) { std::sort(out.begin(), out.end()); }
101
102 return true;
103 }
104
105
106
107 template<typename T1>
108 inline
109 void
apply(Mat<uword> & out,const mtOp<uword,T1,op_find_unique> & in)110 op_find_unique::apply(Mat<uword>& out, const mtOp<uword,T1,op_find_unique>& in)
111 {
112 arma_extra_debug_sigprint();
113
114 const Proxy<T1> P(in.m);
115
116 const bool ascending_indices = (in.aux_uword_a == uword(1));
117
118 const bool all_non_nan = op_find_unique::apply_helper(out, P, ascending_indices);
119
120 if(all_non_nan == false)
121 {
122 arma_debug_check( true, "find_unique(): detected NaN" );
123
124 out.reset();
125 }
126 }
127
128
129
130 //! @}
131