Dune Core Modules (2.4.1)

basearray.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_ISTL_BASEARRAY_HH
4#define DUNE_ISTL_BASEARRAY_HH
5
6#include "assert.h"
7#include <cmath>
8#include <cstddef>
9#include <memory>
10#include <algorithm>
11
12#include "istlexception.hh"
14
19namespace Dune {
20
39 template<class B, class A=std::allocator<B> >
41 {
42 public:
43
44 //===== type definitions and constants
45
47 typedef B member_type;
48
50 typedef A allocator_type;
51
53 typedef typename A::size_type size_type;
54
55
56 //===== access to components
57
60 {
61#ifdef DUNE_ISTL_WITH_CHECKING
62 if (i>=n) DUNE_THROW(ISTLError,"index out of range");
63#endif
64 return p[i];
65 }
66
68 const B& operator[] (size_type i) const
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 template<class T>
79 : public RandomAccessIteratorFacade<RealIterator<T>, T>
80 {
81 public:
83 typedef typename remove_const<T>::type ValueType;
84
87 friend class RealIterator<const ValueType>;
88 friend class RealIterator<ValueType>;
89
92 : p(0), i(0)
93 {}
94
95 RealIterator (const B* _p, B* _i) : p(_p), i(_i)
96 { }
97
98 RealIterator(const RealIterator<ValueType>& it)
99 : p(it.p), i(it.i)
100 {}
101
104 {
105 return i-p;
106 }
107
109 bool equals (const RealIterator<ValueType>& other) const
110 {
111 assert(other.p==p);
112 return i==other.i;
113 }
114
116 bool equals (const RealIterator<const ValueType>& other) const
117 {
118 assert(other.p==p);
119 return i==other.i;
120 }
121
122 std::ptrdiff_t distanceTo(const RealIterator& o) const
123 {
124 return o.i-i;
125 }
126
127 private:
129 void increment()
130 {
131 ++i;
132 }
133
135 void decrement()
136 {
137 --i;
138 }
139
141 B& dereference () const
142 {
143 return *i;
144 }
145
146 void advance(std::ptrdiff_t d)
147 {
148 i+=d;
149 }
150
151 const B* p;
152 B* i;
153 };
154
157
158
161 {
162 return iterator(p,p);
163 }
164
167 {
168 return iterator(p,p+n);
169 }
170
174 {
175 return iterator(p,p+n-1);
176 }
177
181 {
182 return iterator(p,p-1);
183 }
184
187 {
188 if (i<n)
189 return iterator(p,p+i);
190 else
191 return iterator(p,p+n);
192 }
193
196
199 {
200 return const_iterator(p,p+0);
201 }
202
205 {
206 return const_iterator(p,p+n);
207 }
208
212 {
213 return const_iterator(p,p+n-1);
214 }
215
219 {
220 return const_iterator(p,p-1);
221 }
222
225 {
226 if (i<n)
227 return const_iterator(p,p+i);
228 else
229 return const_iterator(p,p+n);
230 }
231
232
233 //===== sizes
234
237 {
238 return n;
239 }
240
241 protected:
244 : n(0), p(0)
245 {}
248 : n(n_), p(p_)
249 {}
250 size_type n; // number of elements in array
251 B *p; // pointer to dynamically allocated built-in array
252 };
253
254
255
270 template<class B, class A=std::allocator<B> >
272 {
273 public:
274
275 //===== type definitions and constants
276
278 typedef B member_type;
279
281 typedef A allocator_type;
282
285
288
291
293 typedef typename A::difference_type difference_type;
294
295 //===== constructors and such
296
299 : base_array_unmanaged<B,A>()
300 { }
301
304 : base_array_unmanaged<B,A>(_n ,_p)
305 {}
306
307 //===== window manipulation methods
308
310 void set (size_type _n, B* _p)
311 {
312 this->n = _n;
313 this->p = _p;
314 }
315
318 {
319 this->p += this->n;
320 this->n = newsize;
321 }
322
324 void move (difference_type offset, size_type newsize)
325 {
326 this->p += offset;
327 this->n = newsize;
328 }
329
331 void move (difference_type offset)
332 {
333 this->p += offset;
334 }
335
337 B* getptr ()
338 {
339 return this->p;
340 }
341 };
342
343
344
360 template<class B, class A=std::allocator<B> >
362 {
363 public:
364
365 //===== type definitions and constants
366
368 typedef B member_type;
369
371 typedef A allocator_type;
372
375
378
381
383 typedef typename A::difference_type difference_type;
384
385 //===== constructors and such
386
389 : base_array_unmanaged<B,A>()
390 {}
391
394 : base_array_unmanaged<B,A>(_n, 0)
395 {
396 if (this->n>0) {
397 this->p = allocator_.allocate(this->n);
398 new (this->p)B[this->n];
399 } else
400 {
401 this->n = 0;
402 this->p = 0;
403 }
404 }
405
408 {
409 // allocate memory with same size as a
410 this->n = a.n;
411
412 if (this->n>0) {
413 this->p = allocator_.allocate(this->n);
414 new (this->p)B[this->n];
415 } else
416 {
417 this->n = 0;
418 this->p = 0;
419 }
420
421 // and copy elements
422 for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
423 }
424
427 {
428 const base_array& a = static_cast<const base_array&>(_a);
429
430 // allocate memory with same size as a
431 this->n = a.n;
432 if (this->n>0) {
433 this->p = allocator_.allocate(this->n);
434 new (this->p)B[this->n];
435 } else
436 {
437 this->n = 0;
438 this->p = 0;
439 }
440
441 // and copy elements
442 for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
443 }
444
445
448 {
449 if (this->n>0) {
450 int i=this->n;
451 while (i)
452 this->p[--i].~B();
453 allocator_.deallocate(this->p,this->n);
454 }
455 }
456
459 {
460 if (this->n==_n) return;
461
462 if (this->n>0) {
463 int i=this->n;
464 while (i)
465 this->p[--i].~B();
466 allocator_.deallocate(this->p,this->n);
467 }
468 this->n = _n;
469 if (this->n>0) {
470 this->p = allocator_.allocate(this->n);
471 new (this->p)B[this->n];
472 } else
473 {
474 this->n = 0;
475 this->p = 0;
476 }
477 }
478
481 {
482 if (&a!=this) // check if this and a are different objects
483 {
484 // adjust size of array
485 if (this->n!=a.n) // check if size is different
486 {
487 if (this->n>0) {
488 int i=this->n;
489 while (i)
490 this->p[--i].~B();
491 allocator_.deallocate(this->p,this->n); // delete old memory
492 }
493 this->n = a.n;
494 if (this->n>0) {
495 this->p = allocator_.allocate(this->n);
496 new (this->p)B[this->n];
497 } else
498 {
499 this->n = 0;
500 this->p = 0;
501 }
502 }
503 // copy data
504 for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
505 }
506 return *this;
507 }
508
511 {
512 return this->operator=(static_cast<const base_array&>(a));
513 }
514
515 protected:
516
517 A allocator_;
518 };
519
520
521
522
542 template<class B, class A=std::allocator<B> >
544 {
545 public:
546
547 //===== type definitions and constants
548
550 typedef B member_type;
551
553 typedef A allocator_type;
554
556 typedef typename A::size_type size_type;
557
558 //===== access to components
559
562 {
563 const size_type* lb = std::lower_bound(j, j+n, i);
564 if (lb == j+n || *lb != i)
565 DUNE_THROW(ISTLError,"index "<<i<<" not in compressed array");
566 return p[lb-j];
567 }
568
570 const B& operator[] (size_type i) const
571 {
572 const size_type* lb = std::lower_bound(j, j+n, i);
573 if (lb == j+n || *lb != i)
574 DUNE_THROW(ISTLError,"index "<<i<<" not in compressed array");
575 return p[lb-j];
576 }
577
579 template<class T>
581 : public BidirectionalIteratorFacade<RealIterator<T>, T>
582 {
583 public:
585 typedef typename remove_const<T>::type ValueType;
586
589 friend class RealIterator<const ValueType>;
590 friend class RealIterator<ValueType>;
591
594 : p(0), j(0), i(0)
595 {}
596
599 : p(_p), j(_j), i(_i)
600 { }
601
606 : p(it.p), j(it.j), i(it.i)
607 {}
608
609
611 bool equals (const RealIterator<ValueType>& it) const
612 {
613 assert(p==it.p);
614 return (i)==(it.i);
615 }
616
619 {
620 assert(p==it.p);
621 return (i)==(it.i);
622 }
623
624
627 {
628 return j[i];
629 }
630
633 {
634 return j[i] = k;
635 }
636
645 {
646 return i;
647 }
648
649 private:
651 void increment()
652 {
653 ++i;
654 }
655
657 void decrement()
658 {
659 --i;
660 }
661
663 B& dereference () const
664 {
665 return p[i];
666 }
667
668 B* p;
669 size_type* j;
670 size_type i;
671 };
672
675
678 {
679 return iterator(p,j,0);
680 }
681
684 {
685 return iterator(p,j,n);
686 }
687
691 {
692 return iterator(p,j,n-1);
693 }
694
698 {
699 return iterator(p,j,-1);
700 }
701
704 {
705 const size_type* lb = std::lower_bound(j, j+n, i);
706 return (lb != j+n && *lb == i)
707 ? iterator(p,j,lb-j)
708 : end();
709 }
710
713
716 {
717 return const_iterator(p,j,0);
718 }
719
722 {
723 return const_iterator(p,j,n);
724 }
725
729 {
730 return const_iterator(p,j,n-1);
731 }
732
736 {
737 return const_iterator(p,j,-1);
738 }
739
742 {
743 const size_type* lb = std::lower_bound(j, j+n, i);
744 return (lb != j+n && *lb == i)
745 ? const_iterator(p,j,lb-j)
746 : end();
747 }
748
749 //===== sizes
750
753 {
754 return n;
755 }
756
757 protected:
760 : n(0), p(0), j(0)
761 {}
762
763 size_type n; // number of elements in array
764 B *p; // pointer to dynamically allocated built-in array
765 size_type* j; // the index set
766 };
767
768} // end namespace
769
770#endif
Facade class for stl conformant bidirectional iterators.
Definition: iteratorfacades.hh:272
derive error class from the base class in common
Definition: istlexception.hh:16
Base class for stl conformant forward iterators.
Definition: iteratorfacades.hh:430
Iterator implementation class
Definition: basearray.hh:80
bool equals(const RealIterator< const ValueType > &other) const
equality
Definition: basearray.hh:116
size_type index() const
return index
Definition: basearray.hh:103
RealIterator()
constructor
Definition: basearray.hh:91
remove_const< T >::type ValueType
The unqualified value type.
Definition: basearray.hh:83
bool equals(const RealIterator< ValueType > &other) const
equality
Definition: basearray.hh:109
A simple array container for objects of type B.
Definition: basearray.hh:41
iterator begin()
begin iterator
Definition: basearray.hh:160
RealIterator< B > iterator
iterator type for sequential access
Definition: basearray.hh:156
const_iterator find(size_type i) const
random access returning iterator (end if not contained)
Definition: basearray.hh:224
const_iterator begin() const
begin const_iterator
Definition: basearray.hh:198
iterator beforeBegin()
Definition: basearray.hh:180
A allocator_type
export the allocator type
Definition: basearray.hh:50
B & operator[](size_type i)
random access to blocks
Definition: basearray.hh:59
RealIterator< const B > const_iterator
iterator class for sequential access
Definition: basearray.hh:195
B member_type
export the type representing the components
Definition: basearray.hh:47
const_iterator beforeEnd() const
Definition: basearray.hh:211
base_array_unmanaged()
makes empty array
Definition: basearray.hh:243
size_type size() const
number of blocks in the array (are of size 1 here)
Definition: basearray.hh:236
const_iterator beforeBegin() const
Definition: basearray.hh:218
base_array_unmanaged(size_type n_, B *p_)
make an initialized array
Definition: basearray.hh:247
iterator find(size_type i)
random access returning iterator (end if not contained)
Definition: basearray.hh:186
const_iterator end() const
end const_iterator
Definition: basearray.hh:204
A::size_type size_type
the type for the index access
Definition: basearray.hh:53
iterator end()
end iterator
Definition: basearray.hh:166
iterator beforeEnd()
Definition: basearray.hh:173
Extend base_array_unmanaged by functions to manipulate.
Definition: basearray.hh:272
void advance(difference_type newsize)
advance pointer by newsize elements and then set size to new size
Definition: basearray.hh:317
base_array_unmanaged< B, A >::iterator iterator
make iterators available as types
Definition: basearray.hh:284
base_array_unmanaged< B, A >::size_type size_type
The type used for the index access.
Definition: basearray.hh:290
void move(difference_type offset, size_type newsize)
increment pointer by offset and set size
Definition: basearray.hh:324
A::difference_type difference_type
The type used for the difference between two iterator positions.
Definition: basearray.hh:293
B member_type
export the type representing the components
Definition: basearray.hh:278
B * getptr()
return the pointer
Definition: basearray.hh:337
base_array_window()
makes empty array
Definition: basearray.hh:298
base_array_unmanaged< B, A >::const_iterator const_iterator
make iterators available as types
Definition: basearray.hh:287
void move(difference_type offset)
increment pointer by offset, leave size
Definition: basearray.hh:331
base_array_window(B *_p, size_type _n)
make array from given pointer and size
Definition: basearray.hh:303
A allocator_type
export the allocator type
Definition: basearray.hh:281
void set(size_type _n, B *_p)
set pointer and length
Definition: basearray.hh:310
This container extends base_array_unmanaged by memory management with the usual copy semantics provid...
Definition: basearray.hh:362
base_array_unmanaged< B, A >::const_iterator const_iterator
make iterators available as types
Definition: basearray.hh:377
base_array & operator=(const base_array &a)
assignment
Definition: basearray.hh:480
base_array(const base_array &a)
copy constructor
Definition: basearray.hh:407
B member_type
export the type representing the components
Definition: basearray.hh:368
base_array_unmanaged< B, A >::iterator iterator
make iterators available as types
Definition: basearray.hh:374
void resize(size_type _n)
reallocate array to given size, any data is lost
Definition: basearray.hh:458
A allocator_type
export the allocator type
Definition: basearray.hh:371
~base_array()
free dynamic memory
Definition: basearray.hh:447
base_array_unmanaged< B, A >::size_type size_type
The type used for the index access.
Definition: basearray.hh:380
base_array()
makes empty array
Definition: basearray.hh:388
base_array(size_type _n)
make array with _n components
Definition: basearray.hh:393
base_array(const base_array_unmanaged< B, A > &_a)
construct from base class object
Definition: basearray.hh:426
A::difference_type difference_type
The type used for the difference between two iterator positions.
Definition: basearray.hh:383
iterator class for sequential access
Definition: basearray.hh:582
RealIterator(const RealIterator< ValueType > &it)
Copy constructor from mutable iterator.
Definition: basearray.hh:605
bool equals(const RealIterator< ValueType > &it) const
equality
Definition: basearray.hh:611
RealIterator()
constructor
Definition: basearray.hh:593
remove_const< T >::type ValueType
The unqualified value type.
Definition: basearray.hh:585
RealIterator(B *_p, size_type *_j, size_type _i)
constructor
Definition: basearray.hh:598
bool equals(const RealIterator< const ValueType > &it) const
equality
Definition: basearray.hh:618
size_type offset() const
offset from the first entry.
Definition: basearray.hh:644
void setindex(size_type k)
Set index corresponding to pointer.
Definition: basearray.hh:632
size_type index() const
return index corresponding to pointer
Definition: basearray.hh:626
A simple array container with non-consecutive index set.
Definition: basearray.hh:544
const_iterator beforeBegin() const
Definition: basearray.hh:735
const_iterator end() const
end const_iterator
Definition: basearray.hh:721
RealIterator< const B > const_iterator
const_iterator class for sequential access
Definition: basearray.hh:712
RealIterator< B > iterator
The iterator type.
Definition: basearray.hh:674
A::size_type size_type
The type used for the index access.
Definition: basearray.hh:556
iterator find(size_type i)
random access returning iterator (end if not contained)
Definition: basearray.hh:703
iterator beforeBegin()
Definition: basearray.hh:697
iterator beforeEnd()
Definition: basearray.hh:690
B member_type
export the type representing the components
Definition: basearray.hh:550
compressed_base_array_unmanaged()
makes empty array
Definition: basearray.hh:759
B & operator[](size_type i)
random access to blocks, assumes ascending ordering
Definition: basearray.hh:561
size_type size() const
number of blocks in the array (are of size 1 here)
Definition: basearray.hh:752
const_iterator find(size_type i) const
random access returning iterator (end if not contained)
Definition: basearray.hh:741
A allocator_type
export the allocator type
Definition: basearray.hh:553
iterator begin()
begin iterator
Definition: basearray.hh:677
const_iterator beforeEnd() const
Definition: basearray.hh:728
const_iterator begin() const
begin const_iterator
Definition: basearray.hh:715
iterator end()
end iterator
Definition: basearray.hh:683
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
This file implements iterator facade classes for writing stl conformant iterators.
Dune namespace.
Definition: alignment.hh:10
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)