max@0: // Copyright (C) 2010 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2010 Conrad Sanderson max@0: // Copyright (C) 2010 Dimitrios Bouzas max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: max@0: //! \addtogroup op_find max@0: //! @{ max@0: max@0: max@0: max@0: template max@0: inline max@0: uword max@0: op_find::helper max@0: ( max@0: Mat& indices, max@0: const Base& X max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::elem_type eT; max@0: typedef typename Proxy::ea_type ea_type; max@0: max@0: const Proxy A(X.get_ref()); max@0: max@0: ea_type PA = A.get_ea(); max@0: const uword n_elem = A.get_n_elem(); max@0: max@0: indices.set_size(n_elem, 1); max@0: max@0: uword* indices_mem = indices.memptr(); max@0: uword n_nz = 0; max@0: max@0: for(uword i=0; i max@0: inline max@0: uword max@0: op_find::helper max@0: ( max@0: Mat& indices, max@0: const mtOp& X, max@0: const typename arma_op_rel_only::result junk1, max@0: const typename arma_not_cx::result junk2 max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(junk1); max@0: arma_ignore(junk2); max@0: max@0: typedef typename T1::elem_type eT; max@0: typedef typename Proxy::ea_type ea_type; max@0: max@0: const eT val = X.aux; max@0: max@0: const Proxy A(X.m); max@0: max@0: ea_type PA = A.get_ea(); max@0: const uword n_elem = A.get_n_elem(); max@0: max@0: indices.set_size(n_elem, 1); max@0: max@0: uword* indices_mem = indices.memptr(); max@0: uword n_nz = 0; max@0: max@0: for(uword i=0; i::value == true) { not_zero = (val < tmp); } max@0: else if(is_same_type::value == true) { not_zero = (tmp < val); } max@0: else if(is_same_type::value == true) { not_zero = (val > tmp); } max@0: else if(is_same_type::value == true) { not_zero = (tmp > val); } max@0: else if(is_same_type::value == true) { not_zero = (val <= tmp); } max@0: else if(is_same_type::value == true) { not_zero = (tmp <= val); } max@0: else if(is_same_type::value == true) { not_zero = (val >= tmp); } max@0: else if(is_same_type::value == true) { not_zero = (tmp >= val); } max@0: else if(is_same_type::value == true) { not_zero = (tmp == val); } max@0: else if(is_same_type::value == true) { not_zero = (tmp != val); } max@0: else not_zero = false; max@0: max@0: if(not_zero == true) max@0: { max@0: indices_mem[n_nz] = i; max@0: ++n_nz; max@0: } max@0: } max@0: max@0: return n_nz; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: uword max@0: op_find::helper max@0: ( max@0: Mat& indices, max@0: const mtOp& X, max@0: const typename arma_op_rel_only::result junk1, max@0: const typename arma_cx_only::result junk2 max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(junk1); max@0: arma_ignore(junk2); max@0: max@0: typedef typename T1::elem_type eT; max@0: typedef typename Proxy::ea_type ea_type; max@0: max@0: const eT val = X.aux; max@0: max@0: const Proxy A(X.m); max@0: max@0: ea_type PA = A.get_ea(); max@0: const uword n_elem = A.get_n_elem(); max@0: max@0: indices.set_size(n_elem, 1); max@0: max@0: uword* indices_mem = indices.memptr(); max@0: uword n_nz = 0; max@0: max@0: for(uword i=0; i::value == true) { not_zero = (tmp == val); } max@0: else if(is_same_type::value == true) { not_zero = (tmp != val); } max@0: else not_zero = false; max@0: max@0: if(not_zero == true) max@0: { max@0: indices_mem[n_nz] = i; max@0: ++n_nz; max@0: } max@0: } max@0: max@0: return n_nz; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: uword max@0: op_find::helper max@0: ( max@0: Mat& indices, max@0: const mtGlue& X, max@0: const typename arma_glue_rel_only::result junk1, max@0: const typename arma_not_cx::result junk2, max@0: const typename arma_not_cx::result junk3 max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(junk1); max@0: arma_ignore(junk2); max@0: arma_ignore(junk3); max@0: max@0: typedef typename T1::elem_type eT1; max@0: typedef typename T2::elem_type eT2; max@0: max@0: typedef typename Proxy::ea_type ea_type1; max@0: typedef typename Proxy::ea_type ea_type2; max@0: max@0: const Proxy A(X.A); max@0: const Proxy B(X.B); max@0: max@0: arma_debug_assert_same_size(A, B, "relational operator"); max@0: max@0: ea_type1 PA = A.get_ea(); max@0: ea_type2 PB = B.get_ea(); max@0: const uword n_elem = B.get_n_elem(); max@0: max@0: indices.set_size(n_elem, 1); max@0: max@0: uword* indices_mem = indices.memptr(); max@0: uword n_nz = 0; max@0: max@0: for(uword i=0; i::value == true) { not_zero = (tmp1 < tmp2); } max@0: else if(is_same_type::value == true) { not_zero = (tmp1 > tmp2); } max@0: else if(is_same_type::value == true) { not_zero = (tmp1 <= tmp2); } max@0: else if(is_same_type::value == true) { not_zero = (tmp1 >= tmp2); } max@0: else if(is_same_type::value == true) { not_zero = (tmp1 == tmp2); } max@0: else if(is_same_type::value == true) { not_zero = (tmp1 != tmp2); } max@0: else not_zero = false; max@0: max@0: if(not_zero == true) max@0: { max@0: indices_mem[n_nz] = i; max@0: ++n_nz; max@0: } max@0: } max@0: max@0: return n_nz; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: uword max@0: op_find::helper max@0: ( max@0: Mat& indices, max@0: const mtGlue& X, max@0: const typename arma_glue_rel_only::result junk1, max@0: const typename arma_cx_only::result junk2, max@0: const typename arma_cx_only::result junk3 max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(junk1); max@0: arma_ignore(junk2); max@0: arma_ignore(junk3); max@0: max@0: typedef typename Proxy::ea_type ea_type1; max@0: typedef typename Proxy::ea_type ea_type2; max@0: max@0: const Proxy A(X.A); max@0: const Proxy B(X.B); max@0: max@0: arma_debug_assert_same_size(A, B, "relational operator"); max@0: max@0: ea_type1 PA = A.get_ea(); max@0: ea_type2 PB = B.get_ea(); max@0: const uword n_elem = B.get_n_elem(); max@0: max@0: indices.set_size(n_elem, 1); max@0: max@0: uword* indices_mem = indices.memptr(); max@0: uword n_nz = 0; max@0: max@0: for(uword i=0; i::value == true) { not_zero = (PA[i] == PB[i]); } max@0: else if(is_same_type::value == true) { not_zero = (PA[i] != PB[i]); } max@0: else not_zero = false; max@0: max@0: if(not_zero == true) max@0: { max@0: indices_mem[n_nz] = i; max@0: ++n_nz; max@0: } max@0: } max@0: max@0: return n_nz; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: op_find::apply(Mat& out, const mtOp& X) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword k = X.aux_uword_a; max@0: const uword type = X.aux_uword_b; max@0: max@0: Mat indices; max@0: const uword n_nz = op_find::helper(indices, X.m); max@0: max@0: if(n_nz > 0) max@0: { max@0: if(type == 0) // "first" max@0: { max@0: out = (k > 0 && k <= n_nz) ? indices.rows(0, k-1 ) : indices.rows(0, n_nz-1); max@0: } max@0: else // "last" max@0: { max@0: out = (k > 0 && k <= n_nz) ? indices.rows(n_nz-k, n_nz-1) : indices.rows(0, n_nz-1); max@0: } max@0: } max@0: else max@0: { max@0: out.reset(); max@0: } max@0: } max@0: max@0: max@0: max@0: //! @}