00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef _UTILS_H
00020 #define _UTILS_H
00021
00022 #include <algorithm>
00023 #include <cassert>
00024 #include <cmath>
00025 #include <functional>
00026
00030 namespace lsp {
00036 template<class T> class less_abs:
00037 public std::binary_function<T, T, bool> {
00038 private:
00039 std::less< T > m_less;
00040 public:
00046 bool operator() (T x, T y) const {
00047 return m_less( std::abs(x), std::abs(y) );
00048 }
00049 };
00050
00056 template<class T, class Less = std::less< typename T::value_type > > class vector_less:
00057 public std::binary_function< typename T::value_type, typename T::value_type, bool> {
00058 private:
00059 typedef T vector_type;
00060 typedef typename vector_type::value_type value_type;
00061 typedef typename vector_type::size_type size_type;
00062 typedef Less less_type;
00063
00064 const vector_type& m_vector;
00065 less_type m_less;
00066 public:
00067 vector_less( const vector_type& v ):
00068 m_vector( v ) {
00069 }
00075 bool operator() (size_type x, size_type y) const {
00076 return m_less( m_vector( x ), m_vector( y ) );
00077 }
00078 };
00079
00085 template<class T, class Less = std::less< typename T::value_type > > class vector_less_nnls1:
00086 public std::binary_function< typename T::value_type, typename T::value_type, bool> {
00087 private:
00088 typedef T vector_type;
00089 typedef typename vector_type::value_type value_type;
00090 typedef typename vector_type::size_type size_type;
00091 typedef Less less_type;
00092
00093 const vector_type& m_vector1,m_vector2;
00094 less_type m_less;
00095 public:
00096 vector_less_nnls1( const vector_type& v1, const vector_type& v2 ):
00097 m_vector1( v1 ),m_vector2( v2 ) {
00098 }
00107 bool operator() (size_type x, size_type y) const {
00108 if( m_vector2( x ) > 0 )
00109 return false;
00110 if( m_vector2( y ) > 0 )
00111 return true;
00112 return m_less( m_vector1( x )/(m_vector1( x )-m_vector2( x )), m_vector1( y )/(m_vector1( y )-m_vector2( y )) );
00113 }
00114 };
00115
00123 template<class V, class IS, class Cond > bool is_vector_elem( const V& vec, const IS& index_space ){
00124 typedef typename V::value_type value_type;
00125 typedef V vector_type;
00126 typedef IS index_space_type;
00127 typedef Cond condition_type;
00128
00129 condition_type cond;
00130 for( typename index_space_type::const_iterator it = index_space.begin(); it != index_space.end(); ++it ) {
00131 assert( *it < vec.size() );
00132 if( ! cond( vec(*it), 0 ) ) return false;
00133 }
00134 return true;
00135 }
00136
00146 template<class IS,class IT> void swap_indexes( IS& src, IS& dest, const IT& index ) {
00147 typedef IS index_space_type;
00148 typedef IT index_type;
00149
00150 typename index_space_type::iterator it = std::find( src.begin(), src.end(), index );
00151 if( it != src.end() ){
00152 dest.push_back( index );
00153 std::swap( *it, src.back() );
00154 src.pop_back();
00155 }
00156 }
00157
00163 class null_type {
00164
00165 public:
00166
00167 static null_type s_null;
00168
00169 public:
00170
00171 };
00172
00173 };
00174
00175 #endif // _UTILS_H