15 #ifndef OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED
16 #define OPENVDB_UTIL_PAGED_ARRAY_HAS_BEEN_INCLUDED
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() );
200 (*mPageTable[index >> Log2PageSize])[index] = value;
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);
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);
266 mPageTable[last_page]->copy(p+last_page*
Page::Size, m);
291 if (size > mCapacity) {
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>
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() );
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);
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>
503 assert(size == Page::Size);
504 if (mSize & Page::Mask) {
505 Page*& tmp = mPageTable.back();
508 mPageTable.push_back(page);
509 mCapacity += Page::Size;
514 template <
typename ValueT,
size_t Log2PageSize>
515 void PagedArray<ValueT, Log2PageSize>::add_partially_full(Page*& page,
size_t size)
517 assert(size > 0 && size < Page::Size);
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;
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])) {}
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
void copy(ValueType *p) const
void parallel_for(int64_t start, int64_t end, std::function< void(int64_t index)> &&task, parallel_options opt=parallel_options(0, Split_Y, 1))
ConstIterator cbegin() const
Return a const iterator pointing to the first element.
ValueBuffer(const ValueBuffer &other)
size_t capacity() const
Return the maximum number of elements that this array can contain without allocating more memory page...
ValueBuffer(PagedArray &parent)
Constructor from a PageArray.
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
Iterator(const Iterator &other)
const ValueT & operator[](const size_t i) const
Iterator end()
Return a non-const iterator pointing to the past-the-last element.
ValueT & operator[](const difference_type &pos) const
const ValueT & operator[](const difference_type &pos) const
PagedArrayType & parent() const
Return a reference to the parent PagedArray.
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...
ConstIterator & operator-=(const difference_type &pos)
#define OPENVDB_USE_VERSION_NAMESPACE
std::random_access_iterator_tag iterator_category
friend ConstIterator operator+(const difference_type &pos, const ConstIterator &other)
ConstIterator cend() const
Return a const iterator pointing to the past-the-last element.
std::ptrdiff_t difference_type
bool operator<=(const Iterator &other) const
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
ConstIterator & operator+=(const difference_type &pos)
bool operator<=(const ConstIterator &other) const
PagedArray()
Default constructor.
size_t push_back_unsafe(const ValueType &value)
bool operator==(const Iterator &other) const
static Ptr create()
Return a shared pointer to a new instance of this class.
void sort()
Parallel sort of all the elements in ascending order.
size_t size() const
Return the number of elements in this array.
ValueType & operator[](size_t i)
Return a reference to the value at the specified offset.
void sort(Functor func)
Parallel sort of all the elements based on a custom functor with the api:
Iterator & operator+=(const difference_type &pos)
Iterator(PagedArray &parent, size_t pos=0)
const ValueType & operator[](size_t i) const
Return a const-reference to the value at the specified offset.
std::shared_ptr< T > SharedPtr
ConstIterator & operator=(const ConstIterator &other)
Iterator & operator-=(const difference_type &pos)
size_t size() const
Return the current number of elements cached in this buffer.
bool isEmpty() const
Return true if the container contains no elements.
OIIO_FORCEINLINE vbool4 operator>(const vint4 &a, const vint4 &b)
static size_t log2PageSize()
Return log2 of the number of elements per memory page.
Iterator & operator=(const Iterator &other)
void flush()
Manually transfers the values in this buffer to the parent PagedArray.
void resize(size_t size)
Resize this array to the specified size.
bool operator>=(const ConstIterator &other) const
bool operator!=(const Iterator &other) const
ConstIterator(const ConstIterator &other)
void fill(const ValueT &v)
bool operator<(const GU_TetrahedronFacet &a, const GU_TetrahedronFacet &b)
IMATH_HOSTDEVICE constexpr Color4< T > operator*(S a, const Color4< T > &v) IMATH_NOEXCEPT
Reverse multiplication: S * Color4.
void copy(ValueType *dst, size_t n) const
bool operator!=(const ConstIterator &other) const
PagedArray & operator=(const PagedArray &)=delete
friend Iterator operator+(const difference_type &pos, const Iterator &other)
Iterator operator+(const difference_type &pos) const
void clear()
Removes all elements from the array and delete all pages.
ConstIterator operator-(const difference_type &pos) const
bool isPartiallyFull() const
Return true if the page table is partially full, i.e. the last non-empty page contains less than page...
void shrink_to_fit()
Reduce the page table to fix the current size.
ConstIterator operator+(const difference_type &pos) const
SharedPtr< PagedArray > Ptr
const ValueT & operator*() const
difference_type operator-(const Iterator &other) const
Library and file format version numbers.
ValueT & operator[](const size_t i)
void fill(const ValueType &v)
Set all elements in the page table to the specified value.
ConstIterator operator++(int)
const ValueT * operator->() const
void push_back(const ValueT &v)
Add a value to the buffer and increment the size.
size_t freeCount() const
Return the number of additional elements that can be added to this array without allocating more memo...
ConstIterator end() const
Return a const iterator pointing to the past-the-last element.
void print(std::ostream &os=std::cout) const
Print information for debugging.
void invSort()
Parallel sort of all the elements in descending order.
ConstIterator & operator++()
static size_t pageSize()
Return the number of elements per memory page.
std::random_access_iterator_tag iterator_category
ValueT & operator*() const
~ValueBuffer()
Destructor that transfers an buffered values to the parent PagedArray.
ValueT * operator->() const
bool operator>=(const Iterator &other) const
std::ptrdiff_t difference_type
size_t memUsage() const
Return the memory footprint of this array in bytes.
ConstIterator(const PagedArray &parent, size_t pos=0)
Iterator operator-(const difference_type &pos) const
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
bool operator==(const ConstIterator &other) const
ConstIterator begin() const
Return a const iterator pointing to the first element.
difference_type operator-(const ConstIterator &other) const
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Concurrent, page-based, dynamically-sized linear data structure with O(1) random access and STL-compl...
ConstIterator operator--(int)
Iterator begin()
Return a non-const iterator pointing to the first element.
~PagedArray()
Destructor removed all allocated pages.
void merge(PagedArray &other)
Transfer all the elements (and pages) from the other array to this array.
size_t pageCount() const
Return the number of allocated memory pages.
void resize(size_t size, const ValueType &v)
Resize this array to the specified size and initialize all values to v.
ConstIterator & operator--()