00001 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 00002 /* ******** *** SparseLib++ */ 00003 /* ******* ** *** *** *** */ 00004 /* ***** *** ******** ******** */ 00005 /* ***** *** ******** ******** R. Pozo */ 00006 /* ** ******* *** ** *** *** K. Remington */ 00007 /* ******** ******** A. Lumsdaine */ 00008 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 00009 /* */ 00010 /* */ 00011 /* SparseLib++ : Sparse Matrix Library */ 00012 /* */ 00013 /* National Institute of Standards and Technology */ 00014 /* University of Notre Dame */ 00015 /* Authors: R. Pozo, K. Remington, A. Lumsdaine */ 00016 /* */ 00017 /* NOTICE */ 00018 /* */ 00019 /* Permission to use, copy, modify, and distribute this software and */ 00020 /* its documentation for any purpose and without fee is hereby granted */ 00021 /* provided that the above notice appear in all copies and supporting */ 00022 /* documentation. */ 00023 /* */ 00024 /* Neither the Institutions (National Institute of Standards and Technology, */ 00025 /* University of Notre Dame) nor the Authors make any representations about */ 00026 /* the suitability of this software for any purpose. This software is */ 00027 /* provided ``as is'' without expressed or implied warranty. */ 00028 /* */ 00029 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 00030 00031 // 00032 // mv_vector_TYPE.h Basic vector class (TYPE precision) 00033 // 00034 00035 #ifndef _MV_VECTOR_TYPE_H 00036 #define _MV_VECTOR_TYPE_H 00037 00038 // 00039 // Key features: 00040 // 00041 // o efficient indexing as fast as native C arrays 00042 // o supports only unit strides (for efficient indexing) 00043 // o copy-by-value semantics 00044 // o optional "share" semantics allows vectors to be constructed as 00045 // "views", or "references" of an existing memory, using 00046 // MV_Vector_::ref modifier in the constuctor. (see note below.) 00047 // o vector views can assign and references sections of vector, but 00048 // cannot modify their *size*. 00049 // o block-range indexing via MV_VecIndex class (e.g. A(I) = B; ) 00050 // (note for the above to work, A(I) must return a vector view.) 00051 // o optional range checking (compile switch) 00052 // o fast copying (A=B) via loop unrolling 00053 // o (experimental derived FMV_Vector class) for even faster copying via 00054 // memcpy() for data elements employing simple bit-wise copying (e.g. 00055 // float, complex, et.c) 00056 // o support for both [] and () style indexing ([] not available 00057 // for matrices.) 00058 // 00059 // NOTES: 00060 // 00061 // o O(N) loops for copying and assigning scalars to vectors unroll 00062 // loops to a depth of 4. Thus on some machines, it is faster 00063 // to execute A=scalar, than to manually assign a native C 00064 // array using an explicit for loop: 00065 // 00066 // for (i=0; i<N; d[i++] = scalar); 00067 // 00068 // o function code for the () and [] operators has been 00069 // inlined into the class declaration, for compilers 00070 // (e.g. Turbo C++ v. 3.0) that refuse to inline otherwise. 00071 // 00072 // o The MV_Vector(*TYPE, int len) constructor is now a deep-copy to 00073 // match the MV_Vector(const &MV_Vector) constructor. To create a view 00074 // (share semantics) use 00075 // 00076 // MV_Vector_TYPE A( &d[0], n, MV_Vector_::ref ); 00077 // 00078 // This allows one to construct vectors as views of any contiguous C 00079 // array. It will not destruct the memory space when the vector 00080 // is destroyed or goes out of scope. 00081 // 00082 00083 00084 00085 00086 00087 #include <sstream> 00088 // for formatted printing of matrices 00089 00090 #ifdef MV_VECTOR_BOUNDS_CHECK 00091 # include <assert.h> 00092 #endif 00093 00094 #include "mv_vecindex.h" 00095 00096 // this is really used as a sort of global constant. The reason 00097 // for creating its own type is that so it can be overloaded to perform 00098 // a deep or shallow assignement. (Any variable of type MV_Vector_::ref_type 00099 // has only one possible value: one.) 00100 00101 #include "mv_vector_ref.h" 00102 00103 class MV_Vector_TYPE 00104 { 00105 protected: 00106 TYPE *p_; 00107 unsigned int dim_; 00108 int ref_; // 0 or 1; does this own its own memory space? 00109 public: 00110 00111 00112 /*::::::::::::::::::::::::::*/ 00113 /* Constructors/Destructors */ 00114 /*::::::::::::::::::::::::::*/ 00115 00116 MV_Vector_TYPE(); 00117 MV_Vector_TYPE(unsigned int); 00118 MV_Vector_TYPE(unsigned int, const TYPE&); // can't be inlined 00119 //because of 'for' 00120 // statement. 00121 MV_Vector_TYPE(TYPE*, unsigned int); // new copy 00122 MV_Vector_TYPE(const TYPE*, unsigned int); // new copy ??? 00123 00124 // reference of an exisiting data structure 00125 // 00126 MV_Vector_TYPE(TYPE*, unsigned int, MV_Vector_::ref_type i); 00127 MV_Vector_TYPE(const MV_Vector_TYPE &); 00128 ~MV_Vector_TYPE(); 00129 00130 /*::::::::::::::::::::::::::::::::*/ 00131 /* Indices and access operations */ 00132 /*::::::::::::::::::::::::::::::::*/ 00133 00134 00135 // code for operator() is defined here, otherwise some compilers 00136 // (e.g. Turbo C++ v 3.0) cannot inline them properly... 00137 // 00138 TYPE& operator()(unsigned int i) 00139 { 00140 # ifdef MV_VECTOR_BOUNDS_CHECK 00141 assert(i < dim_); 00142 # endif 00143 return p_[i]; 00144 } 00145 const TYPE& operator()(unsigned int i) const 00146 { 00147 # ifdef MV_VECTOR_BOUNDS_CHECK 00148 assert(i < dim_); 00149 # endif 00150 return p_[i]; 00151 } 00152 00153 TYPE& operator[](unsigned int i) 00154 { 00155 # ifdef MV_VECTOR_BOUNDS_CHECK 00156 assert(i < dim_); 00157 # endif 00158 return p_[i]; 00159 } 00160 const TYPE& operator[](unsigned int i) const 00161 { 00162 # ifdef MV_VECTOR_BOUNDS_CHECK 00163 assert(i < dim_); 00164 # endif 00165 return p_[i]; 00166 } 00167 00168 00169 00170 MV_Vector_TYPE operator()(const MV_VecIndex &I) ; 00171 MV_Vector_TYPE operator()(void); 00172 const MV_Vector_TYPE operator()(void) const; 00173 const MV_Vector_TYPE operator()(const MV_VecIndex &I) const; 00174 // 00175 // the following line causes ambiguatities with template instantiations 00176 // should be avoid. Used &v(0) explicitly when converting to TYPE*. 00177 // 00178 // inline operator const TYPE*() const {return p_;} 00179 inline unsigned int size() const { return dim_;} 00180 inline int ref() const { return ref_;} 00181 inline int null() const {return dim_== 0;} 00182 // 00183 // Create a new *uninitalized* vector of size N 00184 MV_Vector_TYPE & newsize(unsigned int ); 00185 00186 /*::::::::::::::*/ 00187 /* Assignment */ 00188 /*::::::::::::::*/ 00189 00190 MV_Vector_TYPE & operator=(const MV_Vector_TYPE&); 00191 MV_Vector_TYPE & operator=(const TYPE&); 00192 00193 00194 friend std::ostream& operator<<(std::ostream &s, const MV_Vector_TYPE &A); 00195 00196 }; 00197 00198 #include "mv_blas1_TYPE.h" 00199 00200 00201 #endif