Dune Core Modules (2.3.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_BASEARRAY_HH
4#define DUNE_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 size_type l=0, r=n-1;
564 while (l<r)
565 {
566 size_type q = (l+r)/2;
567 if (i <= j[q]) r=q;
568 else l = q+1;
569 }
570 if (j[l]!=i) {
571 DUNE_THROW(ISTLError,"index "<<i<<" not in compressed array");
572 }
573 return p[l];
574 }
575
577 const B& operator[] (size_type i) const
578 {
579 size_type l=0, r=n-1;
580 while (l<r)
581 {
582 size_type q = (l+r)/2;
583 if (i <= j[q]) r=q;
584 else l = q+1;
585 }
586 if (j[l]!=i) {
587 DUNE_THROW(ISTLError,"index "<<i<<" not in compressed array");
588 }
589 return p[l];
590 }
591
593 template<class T>
595 : public BidirectionalIteratorFacade<RealIterator<T>, T>
596 {
597 public:
599 typedef typename remove_const<T>::type ValueType;
600
603 friend class RealIterator<const ValueType>;
604 friend class RealIterator<ValueType>;
605
608 : p(0), j(0), i(0)
609 {}
610
613 : p(_p), j(_j), i(_i)
614 { }
615
620 : p(it.p), j(it.j), i(it.i)
621 {}
622
623
625 bool equals (const RealIterator<ValueType>& it) const
626 {
627 assert(p==it.p);
628 return (i)==(it.i);
629 }
630
633 {
634 assert(p==it.p);
635 return (i)==(it.i);
636 }
637
638
641 {
642 return j[i];
643 }
644
647 {
648 return j[i] = k;
649 }
650
659 {
660 return i;
661 }
662
663 private:
665 void increment()
666 {
667 ++i;
668 }
669
671 void decrement()
672 {
673 --i;
674 }
675
677 B& dereference () const
678 {
679 return p[i];
680 }
681
682 B* p;
683 size_type* j;
684 size_type i;
685 };
686
689
692 {
693 return iterator(p,j,0);
694 }
695
698 {
699 return iterator(p,j,n);
700 }
701
705 {
706 return iterator(p,j,n-1);
707 }
708
712 {
713 return iterator(p,j,-1);
714 }
715
718 {
719 const size_type* lb = std::lower_bound(j, j+n, i);
720 return (lb != j+n and *lb == i)
721 ? iterator(p,j,lb-j)
722 : end();
723 }
724
727
730 {
731 return const_iterator(p,j,0);
732 }
733
736 {
737 return const_iterator(p,j,n);
738 }
739
743 {
744 return const_iterator(p,j,n-1);
745 }
746
750 {
751 return const_iterator(p,j,-1);
752 }
753
756 {
757 const size_type* lb = std::lower_bound(j, j+n, i);
758 return (lb != j+n and *lb == i)
759 ? const_iterator(p,j,lb-j)
760 : end();
761 }
762
763 //===== sizes
764
767 {
768 return n;
769 }
770
771 protected:
774 : n(0), p(0), j(0)
775 {}
776
777 size_type n; // number of elements in array
778 B *p; // pointer to dynamically allocated built-in array
779 size_type* j; // the index set
780 };
781
782} // end namespace
783
784#endif
Facade class for stl conformant bidirectional iterators.
Definition: iteratorfacades.hh:273
derive error class from the base class in common
Definition: istlexception.hh:16
Base class for stl conformant forward iterators.
Definition: iteratorfacades.hh:431
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:596
RealIterator(const RealIterator< ValueType > &it)
Copy constructor from mutable iterator.
Definition: basearray.hh:619
bool equals(const RealIterator< ValueType > &it) const
equality
Definition: basearray.hh:625
RealIterator()
constructor
Definition: basearray.hh:607
remove_const< T >::type ValueType
The unqualified value type.
Definition: basearray.hh:599
RealIterator(B *_p, size_type *_j, size_type _i)
constructor
Definition: basearray.hh:612
bool equals(const RealIterator< const ValueType > &it) const
equality
Definition: basearray.hh:632
size_type offset() const
offset from the first entry.
Definition: basearray.hh:658
void setindex(size_type k)
Set index corresponding to pointer.
Definition: basearray.hh:646
size_type index() const
return index corresponding to pointer
Definition: basearray.hh:640
A simple array container with non-consecutive index set.
Definition: basearray.hh:544
const_iterator beforeBegin() const
Definition: basearray.hh:749
const_iterator end() const
end const_iterator
Definition: basearray.hh:735
RealIterator< const B > const_iterator
const_iterator class for sequential access
Definition: basearray.hh:726
RealIterator< B > iterator
The iterator type.
Definition: basearray.hh:688
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:717
iterator beforeBegin()
Definition: basearray.hh:711
iterator beforeEnd()
Definition: basearray.hh:704
B member_type
export the type representing the components
Definition: basearray.hh:550
compressed_base_array_unmanaged()
makes empty array
Definition: basearray.hh:773
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:766
const_iterator find(size_type i) const
random access returning iterator (end if not contained)
Definition: basearray.hh:755
A allocator_type
export the allocator type
Definition: basearray.hh:553
iterator begin()
begin iterator
Definition: basearray.hh:691
const_iterator beforeEnd() const
Definition: basearray.hh:742
const_iterator begin() const
begin const_iterator
Definition: basearray.hh:729
iterator end()
end iterator
Definition: basearray.hh:697
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
This file implements iterator facade classes for writing stl conformant iterators.
Dune namespace.
Definition: alignment.hh:14
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)