fn_sort_index.hpp
Go to the documentation of this file.
00001 // Copyright (C) 2009-2010 NICTA (www.nicta.com.au)
00002 // Copyright (C) 2009-2010 Conrad Sanderson
00003 // 
00004 // This file is part of the Armadillo C++ library.
00005 // It is provided without any warranty of fitness
00006 // for any purpose. You can redistribute this file
00007 // and/or modify it under the terms of the GNU
00008 // Lesser General Public License (LGPL) as published
00009 // by the Free Software Foundation, either version 3
00010 // of the License or (at your option) any later version.
00011 // (see http://www.opensource.org/licenses for more info)
00012 
00013 
00016 
00017 
00018 
00019 
00020 template<typename T1, typename T2>
00021 struct arma_sort_index_packet_ascend
00022   {
00023   T1 val;
00024   T2 index;
00025   };
00026 
00027 
00028 
00029 template<typename T1, typename T2>
00030 struct arma_sort_index_packet_descend
00031   {
00032   T1 val;
00033   T2 index;
00034   };
00035 
00036 
00037 
00038 template<typename T1, typename T2>
00039 inline
00040 bool
00041 operator< (const arma_sort_index_packet_ascend<T1,T2>& A, const arma_sort_index_packet_ascend<T1,T2>& B)
00042   {
00043   return A.val < B.val;
00044   }
00045 
00046 
00047 
00048 template<typename T1, typename T2>
00049 inline
00050 bool
00051 operator< (const arma_sort_index_packet_descend<T1,T2>& A, const arma_sort_index_packet_descend<T1,T2>& B)
00052   {
00053   return A.val > B.val;
00054   }
00055 
00056 
00057 
00058 template<typename umat_elem_type, typename packet_type, typename eT>
00059 void
00060 inline
00061 sort_index_helper(umat_elem_type* out_mem, std::vector<packet_type>& packet_vec, const eT* in_mem)
00062   {
00063   arma_extra_debug_sigprint();
00064   
00065   const uword n_elem = packet_vec.size();
00066   
00067   for(uword i=0; i<n_elem; ++i)
00068     {
00069     packet_vec[i].val   = in_mem[i];
00070     packet_vec[i].index = i;
00071     }
00072   
00073   std::sort( packet_vec.begin(), packet_vec.end() );
00074   
00075   for(uword i=0; i<n_elem; ++i)
00076     {
00077     out_mem[i] = packet_vec[i].index;
00078     }
00079   }
00080 
00081 
00082 
00083 template<typename T1>
00084 inline
00085 umat
00086 sort_index(const Base<typename T1::elem_type,T1>& X, const uword sort_type = 0)
00087   {
00088   arma_extra_debug_sigprint();
00089   
00090   typedef typename T1::elem_type eT;
00091   
00092   arma_type_check(( is_complex<eT>::value == true ));
00093   
00094   const unwrap<T1> tmp(X.get_ref());
00095   const Mat<eT>& A = tmp.M;
00096   
00097   if(A.is_empty() == true)
00098     {
00099     return umat();
00100     }
00101   
00102   arma_debug_check( (A.is_vec() == false), "sort_index(): currently only handles vectors");
00103   
00104   typedef typename umat::elem_type out_elem_type;
00105   
00106   umat out(A.n_rows, A.n_cols);
00107   
00108   if(sort_type == 0)
00109     {
00110     std::vector< arma_sort_index_packet_ascend<eT,out_elem_type> > packet_vec(A.n_elem);
00111     
00112     sort_index_helper(out.memptr(), packet_vec, A.mem);
00113     }
00114   else
00115     {
00116     std::vector< arma_sort_index_packet_descend<eT,out_elem_type> > packet_vec(A.n_elem);
00117     
00118     sort_index_helper(out.memptr(), packet_vec, A.mem);
00119     }
00120   
00121   return out;
00122   }
00123 
00124 


armadillo_matrix
Author(s): Conrad Sanderson - NICTA (www.nicta.com.au), (Wrapper by Sjoerd van den Dries)
autogenerated on Tue Jan 7 2014 11:42:04