00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ------------------------------------------------------------------- 00022 */ 00023 #ifndef O2SCL_UVECTOR_TLATE_H 00024 #define O2SCL_UVECTOR_TLATE_H 00025 00026 /** \file uvector_tlate.h 00027 \brief File for definitions of unit-stride vectors 00028 */ 00029 00030 #include <iostream> 00031 #include <cstdlib> 00032 #include <string> 00033 #include <fstream> 00034 #include <sstream> 00035 #include <vector> 00036 #include <o2scl/err_hnd.h> 00037 #include <o2scl/string_conv.h> 00038 #include <o2scl/array.h> 00039 #include <o2scl/vector.h> 00040 #include <gsl/gsl_vector.h> 00041 00042 #ifndef DOXYGENP 00043 namespace o2scl { 00044 #endif 00045 00046 /** 00047 \brief A vector view with unit stride 00048 00049 \future Could allow user-defined specification of 00050 restrict keyword 00051 */ 00052 template<class data_t> class uvector_view_tlate { 00053 00054 #ifndef DOXYGEN_INTERNAL 00055 00056 protected: 00057 00058 /// The data 00059 data_t *data; 00060 /// The vector sz 00061 size_t sz; 00062 /// Zero if memory is owned elsewhere, 1 otherwise 00063 int owner; 00064 00065 #endif 00066 00067 public: 00068 00069 /// \name Copy constructors 00070 //@{ 00071 /// Copy constructor - create a new view of the same vector 00072 uvector_view_tlate(const uvector_view_tlate &v) { 00073 data=v.data; 00074 sz=v.sz; 00075 owner=0; 00076 } 00077 00078 /// Copy constructor - create a new view of the same vector 00079 uvector_view_tlate& operator=(const uvector_view_tlate &v) { 00080 data=v.data; 00081 sz=v.sz; 00082 owner=0; 00083 00084 return *this; 00085 } 00086 //@} 00087 00088 ~uvector_view_tlate() {}; 00089 00090 /// \name Get and set methods 00091 //@{ 00092 /** 00093 \brief Array-like indexing 00094 */ 00095 data_t &operator[](size_t i) { 00096 #if O2SCL_NO_RANGE_CHECK 00097 #else 00098 if (i>=sz) { 00099 set_err((((std::string)"Array index ")+itos(i)+" out of bounds" 00100 +" in uvector_view_tlate::operator[]. Size: "+ 00101 itos(sz)+ 00102 " (index should be less than size).").c_str(),gsl_index); 00103 return data[0]; 00104 } 00105 #endif 00106 return data[i]; 00107 } 00108 00109 /** 00110 \brief Array-like indexing 00111 */ 00112 const data_t &operator[](size_t i) const { 00113 #if O2SCL_NO_RANGE_CHECK 00114 #else 00115 if (i>=sz) { 00116 set_err((((std::string)"Array index ")+itos(i)+" out of bounds" 00117 +" in uvector_view_tlate::operator[] const. Size: "+ 00118 itos(sz)+ 00119 " (index should be less than size).").c_str(),gsl_index); 00120 return data[0]; 00121 } 00122 #endif 00123 return data[i]; 00124 } 00125 00126 /** 00127 \brief Array-like indexing 00128 */ 00129 data_t &operator()(size_t i) { 00130 #if O2SCL_NO_RANGE_CHECK 00131 #else 00132 if (i>=sz) { 00133 set_err((((std::string)"Array index ")+itos(i)+" out of bounds" 00134 +" in uvector_view_tlate::operator(). Size: "+ 00135 itos(sz)+ 00136 " (index should be less than size).").c_str(),gsl_index); 00137 return data[0]; 00138 } 00139 #endif 00140 return data[i]; 00141 } 00142 00143 /** 00144 \brief Array-like indexing 00145 */ 00146 const data_t &operator()(size_t i) const { 00147 #if O2SCL_NO_RANGE_CHECK 00148 #else 00149 if (i>=sz) { 00150 set_err((((std::string)"Array index ")+itos(i)+" out of bounds" 00151 +" in uvector_view_tlate::operator() const. Size: "+ 00152 itos(sz)+ 00153 " (index should be less than size).").c_str(),gsl_index); 00154 return data[0]; 00155 } 00156 #endif 00157 return data[i]; 00158 } 00159 00160 /** \brief Get (with optional range-checking) */ 00161 data_t get(size_t i) const { 00162 #if O2SCL_NO_RANGE_CHECK 00163 #else 00164 if (i>=sz) { 00165 set_err((((std::string)"Array index ")+itos(i)+" out of bounds" 00166 +" in uvector_view_tlate::get(). Size: "+ 00167 itos(sz)+ 00168 " (index should be less than size).").c_str(),gsl_index); 00169 return data[0]; 00170 } 00171 #endif 00172 return data[i]; 00173 } 00174 00175 /** \brief Get pointer (with optional range-checking) */ 00176 data_t *get_ptr(size_t i) { 00177 #if O2SCL_NO_RANGE_CHECK 00178 #else 00179 if (i>=sz) { 00180 set_err((((std::string)"Array index ")+itos(i)+" out of bounds" 00181 +" in uvector_view_tlate::get_ptr(). Size: "+ 00182 itos(sz)+ 00183 " (index should be less than size).").c_str(),gsl_index); 00184 return data; 00185 } 00186 #endif 00187 return data+i; 00188 } 00189 00190 /** \brief Get pointer (with optional range-checking) */ 00191 const data_t *get_const_ptr(size_t i) const { 00192 #if O2SCL_NO_RANGE_CHECK 00193 #else 00194 if (i>=sz) { 00195 set_err("Index out of range in uvector_view::get_const_ptr().",1); 00196 return NULL; 00197 } 00198 #endif 00199 return (const data_t *)(data+i); 00200 } 00201 00202 /** \brief Set (with optional range-checking) */ 00203 int set(size_t i, data_t val) { 00204 #if O2SCL_NO_RANGE_CHECK 00205 #else 00206 if (i>=sz) { 00207 set_err_ret((((std::string)"Array index ")+itos(i)+" out of bounds" 00208 +" in uvector_view_tlate::set(). Size: "+ 00209 itos(sz)+ 00210 " (index should be less than size).").c_str(),gsl_index); 00211 } 00212 #endif 00213 data[i]=val; 00214 return 0; 00215 } 00216 00217 /** \brief Set all of the value to be the value \c val */ 00218 int set_all(data_t val) { 00219 for(size_t i=0;i<sz;i++) { 00220 data[i]=val; 00221 } 00222 return 0; 00223 } 00224 00225 /** 00226 \brief Method to return vector size 00227 00228 If no memory has been allocated, this will quietly 00229 return zero. 00230 */ 00231 size_t size() const { 00232 return sz; 00233 } 00234 //@} 00235 00236 /// \name Other methods 00237 //@{ 00238 00239 /** \brief Swap vectors */ 00240 int swap(uvector_view_tlate<data_t> &x) { 00241 size_t t1, t2; 00242 double *t3; 00243 int t5; 00244 00245 t1=sz; 00246 t3=data; 00247 t5=owner; 00248 00249 sz=x.sz; 00250 data=x.data; 00251 owner=x.owner; 00252 00253 x.sz=t1; 00254 x.data=t3; 00255 x.owner=t5; 00256 00257 return 0; 00258 } 00259 00260 /// Return true if this object owns the data it refers to 00261 bool is_owner() const { 00262 if (owner==1) return true; 00263 return false; 00264 } 00265 00266 /** \brief Exhaustively look through the array for a 00267 particular value 00268 00269 This can only fail if \em all of the entries in the array are 00270 not finite, in which case it calls set_err() and returns 00271 0. The error handler is reset at the beginning of lookup(). 00272 */ 00273 size_t lookup(const data_t x0) const { 00274 err_hnd->reset(); 00275 const uvector_view_tlate<data_t> *a=this; 00276 size_t row=0, i=0, nvar=size(); 00277 while(!finite((*a)[i]) && i<nvar-1) i++; 00278 if (i==nvar-1) { 00279 set_err("Array not finite in intp_base::lookup()",1); 00280 return 0; 00281 } 00282 data_t best=(*a)[i], bdiff=fabs((*a)[i]-x0); 00283 for(;i<nvar;i++) { 00284 if (finite((*a)[i]) && fabs((*a)[i]-x0)<bdiff) { 00285 row=i; 00286 best=(*a)[i]; 00287 bdiff=fabs((*a)[i]-x0); 00288 } 00289 } 00290 return row; 00291 } 00292 00293 /** \brief Find the maximum element */ 00294 data_t max() const { 00295 data_t maxval; 00296 if (sz>0) { 00297 maxval=data[0]; 00298 for(size_t i=1;i<sz;i++) { 00299 if (data[i]>maxval) { 00300 maxval=data[i]; 00301 } 00302 } 00303 } else { 00304 return 0.0; 00305 } 00306 return maxval; 00307 } 00308 00309 /** \brief Find the minimum element */ 00310 data_t min() const { 00311 data_t minval; 00312 if (sz>0) { 00313 minval=data[0]; 00314 for(size_t i=1;i<sz;i++) { 00315 if (data[i]<minval) { 00316 minval=data[i]; 00317 } 00318 } 00319 } else { 00320 return 0.0; 00321 } 00322 return minval; 00323 } 00324 //@} 00325 00326 /// \name Arithmetic 00327 //@{ 00328 /** \brief operator+= */ 00329 uvector_view_tlate<data_t> &operator+= 00330 (const uvector_view_tlate<data_t> &x) { 00331 size_t lsz=x.sz; 00332 if (lsz>sz) lsz=sz; 00333 for(size_t i=0;i<lsz;i++) (*this)[i]+=x[i]; 00334 00335 return *this; 00336 } 00337 00338 /** \brief operator-= */ 00339 uvector_view_tlate<data_t> &operator-= 00340 (const uvector_view_tlate<data_t> &x) { 00341 size_t lsz=x.sz; 00342 if (lsz>sz) lsz=sz; 00343 for(size_t i=0;i<lsz;i++) (*this)[i]-=x[i]; 00344 00345 return *this; 00346 } 00347 00348 /** \brief operator+= */ 00349 uvector_view_tlate<data_t> &operator+=(const data_t &y) { 00350 for(size_t i=0;i<sz;i++) (*this)[i]+=y; 00351 00352 return *this; 00353 } 00354 00355 /** \brief operator-= */ 00356 uvector_view_tlate<data_t> &operator-=(const data_t &y) { 00357 for(size_t i=0;i<sz;i++) (*this)[i]-=y; 00358 00359 return *this; 00360 } 00361 00362 /** \brief operator*= */ 00363 uvector_view_tlate<data_t> &operator*=(const data_t &y) { 00364 for(size_t i=0;i<sz;i++) (*this)[i]*=y; 00365 00366 return *this; 00367 } 00368 00369 /** \brief Norm */ 00370 data_t norm() const { 00371 data_t result=0; 00372 for(size_t i=0;i<sz;i++) { 00373 result+=(*this)[i]*(*this)[i]; 00374 } 00375 return sqrt(result); 00376 } 00377 //@} 00378 00379 #ifndef DOXYGEN_INTERNAL 00380 00381 protected: 00382 00383 /** \brief Empty constructor provided for use by 00384 uvector_tlate(const uvector_tlate &v) 00385 */ 00386 uvector_view_tlate() {}; 00387 00388 #endif 00389 00390 }; 00391 00392 /** 00393 \brief A vector with unit stride 00394 00395 There are several global binary operators associated with 00396 objects of type \ref uvector_tlate. The are documented in the 00397 "Functions" section of \ref uvector_tlate.h. 00398 00399 \todo Create a sort_unique() method as in ovector. 00400 */ 00401 template<class data_t> class uvector_tlate : 00402 public uvector_view_tlate<data_t> { 00403 public: 00404 00405 /// \name Standard constructor 00406 //@{ 00407 /** \brief Create an uvector of size \c n with owner as 'true' */ 00408 uvector_tlate(size_t n=0) { 00409 00410 this->sz=0; 00411 this->data=0; 00412 00413 // This must be set to 1 even if n=0 so that future 00414 // calls to operator= work properly 00415 this->owner=1; 00416 00417 if (n>0) { 00418 this->data=(data_t *)malloc(n*sizeof(data_t)); 00419 if (this->data) { 00420 this->sz=n; 00421 } else { 00422 set_err("No memory for data in uvector_tlate constructor", 00423 gsl_enomem); 00424 } 00425 } 00426 } 00427 //@} 00428 00429 /// \name Copy constructors 00430 //@{ 00431 /// Deep copy constructor - allocate new space and make a copy 00432 uvector_tlate(const uvector_tlate &v) : 00433 uvector_view_tlate<data_t>() { 00434 size_t n=v.sz; 00435 this->sz=0; 00436 this->data=0; 00437 if (n>0) { 00438 this->data=(data_t *)malloc(n*sizeof(data_t)); 00439 if (this->data) { 00440 this->sz=n; 00441 this->owner=1; 00442 for(size_t i=0;i<n;i++) { 00443 this->data[i]=v[i]; 00444 } 00445 } else { 00446 set_err("No memory for data in uvector_tlate constructor", 00447 gsl_enomem); 00448 } 00449 } else { 00450 this->sz=0; 00451 } 00452 } 00453 00454 /// Deep copy constructor - allocate new space and make a copy 00455 uvector_tlate(const uvector_view_tlate<data_t> &v) : 00456 uvector_view_tlate<data_t>() { 00457 size_t n=v.size(); 00458 this->sz=0; 00459 this->data=0; 00460 if (n>0) { 00461 this->data=(data_t *)malloc(n*sizeof(data_t)); 00462 if (this->data) { 00463 this->sz=n; 00464 this->owner=1; 00465 for(size_t i=0;i<n;i++) { 00466 this->data[i]=v[i]; 00467 } 00468 } else { 00469 set_err("No memory for data in uvector_tlate constructor", 00470 gsl_enomem); 00471 } 00472 } else { 00473 this->sz=0; 00474 } 00475 } 00476 00477 /** \brief Deep copy constructor - if owner is true, allocate space and 00478 make a new copy, otherwise, just copy into the view 00479 */ 00480 uvector_tlate& operator=(const uvector_tlate &v) { 00481 size_t sz2=v.sz; 00482 this->sz=0; 00483 this->data=0; 00484 if (this->owner) { 00485 allocate(sz2); 00486 } else { 00487 if (this->sz!=sz2) { 00488 set_err("Sizes don't match in uvector_tlate::operator=()", 00489 gsl_ebadlen); 00490 return *this; 00491 } 00492 } 00493 for(size_t i=0;i<sz2;i++) { 00494 this->data[i]=v[i]; 00495 } 00496 return *this; 00497 } 00498 00499 /** \brief Deep copy constructor - if owner is true, allocate space and 00500 make a new copy, otherwise, just copy into the view 00501 */ 00502 uvector_tlate& operator=(const uvector_view_tlate<data_t> &v) { 00503 size_t sz2=v.size(); 00504 this->sz=0; 00505 this->data=0; 00506 if (this->owner) { 00507 allocate(sz2); 00508 } else { 00509 if (this->sz!=sz2) { 00510 set_err("Sizes don't match in uvector_tlate::operator=()", 00511 gsl_ebadlen); 00512 return *this; 00513 } 00514 } 00515 for(size_t i=0;i<sz2;i++) { 00516 this->data[i]=v[i]; 00517 } 00518 return *this; 00519 } 00520 //@} 00521 00522 ~uvector_tlate() { 00523 if (this->sz>0) { 00524 if (this->owner==1) { 00525 std::free(this->data); 00526 } 00527 this->sz=0; 00528 } 00529 } 00530 00531 /// \name Memory allocation 00532 //@{ 00533 /** 00534 \brief Allocate memory for size \c n after freeing any memory 00535 presently in use 00536 */ 00537 int allocate(size_t nsize) { 00538 if (this->sz>0) free(); 00539 00540 if (nsize>0) { 00541 this->data=(data_t *)malloc(nsize*sizeof(data_t)); 00542 if (this->data) { 00543 this->sz=nsize; 00544 this->owner=1; 00545 } else { 00546 set_err_ret("No memory for data in uvector_tlate::allocate()", 00547 gsl_enomem); 00548 } 00549 } else { 00550 set_err_ret("Zero size in uvector::allocate()",gsl_einval); 00551 } 00552 return 0; 00553 } 00554 00555 /** 00556 \brief Free the memory 00557 00558 This function will safely do nothing if used without first 00559 allocating memory or if called multiple times in succession. 00560 */ 00561 int free() { 00562 if (this->sz>0) { 00563 if (this->owner==1) { 00564 std::free(this->data); 00565 } 00566 this->sz=0; 00567 } 00568 return 0; 00569 } 00570 //@} 00571 00572 /// \name Other methods 00573 //@{ 00574 /// Erase an element from the array. 00575 int erase(size_t ix) { 00576 00577 // Only proceed if the user gave an element inside the array 00578 if (this->sz>ix) { 00579 00580 // Decrement the size 00581 this->sz--; 00582 00583 // Allocate new space 00584 data_t *newdat=(data_t *)(malloc(this->sz*sizeof(data_t))); 00585 00586 // Copy into the new space 00587 for(size_t i=0;i<this->sz;i++) { 00588 if (i<ix) newdat[i]=this->data[i]; 00589 else newdat[i]=this->data[i+1]; 00590 } 00591 00592 // Free the old space and reset the pointer 00593 std::free(this->data); 00594 this->data=newdat; 00595 00596 } else { 00597 set_err_ret("Cannot erase() beyond end of uvector_tlate.", 00598 gsl_einval); 00599 } 00600 00601 return 0; 00602 } 00603 //@} 00604 00605 /** 00606 \brief Sort the vector and ensure all elements are unique by 00607 removing duplicates 00608 */ 00609 int sort_unique() { 00610 o2scl::vector_sort<data_t,uvector_tlate<data_t> >(this->sz,*this); 00611 for(size_t i=1;i<this->sz;i++) { 00612 if ((*this)[i]==(*this)[i-1]) { 00613 int ret=erase(i-1); 00614 if (ret!=0) { 00615 set_err_ret("Erase failed in sort_unique().",gsl_esanity); 00616 } 00617 i--; 00618 } 00619 } 00620 return 0; 00621 } 00622 //@} 00623 00624 }; 00625 00626 /** \brief Create a vector from an array 00627 */ 00628 template<class data_t> class uvector_array_tlate : 00629 public uvector_view_tlate<data_t> { 00630 public: 00631 /** \brief Create a vector from \c dat with size \c n */ 00632 uvector_array_tlate(size_t n, data_t *dat) { 00633 if (n>0) { 00634 this->data=dat; 00635 this->sz=n; 00636 this->owner=0; 00637 } 00638 } 00639 }; 00640 00641 /** \brief Create a vector from a subvector of another 00642 */ 00643 template<class data_t> class uvector_subvector_tlate : 00644 public uvector_view_tlate<data_t> { 00645 public: 00646 /** \brief Create a vector from \c orig */ 00647 uvector_subvector_tlate(uvector_view_tlate<data_t> &orig, 00648 size_t offset, size_t n) { 00649 if (offset+n-1<orig.size) { 00650 this->data=orig.data+offset; 00651 this->sz=n; 00652 this->owner=0; 00653 } else { 00654 this->sz=0; 00655 set_err("Subvector failed in uvector_sub_view().",1); 00656 } 00657 } 00658 }; 00659 00660 /** \brief Create a vector from an const array 00661 */ 00662 template<class data_t> class uvector_const_array_tlate : 00663 public uvector_view_tlate<data_t> { 00664 public: 00665 /** \brief Create a vector from \c dat with size \c n */ 00666 uvector_const_array_tlate(size_t n, const data_t *dat) { 00667 if (n>0) { 00668 // We have to do an explicit cast here, but we prevent the 00669 // user from changing the data. 00670 this->data=(data_t *)dat; 00671 this->sz=n; 00672 this->owner=0; 00673 } 00674 } 00675 00676 ~uvector_const_array_tlate() {}; 00677 00678 #ifndef DOXYGEN_INTERNAL 00679 00680 protected: 00681 00682 /** \name Ensure \c const by hiding non-const members 00683 */ 00684 //@{ 00685 data_t &operator[](size_t i) { return this->data[0]; } 00686 data_t &operator()(size_t i) { return this->data[0]; } 00687 data_t *get_ptr(size_t i) { return NULL; } 00688 int set(size_t i, data_t val) { return 0; } 00689 int swap(uvector_view_tlate<data_t> &x) { 00690 return 0; 00691 } 00692 int set_all(double val) { return 0; } 00693 uvector_view_tlate<data_t> &operator+= 00694 (const uvector_view_tlate<data_t> &x) { 00695 return *this; 00696 } 00697 uvector_view_tlate<data_t> &operator-= 00698 (const uvector_view_tlate<data_t> &x) { 00699 return *this; 00700 } 00701 uvector_view_tlate<data_t> &operator*=(const data_t &y) { 00702 return *this; 00703 } 00704 //@} 00705 00706 #endif 00707 00708 }; 00709 00710 /** \brief Create a const vector from a subvector of another vector 00711 */ 00712 template<class data_t> class uvector_const_subvector_tlate : 00713 public uvector_view_tlate<data_t> { 00714 public: 00715 /** \brief Create a vector from \c orig 00716 */ 00717 uvector_const_subvector_tlate 00718 (const uvector_view_tlate<data_t> &orig, 00719 size_t offset, size_t n) { 00720 if (offset+n-1<orig.sz) { 00721 this->data=orig.data+offset; 00722 this->sz=n; 00723 this->owner=0; 00724 } else { 00725 this->sz=0; 00726 set_err("Subvector failed in uvector_subvector().",1); 00727 } 00728 } 00729 00730 #ifndef DOXYGENP 00731 00732 protected: 00733 00734 /** \name Ensure \c const by hiding non-const members 00735 */ 00736 //@{ 00737 data_t &operator[](size_t i) { return this->data[0]; } 00738 data_t &operator()(size_t i) { return this->data[0]; } 00739 data_t *get_ptr(size_t i) { return NULL; } 00740 int set(size_t i, data_t val) { return 0; } 00741 int swap(uvector_view_tlate<data_t> &x) { 00742 return 0; 00743 } 00744 int set_all(double val) { return 0; } 00745 uvector_view_tlate<data_t> &operator+= 00746 (const uvector_view_tlate<data_t> &x) { 00747 return *this; 00748 } 00749 uvector_view_tlate<data_t> &operator-= 00750 (const uvector_view_tlate<data_t> &x) { 00751 return *this; 00752 } 00753 uvector_view_tlate<data_t> &operator*=(const data_t &y) { 00754 return *this; 00755 } 00756 //@} 00757 00758 #endif 00759 00760 }; 00761 00762 #ifdef DOXYGENP 00763 /// uvector typedef 00764 typedef uvector_tlate<data_t,size_t> uvector; 00765 /// uvector_view typedef 00766 typedef uvector_view_tlate<data_t,size_t> uvector_view; 00767 /// uvector_array typedef 00768 typedef uvector_array_tlate<data_t,size_t> uvector_array; 00769 /// uvector_subvector typedef 00770 typedef uvector_subvector_tlate<data_t,size_t> uvector_subvector; 00771 /// uvector_const_array typedef 00772 typedef uvector_const_array_tlate<data_t,size_t> uvector_const_array; 00773 /// uvector_const_subvector typedef 00774 typedef uvector_const_subvector_tlate<data_t,size_t> uvector_const_subvector; 00775 #else 00776 /// uvector typedef 00777 typedef uvector_tlate<double> uvector; 00778 /// uvector_view typedef 00779 typedef uvector_view_tlate<double> uvector_view; 00780 /// uvector_array typedef 00781 typedef uvector_array_tlate<double> uvector_array; 00782 /// uvector_subvector typedef 00783 typedef uvector_subvector_tlate<double> uvector_subvector; 00784 /// uvector_const_array typedef 00785 typedef uvector_const_array_tlate<double> uvector_const_array; 00786 /// uvector_const_subvector typedef 00787 typedef uvector_const_subvector_tlate<double> uvector_const_subvector; 00788 #endif 00789 00790 /// uvector_int typedef 00791 typedef uvector_tlate<int> uvector_int; 00792 /// uvector_int_view typedef 00793 typedef uvector_view_tlate<int> uvector_int_view; 00794 /// uvector_int_array typedef 00795 typedef uvector_array_tlate<int> uvector_int_array; 00796 /// uvector_int_subvector typedef 00797 typedef uvector_subvector_tlate<int> uvector_int_subvector; 00798 /// uvector_int_const_array typedef 00799 typedef uvector_const_array_tlate<int> uvector_int_const_array; 00800 /// uvector_int_const_subvector typedef 00801 typedef uvector_const_subvector_tlate<int> uvector_int_const_subvector; 00802 00803 /** 00804 \brief A operator for naive vector output 00805 00806 This outputs all of the vector elements. All of these are 00807 separated by one space character, though no trailing space or \c 00808 endl is sent to the output. 00809 */ 00810 template<class data_t> 00811 std::ostream &operator<< 00812 (std::ostream &os, 00813 const uvector_view_tlate<data_t> &v) { 00814 if (v.size()>0) { 00815 for(size_t i=0;i<v.size()-1;i++) { 00816 os << v[i] << ' '; 00817 } 00818 os << v[v.size()-1]; 00819 } else { 00820 os << "<empty>"; 00821 } 00822 return os; 00823 } 00824 00825 /** \brief A simple class to provide an \c allocate() function 00826 for \ref uvector 00827 */ 00828 class uvector_alloc { 00829 public: 00830 /// Allocate \c v for \c i elements 00831 void allocate(uvector &o, int i) { o.allocate(i); } 00832 /// Free memory 00833 void free(uvector &o) { o.free(); } 00834 }; 00835 00836 /** \brief A simple class to provide an \c allocate() function 00837 for \ref uvector_int 00838 */ 00839 class uvector_int_alloc { 00840 public: 00841 /// Allocate \c v for \c i elements 00842 void allocate(uvector_int &o, int i) { o.allocate(i); } 00843 /// Free memory 00844 void free(uvector_int &o) { o.free(); } 00845 }; 00846 00847 /** \brief A vector with unit-stride where the memory allocation is 00848 performed in the constructor 00849 */ 00850 #ifdef DOXYGENP 00851 template<size_t N=0> class ufvector : 00852 public uvector_tlate<data_t> 00853 #else 00854 template<size_t N=0> class ufvector : 00855 public uvector_tlate<double> 00856 #endif 00857 { 00858 public: 00859 ufvector() : uvector_tlate<double>(N) { 00860 } 00861 }; 00862 00863 #ifndef DOXYGENP 00864 } 00865 #endif 00866 00867 #endif 00868
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page