//=========================================================================== // GoTools Core - SINTEF Geometry Tools Core library, version 2.0.1 // // Copyright (C) 2000-2007, 2010 SINTEF ICT, Applied Mathematics, Norway. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation version 2 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., // 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // Contact information: E-mail: tor.dokken@sintef.no // SINTEF ICT, Department of Applied Mathematics, // P.O. Box 124 Blindern, // 0314 Oslo, Norway. // // Other licenses are also available for this software, notably licenses // for: // - Building commercial software. // - Building software whose source code you wish to keep private. //=========================================================================== 00015 #ifndef _SCRATCHVECT_H 00016 #define _SCRATCHVECT_H 00017 00018 00019 #include <vector> 00020 //#include "GoTools/utils/Utils.h" 00021 00022 00023 namespace Go { 00024 00030 template <typename T, size_t N> 00031 class ScratchVect { 00032 00033 private: 00034 T scratch_[N]; 00035 std::vector<T> stl_vect_; 00036 T* begin_; 00037 T* end_; 00038 00039 public: 00040 typedef T* iterator; 00041 typedef const T* const_iterator; 00042 00044 ScratchVect() : 00045 stl_vect_(0), 00046 begin_(scratch_), 00047 end_(scratch_) {} 00048 00050 ScratchVect(size_t size) : 00051 stl_vect_((size>N)?size:0) { 00052 if (size>N) { 00053 begin_ = &stl_vect_[0]; 00054 end_ = (&stl_vect_[0])+size; 00055 } else { 00056 begin_=scratch_; 00057 end_=(&scratch_[size]); 00058 } 00059 } 00060 00063 ScratchVect(size_t size, T elem) : 00064 stl_vect_((size>N)?size:0) { 00065 if (size>N) { 00066 begin_ = &stl_vect_[0]; 00067 end_ = (&stl_vect_[0])+size; 00068 } else { 00069 begin_=scratch_; 00070 end_=(&scratch_[size]); 00071 } 00072 std::fill(begin_, end_, elem); 00073 } 00074 00075 00076 00079 ScratchVect(const std::vector<T>& vec) { 00080 assign(vec.begin(), vec.end()); 00081 } 00082 00085 template<typename RAIter> 00086 ScratchVect(RAIter beg, RAIter end) { 00087 assign(beg, end); 00088 } 00089 00093 template <int M> 00094 ScratchVect(const ScratchVect<T,M> &orig) { 00095 assign(orig.begin(), orig.end()); 00096 } 00097 00099 ScratchVect& operator = (const ScratchVect &orig) { 00100 assign(orig.begin(), orig.end()); 00101 return *this; 00102 } 00103 00105 void resize(size_t new_size) { 00106 const int cur_size = size(); 00107 if ((int)new_size == cur_size) { 00108 return; 00109 } 00110 if ((int)new_size < cur_size) { 00111 if (cur_size < (int)N) { 00112 end_ -= (cur_size - new_size); 00113 } else if (new_size > (int)N) { 00114 // both cur_size and new_size > N 00115 end_ -= (cur_size - new_size); 00116 } else { 00117 // cur_size > N but new_size < N 00118 begin_ = scratch_; 00119 end_ = begin_ + new_size; 00120 stl_vect_.clear(); // release memory 00121 } 00122 } 00123 // new_size > size 00124 if (new_size < N) { 00125 end_ = begin_ + new_size; 00126 } else { 00127 stl_vect_.resize(new_size); 00128 begin_ = &stl_vect_[0]; 00129 end_ = begin_ + new_size; 00130 } 00131 } 00132 00135 template<typename RAIter> 00136 void assign(RAIter beg, RAIter end) { 00137 size_t sz = end - beg; 00138 stl_vect_.clear(); 00139 if (sz>N) { 00140 stl_vect_.insert(stl_vect_.end(), beg, end); 00141 begin_ = &stl_vect_[0]; 00142 end_ = (&stl_vect_[0])+sz; 00143 } else { 00144 begin_=scratch_; 00145 end_=(&scratch_[sz]); 00146 std::copy(beg, end, begin_); 00147 } 00148 } 00149 00151 inline iterator begin() { return begin_;} 00152 00154 inline const_iterator begin() const { return begin_;} 00155 00157 inline iterator end() { return end_;} 00158 00160 inline const_iterator end() const { return end_;} 00161 00163 inline size_t size() const {return (end_-begin_);} 00164 00166 inline const T& operator [] (int i) const { return begin_[i]; } 00167 inline T& operator [] (int i) { return begin_[i]; } 00168 00170 void push_back(const T &e) 00171 { 00172 size_t n=size(); 00173 if (n<N) 00174 *(end_++)=e; 00175 else if (n>N) 00176 { 00177 stl_vect_.push_back(e); 00178 begin_=&stl_vect_[0]; 00179 end_=&stl_vect_[0]+stl_vect_.size(); 00180 } 00181 else 00182 { 00183 stl_vect_.reserve(2*n); 00184 stl_vect_.insert(stl_vect_.end(), begin_, end_); 00185 stl_vect_.insert(stl_vect_.end(), e); 00186 begin_=&stl_vect_[0]; 00187 end_=&stl_vect_[0]+stl_vect_.size(); 00188 } 00189 } 00190 00192 void clear() 00193 { 00194 if (begin_==scratch_) 00195 end_=scratch_; 00196 else 00197 { 00198 stl_vect_.clear(); 00199 begin_=end_=scratch_; 00200 } 00201 } 00202 00203 }; 00204 } 00205 #endif 00206
Generated on Tue Sep 21 15:44:17 2010 for GoTools Core by  doxygen 1.6.3