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_sort_index 20 //! @{ 21 22 23 24 class op_sort_index 25 : public traits_op_col 26 { 27 public: 28 29 template<typename T1> 30 static inline bool apply_noalias(Mat<uword>& out, const Proxy<T1>& P, const uword sort_type); 31 32 template<typename T1> 33 static inline void apply(Mat<uword>& out, const mtOp<uword,T1,op_sort_index>& in); 34 }; 35 36 37 38 class op_stable_sort_index 39 : public traits_op_col 40 { 41 public: 42 43 template<typename T1> 44 static inline bool apply_noalias(Mat<uword>& out, const Proxy<T1>& P, const uword sort_type); 45 46 template<typename T1> 47 static inline void apply(Mat<uword>& out, const mtOp<uword,T1,op_stable_sort_index>& in); 48 }; 49 50 51 52 template<typename eT> 53 struct arma_sort_index_packet 54 { 55 eT val; 56 uword index; 57 }; 58 59 60 61 template<typename eT> 62 struct arma_sort_index_helper_ascend 63 { 64 arma_inline 65 bool operator ()arma_sort_index_helper_ascend66 operator() (const arma_sort_index_packet<eT>& A, const arma_sort_index_packet<eT>& B) const 67 { 68 return (A.val < B.val); 69 } 70 }; 71 72 73 74 template<typename eT> 75 struct arma_sort_index_helper_descend 76 { 77 arma_inline 78 bool operator ()arma_sort_index_helper_descend79 operator() (const arma_sort_index_packet<eT>& A, const arma_sort_index_packet<eT>& B) const 80 { 81 return (A.val > B.val); 82 } 83 }; 84 85 86 87 template<typename T> 88 struct arma_sort_index_helper_ascend< std::complex<T> > 89 { 90 typedef typename std::complex<T> eT; 91 92 inline 93 bool operator ()arma_sort_index_helper_ascend94 operator() (const arma_sort_index_packet<eT>& A, const arma_sort_index_packet<eT>& B) const 95 { 96 return (std::abs(A.val) < std::abs(B.val)); 97 } 98 99 // inline 100 // bool 101 // operator() (const arma_sort_index_packet<eT>& A, const arma_sort_index_packet<eT>& B) const 102 // { 103 // const T abs_A_val = std::abs(A.val); 104 // const T abs_B_val = std::abs(B.val); 105 // 106 // return ( (abs_A_val != abs_B_val) ? (abs_A_val < abs_B_val) : (std::arg(A.val) < std::arg(B.val)) ); 107 // } 108 }; 109 110 111 112 template<typename T> 113 struct arma_sort_index_helper_descend< std::complex<T> > 114 { 115 typedef typename std::complex<T> eT; 116 117 inline 118 bool operator ()arma_sort_index_helper_descend119 operator() (const arma_sort_index_packet<eT>& A, const arma_sort_index_packet<eT>& B) const 120 { 121 return (std::abs(A.val) > std::abs(B.val)); 122 } 123 124 // inline 125 // bool 126 // operator() (const arma_sort_index_packet<eT>& A, const arma_sort_index_packet<eT>& B) const 127 // { 128 // const T abs_A_val = std::abs(A.val); 129 // const T abs_B_val = std::abs(B.val); 130 // 131 // return ( (abs_A_val != abs_B_val) ? (abs_A_val > abs_B_val) : (std::arg(A.val) > std::arg(B.val)) ); 132 // } 133 }; 134 135 136 137 //! @} 138