Files Classes Functions Hierarchy
00001 #ifndef VEC_H 00002 #define VEC_H 00003 00004 #include <string> 00005 #include <sstream> 00006 using namespace std; 00007 00008 #include <typedefs.h> 00009 00010 00041 template < typename A, typename T > 00042 class vec 00043 { 00044 public: 00045 00047 A ai; 00048 00050 uintc N; 00051 00053 vec(A ai_, uintc N_) 00054 : ai(ai_), N(N_) {} 00055 00057 void operator = ( T const x ); 00058 00060 template< typename Z > 00061 void operator = ( Z const & zi ); 00062 00064 void operator = (vec<A,T> const & vb); 00065 00067 template< typename Z > 00068 void operator *= ( Z const & zi ); 00069 00071 template< typename Z > 00072 void operator += ( Z const & zi ); 00073 00075 template< typename Z > 00076 void operator -= ( Z const & zi ); 00077 00079 void operator += ( T const x ); 00080 00082 void operator *= ( T const x ); 00083 00085 T const operator [] (uintc i) const 00086 { return ai[i]; } 00087 00089 T & operator [] (uintc i) 00090 { return ai[i]; } 00091 00092 00094 void identity(uintc k, T const x=1); 00095 00097 void difference(); 00098 00100 void sum(); 00101 00103 void binomial(uintc N); 00104 00106 operator string () const; 00107 00108 }; 00109 00110 00111 //--------------------------------------------------------- 00112 // Implementation 00113 00114 template < typename A, typename T > 00115 vec<A,T>::operator string() const 00116 { 00117 if (N==0) 00118 return ""; 00119 00120 string s; 00121 { stringstream ss; ss << ai[0]; s+=ss.str(); } 00122 00123 for (uint i=1; i<N; ++i) 00124 { 00125 s += " "; 00126 { stringstream ss; ss << ai[i]; s+=ss.str(); } 00127 } 00128 00129 return s; 00130 } 00131 00132 template < typename A, typename T > 00133 void vec<A,T>::binomial(uintc N) 00134 { 00135 identity(0); 00136 for (uint i=0; i<N; ++i) 00137 sum(); 00138 } 00139 00140 template < typename A, typename T > 00141 void vec<A,T>::identity(uintc k, T const x) 00142 { 00143 for (uint i=0; i<N; ++i) 00144 ai[i] = 0; 00145 ai[k]=x; 00146 } 00147 00148 template < typename A, typename T > 00149 void vec<A,T>::difference() 00150 { 00151 for (uint i=N-1; i>0; --i) 00152 ai[i] = ai[i] - ai[i-1]; 00153 } 00154 00155 template < typename A, typename T > 00156 void vec<A,T>::sum() 00157 { 00158 for (uint i=N-1; i>0; --i) 00159 ai[i] = ai[i] + ai[i-1]; 00160 } 00161 00162 template < typename A, typename T > 00163 template< typename Z > 00164 void vec<A,T>::operator = ( Z const & zi ) 00165 { 00166 for (uint i=0; i<N; ++i) 00167 ai[i] = zi[i]; 00168 } 00169 00170 template < typename A, typename T > 00171 template< typename Z > 00172 void vec<A,T>::operator *= ( Z const & zi ) 00173 { 00174 for (uint i=0; i<N; ++i) 00175 ai[i] *= zi[i]; 00176 } 00177 00178 template < typename A, typename T > 00179 template< typename Z > 00180 void vec<A,T>::operator += ( Z const & zi ) 00181 { 00182 for (uint i=0; i<N; ++i) 00183 ai[i] += zi[i]; 00184 } 00185 00186 template < typename A, typename T > 00187 template< typename Z > 00188 void vec<A,T>::operator -= ( Z const & zi ) 00189 { 00190 for (uint i=0; i<N; ++i) 00191 ai[i] -= zi[i]; 00192 } 00193 00194 template < typename A, typename T > 00195 void vec<A,T>::operator = ( T x ) 00196 { 00197 for (uint i=0; i<N; ++i) 00198 ai[i] = x; 00199 } 00200 00201 template < typename A, typename T > 00202 void vec<A,T>::operator *= ( T x ) 00203 { 00204 for (uint i=0; i<N; ++i) 00205 ai[i] *= x; 00206 } 00207 00208 template < typename A, typename T > 00209 void vec<A,T>::operator += ( T x ) 00210 { 00211 for (uint i=0; i<N; ++i) 00212 ai[i] += x; 00213 } 00214 00215 template < typename A, typename T > 00216 void vec<A,T>::operator = ( vec<A,T> const & bi ) 00217 { 00218 for (uint i=0; i<N; ++i) 00219 ai[i] = bi[i]; 00220 } 00221 00222 00223 #endif 00224
1.5.8