/usr/include/dune/istl/basearray.hh is in libdune-istl-dev 2.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_BASEARRAY_HH
#define DUNE_BASEARRAY_HH
#include "assert.h"
#include<cmath>
#include<complex>
#include<cstddef>
#include<memory>
#include "istlexception.hh"
#include <dune/common/iteratorfacades.hh>
/** \file
\brief Implements several basic array containers.
*/
namespace Dune {
/** \brief A simple array container for objects of type B
Implement.
- iterator access
- const_iterator access
- random access
This container has *NO* memory management at all,
copy constuctor, assignment and destructor are all default.
The constructor is made protected to emphasize that objects
are only usable in derived classes.
Error checking: no error checking is provided normally.
Setting the compile time switch DUNE_ISTL_WITH_CHECKING
enables error checking.
*/
template<class B, class A=std::allocator<B> >
class base_array_unmanaged
{
public:
//===== type definitions and constants
//! export the type representing the components
typedef B member_type;
//! export the allocator type
typedef A allocator_type;
//! the type for the index access
typedef typename A::size_type size_type;
//===== access to components
//! random access to blocks
B& operator[] (size_type i)
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (i>=n) DUNE_THROW(ISTLError,"index out of range");
#endif
return p[i];
}
//! same for read only access
const B& operator[] (size_type i) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (i>=n) DUNE_THROW(ISTLError,"index out of range");
#endif
return p[i];
}
/** \brief Iterator implementation class */
template<class T>
class RealIterator
: public RandomAccessIteratorFacade<RealIterator<T>, T>
{
public:
//! \brief The unqualified value type
typedef typename remove_const<T>::type ValueType;
friend class RandomAccessIteratorFacade<RealIterator<const ValueType>, const ValueType>;
friend class RandomAccessIteratorFacade<RealIterator<ValueType>, ValueType>;
friend class RealIterator<const ValueType>;
friend class RealIterator<ValueType>;
//! constructor
RealIterator ()
: p(0), i(0)
{}
RealIterator (const B* _p, B* _i) : p(_p), i(_i)
{ }
RealIterator(const RealIterator<ValueType>& it)
: p(it.p), i(it.i)
{}
//! return index
size_type index () const
{
return i-p;
}
//! equality
bool equals (const RealIterator<ValueType>& other) const
{
assert(other.p==p);
return i==other.i;
}
//! equality
bool equals (const RealIterator<const ValueType>& other) const
{
assert(other.p==p);
return i==other.i;
}
std::ptrdiff_t distanceTo(const RealIterator& o) const
{
return o.i-i;
}
private:
//! prefix increment
void increment()
{
++i;
}
//! prefix decrement
void decrement()
{
--i;
}
//! dereferencing
B& dereference () const
{
return *i;
}
void advance(std::ptrdiff_t d)
{
i+=d;
}
const B* p;
B* i;
};
//! iterator type for sequential access
typedef RealIterator<B> iterator;
//! begin iterator
iterator begin ()
{
return iterator(p,p);
}
//! end iterator
iterator end ()
{
return iterator(p,p+n);
}
//! @returns an iterator that is positioned before
//! the end iterator of the vector, i.e. at the last entry.
iterator beforeEnd ()
{
return iterator(p,p+n-1);
}
//! @returns an iterator that is positioned before
//! the first entry of the vector.
iterator beforeBegin ()
{
return iterator(p,p-1);
}
//! random access returning iterator (end if not contained)
iterator find (size_type i)
{
if (i<n)
return iterator(p,p+i);
else
return iterator(p,p+n);
}
//! iterator class for sequential access
typedef RealIterator<const B> const_iterator;
//! begin const_iterator
const_iterator begin () const
{
return const_iterator(p,p+0);
}
//! end const_iterator
const_iterator end () const
{
return const_iterator(p,p+n);
}
//! @returns an iterator that is positioned before
//! the end iterator of the vector. i.e. at the last element.
const_iterator beforeEnd () const
{
return const_iterator(p,p+n-1);
}
//! @returns an iterator that is positioned before
//! the first entry of the vector.
const_iterator beforeBegin () const
{
return const_iterator(p,p-1);
}
//! random access returning iterator (end if not contained)
const_iterator find (size_type i) const
{
if (i<n)
return const_iterator(p,p+i);
else
return const_iterator(p,p+n);
}
//===== sizes
//! number of blocks in the array (are of size 1 here)
size_type size () const
{
return n;
}
protected:
//! makes empty array
base_array_unmanaged ()
: n(0), p(0)
{}
//! make an initialized array
base_array_unmanaged (size_type n_, B* p_)
: n(n_), p(p_)
{}
size_type n; // number of elements in array
B *p; // pointer to dynamically allocated built-in array
};
/** \brief Extend base_array_unmanaged by functions to manipulate
This container has *NO* memory management at all,
copy constuctor, assignment and destructor are all default.
A container can be constructed as empty or from a given pointer
and size. This class is used to implement a view into a larger
array.
You can copy such an object to a base_array to make a real copy.
Error checking: no error checking is provided normally.
Setting the compile time switch DUNE_ISTL_WITH_CHECKING
enables error checking.
*/
template<class B, class A=std::allocator<B> >
class base_array_window : public base_array_unmanaged<B,A>
{
public:
//===== type definitions and constants
//! export the type representing the components
typedef B member_type;
//! export the allocator type
typedef A allocator_type;
//! make iterators available as types
typedef typename base_array_unmanaged<B,A>::iterator iterator;
//! make iterators available as types
typedef typename base_array_unmanaged<B,A>::const_iterator const_iterator;
//! The type used for the index access
typedef typename base_array_unmanaged<B,A>::size_type size_type;
//! The type used for the difference between two iterator positions
typedef typename A::difference_type difference_type;
//===== constructors and such
//! makes empty array
base_array_window ()
: base_array_unmanaged<B,A>()
{ }
//! make array from given pointer and size
base_array_window (B* _p, size_type _n)
: base_array_unmanaged<B,A>(_n ,_p)
{}
//===== window manipulation methods
//! set pointer and length
void set (size_type _n, B* _p)
{
this->n = _n;
this->p = _p;
}
//! advance pointer by newsize elements and then set size to new size
void advance (difference_type newsize)
{
this->p += this->n;
this->n = newsize;
}
//! increment pointer by offset and set size
void move (difference_type offset, size_type newsize)
{
this->p += offset;
this->n = newsize;
}
//! increment pointer by offset, leave size
void move (difference_type offset)
{
this->p += offset;
}
//! return the pointer
B* getptr ()
{
return this->p;
}
};
/** \brief This container extends base_array_unmanaged by memory management
with the usual copy semantics providing the full range of
copy constructor, destructor and assignment operators.
You can make
- empty array
- array with n components dynamically allocated
- resize an array with complete loss of data
- assign/construct from a base_array_window to make a copy of the data
Error checking: no error checking is provided normally.
Setting the compile time switch DUNE_ISTL_WITH_CHECKING
enables error checking.
*/
template<class B, class A=std::allocator<B> >
class base_array : public base_array_unmanaged<B,A>
{
public:
//===== type definitions and constants
//! export the type representing the components
typedef B member_type;
//! export the allocator type
typedef A allocator_type;
//! make iterators available as types
typedef typename base_array_unmanaged<B,A>::iterator iterator;
//! make iterators available as types
typedef typename base_array_unmanaged<B,A>::const_iterator const_iterator;
//! The type used for the index access
typedef typename base_array_unmanaged<B,A>::size_type size_type;
//! The type used for the difference between two iterator positions
typedef typename A::difference_type difference_type;
//===== constructors and such
//! makes empty array
base_array ()
: base_array_unmanaged<B,A>()
{}
//! make array with _n components
base_array (size_type _n)
: base_array_unmanaged<B,A>(_n, 0)
{
if (this->n>0) {
this->p = allocator_.allocate(this->n);
new (this->p) B[this->n];
} else
{
this->n = 0;
this->p = 0;
}
}
//! copy constructor
base_array (const base_array& a)
{
// allocate memory with same size as a
this->n = a.n;
if (this->n>0) {
this->p = allocator_.allocate(this->n);
new (this->p) B[this->n];
} else
{
this->n = 0;
this->p = 0;
}
// and copy elements
for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
}
//! construct from base class object
base_array (const base_array_unmanaged<B,A>& _a)
{
const base_array& a = static_cast<const base_array&>(_a);
// allocate memory with same size as a
this->n = a.n;
if (this->n>0) {
this->p = allocator_.allocate(this->n);
new (this->p) B[this->n];
} else
{
this->n = 0;
this->p = 0;
}
// and copy elements
for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
}
//! free dynamic memory
~base_array ()
{
if (this->n>0) {
int i=this->n;
while (i)
this->p[--i].~B();
allocator_.deallocate(this->p,this->n);
}
}
//! reallocate array to given size, any data is lost
void resize (size_type _n)
{
if (this->n==_n) return;
if (this->n>0) {
int i=this->n;
while (i)
this->p[--i].~B();
allocator_.deallocate(this->p,this->n);
}
this->n = _n;
if (this->n>0) {
this->p = allocator_.allocate(this->n);
new (this->p) B[this->n];
} else
{
this->n = 0;
this->p = 0;
}
}
//! assignment
base_array& operator= (const base_array& a)
{
if (&a!=this) // check if this and a are different objects
{
// adjust size of array
if (this->n!=a.n) // check if size is different
{
if (this->n>0) {
int i=this->n;
while (i)
this->p[--i].~B();
allocator_.deallocate(this->p,this->n); // delete old memory
}
this->n = a.n;
if (this->n>0) {
this->p = allocator_.allocate(this->n);
new (this->p) B[this->n];
} else
{
this->n = 0;
this->p = 0;
}
}
// copy data
for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
}
return *this;
}
//! assign from base class object
base_array& operator= (const base_array_unmanaged<B,A>& a)
{
return this->operator=(static_cast<const base_array&>(a));
}
protected:
A allocator_;
};
/** \brief A simple array container with non-consecutive index set.
Elements in the array are of type B. This class provides
- iterator access
- const_iterator access
- random access in log(n) steps using binary search
- find returning iterator
This container has *NO* memory management at all,
copy constuctor, assignment and destructor are all default.
The constructor is made protected to emphasize that objects
are only usably in derived classes.
Error checking: no error checking is provided normally.
Setting the compile time switch DUNE_ISTL_WITH_CHECKING
enables error checking.
*/
template<class B, class A=std::allocator<B> >
class compressed_base_array_unmanaged
{
public:
//===== type definitions and constants
//! export the type representing the components
typedef B member_type;
//! export the allocator type
typedef A allocator_type;
//! The type used for the index access
typedef typename A::size_type size_type;
//===== access to components
//! random access to blocks, assumes ascending ordering
B& operator[] (size_type i)
{
size_type l=0, r=n-1;
while (l<r)
{
size_type q = (l+r)/2;
if (i <= j[q]) r=q;
else l = q+1;
}
if (j[l]!=i){
DUNE_THROW(ISTLError,"index "<<i<<" not in compressed array");
}
return p[l];
}
//! same for read only access, assumes ascending ordering
const B& operator[] (size_type i) const
{
size_type l=0, r=n-1;
while (l<r)
{
size_type q = (l+r)/2;
if (i <= j[q]) r=q;
else l = q+1;
}
if (j[l]!=i){
DUNE_THROW(ISTLError,"index "<<i<<" not in compressed array");
}
return p[l];
}
//! iterator class for sequential access
template<class T>
class RealIterator
: public BidirectionalIteratorFacade<RealIterator<T>, T>
{
public:
//! \brief The unqualified value type
typedef typename remove_const<T>::type ValueType;
friend class BidirectionalIteratorFacade<RealIterator<const ValueType>, const ValueType>;
friend class BidirectionalIteratorFacade<RealIterator<ValueType>, ValueType>;
friend class RealIterator<const ValueType>;
friend class RealIterator<ValueType>;
//! constructor
RealIterator ()
: p(0), j(0), i(0)
{}
//! constructor
RealIterator (B* _p, size_type* _j, size_type _i)
: p(_p), j(_j), i(_i)
{ }
/**
* @brief Copy constructor from mutable iterator
*/
RealIterator(const RealIterator<ValueType>& it)
: p(it.p), j(it.j), i(it.i)
{}
//! equality
bool equals (const RealIterator<ValueType>& it) const
{
assert(p==it.p);
return (i)==(it.i);
}
//! equality
bool equals (const RealIterator<const ValueType>& it) const
{
assert(p==it.p);
return (i)==(it.i);
}
//! return index corresponding to pointer
size_type index () const
{
return j[i];
}
//! Set index corresponding to pointer
void setindex (size_type k)
{
return j[i] = k;
}
/**
* @brief offset from the first entry.
*
* An iterator positioned at the beginning
* has to be increment this amount of times to
* the same position.
*/
size_type offset () const
{
return i;
}
private:
//! prefix increment
void increment()
{
++i;
}
//! prefix decrement
void decrement()
{
--i;
}
//! dereferencing
B& dereference () const
{
return p[i];
}
B* p;
size_type* j;
size_type i;
};
/** @brief The iterator type. */
typedef RealIterator<B> iterator;
//! begin iterator
iterator begin ()
{
return iterator(p,j,0);
}
//! end iterator
iterator end ()
{
return iterator(p,j,n);
}
//! @returns an iterator that is positioned before
//! the end iterator of the vector, i.e. at the last entry.
iterator beforeEnd ()
{
return iterator(p,j,n-1);
}
//! @returns an iterator that is positioned before
//! the first entry of the vector.
iterator beforeBegin ()
{
return iterator(p,j,-1);
}
//! random access returning iterator (end if not contained)
iterator find (size_type i)
{
size_type l=0, r=n-1;
while (l<r)
{
size_type q = (l+r)/2;
if (i <= j[q]) r=q;
else l = q+1;
}
if (n && i==j[l])
return iterator(p,j,l);
else
return iterator(p,j,n);
}
//! const_iterator class for sequential access
typedef RealIterator<const B> const_iterator;
//! begin const_iterator
const_iterator begin () const
{
return const_iterator(p,j,0);
}
//! end const_iterator
const_iterator end () const
{
return const_iterator(p,j,n);
}
//! @returns an iterator that is positioned before
//! the end iterator of the vector. i.e. at the last element.
const_iterator beforeEnd () const
{
return const_iterator(p,j,n-1);
}
//! @returns an iterator that is positioned before
//! the first entry of the vector.
const_iterator beforeBegin () const
{
return const_iterator(p,j,-1);
}
//! random access returning iterator (end if not contained)
const_iterator find (size_type i) const
{
size_type l=0, r=n-1;
while (l<r)
{
size_type q = (l+r)/2;
if (i <= j[q]) r=q;
else l = q+1;
}
if (n && i==j[l])
return const_iterator(p,j,l);
else
return const_iterator(p,j,n);
}
//===== sizes
//! number of blocks in the array (are of size 1 here)
size_type size () const
{
return n;
}
protected:
//! makes empty array
compressed_base_array_unmanaged ()
: n(0), p(0), j(0)
{}
size_type n; // number of elements in array
B *p; // pointer to dynamically allocated built-in array
size_type* j; // the index set
};
} // end namespace
#endif
|