Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vector_slice.h
1 // ---------------------------------------------------------------------
2 // @f$Id: vector_slice.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 2004 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__vector_slice_h
18 #define __deal2__vector_slice_h
19 
20 #include <deal.II/base/config.h>
22 
23 DEAL_II_NAMESPACE_OPEN
24 
25 
49 template <class VECTOR>
50 class VectorSlice
51 {
52 public:
64  VectorSlice(VECTOR &v);
71  VectorSlice(VECTOR &v,
72  unsigned int start,
73  unsigned int length);
74 
80  unsigned int size() const;
81 
87  typename VECTOR::reference operator[] (unsigned int i);
88 
95  typename VECTOR::const_reference operator[] (unsigned int i) const;
96 
100  typename VECTOR::iterator begin();
101 
105  typename VECTOR::const_iterator begin() const;
106 
110  typename VECTOR::iterator end();
111 
115  typename VECTOR::const_iterator end() const;
116 
117 private:
121  VECTOR &v;
125  const unsigned int start;
129  const unsigned int length;
130 };
131 
132 
140 template <class VECTOR>
141 inline
143 make_slice (VECTOR &v)
144 {
145  const VectorSlice<const VECTOR> r(v);
146  return r;
147 }
148 
149 
150 
158 template <class VECTOR>
159 inline
161 make_slice (VECTOR &v,
162  const unsigned int start,
163  const unsigned int length)
164 {
165  const VectorSlice<const VECTOR> r(v, start, length);
166  return r;
167 }
168 
169 
170 
171 
172 //---------------------------------------------------------------------------
173 
174 template <class VECTOR>
175 inline
177  :
178  v(v), start(0), length(v.size())
179 {}
180 
181 
182 template <class VECTOR>
183 inline
185  unsigned int start,
186  unsigned int length)
187  :
188  v(v), start(start), length(length)
189 {
190  Assert((start+length<=v.size()),
191  ExcIndexRange(length, 0, v.size()-start+1));
192 }
193 
194 
195 template <class VECTOR>
196 inline
197 unsigned int
199 {
200  return length;
201 }
202 
203 
204 template <class VECTOR>
205 inline
206 typename VECTOR::reference
208 {
209  Assert ((i<length), ExcIndexRange(i, 0, length));
210 
211  return v[start+i];
212 }
213 
214 
215 template <class VECTOR>
216 inline
217 typename VECTOR::const_reference
218 VectorSlice<VECTOR>::operator[](unsigned int i) const
219 {
220  Assert ((i<length), ExcIndexRange(i, 0, length));
221 
222  return v[start+i];
223 }
224 
225 
226 template <class VECTOR>
227 inline
228 typename VECTOR::const_iterator
230 {
231  return v.begin()+start;
232 }
233 
234 
235 template <class VECTOR>
236 inline
237 typename VECTOR::iterator
239 {
240  return v.begin()+start;
241 }
242 
243 
244 template <class VECTOR>
245 inline
246 typename VECTOR::const_iterator
248 {
249  return v.begin()+start+length;
250 }
251 
252 
253 template <class VECTOR>
254 inline
255 typename VECTOR::iterator
257 {
258  return v.begin()+start+length;
259 }
260 
261 DEAL_II_NAMESPACE_CLOSE
262 
263 #endif
unsigned int size() const
Definition: vector_slice.h:198
const unsigned int length
Definition: vector_slice.h:129
VectorSlice(VECTOR &v)
Definition: vector_slice.h:176
VECTOR::reference operator[](unsigned int i)
Definition: vector_slice.h:207
VECTOR::iterator begin()
Definition: vector_slice.h:238
#define Assert(cond, exc)
Definition: exceptions.h:299
::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
const unsigned int start
Definition: vector_slice.h:125
VECTOR::iterator end()
Definition: vector_slice.h:256
const VectorSlice< const VECTOR > make_slice(VECTOR &v)
Definition: vector_slice.h:143
VECTOR & v
Definition: vector_slice.h:121
const VectorSlice< const VECTOR > make_slice(VECTOR &v, const unsigned int start, const unsigned int length)
Definition: vector_slice.h:161