/usr/include/dune/istl/vbvector.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 | // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 6 -*-
// vi: set et ts=8 sw=6 sts=6:
#ifndef DUNE_VBVECTOR_HH
#define DUNE_VBVECTOR_HH
#include<cmath>
#include<complex>
#include<iostream>
#include<memory>
#include "istlexception.hh"
#include "bvector.hh"
/** \file
* \brief ???
*/
namespace Dune {
/**
@addtogroup ISTL_SPMV
@{
*/
/**
\brief A Vector of blocks with different blocksizes.
implements a vector consisting of a number of blocks (to
be given at run-time) which themselves consist of a number
of blocks (also given at run-time) of the given type B.
VariableBlockVector is a container of containers!
*/
template<class B, class A=std::allocator<B> >
class VariableBlockVector : public block_vector_unmanaged<B,A>
// this derivation gives us all the blas level 1 and norms
// on the large array. However, access operators have to be
// overwritten.
{
public:
//===== type definitions and constants
//! export the type representing the field
typedef typename B::field_type field_type;
//! export the allocator type
typedef A allocator_type;
//! The size type for the index access
typedef typename A::size_type size_type;
/** export the type representing the components, note that this
is *not* the type refered to by the iterators and random access.
However, it can be used to copy blocks (which is its only purpose).
*/
typedef BlockVector<B,A> block_type;
/** increment block level counter, yes, it is two levels because
VariableBlockVector is a container of containers
*/
enum {
//! The number of blocklevels this vector contains.
blocklevel = B::blocklevel+2};
// just a shorthand
typedef BlockVectorWindow<B,A> window_type;
//===== constructors and such
/** constructor without arguments makes empty vector,
object cannot be used yet
*/
VariableBlockVector () : block_vector_unmanaged<B,A>()
{
// nothing is known ...
nblocks = 0;
block = 0;
initialized = false;
}
/** make vector with given number of blocks, but size of each block is not yet known,
object cannot be used yet
*/
explicit VariableBlockVector (size_type _nblocks) : block_vector_unmanaged<B,A>()
{
// we can allocate the windows now
nblocks = _nblocks;
if (nblocks>0)
{
block = windowAllocator_.allocate(nblocks);
new (block) window_type[nblocks];
}
else
{
nblocks = 0;
block = 0;;
}
// Note: memory in base class still not allocated
// the vector not usable
initialized = false;
}
/** make vector with given number of blocks each having a constant size,
object is fully usable then.
\param _nblocks Number of blocks
\param m Number of elements in each block
*/
VariableBlockVector (size_type _nblocks, size_type m) : block_vector_unmanaged<B,A>()
{
// and we can allocate the big array in the base class
this->n = _nblocks*m;
if (this->n>0)
{
this->p = allocator_.allocate(this->n);
new (this->p) B[this->n];
}
else
{
this->n = 0;
this->p = 0;
}
// we can allocate the windows now
nblocks = _nblocks;
if (nblocks>0)
{
// allocate and construct the windows
block = windowAllocator_.allocate(nblocks);
new (block) window_type[nblocks];
// set the windows into the big array
for (size_type i=0; i<nblocks; ++i)
block[i].set(m,this->p+(i*m));
}
else
{
nblocks = 0;
block = 0;;
}
// and the vector is usable
initialized = true;
}
//! copy constructor, has copy semantics
VariableBlockVector (const VariableBlockVector& a)
{
// allocate the big array in the base class
this->n = a.n;
if (this->n>0)
{
// allocate and construct objects
this->p = allocator_.allocate(this->n);
new (this->p) B[this->n];
// copy data
for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
}
else
{
this->n = 0;
this->p = 0;
}
// we can allocate the windows now
nblocks = a.nblocks;
if (nblocks>0)
{
// alloc
block = windowAllocator_.allocate(nblocks);
new (block) window_type[nblocks];
// and we must set the windows
block[0].set(a.block[0].getsize(),this->p); // first block
for (size_type i=1; i<nblocks; ++i) // and the rest
block[i].set(a.block[i].getsize(),block[i-1].getptr()+block[i-1].getsize());
}
else
{
nblocks = 0;
block = 0;;
}
// and we have a usable vector
initialized = true;
}
//! free dynamic memory
~VariableBlockVector ()
{
if (this->n>0) {
size_type i=this->n;
while (i)
this->p[--i].~B();
allocator_.deallocate(this->p,this->n);
}
if (nblocks>0) {
size_type i=nblocks;
while (i)
block[--i].~window_type();
windowAllocator_.deallocate(block,nblocks);
}
}
//! same effect as constructor with same argument
void resize (size_type _nblocks)
{
// deconstruct objects and deallocate memory if necessary
if (this->n>0) {
size_type i=this->n;
while (i)
this->p[--i].~B();
allocator_.deallocate(this->p,this->n);
}
if (nblocks>0) {
size_type i=nblocks;
while (i)
block[--i].~window_type();
windowAllocator_.deallocate(block,nblocks);
}
this->n = 0;
this->p = 0;
// we can allocate the windows now
nblocks = _nblocks;
if (nblocks>0)
{
block = windowAllocator_.allocate(nblocks);
new (block) window_type[nblocks];
}
else
{
nblocks = 0;
block = 0;;
}
// and the vector not fully usable
initialized = false;
}
//! same effect as constructor with same argument
void resize (size_type _nblocks, size_type m)
{
// deconstruct objects and deallocate memory if necessary
if (this->n>0) {
size_type i=this->n;
while (i)
this->p[--i].~B();
allocator_.deallocate(this->p,this->n);
}
if (nblocks>0) {
size_type i=nblocks;
while (i)
block[--i].~window_type();
windowAllocator_.deallocate(block,nblocks);
}
// and we can allocate the big array in the base class
this->n = _nblocks*m;
if (this->n>0)
{
this->p = allocator_.allocate(this->n);
new (this->p) B[this->n];
}
else
{
this->n = 0;
this->p = 0;
}
// we can allocate the windows now
nblocks = _nblocks;
if (nblocks>0)
{
// allocate and construct objects
block = windowAllocator_.allocate(nblocks);
new (block) window_type[nblocks];
// set the windows into the big array
for (size_type i=0; i<nblocks; ++i)
block[i].set(m,this->p+(i*m));
}
else
{
nblocks = 0;
block = 0;;
}
// and the vector is usable
initialized = true;
}
//! assignment
VariableBlockVector& operator= (const VariableBlockVector& a)
{
if (&a!=this) // check if this and a are different objects
{
// reallocate arrays if necessary
// Note: still the block sizes may vary !
if (this->n!=a.n || nblocks!=a.nblocks)
{
// deconstruct objects and deallocate memory if necessary
if (this->n>0) {
size_type i=this->n;
while (i)
this->p[--i].~B();
allocator_.deallocate(this->p,this->n);
}
if (nblocks>0) {
size_type i=nblocks;
while (i)
block[--i].~window_type();
windowAllocator_.deallocate(block,nblocks);
}
// allocate the big array in the base class
this->n = a.n;
if (this->n>0)
{
// allocate and construct objects
this->p = allocator_.allocate(this->n);
new (this->p) B[this->n];
}
else
{
this->n = 0;
this->p = 0;
}
// we can allocate the windows now
nblocks = a.nblocks;
if (nblocks>0)
{
// alloc
block = windowAllocator_.allocate(nblocks);
new (block) window_type[nblocks];
}
else
{
nblocks = 0;
block = 0;;
}
}
// copy block structure, might be different although
// sizes are the same !
if (nblocks>0)
{
block[0].set(a.block[0].getsize(),this->p); // first block
for (size_type i=1; i<nblocks; ++i) // and the rest
block[i].set(a.block[i].getsize(),block[i-1].getptr()+block[i-1].getsize());
}
// and copy the data
for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
}
// and we have a usable vector
initialized = true;
return *this; // Gebe Referenz zurueck damit a=b=c; klappt
}
//===== assignment from scalar
//! assign from scalar
VariableBlockVector& operator= (const field_type& k)
{
(static_cast<block_vector_unmanaged<B,A>&>(*this)) = k;
return *this;
}
//===== the creation interface
//! Iterator class for sequential creation of blocks
class CreateIterator
{
public:
//! constructor
CreateIterator (VariableBlockVector& _v, int _i) : v(_v)
{
i = _i;
k = 0;
n = 0;
}
//! prefix increment
CreateIterator& operator++()
{
// we are at block i and the blocks size is known
// set the blocks size to current k
v.block[i].setsize(k);
// accumulate total size
n += k;
// go to next block
++i;
// reset block size
k = 0;
// if we are past the last block, finish off
if (i==v.nblocks)
{
// now we can allocate the big array in the base class of v
v.n = n;
if (n>0)
{
// allocate and construct objects
v.p = v.allocator_.allocate(n);
new (v.p) B[n];
}
else
{
v.n = 0;
v.p = 0;
}
// and we set the window pointer
if (v.nblocks>0)
{
v.block[0].setptr(v.p); // pointer tofirst block
for (size_type j=1; j<v.nblocks; ++j) // and the rest
v.block[j].setptr(v.block[j-1].getptr()+v.block[j-1].getsize());
}
// and the vector is ready
v.initialized = true;
//std::cout << "made vbvector with " << v.n << " components" << std::endl;
}
return *this;
}
//! inequality
bool operator!= (const CreateIterator& it) const
{
return (i!=it.i) || (&v!=&it.v);
}
//! equality
bool operator== (const CreateIterator& it) const
{
return (i==it.i) && (&v==&it.v);
}
//! dereferencing
size_type index () const
{
return i;
}
//! set size of current block
void setblocksize (size_type _k)
{
k = _k;
}
private:
VariableBlockVector& v; // my vector
size_type i; // current block to be defined
size_type k; // size of current block to be defined
size_type n; // total number of elements to be allocated
};
// CreateIterator wants to set all the arrays ...
friend class CreateIterator;
//! get initial create iterator
CreateIterator createbegin ()
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (initialized) DUNE_THROW(ISTLError,"no CreateIterator in initialized state");
#endif
return CreateIterator(*this,0);
}
//! get create iterator pointing to one after the last block
CreateIterator createend ()
{
return CreateIterator(*this,nblocks);
}
//===== access to components
// has to be overwritten from base class because it must
// return access to the windows
//! random access to blocks
window_type& operator[] (size_type i)
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (i>=nblocks) DUNE_THROW(ISTLError,"index out of range");
#endif
return block[i];
}
//! same for read only access
const window_type& operator[] (size_type i) const
{
#ifdef DUNE_ISTL_WITH_CHECKING
if (i<0 || i>=nblocks) DUNE_THROW(ISTLError,"index out of range");
#endif
return block[i];
}
// forward declaration
class ConstIterator;
//! Iterator class for sequential access
class Iterator
{
public:
//! constructor, no arguments
Iterator ()
{
p = 0;
i = 0;
}
//! constructor
Iterator (window_type* _p, size_type _i) : p(_p), i(_i)
{ }
//! prefix increment
Iterator& operator++()
{
++i;
return *this;
}
//! prefix decrement
Iterator& operator--()
{
--i;
return *this;
}
//! equality
bool operator== (const Iterator& it) const
{
return (p+i)==(it.p+it.i);
}
//! inequality
bool operator!= (const Iterator& it) const
{
return (p+i)!=(it.p+it.i);
}
//! equality
bool operator== (const ConstIterator& it) const
{
return (p+i)==(it.p+it.i);
}
//! inequality
bool operator!= (const ConstIterator& it) const
{
return (p+i)!=(it.p+it.i);
}
//! dereferencing
window_type& operator* () const
{
return p[i];
}
//! arrow
window_type* operator-> () const
{
return p+i;
}
// return index corresponding to pointer
size_type index () const
{
return i;
}
friend class ConstIterator;
private:
window_type* p;
size_type i;
};
//! begin Iterator
Iterator begin ()
{
return Iterator(block,0);
}
//! end Iterator
Iterator end ()
{
return Iterator(block,nblocks);
}
//! @returns an iterator that is positioned before
//! the end iterator of the vector, i.e. at the last entry.
Iterator beforeEnd ()
{
return Iterator(block,nblocks-1);
}
//! @returns an iterator that is positioned before
//! the first entry of the vector.
Iterator beforeBegin () const
{
return Iterator(block,-1);
}
//! random access returning iterator (end if not contained)
Iterator find (size_type i)
{
if (i>=0 && i<nblocks)
return Iterator(block,i);
else
return Iterator(block,nblocks);
}
//! random access returning iterator (end if not contained)
ConstIterator find (size_type i) const
{
if (i>=0 && i<nblocks)
return ConstIterator(block,i);
else
return ConstIterator(block,nblocks);
}
//! ConstIterator class for sequential access
class ConstIterator
{
public:
//! constructor
ConstIterator ()
{
p = 0;
i = 0;
}
//! constructor from pointer
ConstIterator (const window_type* _p, size_type _i) : p(_p), i(_i)
{ }
//! constructor from non_const iterator
ConstIterator (const Iterator& it) : p(it.p), i(it.i)
{ }
//! prefix increment
ConstIterator& operator++()
{
++i;
return *this;
}
//! prefix decrement
ConstIterator& operator--()
{
--i;
return *this;
}
//! equality
bool operator== (const ConstIterator& it) const
{
return (p+i)==(it.p+it.i);
}
//! inequality
bool operator!= (const ConstIterator& it) const
{
return (p+i)!=(it.p+it.i);
}
//! equality
bool operator== (const Iterator& it) const
{
return (p+i)==(it.p+it.i);
}
//! inequality
bool operator!= (const Iterator& it) const
{
return (p+i)!=(it.p+it.i);
}
//! dereferencing
const window_type& operator* () const
{
return p[i];
}
//! arrow
const window_type* operator-> () const
{
return p+i;
}
// return index corresponding to pointer
size_type index () const
{
return i;
}
friend class Iterator;
private:
const window_type* p;
size_type i;
};
//! begin ConstIterator
ConstIterator begin () const
{
return ConstIterator(block,0);
}
//! end ConstIterator
ConstIterator end () const
{
return ConstIterator(block,nblocks);
}
//! @returns an iterator that is positioned before
//! the end iterator of the vector. i.e. at the last element.
ConstIterator beforeEnd() const
{
return ConstIterator(block,nblocks-1);
}
//! end ConstIterator
ConstIterator rend () const
{
return ConstIterator(block,-1);
}
//===== sizes
//! number of blocks in the vector (are of variable size here)
size_type N () const
{
return nblocks;
}
private:
size_type nblocks; // number of blocks in vector
window_type* block; // array of blocks pointing to the array in the base class
bool initialized; // true if vector has been initialized
A allocator_;
typename A::template rebind<window_type>::other windowAllocator_;
};
/** @} end documentation */
} // end namespace
#endif
|