DUNE PDELab (git)

basearray.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5#ifndef DUNE_ISTL_BASEARRAY_HH
6#define DUNE_ISTL_BASEARRAY_HH
7
8#include <cassert>
9#include <cmath>
10#include <cstddef>
11#include <memory>
12#include <algorithm>
13
14#include "istlexception.hh"
16
21namespace Dune {
22
24namespace Imp {
25
46 template<class B, class ST=std::size_t >
47 class base_array_unmanaged
48 {
49 public:
50
51 //===== type definitions and constants
52
54 typedef B member_type;
55
57 typedef ST size_type;
58
60 using reference = B&;
61
63 using const_reference = const B&;
64
65 //===== access to components
66
68 reference operator[] (size_type i)
69 {
70#ifdef DUNE_ISTL_WITH_CHECKING
71 if (i>=n) DUNE_THROW(ISTLError,"index out of range");
72#endif
73 return p[i];
74 }
75
77 const_reference operator[] (size_type i) const
78 {
79#ifdef DUNE_ISTL_WITH_CHECKING
80 if (i>=n) DUNE_THROW(ISTLError,"index out of range");
81#endif
82 return p[i];
83 }
84
86 template<class T>
87 class RealIterator
88 : public RandomAccessIteratorFacade<RealIterator<T>, T>
89 {
90 public:
92 typedef typename std::remove_const<T>::type ValueType;
93
94 friend class RandomAccessIteratorFacade<RealIterator<const ValueType>, const ValueType>;
95 friend class RandomAccessIteratorFacade<RealIterator<ValueType>, ValueType>;
96 friend class RealIterator<const ValueType>;
97 friend class RealIterator<ValueType>;
98
100 RealIterator ()
101 : p(0), i(0)
102 {}
103
104 RealIterator (const B* _p, B* _i) : p(_p), i(_i)
105 { }
106
107 RealIterator(const RealIterator<ValueType>& it)
108 : p(it.p), i(it.i)
109 {}
110
112 size_type index () const
113 {
114 return i-p;
115 }
116
118 bool equals (const RealIterator<ValueType>& other) const
119 {
120 assert(other.p==p);
121 return i==other.i;
122 }
123
125 bool equals (const RealIterator<const ValueType>& other) const
126 {
127 assert(other.p==p);
128 return i==other.i;
129 }
130
131 std::ptrdiff_t distanceTo(const RealIterator& o) const
132 {
133 return o.i-i;
134 }
135
136 private:
138 void increment()
139 {
140 ++i;
141 }
142
144 void decrement()
145 {
146 --i;
147 }
148
149 // Needed for operator[] of the iterator
150 reference elementAt (std::ptrdiff_t offset) const
151 {
152 return *(i+offset);
153 }
154
156 reference dereference () const
157 {
158 return *i;
159 }
160
161 void advance(std::ptrdiff_t d)
162 {
163 i+=d;
164 }
165
166 const B* p;
167 B* i;
168 };
169
171 typedef RealIterator<B> iterator;
172
173
175 iterator begin ()
176 {
177 return iterator(p,p);
178 }
179
181 iterator end ()
182 {
183 return iterator(p,p+n);
184 }
185
188 iterator beforeEnd ()
189 {
190 return iterator(p,p+n-1);
191 }
192
195 iterator beforeBegin ()
196 {
197 return iterator(p,p-1);
198 }
199
201 iterator find (size_type i)
202 {
203 return iterator(p,p+std::min(i,n));
204 }
205
207 typedef RealIterator<const B> const_iterator;
208
210 const_iterator begin () const
211 {
212 return const_iterator(p,p+0);
213 }
214
216 const_iterator end () const
217 {
218 return const_iterator(p,p+n);
219 }
220
223 const_iterator beforeEnd () const
224 {
225 return const_iterator(p,p+n-1);
226 }
227
230 const_iterator beforeBegin () const
231 {
232 return const_iterator(p,p-1);
233 }
234
236 const_iterator find (size_type i) const
237 {
238 return const_iterator(p,p+std::min(i,n));
239 }
240
241
242 //===== sizes
243
245 size_type size () const
246 {
247 return n;
248 }
249
251 const B* data() const
252 {
253 return p;
254 }
255
257 B* data()
258 {
259 return p;
260 }
261
262 protected:
264 base_array_unmanaged ()
265 : n(0), p(0)
266 {}
268 base_array_unmanaged (size_type n_, B* p_)
269 : n(n_), p(p_)
270 {}
271 size_type n; // number of elements in array
272 B *p; // pointer to dynamically allocated built-in array
273 };
274
275
276
298 template<class B, class ST=std::size_t >
299 class compressed_base_array_unmanaged
300 {
301 public:
302
303 //===== type definitions and constants
304
306 typedef B member_type;
307
309 typedef ST size_type;
310
312 using reference = B&;
313
315 using const_reference = const B&;
316
317 //===== access to components
318
320 reference operator[] (size_type i)
321 {
322 const size_type* lb = std::lower_bound(j, j+n, i);
323 if (lb == j+n || *lb != i)
324 DUNE_THROW(ISTLError,"index "<<i<<" not in compressed array");
325 return p[lb-j];
326 }
327
329 const_reference operator[] (size_type i) const
330 {
331 const size_type* lb = std::lower_bound(j, j+n, i);
332 if (lb == j+n || *lb != i)
333 DUNE_THROW(ISTLError,"index "<<i<<" not in compressed array");
334 return p[lb-j];
335 }
336
338 template<class T>
339 class RealIterator
340 : public BidirectionalIteratorFacade<RealIterator<T>, T>
341 {
342 public:
344 typedef typename std::remove_const<T>::type ValueType;
345
346 friend class BidirectionalIteratorFacade<RealIterator<const ValueType>, const ValueType>;
347 friend class BidirectionalIteratorFacade<RealIterator<ValueType>, ValueType>;
348 friend class RealIterator<const ValueType>;
349 friend class RealIterator<ValueType>;
350
352 RealIterator ()
353 : p(0), j(0), i(0)
354 {}
355
357 RealIterator (B* _p, size_type* _j, size_type _i)
358 : p(_p), j(_j), i(_i)
359 { }
360
364 RealIterator(const RealIterator<ValueType>& it)
365 : p(it.p), j(it.j), i(it.i)
366 {}
367
368
370 bool equals (const RealIterator<ValueType>& it) const
371 {
372 assert(p==it.p);
373 return (i)==(it.i);
374 }
375
377 bool equals (const RealIterator<const ValueType>& it) const
378 {
379 assert(p==it.p);
380 return (i)==(it.i);
381 }
382
383
385 size_type index () const
386 {
387 return j[i];
388 }
389
391 void setindex (size_type k)
392 {
393 return j[i] = k;
394 }
395
403 size_type offset () const
404 {
405 return i;
406 }
407
408 private:
410 void increment()
411 {
412 ++i;
413 }
414
416 void decrement()
417 {
418 --i;
419 }
420
422 reference dereference () const
423 {
424 return p[i];
425 }
426
427 B* p;
428 size_type* j;
429 size_type i;
430 };
431
433 typedef RealIterator<B> iterator;
434
436 iterator begin ()
437 {
438 return iterator(p,j,0);
439 }
440
442 iterator end ()
443 {
444 return iterator(p,j,n);
445 }
446
449 iterator beforeEnd ()
450 {
451 return iterator(p,j,n-1);
452 }
453
456 iterator beforeBegin ()
457 {
458 return iterator(p,j,-1);
459 }
460
462 iterator find (size_type i)
463 {
464 const size_type* lb = std::lower_bound(j, j+n, i);
465 return (lb != j+n && *lb == i)
466 ? iterator(p,j,lb-j)
467 : end();
468 }
469
471 typedef RealIterator<const B> const_iterator;
472
474 const_iterator begin () const
475 {
476 return const_iterator(p,j,0);
477 }
478
480 const_iterator end () const
481 {
482 return const_iterator(p,j,n);
483 }
484
487 const_iterator beforeEnd () const
488 {
489 return const_iterator(p,j,n-1);
490 }
491
494 const_iterator beforeBegin () const
495 {
496 return const_iterator(p,j,-1);
497 }
498
500 const_iterator find (size_type i) const
501 {
502 const size_type* lb = std::lower_bound(j, j+n, i);
503 return (lb != j+n && *lb == i)
504 ? const_iterator(p,j,lb-j)
505 : end();
506 }
507
508 //===== sizes
509
511 size_type size () const
512 {
513 return n;
514 }
515
516 protected:
518 compressed_base_array_unmanaged ()
519 : n(0), p(0), j(0)
520 {}
521
522 size_type n; // number of elements in array
523 B *p; // pointer to dynamically allocated built-in array
524 size_type* j; // the index set
525 };
526
527} // end namespace Imp
528
529} // end namespace
530
531#endif
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:506
constexpr auto equals
Function object for performing equality comparison.
Definition: hybridutilities.hh:572
constexpr decltype(auto) elementAt(Container &&c, Index &&i)
Get element at given position from container.
Definition: hybridutilities.hh:126
This file implements iterator facade classes for writing stl conformant iterators.
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)