Go to the documentation of this file.
15 #ifndef OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED
16 #define OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED
24 #include <tbb/atomic.h>
25 #include <tbb/spin_mutex.h>
26 #include <tbb/parallel_for.h>
27 #include <tbb/parallel_sort.h>
155 template<
typename ValueT,
size_t Log2PageSize = 10UL>
162 using PageTableT = std::deque<Page*>;
206 const size_t index = mSize.fetch_and_increment();
207 if (index >= mCapacity) this->grow(index);
208 (*mPageTable[index >> Log2PageSize])[index] = value;
221 const size_t index = mSize.fetch_and_increment();
222 if (index >= mCapacity) {
223 mPageTable.push_back(
new Page() );
224 mCapacity += Page::Size;
226 (*mPageTable[index >> Log2PageSize])[index] = value;
233 void shrink_to_fit();
245 return (*mPageTable[i>>Log2PageSize])[i];
258 return (*mPageTable[i>>Log2PageSize])[i];
268 auto op = [&](
const tbb::blocked_range<size_t>& r){
269 for(
size_t i=r.begin(); i!=r.end(); ++i) mPageTable[i]->fill(v);
271 tbb::parallel_for(tbb::blocked_range<size_t>(0, this->pageCount()), op);
283 size_t last_page = count >> Log2PageSize;
284 if (last_page >= this->pageCount())
return false;
285 auto op = [&](
const tbb::blocked_range<size_t>& r){
286 for (
size_t i=r.begin(); i!=r.end(); ++i) {
287 mPageTable[i]->copy(p+i*Page::Size, Page::Size);
290 if (
size_t m = count & Page::Mask) {
291 tbb::parallel_for(tbb::blocked_range<size_t>(0, last_page, 32), op);
292 mPageTable[last_page]->copy(p+last_page*Page::Size, m);
294 tbb::parallel_for(tbb::blocked_range<size_t>(0, last_page+1, 32), op);
317 if (size > mCapacity) {
320 this->shrink_to_fit();
346 size_t size()
const {
return mSize; }
368 return sizeof(*this) + mPageTable.size() * Page::memUsage();
387 for (
size_t i=0, n=mPageTable.size(); i<n; ++i)
delete mPageTable[i];
388 PageTableT().swap(mPageTable);
404 ConstIterator cbegin()
const {
return ConstIterator(*
this, 0); }
410 ConstIterator cend()
const {
return ConstIterator(*
this, mSize); }
420 void sort() { tbb::parallel_sort(this->begin(), this->end(), std::less<ValueT>() ); }
423 void invSort() { tbb::parallel_sort(this->begin(), this->end(), std::greater<ValueT>()); }
426 template <
typename Functor>
431 void sort(Functor func) { tbb::parallel_sort(this->begin(), this->end(), func ); }
444 void print(std::ostream& os = std::cout)
const
446 os <<
"PagedArray:\n"
447 <<
"\tSize: " << this->size() <<
" elements\n"
448 <<
"\tPage table: " << this->pageCount() <<
" pages\n"
449 <<
"\tPage size: " << this->pageSize() <<
" elements\n"
450 <<
"\tCapacity: " << this->capacity() <<
" elements\n"
451 <<
"\tFootprint: " << this->memUsage() <<
" bytes\n";
458 void grow(
size_t index)
460 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
461 while(index >= mCapacity) {
462 mPageTable.push_back(
new Page() );
463 mCapacity += Page::Size;
467 void add_full(Page*& page,
size_t size);
469 void add_partially_full(Page*& page,
size_t size);
471 void add(Page*& page,
size_t size) {
472 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
473 if (size == Page::Size) {
474 this->add_full(page, size);
476 this->add_partially_full(page, size);
479 PageTableT mPageTable;
480 tbb::atomic<size_t> mSize;
482 tbb::spin_mutex mGrowthMutex;
487 template <
typename ValueT,
size_t Log2PageSize>
490 if (mPageTable.size() > (mSize >> Log2PageSize) + 1) {
491 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
492 const size_t pageCount = (mSize >> Log2PageSize) + 1;
493 if (mPageTable.size() > pageCount) {
494 delete mPageTable.back();
495 mPageTable.pop_back();
496 mCapacity -= Page::Size;
501 template <
typename ValueT,
size_t Log2PageSize>
504 if (&other !=
this && !other.
isEmpty()) {
505 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
507 Page* page =
nullptr;
508 const size_t size = mSize & Page::Mask;
510 page = mPageTable.back();
511 mPageTable.pop_back();
515 mPageTable.insert(mPageTable.end(), other.mPageTable.begin(), other.mPageTable.end());
516 mSize += other.mSize;
517 mCapacity = Page::Size*mPageTable.size();
520 PageTableT().swap(other.mPageTable);
522 if (page) this->add_partially_full(page, size);
526 template <
typename ValueT,
size_t Log2PageSize>
529 assert(size == Page::Size);
530 if (mSize & Page::Mask) {
531 Page*& tmp = mPageTable.back();
532 std::swap(tmp, page);
534 mPageTable.push_back(page);
535 mCapacity += Page::Size;
540 template <
typename ValueT,
size_t Log2PageSize>
541 void PagedArray<ValueT, Log2PageSize>::add_partially_full(Page*& page,
size_t size)
543 assert(size > 0 && size < Page::Size);
544 if (
size_t m = mSize & Page::Mask) {
545 ValueT *s = page->data(), *t = mPageTable.back()->data() + m;
546 for (
size_t i=
std::min(mSize+size, mCapacity)-mSize; i; --i) *t++ = *s++;
547 if (mSize+size > mCapacity) {
548 mPageTable.push_back(
new Page() );
549 t = mPageTable.back()->data();
550 for (
size_t i=mSize+size-mCapacity; i; --i) *t++ = *s++;
551 mCapacity += Page::Size;
554 mPageTable.push_back( page );
555 mCapacity += Page::Size;
564 template <
typename ValueT,
size_t Log2PageSize>
585 (*mPage)[mSize++] = v;
586 if (mSize == Page::Size) this->flush();
594 mParent->add(mPage, mSize);
595 if (mPage ==
nullptr) mPage =
new Page();
601 size_t size()
const {
return mSize; }
612 template <
typename ValueT,
size_t Log2PageSize>
614 ConstIterator :
public std::iterator<std::random_access_iterator_tag, ValueT>
617 using BaseT = std::iterator<std::random_access_iterator_tag, ValueT>;
625 mParent=other.mParent;
635 const ValueT&
operator*()
const {
return (*mParent)[mPos]; }
652 bool isValid()
const {
return mParent !=
nullptr && mPos < mParent->size(); }
653 size_t pos()
const {
return mPos; }
663 template <
typename ValueT,
size_t Log2PageSize>
665 Iterator :
public std::iterator<std::random_access_iterator_tag, ValueT>
668 using BaseT = std::iterator<std::random_access_iterator_tag, ValueT>;
676 mParent=other.mParent;
703 bool isValid()
const {
return mParent !=
nullptr && mPos < mParent->size(); }
704 size_t pos()
const {
return mPos; }
713 template <
typename ValueT,
size_t Log2PageSize>
718 static const size_t Size = 1UL << Log2PageSize;
719 static const size_t Mask = Size - 1UL;
720 static size_t memUsage() {
return sizeof(ValueT)*Size; }
722 Page() : mData(reinterpret_cast<ValueT*>(new char[sizeof(ValueT)*Size])) {}
726 ValueT&
operator[](
const size_t i) {
return mData[i & Mask]; }
727 const ValueT&
operator[](
const size_t i)
const {
return mData[i & Mask]; }
730 for (
size_t i=Size; i; --i) *dst++ = v;
732 ValueT*
data() {
return mData; }
736 const ValueT* src = mData;
737 for (
size_t i=n; i; --i) *dst++ = *src++;
749 #endif // OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED
static size_t pageSize()
Return the number of elements per memory page.
Definition: PagedArray.h:360
size_t size() const
Return the current number of elements cached in this buffer.
Definition: PagedArray.h:601
PagedArray(const PagedArray &)=delete
SharedPtr< PagedArray > Ptr
Definition: PagedArray.h:166
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:204
Definition: PagedArray.h:567
ConstIterator operator-(const difference_type &pos) const
Definition: PagedArray.h:642
void resize(size_t size)
Resize this array to the specified size.
Definition: PagedArray.h:314
const ValueType & operator[](size_t i) const
Return a const-reference to the value at the specified offset.
Definition: PagedArray.h:255
PagedArray()
Default constructor.
Definition: PagedArray.h:169
void copy(ValueType *p) const
Definition: PagedArray.h:298
size_t push_back_unsafe(const ValueType &value)
Slightly faster than the thread-safe push_back above.
Definition: PagedArray.h:219
Page(const Page &)=delete
ConstIterator(const PagedArray &parent, size_t pos=0)
Definition: PagedArray.h:621
PagedArrayType & parent() const
Return a reference to the parent PagedArray.
Definition: PagedArray.h:599
Definition: PagedArray.h:615
Page & operator=(const Page &)=delete
ValueT & operator*() const
Definition: PagedArray.h:686
static Ptr create()
Return a shared pointer to a new instance of this class.
Definition: PagedArray.h:179
size_t pageCount() const
Return the number of allocated memory pages.
Definition: PagedArray.h:357
Definition: PagedArray.h:666
void resize(size_t size, const ValueType &v)
Resize this array to the specified size and initialize all values to v.
Definition: PagedArray.h:339
Concurrent, page-based, dynamically-sized linear data structure with O(1) random access and STL-compl...
Definition: PagedArray.h:157
bool isValid() const
Definition: PagedArray.h:703
Iterator begin()
Return a non-const iterator pointing to the first element.
Definition: PagedArray.h:394
Library and file format version numbers.
~ValueBuffer()
Destructor that transfers an buffered values to the parent PagedArray.
Definition: PagedArray.h:576
ConstIterator begin() const
Definition: PagedArray.h:406
Iterator end()
Return a non-const iterator pointing to the past-the-last element.
Definition: PagedArray.h:401
static size_t log2PageSize()
Return log2 of the number of elements per memory page.
Definition: PagedArray.h:363
ConstIterator & operator++()
Definition: PagedArray.h:629
Definition: PagedArray.h:716
difference_type operator-(const Iterator &other) const
Definition: PagedArray.h:694
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:192
Iterator & operator-=(const difference_type &pos)
Definition: PagedArray.h:691
typename BaseT::difference_type difference_type
Definition: PagedArray.h:618
ValueBuffer(PagedArray &parent)
Constructor from a PageArray.
Definition: PagedArray.h:571
ValueT ValueType
Definition: PagedArray.h:165
Iterator & operator=(const Iterator &other)
Definition: PagedArray.h:674
size_t capacity() const
Return the maximum number of elements that this array can contain without allocating more memory page...
Definition: PagedArray.h:350
Iterator(PagedArray &parent, size_t pos=0)
Definition: PagedArray.h:672
bool operator>=(const Iterator &other) const
Definition: PagedArray.h:698
ConstIterator()
Definition: PagedArray.h:620
Iterator operator-(const difference_type &pos) const
Definition: PagedArray.h:693
const ValueT & operator*() const
Definition: PagedArray.h:635
const ValueT & operator[](const difference_type &pos) const
Definition: PagedArray.h:637
size_t pos() const
Definition: PagedArray.h:704
void clear()
Removes all elements from the array and delete all pages.
Definition: PagedArray.h:385
void invSort()
Parallel sort of all the elements in descending order.
Definition: PagedArray.h:423
Iterator & operator++()
Definition: PagedArray.h:680
bool operator!=(const Iterator &other) const
Definition: PagedArray.h:697
ValueBuffer(const ValueBuffer &other)
Definition: PagedArray.h:574
~PagedArray()
Destructor removed all allocated pages.
Definition: PagedArray.h:172
size_t size() const
Return the number of elements in this array.
Definition: PagedArray.h:346
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:281
void sort(Functor func)
Parallel sort of all the elements based on a custom functor with the api:
Definition: PagedArray.h:431
Iterator operator+(const difference_type &pos) const
Definition: PagedArray.h:692
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
ConstIterator & operator=(const ConstIterator &other)
Definition: PagedArray.h:623
bool operator>=(const ConstIterator &other) const
Definition: PagedArray.h:647
ConstIterator & operator-=(const difference_type &pos)
Definition: PagedArray.h:640
void flush()
Manually transfers the values in this buffer to the parent PagedArray.
Definition: PagedArray.h:593
typename BaseT::difference_type difference_type
Definition: PagedArray.h:669
ConstIterator(const ConstIterator &other)
Definition: PagedArray.h:622
bool operator==(const ConstIterator &other) const
Definition: PagedArray.h:645
Iterator & operator+=(const difference_type &pos)
Definition: PagedArray.h:690
const ValueT & operator[](const size_t i) const
Definition: PagedArray.h:727
ConstIterator operator+(const difference_type &pos) const
Definition: PagedArray.h:641
ConstIterator & operator--()
Definition: PagedArray.h:630
Iterator(const Iterator &other)
Definition: PagedArray.h:673
ConstIterator operator--(int)
Definition: PagedArray.h:633
size_t freeCount() const
Return the number of additional elements that can be added to this array without allocating more memo...
Definition: PagedArray.h:354
void print(std::ostream &os=std::cout) const
Print information for debugging.
Definition: PagedArray.h:444
bool operator<=(const Iterator &other) const
Definition: PagedArray.h:699
void sort()
Parallel sort of all the elements in ascending order.
Definition: PagedArray.h:420
bool operator!=(const ConstIterator &other) const
Definition: PagedArray.h:646
ValueT * mData
Definition: PagedArray.h:740
ConstIterator end() const
Definition: PagedArray.h:416
Iterator & operator--()
Definition: PagedArray.h:681
bool operator==(const Iterator &other) const
Definition: PagedArray.h:696
bool operator<=(const ConstIterator &other) const
Definition: PagedArray.h:648
bool isValid() const
Definition: PagedArray.h:652
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
ValueType & operator[](size_t i)
Return a reference to the value at the specified offset.
Definition: PagedArray.h:242
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:380
ValueT & operator[](const difference_type &pos) const
Definition: PagedArray.h:688
~Page()
Definition: PagedArray.h:723
ValueT * data()
Definition: PagedArray.h:732
ValueBuffer & operator=(const ValueBuffer &)=delete
ValueT & operator[](const size_t i)
Definition: PagedArray.h:726
ConstIterator operator++(int)
Definition: PagedArray.h:632
std::iterator< std::random_access_iterator_tag, ValueT > BaseT
Definition: PagedArray.h:617
std::iterator< std::random_access_iterator_tag, ValueT > BaseT
Definition: PagedArray.h:668
Iterator operator++(int)
Definition: PagedArray.h:683
Page()
Definition: PagedArray.h:722
Definition: Exceptions.h:13
void fill(const ValueType &v)
Set all elements in the page table to the specified value.
Definition: PagedArray.h:266
bool operator<(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:180
void push_back(const ValueT &v)
Add a value to the buffer and increment the size.
Definition: PagedArray.h:584
size_t pos() const
Definition: PagedArray.h:653
ConstIterator & operator+=(const difference_type &pos)
Definition: PagedArray.h:639
PagedArray & operator=(const PagedArray &)=delete
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
Iterator()
Definition: PagedArray.h:671
const ValueT * operator->() const
Definition: PagedArray.h:636
size_t memUsage() const
Return the memory footprint of this array in bytes.
Definition: PagedArray.h:366
void copy(ValueType *dst, size_t n) const
Definition: PagedArray.h:735
static size_t memUsage()
Definition: PagedArray.h:720
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:618
difference_type operator-(const ConstIterator &other) const
Definition: PagedArray.h:643
ValueT * operator->() const
Definition: PagedArray.h:687
void fill(const ValueT &v)
Definition: PagedArray.h:728
bool isEmpty() const
Return true if the container contains no elements.
Definition: PagedArray.h:372
Iterator operator--(int)
Definition: PagedArray.h:684