00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __UT_Vector_H__
00022 #define __UT_Vector_H__
00023
00024 #include "UT_API.h"
00025 #include <stdlib.h>
00026 #include <iostream.h>
00027 #include <SYS/SYS_Types.h>
00028 #include "UT_Assert.h"
00029 #include "UT_ThreadedAlgorithm.h"
00030
00031 class UT_Vector2;
00032 class UT_Vector3;
00033 class UT_Vector4;
00034
00035
00036
00037
00038
00039
00040
00041 template <typename T>
00042 class UT_VectorT
00043 {
00044 public:
00045
00046 UT_VectorT() { myVector = 0; myOwnData = 0; }
00047 UT_VectorT(int nl, int nh);
00048 UT_VectorT(const UT_VectorT<T> &v);
00049 ~UT_VectorT();
00050
00051
00052 void init(int nl, int nh);
00053
00054
00055 void subvector(const UT_VectorT<T> &v, int nl, int nh);
00056
00057
00058
00059 void assign(const fpreal32 *data, int nl, int nh);
00060 void assign(const fpreal64 *data, int nl, int nh);
00061
00062 int isInit() const { return myVector ? 1 : 0; }
00063
00064
00065 void zero(int nl, int nh);
00066 void zero()
00067 { zero(myNL, myNH); }
00068
00069
00070
00071
00072
00073 void getSubvector2(UT_Vector2 &v, int idx) const;
00074 void setSubvector2(int idx, const UT_Vector2 &v);
00075 void getSubvector3(UT_Vector3 &v, int idx) const;
00076 void setSubvector3(int idx, const UT_Vector3 &v);
00077 void getSubvector4(UT_Vector4 &v, int idx) const;
00078 void setSubvector4(int idx, const UT_Vector4 &v);
00079
00080
00081 int getNL() const { return myNL; }
00082
00083
00084 int getNH() const { return myNH; }
00085
00086
00087 int length() const { return myNH - myNL + 1; }
00088
00089
00090
00091
00092
00093 bool shouldMultiThread() const
00094 {
00095 #ifdef CELLBE
00096 return false;
00097 #else
00098 return (myNH - myNL) > 4999;
00099 #endif
00100 }
00101
00102
00103 void getPartialRange(int &start, int &end, const UT_JobInfo &info) const;
00104
00105
00106 void changeNL(int nl);
00107
00108
00109
00110
00111 void setShallowNL(int nl) { myNL = nl; }
00112 void setShallowNH(int nh) { myNH = nh; }
00113
00114
00115 T &operator()(int i)
00116 {
00117 UT_ASSERT_P(i >= myNL && i <= myNH);
00118 return myVector[i];
00119 }
00120 T operator()(int i) const
00121 {
00122 UT_ASSERT_P(i >= myNL && i <= myNH);
00123 return myVector[i];
00124 }
00125
00126
00127
00128
00129
00130 T norm(int type=2) const;
00131
00132
00133 T norm2() const;
00134
00135 T distance2(const UT_VectorT<T> &v) const;
00136
00137
00138 THREADED_METHOD(UT_VectorT, shouldMultiThread(), neg)
00139 void negPartial(const UT_JobInfo &info);
00140 THREADED_METHOD1(UT_VectorT, shouldMultiThread(), negPlus,
00141 const UT_VectorT<T> &, v)
00142 void negPlusPartial(const UT_VectorT<T> &v, const UT_JobInfo &info);
00143
00144
00145 THREADED_METHOD2(UT_VectorT, shouldMultiThread(), addScaledVec,
00146 T, s,
00147 const UT_VectorT<T> &, v)
00148 void addScaledVecPartial(T s, const UT_VectorT<T> &v,
00149 const UT_JobInfo &info);
00150
00151
00152 THREADED_METHOD3(UT_VectorT, shouldMultiThread(), addScaledVecNorm2,
00153 T, s,
00154 const UT_VectorT<T> &, v,
00155 fpreal64 *, norm2)
00156 void addScaledVecNorm2Partial(T s, const UT_VectorT<T> &v,
00157 fpreal64 *norm2,
00158 const UT_JobInfo &info);
00159
00160
00161 THREADED_METHOD2(UT_VectorT<T>, shouldMultiThread(), scaleAddVec,
00162 T, s,
00163 const UT_VectorT<T> &, v)
00164 void scaleAddVecPartial(T s, const UT_VectorT<T> &v,
00165 const UT_JobInfo &info);
00166
00167
00168
00169 THREADED_METHOD2(UT_VectorT<T>, shouldMultiThread(), multAndSet,
00170 const UT_VectorT<T> &, a,
00171 const UT_VectorT<T> &, b)
00172 void multAndSetPartial(const UT_VectorT<T> &a,
00173 const UT_VectorT<T> &b,
00174 const UT_JobInfo &info);
00175
00176 T dot(const UT_VectorT<T> &v) const;
00177
00178
00179 UT_VectorT &operator= (const UT_VectorT<T> &v);
00180 UT_VectorT &operator+= (const UT_VectorT<T> &v);
00181 UT_VectorT &operator-= (const UT_VectorT<T> &v);
00182
00183
00184 UT_VectorT &operator*= (const UT_VectorT<T> &v);
00185 UT_VectorT &operator/= (const UT_VectorT<T> &v);
00186
00187
00188 UT_VectorT &operator*= (T scalar);
00189 UT_VectorT &operator/= (T scalar);
00190
00191
00192
00193
00194
00195
00196
00197 bool isEqual(const UT_VectorT<T> &v, int64 ulps);
00198
00199
00200 ostream &save(ostream &os) const;
00201 friend ostream &operator<<(ostream &os, const UT_VectorT<T> &v)
00202 { v.save(os); return os; }
00203
00204 T *getData() const { return &myVector[myNL]; }
00205
00206 protected:
00207
00208
00209 THREADED_METHOD2_CONST(UT_VectorT, shouldMultiThread(), normInternal,
00210 fpreal64 *, result,
00211 int, type)
00212 void normInternalPartial(fpreal64 *result, int type, const UT_JobInfo &info) const;
00213
00214 THREADED_METHOD2_CONST(UT_VectorT, shouldMultiThread(), distance2Internal,
00215 fpreal64 *, result,
00216 const UT_VectorT<T> &, v)
00217 void distance2InternalPartial(fpreal64 *result, const UT_VectorT<T> &v, const UT_JobInfo &info) const;
00218
00219
00220 THREADED_METHOD2_CONST(UT_VectorT, shouldMultiThread(), dotInternal,
00221 fpreal64 *, result,
00222 const UT_VectorT<T> &, v)
00223 void dotInternalPartial(fpreal64 *result,
00224 const UT_VectorT<T> &v,
00225 const UT_JobInfo &info) const;
00226
00227 private:
00228 int myNL, myNH;
00229 int myOwnData;
00230 T *myVector;
00231 };
00232
00233
00234
00235
00236
00237
00238
00239
00240 template <typename T>
00241 class UT_PermutationT
00242 {
00243 public:
00244
00245 UT_PermutationT(int nl, int nh);
00246 ~UT_PermutationT();
00247
00248
00249 UT_PermutationT(const UT_PermutationT<T> &p);
00250 UT_PermutationT<T> &operator=(const UT_PermutationT<T> &p);
00251
00252
00253 void zero();
00254
00255
00256 int getNL() const { return myNL; }
00257
00258
00259 int getNH() const { return myNH; }
00260
00261 int length() const { return myNH - myNL + 1; }
00262
00263
00264 void changeNL(int nl);
00265
00266
00267
00268
00269 void setShallowNL(int nl) { myNL = nl; }
00270 void setShallowNH(int nh) { myNH = nh; }
00271
00272
00273 T &operator()(int i)
00274 {
00275 UT_ASSERT_P(i >= myNL && i <= myNH);
00276 return myVector[i];
00277 }
00278 T operator()(int i) const
00279 {
00280 UT_ASSERT_P(i >= myNL && i <= myNH);
00281 return myVector[i];
00282 }
00283
00284 private:
00285 void clone(const UT_PermutationT<T> &p);
00286 int myNL, myNH;
00287 T *myVector;
00288 };
00289
00290 #if defined( WIN32 ) || defined( LINUX ) || defined( MBSD ) || defined(GAMEOS)
00291 #include "UT_Vector.C"
00292 #endif
00293
00294
00295
00296
00297
00298
00299
00300 typedef UT_PermutationT<int> UT_Permutation;
00301 typedef UT_VectorT<fpreal32> UT_VectorF;
00302 typedef UT_VectorT<fpreal64> UT_VectorD;
00303 typedef UT_VectorT<fpreal64> UT_Vector;
00304
00305
00306
00307
00308
00309
00310
00311 inline fpreal64 dot(const UT_VectorD &v1, const UT_VectorD &v2);
00312 inline fpreal64 distance2(const UT_VectorD &v1, const UT_VectorD &v2);
00313 inline fpreal32 dot(const UT_VectorF &v1, const UT_VectorF &v2);
00314 inline fpreal32 distance2(const UT_VectorF &v1, const UT_VectorF &v2);
00315
00316
00317 inline fpreal64
00318 dot(const UT_VectorD &v1, const UT_VectorD &v2)
00319 {
00320 return v1.dot(v2);
00321 }
00322
00323
00324 inline fpreal64
00325 distance2(const UT_VectorD &v1, const UT_VectorD &v2)
00326 {
00327 return v1.distance2(v2);
00328 }
00329
00330 inline fpreal32
00331 dot(const UT_VectorF &v1, const UT_VectorF &v2)
00332 {
00333 return v1.dot(v2);
00334 }
00335
00336
00337 inline fpreal32
00338 distance2(const UT_VectorF &v1, const UT_VectorF &v2)
00339 {
00340 return v1.distance2(v2);
00341 }
00342
00343
00344 #endif
00345