Changeset 783
- Timestamp:
- Mar 5, 2007, 10:45:32 PM (16 years ago)
- Location:
- trunk/yat/utility
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/utility/vector.cc
r782 r783 30 30 #include "utility.h" 31 31 32 #include <cassert> 32 33 #include <iostream> 33 34 #include <sstream> 35 #include <utility> 34 36 #include <vector> 35 #include <utility>36 37 37 38 namespace theplu { … … 211 212 void vector::div(const vector& other) 212 213 { 214 assert(v_); assert(other.v_); 213 215 int status=gsl_vector_div(v_,other.v_); 214 216 if (status) … … 237 239 void vector::mul(const vector& other) 238 240 { 241 assert(v_); assert(other.v_); 239 242 int status=gsl_vector_mul(v_,other.v_); 240 243 if (status) … … 245 248 void vector::reverse(void) 246 249 { 250 assert(v_); 247 251 gsl_vector_reverse(v_); 248 252 } … … 251 255 void vector::set(const vector& vec) 252 256 { 257 assert(v_); assert(vec.v_); 253 258 if (gsl_vector_memcpy(v_,vec.v_)) 254 259 throw utility::GSL_error("vector::set dimension mis-match"); … … 258 263 void vector::set_all(const double& value) 259 264 { 265 assert(v_); 260 266 gsl_vector_set_all(v_,value); 261 267 } … … 272 278 void vector::swap(size_t i, size_t j) 273 279 { 280 assert(v_); 274 281 int status=gsl_vector_swap_elements(v_, i, j); 275 282 if (status) … … 280 287 double& vector::operator()(size_t i) 281 288 { 289 assert(v_); 282 290 double* d=gsl_vector_ptr(v_, i); 283 291 if (!d) … … 348 356 const vector& vector::operator+=(const vector& other) 349 357 { 350 int status=gsl_vector_add(v_,other. v_); 358 assert(v_); 359 int status=gsl_vector_add(v_, other.v_); 351 360 if (status) 352 361 throw utility::GSL_error(std::string("vector::add", status)); … … 357 366 const vector& vector::operator+=(double d) 358 367 { 368 assert(v_); 359 369 gsl_vector_add_constant(v_, d); 360 370 return *this; … … 364 374 const vector& vector::operator-=(const vector& other) 365 375 { 376 assert(v_); 366 377 int status=gsl_vector_sub(v_, other.v_); 367 378 if (status) … … 373 384 const vector& vector::operator*=(const double d) 374 385 { 386 assert(v_); 375 387 gsl_vector_scale(v_, d); 376 388 return *this; … … 426 438 void set_basis(vector& v, size_t i) 427 439 { 440 assert(v.gsl_vector_p()); 428 441 gsl_vector_set_basis(v.gsl_vector_p(),i); 429 442 } … … 432 445 void sort(vector& v) 433 446 { 447 assert(v.gsl_vector_p()); 434 448 gsl_sort_vector(v.gsl_vector_p()); 435 449 } … … 448 462 void swap(vector& v, vector& w) 449 463 { 464 assert(v.gsl_vector_p()); assert(w.gsl_vector_p()); 450 465 int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p()); 451 466 if (status) -
trunk/yat/utility/vector.h
r782 r783 63 63 \par 64 64 Currently there is no restriction on how a vector is used when 65 the vector is a const view into another vector or 66 matrix. To avoid unexpected runtime errors, the programmer must 67 declare const view vectors as const in order to get compile time 68 diagnostics about improper use of a const view vector object. An 69 example of improper use of a const view vector is assignment of 70 values to an element in the object. If the const view object was 71 declared const the assigment will be caught by the compiler 72 whereas it will cause a (un-catchable) runtime error. Example: 65 the vector is a const view into another vector or matrix. To 66 avoid unexpected runtime errors, the programmer must declare 67 const view vectors as 'const' in order to get compile time 68 diagnostics about improper use of a const view vector object. If 69 'const' is not used and the const view vector is used erroneously 70 (such as on the left hand side in assignments), the compiler will 71 not catch the error and runtime error will occur. assert(3) is 72 used to catch the runtime error during development. Example on 73 bad and proper use of const view vectors: 73 74 @code 74 75 const vector vm(13,1.0); 75 76 vector v1(vm,2,4); // bad code! not const! 76 77 v1(0)=-123; // accepted by compiler, runtime abort will occur 78 // or catched by assert depending on compiler flags 77 79 const vector v2(vm,2,4); // proper code 78 80 v2(0)=-123; // not acceptable for the compiler … … 415 417 gsl_vector_const_view* view_const_; 416 418 // proxy_v_ is used to access the proper underlying v_ or v_const_ 417 // in all const member functions. 419 // in all const member functions. It is not used by design for 420 // non-const vector functions and operators. This is to make sure 421 // that runtime errors occur if a const vector is used in an 422 // inappropriate manner such as on left hand side in assignment 423 // (remember, v_ is null for const vector views). 418 424 const gsl_vector* proxy_v_; 419 425 };
Note: See TracChangeset
for help on using the changeset viewer.