PackedListI.H
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "error.H"
00029 #include <climits>
00030
00031
00032
00033 template<unsigned nBits>
00034 inline unsigned int Foam::PackedList<nBits>::max_bits()
00035 {
00036 return sizeof(StorageType)*CHAR_BIT - 1;
00037 }
00038
00039
00040 template<unsigned nBits>
00041 inline unsigned int Foam::PackedList<nBits>::max_value()
00042 {
00043 return (1u << nBits) - 1;
00044 }
00045
00046
00047 template<unsigned nBits>
00048 inline unsigned int Foam::PackedList<nBits>::packing()
00049 {
00050 return sizeof(StorageType)*CHAR_BIT / nBits;
00051 }
00052
00053
00054 template<unsigned nBits>
00055 inline unsigned int Foam::PackedList<nBits>::maskLower(unsigned offset)
00056 {
00057
00058
00059
00060 return (~0u >> ( sizeof(StorageType)*CHAR_BIT - nBits * offset));
00061 }
00062
00063
00064 template<unsigned nBits>
00065 inline Foam::label Foam::PackedList<nBits>::packedLength(const label nElem)
00066 {
00067 return (nElem + packing() - 1) / packing();
00068 }
00069
00070
00071
00072
00073 template<unsigned nBits>
00074 inline Foam::PackedList<nBits>::PackedList()
00075 :
00076 StorageList(),
00077 size_(0)
00078 {}
00079
00080
00081 template<unsigned nBits>
00082 inline Foam::PackedList<nBits>::PackedList(const label size)
00083 :
00084 StorageList(packedLength(size), 0u),
00085 size_(size)
00086 {}
00087
00088
00089 template<unsigned nBits>
00090 inline Foam::PackedList<nBits>::PackedList(const PackedList<nBits>& lst)
00091 :
00092 StorageList(lst),
00093 size_(lst.size_)
00094 {}
00095
00096
00097 template<unsigned nBits>
00098 inline Foam::PackedList<nBits>::PackedList(const Xfer<PackedList<nBits> >& lst)
00099 {
00100 transfer(lst());
00101 }
00102
00103
00104 template<unsigned nBits>
00105 inline Foam::autoPtr<Foam::PackedList<nBits> >
00106 Foam::PackedList<nBits>::clone() const
00107 {
00108 return autoPtr<PackedList<nBits> >(new PackedList<nBits>(*this));
00109 }
00110
00111
00112
00113
00114
00115
00116 template<unsigned nBits>
00117 inline Foam::PackedList<nBits>::iteratorBase::iteratorBase()
00118 :
00119 list_(0),
00120 index_(0)
00121 {}
00122
00123
00124 template<unsigned nBits>
00125 inline Foam::PackedList<nBits>::iteratorBase::iteratorBase
00126 (
00127 const PackedList<nBits>* lst,
00128 const label i
00129 )
00130 :
00131 list_(const_cast<PackedList<nBits>*>(lst)),
00132 index_(i)
00133 {}
00134
00135
00136 template<unsigned nBits>
00137 inline unsigned int
00138 Foam::PackedList<nBits>::iteratorBase::get() const
00139 {
00140 const unsigned int seg = index_ / packing();
00141 const unsigned int off = index_ % packing();
00142
00143 const unsigned int& stored = list_->StorageList::operator[](seg);
00144 return (stored >> (nBits * off)) & max_value();
00145 }
00146
00147
00148 template<unsigned nBits>
00149 inline bool
00150 Foam::PackedList<nBits>::iteratorBase::set(const unsigned int val)
00151 {
00152 const unsigned int seg = index_ / packing();
00153 const unsigned int off = index_ % packing();
00154
00155 unsigned int& stored = list_->StorageList::operator[](seg);
00156 const unsigned int prev = stored;
00157
00158 const unsigned int startBit = nBits * off;
00159 const unsigned int maskNew = max_value() << startBit;
00160
00161 if (val & ~max_value())
00162 {
00163
00164 stored |= maskNew;
00165 }
00166 else
00167 {
00168 stored &= ~maskNew;
00169 stored |= maskNew & (val << startBit);
00170 }
00171
00172 return prev != stored;
00173 }
00174
00175
00176 template<unsigned nBits>
00177 inline bool Foam::PackedList<nBits>::iteratorBase::operator==
00178 (
00179 const iteratorBase& iter
00180 ) const
00181 {
00182 return this->get() == iter.get();
00183 }
00184
00185
00186 template<unsigned nBits>
00187 inline bool Foam::PackedList<nBits>::iteratorBase::operator!=
00188 (
00189 const iteratorBase& iter
00190 ) const
00191 {
00192 return this->get() != iter.get();
00193 }
00194
00195
00196 template<unsigned nBits>
00197 inline unsigned int
00198 Foam::PackedList<nBits>::iteratorBase::operator=(const iteratorBase& iter)
00199 {
00200 const unsigned int val = iter.get();
00201 this->set(val);
00202 return val;
00203 }
00204
00205
00206 template<unsigned nBits>
00207 inline unsigned int
00208 Foam::PackedList<nBits>::iteratorBase::operator=(const unsigned int val)
00209 {
00210
00211 if (index_ >= list_->size_)
00212 {
00213 list_->resize(index_ + 1);
00214 }
00215
00216 this->set(val);
00217 return val;
00218 }
00219
00220
00221 template<unsigned nBits>
00222 inline Foam::PackedList<nBits>::iteratorBase::operator
00223 unsigned int () const
00224 {
00225
00226 if (index_ >= list_->size_)
00227 {
00228 return 0;
00229 }
00230
00231 return this->get();
00232 }
00233
00234
00235
00236
00237 template<unsigned nBits>
00238 inline Foam::PackedList<nBits>::iterator::iterator()
00239 :
00240 iteratorBase()
00241 {}
00242
00243
00244 template<unsigned nBits>
00245 inline Foam::PackedList<nBits>::const_iterator::const_iterator()
00246 :
00247 iteratorBase()
00248 {}
00249
00250
00251 template<unsigned nBits>
00252 inline Foam::PackedList<nBits>::iterator::iterator
00253 (
00254 const iteratorBase& iter
00255 )
00256 :
00257 iteratorBase(iter)
00258 {
00259
00260
00261 if (this->index_ > this->list_->size_)
00262 {
00263 this->index_ = this->list_->size_;
00264 }
00265 }
00266
00267
00268 template<unsigned nBits>
00269 inline Foam::PackedList<nBits>::const_iterator::const_iterator
00270 (
00271 const iteratorBase& iter
00272 )
00273 :
00274 iteratorBase(iter)
00275 {
00276
00277
00278 if (this->index_ > this->list_->size_)
00279 {
00280 this->index_ = this->list_->size_;
00281 }
00282 }
00283
00284
00285 template<unsigned nBits>
00286 inline Foam::PackedList<nBits>::iterator::iterator
00287 (
00288 const PackedList<nBits>* lst,
00289 const label i
00290 )
00291 :
00292 iteratorBase(lst, i)
00293 {}
00294
00295
00296 template<unsigned nBits>
00297 inline Foam::PackedList<nBits>::const_iterator::const_iterator
00298 (
00299 const PackedList<nBits>* lst,
00300 const label i
00301 )
00302 :
00303 iteratorBase(lst, i)
00304 {}
00305
00306
00307 template<unsigned nBits>
00308 inline Foam::PackedList<nBits>::const_iterator::const_iterator
00309 (
00310 const iterator& iter
00311 )
00312 :
00313 iteratorBase(static_cast<const iteratorBase&>(iter))
00314 {}
00315
00316
00317 template<unsigned nBits>
00318 inline bool Foam::PackedList<nBits>::iterator::operator==
00319 (
00320 const iteratorBase& iter
00321 ) const
00322 {
00323 return this->index_ == iter.index_;
00324 }
00325
00326
00327 template<unsigned nBits>
00328 inline bool Foam::PackedList<nBits>::iterator::operator!=
00329 (
00330 const iteratorBase& iter
00331 ) const
00332 {
00333 return this->index_ != iter.index_;
00334 }
00335
00336
00337
00338 template<unsigned nBits>
00339 inline bool Foam::PackedList<nBits>::const_iterator::operator==
00340 (
00341 const iteratorBase& iter
00342 ) const
00343 {
00344 return this->index_ == iter.index_;
00345 }
00346
00347
00348 template<unsigned nBits>
00349 inline bool Foam::PackedList<nBits>::const_iterator::operator!=
00350 (
00351 const iteratorBase& iter
00352 ) const
00353 {
00354 return this->index_ != iter.index_;
00355 }
00356
00357
00358
00359 template<unsigned nBits>
00360 inline typename Foam::PackedList<nBits>::iterator&
00361 Foam::PackedList<nBits>::iterator::operator=(const iteratorBase& iter)
00362 {
00363 this->list_ = iter.list_;
00364 this->index_ = iter.index_;
00365
00366
00367
00368 if (this->index_ > this->list_->size_)
00369 {
00370 this->index_ = this->list_->size_;
00371 }
00372
00373 return *this;
00374 }
00375
00376
00377 template<unsigned nBits>
00378 inline typename Foam::PackedList<nBits>::const_iterator&
00379 Foam::PackedList<nBits>::const_iterator::operator=(const iteratorBase& iter)
00380 {
00381 this->list_ = iter.list_;
00382 this->index_ = iter.index_;
00383
00384
00385
00386 if (this->index_ > this->list_->size_)
00387 {
00388 this->index_ = this->list_->size_;
00389 }
00390
00391 return *this;
00392 }
00393
00394
00395 template<unsigned nBits>
00396 inline typename Foam::PackedList<nBits>::iterator&
00397 Foam::PackedList<nBits>::iterator::operator++()
00398 {
00399 ++this->index_;
00400 return *this;
00401 }
00402
00403
00404 template<unsigned nBits>
00405 inline typename Foam::PackedList<nBits>::const_iterator&
00406 Foam::PackedList<nBits>::const_iterator::operator++()
00407 {
00408 ++this->index_;
00409 return *this;
00410 }
00411
00412
00413 template<unsigned nBits>
00414 inline typename Foam::PackedList<nBits>::iterator
00415 Foam::PackedList<nBits>::iterator::operator++(int)
00416 {
00417 iterator old = *this;
00418 ++this->index_;
00419 return old;
00420 }
00421
00422
00423 template<unsigned nBits>
00424 inline typename Foam::PackedList<nBits>::const_iterator
00425 Foam::PackedList<nBits>::const_iterator::operator++(int)
00426 {
00427 const_iterator old = *this;
00428 ++this->index_;
00429 return old;
00430 }
00431
00432
00433 template<unsigned nBits>
00434 inline typename Foam::PackedList<nBits>::iterator&
00435 Foam::PackedList<nBits>::iterator::operator--()
00436 {
00437 --this->index_;
00438 return *this;
00439 }
00440
00441
00442 template<unsigned nBits>
00443 inline typename Foam::PackedList<nBits>::const_iterator&
00444 Foam::PackedList<nBits>::const_iterator::operator--()
00445 {
00446 --this->index_;
00447 return *this;
00448 }
00449
00450
00451 template<unsigned nBits>
00452 inline typename Foam::PackedList<nBits>::iterator
00453 Foam::PackedList<nBits>::iterator::operator--(int)
00454 {
00455 iterator old = *this;
00456 --this->index_;
00457 return old;
00458 }
00459
00460
00461 template<unsigned nBits>
00462 inline typename Foam::PackedList<nBits>::const_iterator
00463 Foam::PackedList<nBits>::const_iterator::operator--(int)
00464 {
00465 const_iterator old = *this;
00466 --this->index_;
00467 return old;
00468 }
00469
00470
00471 template<unsigned nBits>
00472 inline typename Foam::PackedList<nBits>::iteratorBase&
00473 Foam::PackedList<nBits>::iterator::operator*()
00474 {
00475 return static_cast<iteratorBase&>(*this);
00476 }
00477
00478
00479 template<unsigned nBits>
00480 inline typename Foam::PackedList<nBits>::iteratorBase&
00481 Foam::PackedList<nBits>::iterator::operator()()
00482 {
00483 return static_cast<iteratorBase&>(*this);
00484 }
00485
00486
00487 template<unsigned nBits>
00488 inline unsigned int
00489 Foam::PackedList<nBits>::const_iterator::operator*() const
00490 {
00491 return this->get();
00492 }
00493
00494
00495 template<unsigned nBits>
00496 inline unsigned int
00497 Foam::PackedList<nBits>::const_iterator::operator()() const
00498 {
00499 return this->get();
00500 }
00501
00502
00503 template<unsigned nBits>
00504 inline typename Foam::PackedList<nBits>::iterator
00505 Foam::PackedList<nBits>::begin()
00506 {
00507 return iterator(this, 0);
00508 }
00509
00510
00511 template<unsigned nBits>
00512 inline typename Foam::PackedList<nBits>::const_iterator
00513 Foam::PackedList<nBits>::begin() const
00514 {
00515 return const_iterator(this, 0);
00516 }
00517
00518
00519 template<unsigned nBits>
00520 inline typename Foam::PackedList<nBits>::const_iterator
00521 Foam::PackedList<nBits>::cbegin() const
00522 {
00523 return const_iterator(this, 0);
00524 }
00525
00526
00527 template<unsigned nBits>
00528 inline typename Foam::PackedList<nBits>::iterator
00529 Foam::PackedList<nBits>::end()
00530 {
00531 return iterator(this, size_);
00532 }
00533
00534
00535 template<unsigned nBits>
00536 inline typename Foam::PackedList<nBits>::const_iterator
00537 Foam::PackedList<nBits>::end() const
00538 {
00539 return const_iterator(this, size_);
00540 }
00541
00542
00543 template<unsigned nBits>
00544 inline typename Foam::PackedList<nBits>::const_iterator
00545 Foam::PackedList<nBits>::cend() const
00546 {
00547 return const_iterator(this, size_);
00548 }
00549
00550
00551
00552
00553 template<unsigned nBits>
00554 inline Foam::label Foam::PackedList<nBits>::size() const
00555 {
00556 return size_;
00557 }
00558
00559
00560 template<unsigned nBits>
00561 inline bool Foam::PackedList<nBits>::empty() const
00562 {
00563 return !size_;
00564 }
00565
00566
00567 template<unsigned nBits>
00568 inline void Foam::PackedList<nBits>::resize
00569 (
00570 const label nElem,
00571 const unsigned int& val
00572 )
00573 {
00574 reserve(nElem);
00575
00576 if (nElem > size_)
00577 {
00578
00579 if (size_)
00580 {
00581
00582 unsigned int fill = val;
00583
00584 if (fill & ~max_value())
00585 {
00586
00587 fill = ~0u;
00588 }
00589 else
00590 {
00591 for (unsigned int i = 1; i < packing(); ++i)
00592 {
00593 fill |= (fill << nBits);
00594 }
00595 }
00596
00597 unsigned int seg = size_ / packing();
00598 unsigned int off = size_ % packing();
00599
00600
00601 if (off)
00602 {
00603 unsigned int maskOld = maskLower(off);
00604
00605 StorageList::operator[](seg) &= maskOld;
00606 StorageList::operator[](seg) |= ~maskOld & fill;
00607
00608
00609 seg++;
00610 }
00611
00612 unsigned int endSeg = nElem / packing();
00613
00614 while (seg < endSeg)
00615 {
00616 StorageList::operator[](seg++) = fill;
00617 }
00618 }
00619 else
00620 {
00621
00622 operator=(val);
00623 }
00624 }
00625
00626 size_ = nElem;
00627 }
00628
00629
00630 template<unsigned nBits>
00631 inline void Foam::PackedList<nBits>::setSize
00632 (
00633 const label newSize,
00634 const unsigned int& val
00635 )
00636 {
00637 resize(newSize, val);
00638 }
00639
00640
00641
00642 template<unsigned nBits>
00643 inline Foam::label Foam::PackedList<nBits>::capacity() const
00644 {
00645 return packing() * StorageList::size();
00646 }
00647
00648
00649 template<unsigned nBits>
00650 inline void Foam::PackedList<nBits>::setCapacity(const label nElem)
00651 {
00652 StorageList::setSize(packedLength(nElem), 0u);
00653
00654
00655 if (size_ > nElem)
00656 {
00657 size_ = nElem;
00658 }
00659 }
00660
00661
00662 template<unsigned nBits>
00663 inline void Foam::PackedList<nBits>::reserve
00664 (
00665 const label nElem
00666 )
00667 {
00668 label len = packedLength(nElem);
00669
00670
00671 if (len > StorageList::size())
00672 {
00673 StorageList::setSize(len, 0u);
00674 }
00675 }
00676
00677
00678 template<unsigned nBits>
00679 inline void Foam::PackedList<nBits>::clear()
00680 {
00681 size_ = 0;
00682 }
00683
00684
00685 template<unsigned nBits>
00686 inline void Foam::PackedList<nBits>::clearStorage()
00687 {
00688 StorageList::clear();
00689 size_ = 0;
00690 }
00691
00692
00693 template<unsigned nBits>
00694 inline void Foam::PackedList<nBits>::shrink()
00695 {
00696 label len = packedLength(size_);
00697
00698
00699 if (len < StorageList::size())
00700 {
00701 StorageList::setSize(len);
00702 }
00703 }
00704
00705 template<unsigned nBits>
00706 inline Foam::List<unsigned int>&
00707 Foam::PackedList<nBits>::storage()
00708 {
00709 return static_cast<StorageList&>(*this);
00710 }
00711
00712
00713 template<unsigned nBits>
00714 inline const Foam::List<unsigned int>&
00715 Foam::PackedList<nBits>::storage() const
00716 {
00717 return static_cast<const StorageList&>(*this);
00718 }
00719
00720
00721 template<unsigned nBits>
00722 inline void Foam::PackedList<nBits>::transfer(PackedList<nBits>& lst)
00723 {
00724 size_ = lst.size_;
00725 lst.size_ = 0;
00726
00727 StorageList::transfer(lst);
00728 }
00729
00730
00731 template<unsigned nBits>
00732 inline Foam::Xfer< Foam::PackedList<nBits> >
00733 Foam::PackedList<nBits>::xfer()
00734 {
00735 return xferMove(*this);
00736 }
00737
00738
00739 template<unsigned nBits>
00740 inline unsigned int Foam::PackedList<nBits>::get(const label i) const
00741 {
00742 # ifdef FULLDEBUG
00743 if (i < 0)
00744 {
00745 FatalErrorIn("PackedList<nBits>::get(const label)")
00746 << "negative index " << i << " max=" << size_-1
00747 << abort(FatalError);
00748 }
00749 # endif
00750
00751
00752 if (i < size_)
00753 {
00754 return iteratorBase(this, i).get();
00755 }
00756 else
00757 {
00758 return 0;
00759 }
00760 }
00761
00762
00763 template<unsigned nBits>
00764 inline unsigned int Foam::PackedList<nBits>::operator[](const label i) const
00765 {
00766
00767 if (i < size_)
00768 {
00769 return iteratorBase(this, i).get();
00770 }
00771 else
00772 {
00773 return 0;
00774 }
00775 }
00776
00777
00778 template<unsigned nBits>
00779 inline bool Foam::PackedList<nBits>::set
00780 (
00781 const label i,
00782 const unsigned int val
00783 )
00784 {
00785 # ifdef FULLDEBUG
00786 if (i < 0)
00787 {
00788 FatalErrorIn("PackedList<nBits>::set(const label)")
00789 << "negative index " << i << " max=" << size_-1
00790 << abort(FatalError);
00791 }
00792 # endif
00793
00794
00795 if (i >= size_)
00796 {
00797 resize(i + 1);
00798 }
00799
00800 return iteratorBase(this, i).set(val);
00801 }
00802
00803
00804 template<unsigned nBits>
00805 inline void Foam::PackedList<nBits>::append(const unsigned int val)
00806 {
00807 label elemI = size_;
00808 reserve(elemI + 1);
00809 size_++;
00810
00811 iteratorBase(this, elemI).set(val);
00812 }
00813
00814
00815 template<unsigned nBits>
00816 inline unsigned int Foam::PackedList<nBits>::remove()
00817 {
00818 if (!size_)
00819 {
00820 FatalErrorIn
00821 (
00822 "Foam::PackedList<nBits>::remove()"
00823 ) << "List is empty" << abort(FatalError);
00824 }
00825
00826 label elemI = size_ - 1;
00827 const unsigned int val = iteratorBase(this, elemI).get();
00828 resize(elemI);
00829
00830 return val;
00831 }
00832
00833
00834 template<unsigned nBits>
00835 inline typename Foam::PackedList<nBits>::iteratorBase
00836 Foam::PackedList<nBits>::operator[](const label i)
00837 {
00838 return iteratorBase(this, i);
00839 }
00840
00841
00842
00843 template<unsigned nBits>
00844 inline void Foam::PackedList<nBits>::operator=(const unsigned int val)
00845 {
00846 if (val)
00847 {
00848 unsigned int fill = val;
00849
00850 if (fill & ~max_value())
00851 {
00852
00853 fill = ~0u;
00854 }
00855 else
00856 {
00857 for (unsigned int i = 1; i < packing(); ++i)
00858 {
00859 fill |= (fill << nBits);
00860 }
00861 }
00862
00863 StorageList::operator=(fill);
00864 }
00865 else
00866 {
00867 StorageList::operator=(0u);
00868 }
00869 }
00870
00871
00872
Copyright © 2000-2009 OpenCFD Ltd