Go to the documentation of this file.
42 #ifndef OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED
43 #define OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED
51 #include <tbb/atomic.h>
52 #include <tbb/spin_mutex.h>
53 #include <tbb/parallel_for.h>
54 #include <tbb/parallel_sort.h>
182 template<
typename ValueT,
size_t Log2PageSize = 10UL>
189 using PageTableT = std::deque<Page*>;
233 const size_t index = mSize.fetch_and_increment();
234 if (index >= mCapacity) this->grow(index);
235 (*mPageTable[index >> Log2PageSize])[index] = value;
248 const size_t index = mSize.fetch_and_increment();
249 if (index >= mCapacity) {
250 mPageTable.push_back(
new Page() );
251 mCapacity += Page::Size;
253 (*mPageTable[index >> Log2PageSize])[index] = value;
260 void shrink_to_fit();
272 return (*mPageTable[i>>Log2PageSize])[i];
285 return (*mPageTable[i>>Log2PageSize])[i];
295 auto op = [&](
const tbb::blocked_range<size_t>& r){
296 for(
size_t i=r.begin(); i!=r.end(); ++i) mPageTable[i]->fill(v);
298 tbb::parallel_for(tbb::blocked_range<size_t>(0, this->pageCount()), op);
310 size_t last_page = count >> Log2PageSize;
311 if (last_page >= this->pageCount())
return false;
312 auto op = [&](
const tbb::blocked_range<size_t>& r){
313 for (
size_t i=r.begin(); i!=r.end(); ++i) {
314 mPageTable[i]->copy(p+i*Page::Size, Page::Size);
317 if (
size_t m = count & Page::Mask) {
318 tbb::parallel_for(tbb::blocked_range<size_t>(0, last_page, 32), op);
319 mPageTable[last_page]->copy(p+last_page*Page::Size, m);
321 tbb::parallel_for(tbb::blocked_range<size_t>(0, last_page+1, 32), op);
344 if (size > mCapacity) {
347 this->shrink_to_fit();
373 size_t size()
const {
return mSize; }
395 return sizeof(*this) + mPageTable.size() * Page::memUsage();
414 for (
size_t i=0, n=mPageTable.size(); i<n; ++i)
delete mPageTable[i];
415 PageTableT().swap(mPageTable);
431 ConstIterator cbegin()
const {
return ConstIterator(*
this, 0); }
437 ConstIterator cend()
const {
return ConstIterator(*
this, mSize); }
447 void sort() { tbb::parallel_sort(this->begin(), this->end(), std::less<ValueT>() ); }
450 void invSort() { tbb::parallel_sort(this->begin(), this->end(), std::greater<ValueT>()); }
453 template <
typename Functor>
458 void sort(Functor func) { tbb::parallel_sort(this->begin(), this->end(), func ); }
471 void print(std::ostream& os = std::cout)
const
473 os <<
"PagedArray:\n"
474 <<
"\tSize: " << this->size() <<
" elements\n"
475 <<
"\tPage table: " << this->pageCount() <<
" pages\n"
476 <<
"\tPage size: " << this->pageSize() <<
" elements\n"
477 <<
"\tCapacity: " << this->capacity() <<
" elements\n"
478 <<
"\tFootprint: " << this->memUsage() <<
" bytes\n";
485 void grow(
size_t index)
487 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
488 while(index >= mCapacity) {
489 mPageTable.push_back(
new Page() );
490 mCapacity += Page::Size;
494 void add_full(Page*& page,
size_t size);
496 void add_partially_full(Page*& page,
size_t size);
498 void add(Page*& page,
size_t size) {
499 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
500 if (size == Page::Size) {
501 this->add_full(page, size);
503 this->add_partially_full(page, size);
506 PageTableT mPageTable;
507 tbb::atomic<size_t> mSize;
509 tbb::spin_mutex mGrowthMutex;
514 template <
typename ValueT,
size_t Log2PageSize>
517 if (mPageTable.size() > (mSize >> Log2PageSize) + 1) {
518 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
519 const size_t pageCount = (mSize >> Log2PageSize) + 1;
520 if (mPageTable.size() > pageCount) {
521 delete mPageTable.back();
522 mPageTable.pop_back();
523 mCapacity -= Page::Size;
528 template <
typename ValueT,
size_t Log2PageSize>
531 if (&other !=
this && !other.
isEmpty()) {
532 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
534 Page* page =
nullptr;
535 const size_t size = mSize & Page::Mask;
537 page = mPageTable.back();
538 mPageTable.pop_back();
542 mPageTable.insert(mPageTable.end(), other.mPageTable.begin(), other.mPageTable.end());
543 mSize += other.mSize;
544 mCapacity = Page::Size*mPageTable.size();
547 PageTableT().swap(other.mPageTable);
549 if (page) this->add_partially_full(page, size);
553 template <
typename ValueT,
size_t Log2PageSize>
556 assert(size == Page::Size);
557 if (mSize & Page::Mask) {
558 Page*& tmp = mPageTable.back();
559 std::swap(tmp, page);
561 mPageTable.push_back(page);
562 mCapacity += Page::Size;
567 template <
typename ValueT,
size_t Log2PageSize>
568 void PagedArray<ValueT, Log2PageSize>::add_partially_full(Page*& page,
size_t size)
570 assert(size > 0 && size < Page::Size);
571 if (
size_t m = mSize & Page::Mask) {
572 ValueT *s = page->data(), *t = mPageTable.back()->data() + m;
573 for (
size_t i=
std::min(mSize+size, mCapacity)-mSize; i; --i) *t++ = *s++;
574 if (mSize+size > mCapacity) {
575 mPageTable.push_back(
new Page() );
576 t = mPageTable.back()->data();
577 for (
size_t i=mSize+size-mCapacity; i; --i) *t++ = *s++;
578 mCapacity += Page::Size;
581 mPageTable.push_back( page );
582 mCapacity += Page::Size;
591 template <
typename ValueT,
size_t Log2PageSize>
612 (*mPage)[mSize++] = v;
613 if (mSize == Page::Size) this->flush();
621 mParent->add(mPage, mSize);
622 if (mPage ==
nullptr) mPage =
new Page();
628 size_t size()
const {
return mSize; }
639 template <
typename ValueT,
size_t Log2PageSize>
641 ConstIterator :
public std::iterator<std::random_access_iterator_tag, ValueT>
644 using BaseT = std::iterator<std::random_access_iterator_tag, ValueT>;
652 mParent=other.mParent;
662 const ValueT&
operator*()
const {
return (*mParent)[mPos]; }
679 bool isValid()
const {
return mParent !=
nullptr && mPos < mParent->size(); }
680 size_t pos()
const {
return mPos; }
690 template <
typename ValueT,
size_t Log2PageSize>
692 Iterator :
public std::iterator<std::random_access_iterator_tag, ValueT>
695 using BaseT = std::iterator<std::random_access_iterator_tag, ValueT>;
703 mParent=other.mParent;
730 bool isValid()
const {
return mParent !=
nullptr && mPos < mParent->size(); }
731 size_t pos()
const {
return mPos; }
740 template <
typename ValueT,
size_t Log2PageSize>
745 static const size_t Size = 1UL << Log2PageSize;
746 static const size_t Mask = Size - 1UL;
747 static size_t memUsage() {
return sizeof(ValueT)*Size; }
749 Page() : mData(reinterpret_cast<ValueT*>(new char[sizeof(ValueT)*Size])) {}
752 Page& operator=(
const Page&) =
delete;
753 ValueT&
operator[](
const size_t i) {
return mData[i & Mask]; }
754 const ValueT&
operator[](
const size_t i)
const {
return mData[i & Mask]; }
757 for (
size_t i=Size; i; --i) *dst++ = v;
759 ValueT*
data() {
return mData; }
763 const ValueT* src = mData;
764 for (
size_t i=n; i; --i) *dst++ = *src++;
776 #endif // OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED
ConstIterator operator+(const difference_type &pos) const
Definition: PagedArray.h:668
ValueT ValueType
Definition: PagedArray.h:192
Definition: PagedArray.h:592
bool copy(ValueType *p, size_t count) const
Copy the first count values in this PageArray into a raw c-style array, assuming it to be at least co...
Definition: PagedArray.h:308
bool operator<=(const ConstIterator &other) const
Definition: PagedArray.h:675
size_t capacity() const
Return the maximum number of elements that this array can contain without allocating more memory page...
Definition: PagedArray.h:377
const ValueT & operator[](const difference_type &pos) const
Definition: PagedArray.h:664
void sort(Functor func)
Parallel sort of all the elements based on a custom functor with the api:
Definition: PagedArray.h:458
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:219
size_t freeCount() const
Return the number of additional elements that can be added to this array without allocating more memo...
Definition: PagedArray.h:381
Iterator & operator++()
Definition: PagedArray.h:707
void invSort()
Parallel sort of all the elements in descending order.
Definition: PagedArray.h:450
difference_type operator-(const Iterator &other) const
Definition: PagedArray.h:721
void print(std::ostream &os=std::cout) const
Print information for debugging.
Definition: PagedArray.h:471
ConstIterator & operator--()
Definition: PagedArray.h:657
Concurrent, page-based, dynamically-sized linear data structure with O(1) random access and STL-compl...
Definition: PagedArray.h:183
bool operator!=(const ConstIterator &other) const
Definition: PagedArray.h:673
Iterator operator+(const difference_type &pos) const
Definition: PagedArray.h:719
void sort()
Parallel sort of all the elements in ascending order.
Definition: PagedArray.h:447
ConstIterator end() const
Definition: PagedArray.h:443
Iterator & operator=(const Iterator &other)
Definition: PagedArray.h:701
~Page()
Definition: PagedArray.h:750
ValueT * data()
Definition: PagedArray.h:759
difference_type operator-(const ConstIterator &other) const
Definition: PagedArray.h:670
static size_t memUsage()
Definition: PagedArray.h:747
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Multiply m0 by m1 and return the resulting matrix.
Definition: Mat3.h:645
size_t pos() const
Definition: PagedArray.h:680
size_t size() const
Return the number of elements in this array.
Definition: PagedArray.h:373
typename BaseT::difference_type difference_type
Definition: PagedArray.h:645
void fill(const ValueType &v)
Set all elements in the page table to the specified value.
Definition: PagedArray.h:293
bool isPartiallyFull() const
Return true if the page table is partially full, i.e. the last non-empty page contains less than page...
Definition: PagedArray.h:407
bool operator>=(const Iterator &other) const
Definition: PagedArray.h:725
void push_back(const ValueT &v)
Add a value to the buffer and increment the size.
Definition: PagedArray.h:611
ConstIterator & operator-=(const difference_type &pos)
Definition: PagedArray.h:667
Library and file format version numbers.
bool operator!=(const Iterator &other) const
Definition: PagedArray.h:724
Iterator operator-(const difference_type &pos) const
Definition: PagedArray.h:720
ConstIterator & operator+=(const difference_type &pos)
Definition: PagedArray.h:666
bool isValid() const
Definition: PagedArray.h:730
std::iterator< std::random_access_iterator_tag, ValueT > BaseT
Definition: PagedArray.h:695
bool operator<(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:207
ValueType & operator[](size_t i)
Return a reference to the value at the specified offset.
Definition: PagedArray.h:269
ConstIterator operator-(const difference_type &pos) const
Definition: PagedArray.h:669
Iterator & operator--()
Definition: PagedArray.h:708
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
Iterator & operator+=(const difference_type &pos)
Definition: PagedArray.h:717
void flush()
Manually transfers the values in this buffer to the parent PagedArray.
Definition: PagedArray.h:620
Definition: PagedArray.h:691
static size_t pageSize()
Return the number of elements per memory page.
Definition: PagedArray.h:387
Page()
Definition: PagedArray.h:749
PagedArray()
Default constructor.
Definition: PagedArray.h:196
bool isEmpty() const
Return true if the container contains no elements.
Definition: PagedArray.h:399
ValueT & operator[](const size_t i)
Definition: PagedArray.h:753
size_t push_back(const ValueType &value)
Thread safe insertion, adds a new element at the end and increases the container size by one and retu...
Definition: PagedArray.h:231
ValueT * mData
Definition: PagedArray.h:767
ConstIterator & operator=(const ConstIterator &other)
Definition: PagedArray.h:650
~ValueBuffer()
Destructor that transfers an buffered values to the parent PagedArray.
Definition: PagedArray.h:603
static Ptr create()
Return a shared pointer to a new instance of this class.
Definition: PagedArray.h:206
std::iterator< std::random_access_iterator_tag, ValueT > BaseT
Definition: PagedArray.h:644
Iterator()
Definition: PagedArray.h:698
Iterator operator++(int)
Definition: PagedArray.h:710
void copy(ValueType *p) const
Definition: PagedArray.h:325
size_t push_back_unsafe(const ValueType &value)
Slightly faster than the thread-safe push_back above.
Definition: PagedArray.h:246
size_t memUsage() const
Return the memory footprint of this array in bytes.
Definition: PagedArray.h:393
Definition: PagedArray.h:640
const ValueT * operator->() const
Definition: PagedArray.h:663
SharedPtr< PagedArray > Ptr
Definition: PagedArray.h:193
bool operator>=(const ConstIterator &other) const
Definition: PagedArray.h:674
ValueT & operator*() const
Definition: PagedArray.h:713
ValueBuffer(PagedArray &parent)
Constructor from a PageArray.
Definition: PagedArray.h:598
bool operator==(const ConstIterator &other) const
Definition: PagedArray.h:672
const ValueT & operator[](const size_t i) const
Definition: PagedArray.h:754
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:180
Definition: PagedArray.h:741
Iterator end()
Return a non-const iterator pointing to the past-the-last element.
Definition: PagedArray.h:428
ConstIterator()
Definition: PagedArray.h:647
void resize(size_t size, const ValueType &v)
Resize this array to the specified size and initialize all values to v.
Definition: PagedArray.h:366
Iterator & operator-=(const difference_type &pos)
Definition: PagedArray.h:718
static size_t log2PageSize()
Return log2 of the number of elements per memory page.
Definition: PagedArray.h:390
bool operator==(const Iterator &other) const
Definition: PagedArray.h:723
size_t pos() const
Definition: PagedArray.h:731
ConstIterator(const ConstIterator &other)
Definition: PagedArray.h:649
void resize(size_t size)
Resize this array to the specified size.
Definition: PagedArray.h:341
size_t size() const
Return the current number of elements cached in this buffer.
Definition: PagedArray.h:628
const ValueType & operator[](size_t i) const
Return a const-reference to the value at the specified offset.
Definition: PagedArray.h:282
PagedArrayType & parent() const
Return a reference to the parent PagedArray.
Definition: PagedArray.h:626
Iterator begin()
Return a non-const iterator pointing to the first element.
Definition: PagedArray.h:421
#define OPENVDB_VERSION_NAME
Definition: version.h:134
ValueT * operator->() const
Definition: PagedArray.h:714
bool isValid() const
Definition: PagedArray.h:679
ValueBuffer(const ValueBuffer &other)
Definition: PagedArray.h:601
Iterator(PagedArray &parent, size_t pos=0)
Definition: PagedArray.h:699
size_t pageCount() const
Return the number of allocated memory pages.
Definition: PagedArray.h:384
Iterator(const Iterator &other)
Definition: PagedArray.h:700
Definition: Exceptions.h:40
ConstIterator operator++(int)
Definition: PagedArray.h:659
ConstIterator & operator++()
Definition: PagedArray.h:656
void copy(ValueType *dst, size_t n) const
Definition: PagedArray.h:762
void fill(const ValueT &v)
Definition: PagedArray.h:755
ConstIterator(const PagedArray &parent, size_t pos=0)
Definition: PagedArray.h:648
typename BaseT::difference_type difference_type
Definition: PagedArray.h:696
ConstIterator operator--(int)
Definition: PagedArray.h:660
Iterator operator--(int)
Definition: PagedArray.h:711
~PagedArray()
Destructor removed all allocated pages.
Definition: PagedArray.h:199
ConstIterator begin() const
Definition: PagedArray.h:433
const ValueT & operator*() const
Definition: PagedArray.h:662
void clear()
Removes all elements from the array and delete all pages.
Definition: PagedArray.h:412
ValueT & operator[](const difference_type &pos) const
Definition: PagedArray.h:715
bool operator<=(const Iterator &other) const
Definition: PagedArray.h:726