42 #ifndef OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED 43 #define OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED 49 #include <tbb/atomic.h> 50 #include <tbb/spin_mutex.h> 51 #include <tbb/parallel_for.h> 52 #include <tbb/parallel_sort.h> 185 template<
typename ValueT,
186 size_t Log2PageSize = 10UL,
187 template<
typename ...>
class TableT = std::vector>
193 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1700 195 using PageTableT = TableT<Page*, std::allocator<Page*>>;
197 using PageTableT = TableT<Page*>;
238 const size_t index = mSize.fetch_and_increment();
239 if (index >= mCapacity) this->grow(index);
240 (*mPageTable[index >> Log2PageSize])[index] = value;
253 const size_t index = mSize.fetch_and_increment();
254 if (index >= mCapacity) {
255 mPageTable.push_back(
new Page() );
256 mCapacity += Page::Size;
258 (*mPageTable[index >> Log2PageSize])[index] = value;
265 void shrink_to_fit();
277 return (*mPageTable[i>>Log2PageSize])[i];
290 return (*mPageTable[i>>Log2PageSize])[i];
300 auto op = [&](
const tbb::blocked_range<size_t>& r){
301 for(
size_t i=r.begin(); i!=r.end(); ++i) mPageTable[i]->fill(v);
303 tbb::parallel_for(tbb::blocked_range<size_t>(0, this->pageCount()), op);
315 size_t last_page = count >> Log2PageSize;
316 if (last_page >= this->pageCount())
return false;
317 auto op = [&](
const tbb::blocked_range<size_t>& r){
318 for (
size_t i=r.begin(); i!=r.end(); ++i) {
319 mPageTable[i]->copy(p+i*Page::Size, Page::Size);
322 if (
size_t m = count & Page::Mask) {
323 tbb::parallel_for(tbb::blocked_range<size_t>(0, last_page, 32), op);
324 mPageTable[last_page]->copy(p+last_page*Page::Size, m);
326 tbb::parallel_for(tbb::blocked_range<size_t>(0, last_page+1, 32), op);
349 if (size > mCapacity) {
352 this->shrink_to_fit();
378 size_t size()
const {
return mSize; }
400 return sizeof(*this) + mPageTable.size() * Page::memUsage();
419 for (
size_t i=0, n=mPageTable.size(); i<n; ++i)
delete mPageTable[i];
420 PageTableT().swap(mPageTable);
436 ConstIterator cbegin()
const {
return ConstIterator(*
this, 0); }
442 ConstIterator cend()
const {
return ConstIterator(*
this, mSize); }
452 void sort() { tbb::parallel_sort(this->begin(), this->end(), std::less<ValueT>() ); }
455 void invSort() { tbb::parallel_sort(this->begin(), this->end(), std::greater<ValueT>()); }
458 template <
typename Functor>
463 void sort(Functor func) { tbb::parallel_sort(this->begin(), this->end(), func ); }
476 void print(std::ostream& os = std::cout)
const 478 os <<
"PagedArray:\n" 479 <<
"\tSize: " << this->size() <<
" elements\n" 480 <<
"\tPage table: " << this->pageCount() <<
" pages\n" 481 <<
"\tPage size: " << this->pageSize() <<
" elements\n" 482 <<
"\tCapacity: " << this->capacity() <<
" elements\n" 483 <<
"\tFootprint: " << this->memUsage() <<
" bytes\n";
490 void grow(
size_t index)
492 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
493 while(index >= mCapacity) {
494 mPageTable.push_back(
new Page() );
495 mCapacity += Page::Size;
499 void add_full(Page*& page,
size_t size);
501 void add_partially_full(Page*& page,
size_t size);
503 void add(Page*& page,
size_t size) {
504 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
505 if (size == Page::Size) {
506 this->add_full(page, size);
508 this->add_partially_full(page, size);
511 PageTableT mPageTable;
512 tbb::atomic<size_t> mSize{0};
513 size_t mCapacity = 0;
514 tbb::spin_mutex mGrowthMutex;
519 template <
typename ValueT,
size_t Log2PageSize,
template<
typename ...>
class TableT>
522 if (mPageTable.size() > (mSize >> Log2PageSize) + 1) {
523 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
524 const size_t pageCount = (mSize >> Log2PageSize) + 1;
525 if (mPageTable.size() > pageCount) {
526 delete mPageTable.back();
527 mPageTable.pop_back();
528 mCapacity -= Page::Size;
533 template <
typename ValueT,
size_t Log2PageSize,
template<
typename ...>
class TableT>
536 if (&other !=
this && !other.
isEmpty()) {
537 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
539 Page* page =
nullptr;
540 const size_t size = mSize & Page::Mask;
542 page = mPageTable.back();
543 mPageTable.pop_back();
547 mPageTable.insert(mPageTable.end(), other.mPageTable.begin(), other.mPageTable.end());
548 mSize += other.mSize;
549 mCapacity = Page::Size*mPageTable.size();
552 PageTableT().swap(other.mPageTable);
554 if (page) this->add_partially_full(page, size);
558 template <
typename ValueT,
size_t Log2PageSize,
template<
typename ...>
class TableT>
561 assert(size == Page::Size);
562 if (mSize & Page::Mask) {
563 Page*& tmp = mPageTable.back();
564 std::swap(tmp, page);
566 mPageTable.push_back(page);
567 mCapacity += Page::Size;
572 template <
typename ValueT,
size_t Log2PageSize,
template<
typename ...>
class TableT>
573 void PagedArray<ValueT, Log2PageSize, TableT>::add_partially_full(Page*& page,
size_t size)
575 assert(size > 0 && size < Page::Size);
576 if (
size_t m = mSize & Page::Mask) {
577 ValueT *s = page->data(), *t = mPageTable.back()->data() + m;
578 for (
size_t i=
std::min(mSize+size, mCapacity)-mSize; i; --i) *t++ = *s++;
579 if (mSize+size > mCapacity) {
580 mPageTable.push_back(
new Page() );
581 t = mPageTable.back()->data();
582 for (
size_t i=mSize+size-mCapacity; i; --i) *t++ = *s++;
583 mCapacity += Page::Size;
586 mPageTable.push_back( page );
587 mCapacity += Page::Size;
596 template <
typename ValueT,
size_t Log2PageSize,
template<
typename ...>
class TableT>
617 (*mPage)[mSize++] = v;
618 if (mSize == Page::Size) this->flush();
626 mParent->add(mPage, mSize);
627 if (mPage ==
nullptr) mPage =
new Page();
633 size_t size()
const {
return mSize; }
644 template <
typename ValueT,
size_t Log2PageSize,
template<
typename ...>
class TableT>
646 ConstIterator :
public std::iterator<std::random_access_iterator_tag, ValueT>
649 using BaseT = std::iterator<std::random_access_iterator_tag, ValueT>;
657 mParent=other.mParent;
667 const ValueT&
operator*()
const {
return (*mParent)[mPos]; }
684 bool isValid()
const {
return mParent !=
nullptr && mPos < mParent->size(); }
685 size_t pos()
const {
return mPos; }
695 template <
typename ValueT,
size_t Log2PageSize,
template<
typename ...>
class TableT>
697 Iterator :
public std::iterator<std::random_access_iterator_tag, ValueT>
700 using BaseT = std::iterator<std::random_access_iterator_tag, ValueT>;
708 mParent=other.mParent;
735 bool isValid()
const {
return mParent !=
nullptr && mPos < mParent->size(); }
736 size_t pos()
const {
return mPos; }
745 template <
typename ValueT,
size_t Log2PageSize,
template<
typename ...>
class TableT>
750 static const size_t Size = 1UL << Log2PageSize;
751 static const size_t Mask = Size - 1UL;
752 static size_t memUsage() {
return sizeof(ValueT)*Size; }
754 Page() : mData(reinterpret_cast<ValueT*>(new char[sizeof(ValueT)*Size])) {}
757 Page& operator=(
const Page&) =
delete;
758 ValueT&
operator[](
const size_t i) {
return mData[i & Mask]; }
759 const ValueT&
operator[](
const size_t i)
const {
return mData[i & Mask]; }
762 for (
size_t i=Size; i; --i) *dst++ = v;
764 ValueT*
data() {
return mData; }
768 const ValueT* src = mData;
769 for (
size_t i=n; i; --i) *dst++ = *src++;
781 #endif // OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED Iterator & operator+=(const difference_type &pos)
Definition: PagedArray.h:722
Definition: PagedArray.h:645
size_t freeCount() const
Return the number of additional elements that can be added to this array without allocating more memo...
Definition: PagedArray.h:386
Definition: PagedArray.h:746
const ValueT & operator[](const size_t i) const
Definition: PagedArray.h:759
void fill(const ValueT &v)
Definition: PagedArray.h:760
ConstIterator & operator=(const ConstIterator &other)
Definition: PagedArray.h:655
size_t pos() const
Definition: PagedArray.h:736
Iterator end()
Return a non-const iterator pointing to the past-the-last element.
Definition: PagedArray.h:433
ConstIterator end() const
Return a const iterator pointing to the past-the-last element.
Definition: PagedArray.h:448
difference_type operator-(const ConstIterator &other) const
Definition: PagedArray.h:675
typename BaseT::difference_type difference_type
Definition: PagedArray.h:650
bool operator<=(const ConstIterator &other) const
Definition: PagedArray.h:680
ValueT ValueType
Definition: PagedArray.h:201
Iterator & operator--()
Definition: PagedArray.h:713
bool operator<(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:206
ConstIterator operator-(const difference_type &pos) const
Definition: PagedArray.h:674
bool isEmpty() const
Return true if the container contains no elements.
Definition: PagedArray.h:404
void push_back(const ValueT &v)
Add a value to the buffer and increment the size.
Definition: PagedArray.h:616
ConstIterator begin() const
Return a const iterator pointing to the first element.
Definition: PagedArray.h:438
void invSort()
Parallel sort of all the elements in descending order.
Definition: PagedArray.h:455
bool operator!=(const Iterator &other) const
Definition: PagedArray.h:729
void fill(const ValueType &v)
Set all elements in the page table to the specified value.
Definition: PagedArray.h:298
Concurrent, page-based, dynamically-sized linear data structure with O(1) random access and STL-compl...
Definition: PagedArray.h:188
size_t size() const
Return the current number of elements cached in this buffer.
Definition: PagedArray.h:633
ValueT & operator[](const size_t i)
Definition: PagedArray.h:758
static size_t pageSize()
Return the number of elements per memory page.
Definition: PagedArray.h:392
size_t size() const
Return the number of elements in this array.
Definition: PagedArray.h:378
PagedArrayType & parent() const
Return a reference to the parent PagedArray.
Definition: PagedArray.h:631
Iterator(PagedArray &parent, size_t pos=0)
Definition: PagedArray.h:704
const ValueT * operator->() const
Definition: PagedArray.h:668
Iterator(const Iterator &other)
Definition: PagedArray.h:705
~PagedArray()
Destructor removed all allocated pages.
Definition: PagedArray.h:207
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:218
void print(std::ostream &os=std::cout) const
Print information for debugging.
Definition: PagedArray.h:476
ConstIterator & operator++()
Definition: PagedArray.h:661
Iterator operator+(const difference_type &pos) const
Definition: PagedArray.h:724
size_t push_back_unsafe(const ValueType &value)
Slightly faster than the thread-safe push_back above.
Definition: PagedArray.h:251
Iterator begin()
Return a non-const iterator pointing to the first element.
Definition: PagedArray.h:426
ValueType & operator[](size_t i)
Return a reference to the value at the specified offset.
Definition: PagedArray.h:274
bool isValid() const
Definition: PagedArray.h:735
Iterator & operator-=(const difference_type &pos)
Definition: PagedArray.h:723
ValueT & operator[](const difference_type &pos) const
Definition: PagedArray.h:720
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
~Page()
Definition: PagedArray.h:755
Iterator()
Definition: PagedArray.h:703
bool operator==(const Iterator &other) const
Definition: PagedArray.h:728
size_t pageCount() const
Return the number of allocated memory pages.
Definition: PagedArray.h:389
ConstIterator()
Definition: PagedArray.h:652
Iterator operator++(int)
Definition: PagedArray.h:715
const ValueT & operator*() const
Definition: PagedArray.h:667
void resize(size_t size, const ValueType &v)
Resize this array to the specified size and initialize all values to v.
Definition: PagedArray.h:371
~ValueBuffer()
Destructor that transfers an buffered values to the parent PagedArray.
Definition: PagedArray.h:608
ValueT * operator->() const
Definition: PagedArray.h:719
ValueT & operator*() const
Definition: PagedArray.h:718
void flush()
Manually transfers the values in this buffer to the parent PagedArray.
Definition: PagedArray.h:625
void resize(size_t size)
Resize this array to the specified size.
Definition: PagedArray.h:346
ConstIterator operator+(const difference_type &pos) const
Definition: PagedArray.h:673
const ValueType & operator[](size_t i) const
Return a const-reference to the value at the specified offset.
Definition: PagedArray.h:287
bool operator<=(const Iterator &other) const
Definition: PagedArray.h:731
void copy(ValueType *p) const
Definition: PagedArray.h:330
const ValueT & operator[](const difference_type &pos) const
Definition: PagedArray.h:669
Definition: Exceptions.h:39
std::iterator< std::random_access_iterator_tag, ValueT > BaseT
Definition: PagedArray.h:700
Definition: PagedArray.h:597
bool operator!=(const ConstIterator &other) const
Definition: PagedArray.h:678
ConstIterator operator++(int)
Definition: PagedArray.h:664
Definition: PagedArray.h:696
typename BaseT::difference_type difference_type
Definition: PagedArray.h:701
ConstIterator & operator+=(const difference_type &pos)
Definition: PagedArray.h:671
static size_t log2PageSize()
Return log2 of the number of elements per memory page.
Definition: PagedArray.h:395
difference_type operator-(const Iterator &other) const
Definition: PagedArray.h:726
size_t capacity() const
Return the maximum number of elements that this array can contain without allocating more memory page...
Definition: PagedArray.h:382
ConstIterator operator--(int)
Definition: PagedArray.h:665
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:412
size_t memUsage() const
Return the memory footprint of this array in bytes.
Definition: PagedArray.h:398
ConstIterator(const ConstIterator &other)
Definition: PagedArray.h:654
bool operator==(const ConstIterator &other) const
Definition: PagedArray.h:677
std::iterator< std::random_access_iterator_tag, ValueT > BaseT
Definition: PagedArray.h:649
Page()
Definition: PagedArray.h:754
bool operator>=(const Iterator &other) const
Definition: PagedArray.h:730
size_t pos() const
Definition: PagedArray.h:685
bool operator>=(const ConstIterator &other) const
Definition: PagedArray.h:679
void copy(ValueType *dst, size_t n) const
Definition: PagedArray.h:767
Iterator & operator++()
Definition: PagedArray.h:712
void clear()
Removes all elements from the array and delete all pages.
Definition: PagedArray.h:417
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 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:236
ConstIterator & operator-=(const difference_type &pos)
Definition: PagedArray.h:672
Iterator operator--(int)
Definition: PagedArray.h:716
Iterator & operator=(const Iterator &other)
Definition: PagedArray.h:706
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
void sort(Functor func)
Parallel sort of all the elements based on a custom functor with the api:
Definition: PagedArray.h:463
bool isValid() const
Definition: PagedArray.h:684
ValueBuffer(const ValueBuffer &other)
Definition: PagedArray.h:606
ConstIterator & operator--()
Definition: PagedArray.h:662
void sort()
Parallel sort of all the elements in ascending order.
Definition: PagedArray.h:452
ValueT * data()
Definition: PagedArray.h:764
ValueT * mData
Definition: PagedArray.h:772
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:313
static size_t memUsage()
Definition: PagedArray.h:752
ConstIterator(const PagedArray &parent, size_t pos=0)
Definition: PagedArray.h:653
ValueBuffer(PagedArray &parent)
Constructor from a PageArray.
Definition: PagedArray.h:603
Iterator operator-(const difference_type &pos) const
Definition: PagedArray.h:725