include/deal.II/base/vector_slice.h

00001 //---------------------------------------------------------------------------
00002 //    @f$Id: vector_slice.h 25345 2012-03-31 08:37:04Z bangerth @f$
00003 //
00004 //    Copyright (C) 2004, 2005, 2006, 2010, 2012 by the deal.II authors
00005 //
00006 //    This file is subject to QPL and may not be  distributed
00007 //    without copyright and license information. Please refer
00008 //    to the file deal.II/doc/license.html for the  text  and
00009 //    further information on this license.
00010 //
00011 //---------------------------------------------------------------------------
00012 #ifndef __deal2__vector_slice_h
00013 #define __deal2__vector_slice_h
00014 
00015 #include <deal.II/base/config.h>
00016 #include <deal.II/base/exceptions.h>
00017 
00018 DEAL_II_NAMESPACE_OPEN
00019 
00020 
00044 template <class VECTOR>
00045 class VectorSlice
00046 {
00047   public:
00059     VectorSlice(VECTOR& v);
00066     VectorSlice(VECTOR& v,
00067                 unsigned int start,
00068                 unsigned int length);
00069 
00075     unsigned int size() const;
00076 
00082     typename VECTOR::reference operator[] (unsigned int i);
00083 
00090     typename VECTOR::const_reference operator[] (unsigned int i) const;
00091 
00095     typename VECTOR::iterator begin();
00096 
00100     typename VECTOR::const_iterator begin() const;
00101 
00105     typename VECTOR::iterator end();
00106 
00110     typename VECTOR::const_iterator end() const;
00111 
00112   private:
00116     VECTOR& v;
00120     const unsigned int start;
00124     const unsigned int length;
00125 };
00126 
00127 
00135 template <class VECTOR>
00136 inline
00137 const VectorSlice<const VECTOR>
00138 make_slice (VECTOR& v)
00139 {
00140   const VectorSlice<const VECTOR> r(v);
00141   return r;
00142 }
00143 
00144 
00145 
00153 template <class VECTOR>
00154 inline
00155 const VectorSlice<const VECTOR>
00156 make_slice (VECTOR& v,
00157             const unsigned int start,
00158             const unsigned int length)
00159 {
00160   const VectorSlice<const VECTOR> r(v, start, length);
00161   return r;
00162 }
00163 
00164 
00165 
00166 
00167 //---------------------------------------------------------------------------
00168 
00169 template <class VECTOR>
00170 inline
00171 VectorSlice<VECTOR>::VectorSlice(VECTOR& v)
00172                 :
00173                 v(v), start(0), length(v.size())
00174 {}
00175 
00176 
00177 template <class VECTOR>
00178 inline
00179 VectorSlice<VECTOR>::VectorSlice(VECTOR& v,
00180                          unsigned int start,
00181                          unsigned int length)
00182                 :
00183                 v(v), start(start), length(length)
00184 {
00185   Assert((start+length<=v.size()),
00186          ExcIndexRange(length, 0, v.size()-start+1));
00187 }
00188 
00189 
00190 template <class VECTOR>
00191 inline
00192 unsigned int
00193 VectorSlice<VECTOR>::size() const
00194 {
00195   return length;
00196 }
00197 
00198 
00199 template <class VECTOR>
00200 inline
00201 typename VECTOR::reference
00202 VectorSlice<VECTOR>::operator[](unsigned int i)
00203 {
00204   Assert ((i<length), ExcIndexRange(i, 0, length));
00205 
00206   return v[start+i];
00207 }
00208 
00209 
00210 template <class VECTOR>
00211 inline
00212 typename VECTOR::const_reference
00213 VectorSlice<VECTOR>::operator[](unsigned int i) const
00214 {
00215   Assert ((i<length), ExcIndexRange(i, 0, length));
00216 
00217   return v[start+i];
00218 }
00219 
00220 
00221 template <class VECTOR>
00222 inline
00223 typename VECTOR::const_iterator
00224 VectorSlice<VECTOR>::begin() const
00225 {
00226   return v.begin()+start;
00227 }
00228 
00229 
00230 template <class VECTOR>
00231 inline
00232 typename VECTOR::iterator
00233 VectorSlice<VECTOR>::begin()
00234 {
00235   return v.begin()+start;
00236 }
00237 
00238 
00239 template <class VECTOR>
00240 inline
00241 typename VECTOR::const_iterator
00242 VectorSlice<VECTOR>::end() const
00243 {
00244   return v.begin()+start+length;
00245 }
00246 
00247 
00248 template <class VECTOR>
00249 inline
00250 typename VECTOR::iterator
00251 VectorSlice<VECTOR>::end()
00252 {
00253   return v.begin()+start+length;
00254 }
00255 
00256 DEAL_II_NAMESPACE_CLOSE
00257 
00258 #endif
00259 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

deal.II documentation generated on Wed May 23 2012 12:03:22 by doxygen 1.7.3