15 #ifndef OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED 16 #define OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED 18 #include <openvdb/version.h> 26 #include <tbb/spin_mutex.h> 27 #include <tbb/parallel_for.h> 28 #include <tbb/parallel_sort.h> 142 template<
typename ValueT,
size_t Log2PageSize = 10UL>
146 static_assert(Log2PageSize > 1UL,
"Expected Log2PageSize > 1");
150 using PageTableT = std::deque<Page*>;
195 const size_t index = mSize.fetch_add(1);
196 if (index >= mCapacity) {
197 mPageTable.push_back(
new Page() );
198 mCapacity += Page::Size;
200 (*mPageTable[index >> Log2PageSize])[index] = value;
207 void shrink_to_fit();
219 return (*mPageTable[i>>Log2PageSize])[i];
232 return (*mPageTable[i>>Log2PageSize])[i];
242 auto op = [&](
const tbb::blocked_range<size_t>& r){
243 for(
size_t i=r.begin(); i!=r.end(); ++i) mPageTable[i]->fill(v);
245 tbb::parallel_for(tbb::blocked_range<size_t>(0, this->pageCount()),
op);
257 size_t last_page = count >> Log2PageSize;
258 if (last_page >= this->pageCount())
return false;
259 auto op = [&](
const tbb::blocked_range<size_t>& r){
260 for (
size_t i=r.begin(); i!=r.end(); ++i) {
261 mPageTable[i]->copy(p+i*Page::Size, Page::Size);
264 if (
size_t m = count & Page::Mask) {
265 tbb::parallel_for(tbb::blocked_range<size_t>(0, last_page, 32),
op);
266 mPageTable[last_page]->copy(p+last_page*Page::Size, m);
268 tbb::parallel_for(tbb::blocked_range<size_t>(0, last_page+1, 32),
op);
291 if (size > mCapacity) {
294 this->shrink_to_fit();
320 size_t size()
const {
return mSize; }
361 for (
size_t i=0, n=mPageTable.size(); i<n; ++i)
delete mPageTable[i];
362 PageTableT().swap(mPageTable);
394 void sort() { tbb::parallel_sort(this->begin(), this->end(), std::less<ValueT>() ); }
397 void invSort() { tbb::parallel_sort(this->begin(), this->end(), std::greater<ValueT>()); }
404 template <
typename Functor>
405 void sort(Functor func) { tbb::parallel_sort(this->begin(), this->end(), func ); }
418 void print(std::ostream& os = std::cout)
const 420 os <<
"PagedArray:\n" 421 <<
"\tSize: " << this->size() <<
" elements\n" 422 <<
"\tPage table: " << this->pageCount() <<
" pages\n" 423 <<
"\tPage size: " << this->pageSize() <<
" elements\n" 424 <<
"\tCapacity: " << this->capacity() <<
" elements\n" 425 <<
"\tFootprint: " << this->
memUsage() <<
" bytes\n";
432 void grow(
size_t index)
434 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
435 while(index >= mCapacity) {
436 mPageTable.push_back(
new Page() );
437 mCapacity += Page::Size;
441 void add_full(
Page*& page,
size_t size);
443 void add_partially_full(
Page*& page,
size_t size);
445 void add(
Page*& page,
size_t size) {
446 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
447 if (size == Page::Size) {
448 this->add_full(page, size);
450 this->add_partially_full(page, size);
453 PageTableT mPageTable;
454 std::atomic<size_t> mSize;
456 tbb::spin_mutex mGrowthMutex;
461 template <
typename ValueT,
size_t Log2PageSize>
464 if (mPageTable.size() > (mSize >> Log2PageSize) + 1) {
465 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
466 const size_t pageCount = (mSize >> Log2PageSize) + 1;
467 if (mPageTable.size() > pageCount) {
468 delete mPageTable.back();
469 mPageTable.pop_back();
470 mCapacity -= Page::Size;
475 template <
typename ValueT,
size_t Log2PageSize>
478 if (&other !=
this && !other.
isEmpty()) {
479 tbb::spin_mutex::scoped_lock lock(mGrowthMutex);
481 Page* page =
nullptr;
482 const size_t size = mSize & Page::Mask;
484 page = mPageTable.back();
485 mPageTable.pop_back();
489 mPageTable.insert(mPageTable.end(), other.mPageTable.begin(), other.mPageTable.end());
490 mSize += other.mSize;
491 mCapacity = Page::Size*mPageTable.size();
494 PageTableT().swap(other.mPageTable);
496 if (page) this->add_partially_full(page, size);
500 template <
typename ValueT,
size_t Log2PageSize>
504 if (mSize & Page::Mask) {
505 Page*& tmp = mPageTable.back();
506 std::swap(tmp, page);
508 mPageTable.push_back(page);
509 mCapacity += Page::Size;
514 template <
typename ValueT,
size_t Log2PageSize>
518 if (
size_t m = mSize & Page::Mask) {
519 ValueT *s = page->
data(), *t = mPageTable.back()->data() + m;
520 for (
size_t i=
std::min(mSize+size, mCapacity)-mSize; i; --i) *t++ = *s++;
521 if (mSize+size > mCapacity) {
522 mPageTable.push_back(
new Page() );
523 t = mPageTable.back()->data();
524 for (
size_t i=mSize+size-mCapacity; i; --i) *t++ = *s++;
525 mCapacity += Page::Size;
528 mPageTable.push_back( page );
529 mCapacity += Page::Size;
538 template <
typename ValueT,
size_t Log2PageSize>
559 (*mPage)[mSize++] = v;
560 if (mSize == Page::Size) this->flush();
568 mParent->add(mPage, mSize);
569 if (mPage ==
nullptr) mPage =
new Page();
575 size_t size()
const {
return mSize; }
576 static size_t pageSize() {
return 1UL << Log2PageSize; }
587 template <
typename ValueT,
size_t Log2PageSize>
603 mParent=other.mParent;
613 const ValueT&
operator*()
const {
return (*mParent)[mPos]; }
631 bool isValid()
const {
return mParent !=
nullptr && mPos < mParent->size(); }
632 size_t pos()
const {
return mPos; }
643 template <
typename ValueT,
size_t Log2PageSize>
659 mParent=other.mParent;
687 bool isValid()
const {
return mParent !=
nullptr && mPos < mParent->size(); }
688 size_t pos()
const {
return mPos; }
697 template <
typename ValueT,
size_t Log2PageSize>
702 static const size_t Size = 1UL << Log2PageSize;
703 static const size_t Mask = Size - 1UL;
704 static size_t memUsage() {
return sizeof(ValueT)*Size; }
706 Page() : mData(reinterpret_cast<ValueT*>(new char[sizeof(ValueT)*Size])) {}
709 Page& operator=(
const Page&) =
delete;
710 ValueT&
operator[](
const size_t i) {
return mData[i & Mask]; }
711 const ValueT&
operator[](
const size_t i)
const {
return mData[i & Mask]; }
714 for (
size_t i=Size; i; --i) *dst++ = v;
716 ValueT*
data() {
return mData; }
720 const ValueT* src = mData;
721 for (
size_t i=n; i; --i) *dst++ = *src++;
733 #endif // OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED size_t pos() const
Definition: PagedArray.h:688
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:354
~ValueBuffer()
Destructor that transfers an buffered values to the parent PagedArray.
Definition: PagedArray.h:550
ConstIterator & operator-=(const difference_type &pos)
Definition: PagedArray.h:618
ValueT * pointer
Definition: PagedArray.h:650
void clear()
Removes all elements from the array and delete all pages.
Definition: PagedArray.h:359
~Page()
Definition: PagedArray.h:707
Iterator & operator=(const Iterator &other)
Definition: PagedArray.h:657
void fill(const ValueT &v)
Definition: PagedArray.h:712
bool operator!=(const ConstIterator &other) const
Definition: PagedArray.h:625
bool operator<=(const ConstIterator &other) const
Definition: PagedArray.h:627
bool operator>=(const Iterator &other) const
Definition: PagedArray.h:682
size_t capacity() const
Return the maximum number of elements that this array can contain without allocating more memory page...
Definition: PagedArray.h:324
void push_back(const ValueT &v)
Add a value to the buffer and increment the size.
Definition: PagedArray.h:558
ConstIterator cend() const
Return a const iterator pointing to the past-the-last element.
Definition: PagedArray.h:389
PagedArray()
Default constructor.
Definition: PagedArray.h:157
Iterator(PagedArray &parent, size_t pos=0)
Definition: PagedArray.h:655
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:255
static size_t pageSize()
Definition: PagedArray.h:576
size_t freeCount() const
Return the number of additional elements that can be added to this array without allocating more memo...
Definition: PagedArray.h:328
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:187
Iterator & operator--()
Definition: PagedArray.h:664
~PagedArray()
Destructor removed all allocated pages.
Definition: PagedArray.h:160
Iterator(const Iterator &other)
Definition: PagedArray.h:656
ValueT & operator[](const size_t i)
Definition: PagedArray.h:710
ConstIterator & operator=(const ConstIterator &other)
Definition: PagedArray.h:601
const ValueT & operator[](const size_t i) const
Definition: PagedArray.h:711
static size_t log2PageSize()
Return log2 of the number of elements per memory page.
Definition: PagedArray.h:337
size_t push_back_unsafe(const ValueType &value)
Definition: PagedArray.h:193
Iterator & operator+=(const difference_type &pos)
Definition: PagedArray.h:673
size_t memUsage() const
Return the memory footprint of this array in bytes.
Definition: PagedArray.h:340
std::ptrdiff_t difference_type
Definition: PagedArray.h:593
ConstIterator end() const
Return a const iterator pointing to the past-the-last element.
Definition: PagedArray.h:390
Iterator operator+(const difference_type &pos) const
Definition: PagedArray.h:675
void sort(Functor func)
Parallel sort of all the elements based on a custom functor with the api:
Definition: PagedArray.h:405
ValueT & operator[](const difference_type &pos) const
Definition: PagedArray.h:671
ValueBuffer getBuffer()
Definition: PagedArray.h:180
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:597
ValueBuffer(PagedArray &parent)
Constructor from a PageArray.
Definition: PagedArray.h:545
static Ptr create()
Return a shared pointer to a new instance of this class.
Definition: PagedArray.h:167
const ValueType & operator[](size_t i) const
Return a const-reference to the value at the specified offset.
Definition: PagedArray.h:229
Page()
Definition: PagedArray.h:706
Iterator operator++(int)
Definition: PagedArray.h:666
bool isEmpty() const
Return true if the container contains no elements.
Definition: PagedArray.h:346
void sort()
Parallel sort of all the elements in ascending order.
Definition: PagedArray.h:394
static size_t pageSize()
Return the number of elements per memory page.
Definition: PagedArray.h:334
Iterator & operator-=(const difference_type &pos)
Definition: PagedArray.h:674
std::random_access_iterator_tag iterator_category
Definition: PagedArray.h:647
void print(std::ostream &os=std::cout) const
Print information for debugging.
Definition: PagedArray.h:418
Iterator operator-(const difference_type &pos) const
Definition: PagedArray.h:676
difference_type operator-(const ConstIterator &other) const
Definition: PagedArray.h:621
std::shared_ptr< T > SharedPtr
Definition: Types.h:114
Definition: PagedArray.h:698
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
size_t size() const
Return the number of elements in this array.
Definition: PagedArray.h:320
ValueT ValueType
Definition: PagedArray.h:153
Definition: PagedArray.h:539
void flush()
Manually transfers the values in this buffer to the parent PagedArray.
Definition: PagedArray.h:567
ValueT * data()
Definition: PagedArray.h:716
ConstIterator begin() const
Return a const iterator pointing to the first element.
Definition: PagedArray.h:380
void resize(size_t size)
Resize this array to the specified size.
Definition: PagedArray.h:288
ValueBuffer(const ValueBuffer &other)
Definition: PagedArray.h:548
SharedPtr< PagedArray > Ptr
Definition: PagedArray.h:154
bool operator==(const ConstIterator &other) const
Definition: PagedArray.h:624
Definition: Exceptions.h:13
bool operator<(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:175
std::ptrdiff_t difference_type
Definition: PagedArray.h:649
Concurrent, page-based, dynamically-sized linear data structure with O(1) random access and STL-compl...
Definition: PagedArray.h:143
bool isValid() const
Definition: PagedArray.h:687
ValueT * mData
Definition: PagedArray.h:724
ValueT value_type
Definition: PagedArray.h:648
Iterator begin()
Return a non-const iterator pointing to the first element.
Definition: PagedArray.h:368
ValueT value_type
Definition: PagedArray.h:592
ValueType & operator[](size_t i)
Return a reference to the value at the specified offset.
Definition: PagedArray.h:216
ConstIterator cbegin() const
Return a const iterator pointing to the first element.
Definition: PagedArray.h:379
ValueT & reference
Definition: PagedArray.h:651
bool isValid() const
Definition: PagedArray.h:631
size_t pos() const
Definition: PagedArray.h:632
ConstIterator & operator+=(const difference_type &pos)
Definition: PagedArray.h:617
const ValueT & operator[](const difference_type &pos) const
Definition: PagedArray.h:615
bool operator>=(const ConstIterator &other) const
Definition: PagedArray.h:626
size_t pageCount() const
Return the number of allocated memory pages.
Definition: PagedArray.h:331
ConstIterator operator++(int)
Definition: PagedArray.h:610
Definition: PagedArray.h:644
friend Iterator operator+(const difference_type &pos, const Iterator &other)
Definition: PagedArray.h:678
ConstIterator()
Definition: PagedArray.h:598
void copy(ValueType *p) const
Definition: PagedArray.h:272
Iterator end()
Return a non-const iterator pointing to the past-the-last element.
Definition: PagedArray.h:375
bool operator==(const Iterator &other) const
Definition: PagedArray.h:680
const ValueT * operator->() const
Definition: PagedArray.h:614
Iterator operator--(int)
Definition: PagedArray.h:667
ConstIterator operator--(int)
Definition: PagedArray.h:611
Definition: PagedArray.h:588
ValueT * pointer
Definition: PagedArray.h:594
PagedArrayType & parent() const
Return a reference to the parent PagedArray.
Definition: PagedArray.h:573
ConstIterator operator+(const difference_type &pos) const
Definition: PagedArray.h:619
ConstIterator & operator--()
Definition: PagedArray.h:608
ValueT & operator*() const
Definition: PagedArray.h:669
size_t size() const
Return the current number of elements cached in this buffer.
Definition: PagedArray.h:575
Iterator()
Definition: PagedArray.h:654
const ValueT & operator*() const
Definition: PagedArray.h:613
void resize(size_t size, const ValueType &v)
Resize this array to the specified size and initialize all values to v.
Definition: PagedArray.h:313
bool operator<=(const Iterator &other) const
Definition: PagedArray.h:683
std::random_access_iterator_tag iterator_category
Definition: PagedArray.h:591
void copy(ValueType *dst, size_t n) const
Definition: PagedArray.h:719
ConstIterator(const ConstIterator &other)
Definition: PagedArray.h:600
void invSort()
Parallel sort of all the elements in descending order.
Definition: PagedArray.h:397
friend ConstIterator operator+(const difference_type &pos, const ConstIterator &other)
Definition: PagedArray.h:622
ValueT * operator->() const
Definition: PagedArray.h:670
difference_type operator-(const Iterator &other) const
Definition: PagedArray.h:677
Iterator & operator++()
Definition: PagedArray.h:663
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
bool operator!=(const Iterator &other) const
Definition: PagedArray.h:681
ConstIterator(const PagedArray &parent, size_t pos=0)
Definition: PagedArray.h:599
void fill(const ValueType &v)
Set all elements in the page table to the specified value.
Definition: PagedArray.h:240
ConstIterator & operator++()
Definition: PagedArray.h:607
ConstIterator operator-(const difference_type &pos) const
Definition: PagedArray.h:620
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218
ValueT & reference
Definition: PagedArray.h:595
static size_t memUsage()
Definition: PagedArray.h:704