max@0: // Copyright (C) 2009-2010 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2009-2010 Conrad Sanderson 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: //! \addtogroup fn_sort_index max@0: //! @{ max@0: max@0: max@0: max@0: max@0: template max@0: struct arma_sort_index_packet_ascend max@0: { max@0: T1 val; max@0: T2 index; max@0: }; max@0: max@0: max@0: max@0: template max@0: struct arma_sort_index_packet_descend max@0: { max@0: T1 val; max@0: T2 index; max@0: }; max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: operator< (const arma_sort_index_packet_ascend& A, const arma_sort_index_packet_ascend& B) max@0: { max@0: return A.val < B.val; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: bool max@0: operator< (const arma_sort_index_packet_descend& A, const arma_sort_index_packet_descend& B) max@0: { max@0: return A.val > B.val; max@0: } max@0: max@0: max@0: max@0: template max@0: void max@0: inline max@0: sort_index_helper(umat_elem_type* out_mem, std::vector& packet_vec, const eT* in_mem) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword n_elem = packet_vec.size(); max@0: max@0: for(uword i=0; i max@0: inline max@0: umat max@0: sort_index(const Base& X, const uword sort_type = 0) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: arma_type_check(( is_complex::value == true )); max@0: max@0: const unwrap tmp(X.get_ref()); max@0: const Mat& A = tmp.M; max@0: max@0: if(A.is_empty() == true) max@0: { max@0: return umat(); max@0: } max@0: max@0: arma_debug_check( (A.is_vec() == false), "sort_index(): currently only handles vectors"); max@0: max@0: typedef typename umat::elem_type out_elem_type; max@0: max@0: umat out(A.n_rows, A.n_cols); max@0: max@0: if(sort_type == 0) max@0: { max@0: std::vector< arma_sort_index_packet_ascend > packet_vec(A.n_elem); max@0: max@0: sort_index_helper(out.memptr(), packet_vec, A.mem); max@0: } max@0: else max@0: { max@0: std::vector< arma_sort_index_packet_descend > packet_vec(A.n_elem); max@0: max@0: sort_index_helper(out.memptr(), packet_vec, A.mem); max@0: } max@0: max@0: return out; max@0: } max@0: max@0: max@0: //! @}