Reference documentation for deal.II version 8.5.1
vector_slice.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2004 - 2016 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__vector_slice_h
17 #define dealii__vector_slice_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/exceptions.h>
21 #include <deal.II/base/array_view.h>
22 
23 DEAL_II_NAMESPACE_OPEN
24 
25 
48 template <typename VectorType>
50 {
51 public:
58  VectorSlice(VectorType &v);
63  VectorSlice(VectorType &v,
64  unsigned int start,
65  unsigned int length);
66 
72 
78 
83  unsigned int size() const;
84 
89  typename VectorType::reference operator[] (unsigned int i);
90 
95  typename VectorType::const_reference operator[] (unsigned int i) const;
96 
100  typename VectorType::iterator begin();
101 
105  typename VectorType::const_iterator begin() const;
106 
110  typename VectorType::iterator end();
111 
115  typename VectorType::const_iterator end() const;
116 
117 private:
121  VectorType &v;
125  const unsigned int start;
129  const unsigned int length;
130 };
131 
132 
140 template <typename VectorType>
141 inline
143 make_slice (VectorType &v)
144 {
146  return r;
147 }
148 
149 
150 
158 template <typename VectorType>
159 inline
161 make_slice (VectorType &v,
162  const unsigned int start,
163  const unsigned int length)
164 {
165  const VectorSlice<const VectorType> r(v, start, length);
166  return r;
167 }
168 
169 
170 
171 
172 //---------------------------------------------------------------------------
173 
174 template <typename VectorType>
175 inline
177  :
178  v(v), start(0), length(v.size())
179 {}
180 
181 
182 template <typename VectorType>
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 <typename VectorType>
196 inline
197 unsigned int
199 {
200  return length;
201 }
202 
203 
204 template <typename VectorType>
207 {
208  return ArrayView<typename VectorType::value_type *> (&v[start], length);
209 }
210 
211 
212 template <typename VectorType>
215 {
216  return ArrayView<const typename VectorType::value_type *> (&v[start], length);
217 }
218 
219 
220 template <typename VectorType>
221 inline
222 typename VectorType::reference
224 {
225  Assert ((i<length), ExcIndexRange(i, 0, length));
226 
227  return v[start+i];
228 }
229 
230 
231 template <typename VectorType>
232 inline
233 typename VectorType::const_reference
235 {
236  Assert ((i<length), ExcIndexRange(i, 0, length));
237 
238  return v[start+i];
239 }
240 
241 
242 template <typename VectorType>
243 inline
244 typename VectorType::const_iterator
246 {
247  return v.begin()+start;
248 }
249 
250 
251 template <typename VectorType>
252 inline
253 typename VectorType::iterator
255 {
256  return v.begin()+start;
257 }
258 
259 
260 template <typename VectorType>
261 inline
262 typename VectorType::const_iterator
264 {
265  return v.begin()+start+length;
266 }
267 
268 
269 template <typename VectorType>
270 inline
271 typename VectorType::iterator
273 {
274  return v.begin()+start+length;
275 }
276 
277 DEAL_II_NAMESPACE_CLOSE
278 
279 #endif
VectorType::iterator begin()
Definition: vector_slice.h:254
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
const VectorSlice< const VectorType > make_slice(VectorType &v)
Definition: vector_slice.h:143
unsigned int size() const
Definition: vector_slice.h:198
const unsigned int start
Definition: vector_slice.h:125
VectorType & v
Definition: vector_slice.h:121
#define Assert(cond, exc)
Definition: exceptions.h:313
VectorType::reference operator[](unsigned int i)
Definition: vector_slice.h:223
VectorType::iterator end()
Definition: vector_slice.h:272
const unsigned int length
Definition: vector_slice.h:129
VectorSlice(VectorType &v)
Definition: vector_slice.h:176
const VectorSlice< const VectorType > make_slice(VectorType &v, const unsigned int start, const unsigned int length)
Definition: vector_slice.h:161