Changeset 1046
- Timestamp:
- Feb 7, 2008, 12:56:23 AM (15 years ago)
- Location:
- trunk/yat/utility
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/utility/VectorBase.h
r1041 r1046 43 43 namespace yat { 44 44 namespace utility { 45 45 46 46 class matrix; 47 47 class vector; … … 50 50 @brief This is the yat interface to GSL vector. 51 51 52 For time being 'double' is the only type supported. 53 54 \par File streams: 55 Reading and writing vectors to file streams are of course 56 supported. These are implemented without using GSL functionality, 57 and thus binary read and write to streams are not supported. 58 59 \par Vector views: 60 GSL vector views are supported and these are disguised as 61 ordinary utility::vectors. A support function is added, 62 utility::vector::isview(), that can be used to check if a vector 63 object is a view. Note that view vectors do not own the 64 underlying data, and a view is not valid if the vector/matrix 65 owing the data is deallocated. 66 67 \par 68 Currently there is no restriction on how a vector is used when 69 the vector is a const view into another vector or matrix. To 70 avoid unexpected runtime errors, the programmer must declare 71 const view vectors as 'const' in order to get compile time 72 diagnostics about improper use of a const view vector object. If 73 'const' is not used and the const view vector is used erroneously 74 (such as on the left hand side in assignments), the compiler will 75 not catch the error and runtime error will occur. assert(3) is 76 used to catch the runtime error during development. Example on 77 bad and proper use of const view vectors: 78 @code 79 const vector vm(13,1.0); 80 vector v1(vm,2,4); // bad code! not const! 81 v1(0)=-123; // accepted by compiler, runtime abort will occur 82 // or catched by assert depending on compiler flags 83 const vector v2(vm,2,4); // proper code 84 v2(0)=-123; // not acceptable for the compiler 85 @endcode 86 */ 87 52 This is an interface class for vectors containing the const 53 interface. For mutable functionality see VectorMutable. 54 */ 88 55 class VectorBase 89 56 { … … 123 90 bool equal(const VectorBase&, const double precision=0) const; 124 91 125 / //126 /// @return A const pointer to the internal GSL vector,127 ///92 /** 93 \return A const pointer to the internal GSL vector, 94 */ 128 95 const gsl_vector* gsl_vector_p(void) const; 129 96 … … 136 103 virtual bool isview(void) const=0; 137 104 138 / //139 /// @return thenumber of elements in the VectorBase.140 ///105 /** 106 \return number of elements in the VectorBase. 107 */ 141 108 size_t size(void) const; 142 109 … … 241 208 valid and a zero means that the corresponding element is a NaN. 242 209 243 \note Space for VectorBase\a flag is reallocated to fit the size of210 \note Space for vector \a flag is reallocated to fit the size of 244 211 VectorBase \a templat if sizes mismatch. 245 212 … … 261 228 262 229 /** 263 Similar to sort_index but creates a VectorBase with indices to the \a k264 smallest elements in \a invec.230 Similar to sort_index but creates a VectorBase with indices to 231 the \a k smallest elements in \a invec. 265 232 */ 266 233 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, … … 268 235 269 236 /** 270 Similar to sort_index but creates a VectorBase with indices to the \a k271 largest elements in \a invec.237 Similar to sort_index but creates a VectorBase with indices to 238 the \a k largest elements in \a invec. 272 239 */ 273 240 void sort_largest_index(std::vector<size_t>& sort_index, size_t k, -
trunk/yat/utility/VectorMutable.h
r1041 r1046 49 49 50 50 /** 51 @brief This is the yat interface to GSL vector. 52 53 For time being 'double' is the only type supported. 54 55 \par File streams: 56 Reading and writing vectors to file streams are of course 57 supported. These are implemented without using GSL functionality, 58 and thus binary read and write to streams are not supported. 59 60 \par Vector views: 61 GSL vector views are supported and these are disguised as 62 ordinary utility::vectors. A support function is added, 63 utility::vector::isview(), that can be used to check if a vector 64 object is a view. Note that view vectors do not own the 65 underlying data, and a view is not valid if the vector/matrix 66 owing the data is deallocated. 67 68 \par 69 Currently there is no restriction on how a vector is used when 70 the vector is a const view into another vector or matrix. To 71 avoid unexpected runtime errors, the programmer must declare 72 const view vectors as 'const' in order to get compile time 73 diagnostics about improper use of a const view vector object. If 74 'const' is not used and the const view vector is used erroneously 75 (such as on the left hand side in assignments), the compiler will 76 not catch the error and runtime error will occur. assert(3) is 77 used to catch the runtime error during development. Example on 78 bad and proper use of const view vectors: 79 @code 80 const vector vm(13,1.0); 81 vector v1(vm,2,4); // bad code! not const! 82 v1(0)=-123; // accepted by compiler, runtime abort will occur 83 // or catched by assert depending on compiler flags 84 const vector v2(vm,2,4); // proper code 85 v2(0)=-123; // not acceptable for the compiler 86 @endcode 51 @brief This is the mutable interface to GSL vector. 52 53 This class contains the mutable interface to vector classes. 54 55 The non-mutable interface is inherited from VectorBase. When 56 dealing with const vectors, it is preferable to use the 57 VectorBase signature because this allows usage of VectorConstView 58 too. 87 59 */ 88 89 60 class VectorMutable : public VectorBase 90 61 { … … 110 81 VectorMutable(const gsl_vector*); 111 82 112 / //113 ///The destructor.114 ///83 /** 84 The destructor. 85 */ 115 86 virtual ~VectorMutable(void); 116 87 117 / //118 ///Set all elements to \a value.119 ///88 /** 89 Set all elements to \a value. 90 */ 120 91 void all(double value); 121 92 … … 241 212 gsl_vector* vec_; 242 213 243 // Peter document 214 /** 215 Proxy class used to allow copy and assignment of VectorView. By 216 design vectors and matrices are passed as non-const references 217 in all constructors of VectorView. Because the standard does 218 not allow temporary objects to be bound to non-const 219 references, it is not possible to directly construct a 220 VectorView from a temporary VectorView returned from a 221 function. Instead this proxy class is created from the 222 temporary object and then a VectorView can be created from thsi 223 proxy. 224 225 \see VectorView 226 */ 244 227 struct proxy 245 228 { … … 252 235 public: 253 236 /** 237 conversion operator to protected proxy class. 254 238 */ 255 239 operator proxy(); … … 258 242 259 243 /** 260 Randomly shuffles the elements in Vector Base \a invec244 Randomly shuffles the elements in VectorMutable \a invec 261 245 */ 262 246 void shuffle(VectorMutable& invec); 263 247 264 248 /** 265 Sort the elements in the Vector Base.249 Sort the elements in the VectorMutable. 266 250 */ 267 251 void sort(VectorMutable&); -
trunk/yat/utility/VectorView.h
r1027 r1046 47 47 48 48 /** 49 @brief This is the yat interface to GSL vector.49 @brief This is the yat interface to gsl_vector_view. 50 50 51 For time being 'double' is the only type supported. 51 This class can be used to create a vector view into a Matrix or a 52 VectorMutable. Modifying a view will also modify the underlying 53 data, i.e., the Matrix or VectorMutable that is viewed into. For 54 that reason all constructors are taking non-const references to 55 disallow mutable views into a const objects. 52 56 53 \par File streams: 54 Reading and writing vectors to file streams are of course 55 supported. These are implemented without using GSL functionality, 56 and thus binary read and write to streams are not supported. 57 The fact that there is no copy constructor with const argument, 58 but only a so-called move constructor (copying a non-const 59 reference), implies that temporary objects such as objects 60 returned from functions can not be copied directly. Instead the 61 copying is done via a proxy class (see VectorMutable). Also since 62 we can not bind a temporary to a non-const reference, a 63 VectorView returned from a function cannot be sent directly to a 64 function. 65 For example 66 @code 67 Matrix m(10,10); 68 sum(m.row_view(0)); 69 @endcode 70 but you need to use create a dummie object 71 @code 72 Matrix m(10,10); 73 VectorView vv = m.row_view(0); 74 sum(vv); 75 @endcode 76 or since sum is a const function, you can use VectorConstView 77 @code 78 Matrix m(10,10); 79 sum(m.row_const_view(0)); 80 @endcode 57 81 58 \par Vector views: 59 GSL vector views are supported and these are disguised as 60 ordinary utility::vectors. A support function is added, 61 utility::vector::isview(), that can be used to check if a vector 62 object is a view. Note that view vectors do not own the 63 underlying data, and a view is not valid if the vector/matrix 64 owing the data is deallocated. 65 66 \par 67 Currently there is no restriction on how a vector is used when 68 the vector is a const view into another vector or matrix. To 69 avoid unexpected runtime errors, the programmer must declare 70 const view vectors as 'const' in order to get compile time 71 diagnostics about improper use of a const view vector object. If 72 'const' is not used and the const view vector is used erroneously 73 (such as on the left hand side in assignments), the compiler will 74 not catch the error and runtime error will occur. assert(3) is 75 used to catch the runtime error during development. Example on 76 bad and proper use of const view vectors: 77 @code 78 const vector vm(13,1.0); 79 vector v1(vm,2,4); // bad code! not const! 80 v1(0)=-123; // accepted by compiler, runtime abort will occur 81 // or catched by assert depending on compiler flags 82 const vector v2(vm,2,4); // proper code 83 v2(0)=-123; // not acceptable for the compiler 84 @endcode 82 Note that VectorView does not own underlying data, and a 83 VectorView is not valid if Vector/Matrix owning the data is 84 deallocated. 85 85 */ 86 87 86 class VectorView : public VectorMutable 88 87 { … … 90 89 /** 91 90 \brief Default constructor. 91 92 Creates a view into nothing and behaves like an empty vector. 92 93 */ 93 94 VectorView(void); … … 95 96 /** 96 97 \brief The copy constructor. 98 99 Modifications to created VectorView will also modify \a 100 other. Created VectorView is not dependent on \a other, but if 101 underlying data (Vector or Matrix) is deallocated VectorView is 102 invalid. 97 103 */ 104 VectorView(VectorView& other); 105 106 /** 107 \brief copy another VectorMutable 108 109 \note If the object viewed by the view goes out of scope or is 110 deleted, the view becomes invalid and the result of further use 111 is undefined. 112 */ 98 113 VectorView(VectorMutable& other); 99 VectorView(VectorView& other);100 114 101 115 /** 102 116 \brief VectorView constructor. 103 117 104 Create a view of Vector View\a v, with starting index \a offset,118 Create a view of VectorMutable \a v, with starting index \a offset, 105 119 size \a n, and an optional \a stride. 106 107 A VectorView view can be used as any VectorView with the difference108 that changes made to the view will also change the object that109 is viewed. Also, using the copy constructor will create a new110 VectorView object that is a copy of whatever is viewed. If a copy111 of the view is needed then you should use this constructor to112 obtain a copy.113 120 114 121 \note If the object viewed by the view goes out of scope or is … … 129 136 /// naturally, a column view otherwise. 130 137 /// 131 /// A VectorView view can be used as any VectorView with the difference 132 /// that changes made to the view will also change the object that 133 /// is viewed. Also, using the copy constructor will create a new 134 /// VectorView object that is a copy of whatever is viewed. If a copy 135 /// of the view is needed then you should use the VectorView view 136 /// constructor to obtain a copy. 138 /// A VectorView view can be used as any VectorMutable with the 139 /// difference that changes made to the view will also change the 140 /// object that is viewed. 137 141 /// 138 142 /// @note If the object viewed by the view goes out of scope or is … … 143 147 144 148 /** 149 \brief create VectorView from proxy class 145 150 */ 146 151 VectorView(proxy p); … … 161 166 \return A const reference to the resulting vector. 162 167 168 \note modifies underlying data. 169 170 \throw GSL_error if dimensions mis-match. 171 */ 172 const VectorView& operator=(const VectorView&); 173 174 /** 175 \brief The assignment operator. 176 177 \return A const reference to the resulting vector. 178 179 \note modifies underlying data. 180 163 181 \throw GSL_error if dimensions mis-match. 164 182 */ 165 183 const VectorView& operator=(const VectorBase&); 166 const VectorView& operator=(const VectorView&);167 184 168 185 private: … … 171 188 172 189 gsl_vector_view* view_; 173 gsl_vector_const_view* const_view_;174 175 190 }; 176 191
Note: See TracChangeset
for help on using the changeset viewer.