24 #ifndef PXR_BASE_VT_ARRAY_H
25 #define PXR_BASE_VT_ARRAY_H
30 #include "pxr/base/vt/api.h"
31 #include "pxr/base/vt/hash.h"
32 #include "pxr/base/vt/operators.h"
33 #include "pxr/base/vt/streamOut.h"
42 #include <boost/functional/hash.hpp>
43 #include <boost/iterator_adaptors.hpp>
44 #include <boost/iterator/reverse_iterator.hpp>
51 #include <type_traits>
53 PXR_NAMESPACE_OPEN_SCOPE
57 class Vt_ArrayForeignDataSource
60 explicit Vt_ArrayForeignDataSource(
61 void (*detachedFn)(Vt_ArrayForeignDataSource *
self) =
nullptr,
62 size_t initRefCount = 0)
63 : _refCount(initRefCount)
64 , _detachedFn(detachedFn) {}
67 template <
class T>
friend class VtArray;
69 void _ArraysDetached() {
if (_detachedFn) { _detachedFn(
this); } }
71 std::atomic<size_t> _refCount;
72 void (*_detachedFn)(Vt_ArrayForeignDataSource *
self);
79 Vt_ArrayBase() : _shapeData { 0 }, _foreignSource(
nullptr) {}
81 Vt_ArrayBase(Vt_ArrayForeignDataSource *foreignSrc)
82 : _shapeData { 0 }, _foreignSource(foreignSrc) {}
84 Vt_ArrayBase(Vt_ArrayBase
const &other) =
default;
85 Vt_ArrayBase(Vt_ArrayBase &&other) : Vt_ArrayBase(other) {
86 other._shapeData.clear();
87 other._foreignSource =
nullptr;
90 Vt_ArrayBase &operator=(Vt_ArrayBase
const &other) =
default;
91 Vt_ArrayBase &operator=(Vt_ArrayBase &&other) {
95 other._shapeData.clear();
96 other._foreignSource =
nullptr;
105 struct _ControlBlock {
106 _ControlBlock() : nativeRefCount(0), capacity(0) {}
107 _ControlBlock(
size_t initCount,
size_t initCap)
108 : nativeRefCount(initCount), capacity(initCap) {}
109 mutable std::atomic<size_t> nativeRefCount;
113 _ControlBlock &_GetControlBlock(
void *nativeData) {
115 return *(
reinterpret_cast<_ControlBlock *
>(nativeData) - 1);
118 _ControlBlock
const &_GetControlBlock(
void *nativeData)
const {
120 return *(
reinterpret_cast<_ControlBlock *
>(nativeData) - 1);
124 std::atomic<size_t> &_GetNativeRefCount(
void *nativeData)
const {
125 return _GetControlBlock(nativeData).nativeRefCount;
128 size_t &_GetCapacity(
void *nativeData) {
129 return _GetControlBlock(nativeData).capacity;
131 size_t const &_GetCapacity(
void *nativeData)
const {
132 return _GetControlBlock(nativeData).capacity;
135 VT_API
void _DetachCopyHook(
char const *funcName)
const;
137 Vt_ShapeData _shapeData;
138 Vt_ArrayForeignDataSource *_foreignSource;
228 template<
typename ELEM>
236 template <
typename Value>
237 class PointerIterator
238 :
public boost::iterator_adaptor<PointerIterator<Value>, Value *> {
241 PointerIterator::iterator_adaptor_(0) {}
242 explicit PointerIterator(Value *p) :
243 PointerIterator::iterator_adaptor_(p) {}
244 template <
typename OtherValue>
245 PointerIterator(PointerIterator<OtherValue>
const &other,
246 typename boost::enable_if_convertible
247 <OtherValue*, Value*>::type* = 0) :
248 PointerIterator::iterator_adaptor_(other.base()) {}
250 friend class boost::iterator_core_access;
266 typedef typename PointerIterator<ElementType>::reference
269 typedef typename PointerIterator<const ElementType>::reference
272 typedef typename PointerIterator<ElementType>::pointer
pointer;
293 template <
typename LegacyInputIterator>
294 VtArray(LegacyInputIterator first, LegacyInputIterator last,
295 typename std::enable_if<
296 !std::is_integral<LegacyInputIterator>::value,
297 void>::type* =
nullptr)
303 VtArray(Vt_ArrayForeignDataSource *foreignSrc,
305 : Vt_ArrayBase(foreignSrc)
308 foreignSrc->_refCount.fetch_add(1, std::memory_order_relaxed);
310 _shapeData.totalSize =
size;
315 , _data(other._data) {
319 if (ARCH_LIKELY(!_foreignSource)) {
320 _GetNativeRefCount(_data).fetch_add(1, std::memory_order_relaxed);
323 _foreignSource->_refCount.fetch_add(1, std::memory_order_relaxed);
330 , _data(other._data) {
331 other._data =
nullptr;
335 VtArray(std::initializer_list<ELEM> initializerList)
368 static_cast<Vt_ArrayBase &
>(*this) = std::move(other);
370 other._data =
nullptr;
376 this->
assign(initializerList.begin(), initializerList.end());
448 template <
typename... Args>
451 if (ARCH_UNLIKELY(_shapeData.otherDims[0])) {
456 size_t curSize =
size();
458 _foreignSource || !_IsUnique() || curSize ==
capacity())) {
460 _data, _CapacityForSize(curSize + 1), curSize);
465 ::new (static_cast<void*>(_data + curSize))
value_type(
466 std::forward<Args>(args)...);
468 ++_shapeData.totalSize;
493 if (ARCH_UNLIKELY(_shapeData.otherDims[0])) {
497 _DetachIfNotUnique();
501 --_shapeData.totalSize;
505 size_t size()
const {
return _shapeData.totalSize; }
517 return ARCH_UNLIKELY(_foreignSource) ?
size() : _GetCapacity(_data);
531 _data ? _AllocateCopy(_data, num,
size()) : _AllocateNew(num);
570 return resize(newSize, _Filler());
577 template <
class FillElemsFn>
578 void resize(
size_t newSize, FillElemsFn &&fillElems) {
579 const size_t oldSize =
size();
580 if (oldSize == newSize) {
588 const bool growing = newSize > oldSize;
593 newData = _AllocateNew(newSize);
594 std::forward<FillElemsFn>(fillElems)(newData, newData + newSize);
596 else if (_IsUnique()) {
598 if (newSize > _GetCapacity(_data)) {
599 newData = _AllocateCopy(_data, newSize, oldSize);
602 std::forward<FillElemsFn>(fillElems)(newData + oldSize,
607 for (
auto *cur = newData + newSize,
608 *
end = newData + oldSize; cur !=
end; ++cur) {
615 _AllocateCopy(_data, newSize, growing ? oldSize : newSize);
618 std::forward<FillElemsFn>(fillElems)(newData + oldSize,
624 if (newData != _data) {
629 _shapeData.totalSize = newSize;
646 _shapeData.totalSize = 0;
663 return erase(pos, pos+1);
683 return std::next(
begin(), std::distance(
cbegin(), last));
695 size_t newSize =
size() - std::distance(first, last);
699 value_type* deleteIt = std::move(removeEnd, endIt, removeStart);
700 for (; deleteIt != endIt; ++deleteIt) {
701 deleteIt->~value_type();
703 _shapeData.totalSize = newSize;
711 value_type* newMiddle = std::uninitialized_copy(
712 _data, removeStart, newData);
714 removeEnd, endIt, newMiddle);
717 std::distance(_data, removeStart));
720 _shapeData.totalSize = newSize;
731 template <
class ForwardIter>
732 typename std::enable_if<!std::is_integral<ForwardIter>::value>::type
733 assign(ForwardIter first, ForwardIter last) {
736 std::uninitialized_copy(first, last, b);
738 ForwardIter
const &first, &last;
741 resize(std::distance(first, last), _Copier { first, last });
753 std::uninitialized_fill(b, e, fill);
758 resize(n, _Filler { fill });
766 void assign(std::initializer_list<ELEM> initializerList) {
767 assign(initializerList.begin(), initializerList.end());
774 std::swap(_foreignSource, other._foreignSource);
781 return data()[index];
786 return data()[index];
793 _data == other._data &&
794 _shapeData == other._shapeData &&
795 _foreignSource == other._foreignSource;
801 (*_GetShapeData() == *other._GetShapeData() &&
807 return !(*
this == other);
811 ARCH_PRAGMA_FORCING_TO_BOOL
812 ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
813 ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
814 VTOPERATOR_CPPARRAY(+)
815 VTOPERATOR_CPPARRAY(-)
816 VTOPERATOR_CPPARRAY(*)
817 VTOPERATOR_CPPARRAY(/)
818 VTOPERATOR_CPPARRAY(%)
819 VTOPERATOR_CPPARRAY_UNARY(-)
824 Vt_ShapeData const *_GetShapeData()
const {
827 Vt_ShapeData *_GetShapeData() {
832 class _Streamer :
public VtStreamOutIterator {
835 virtual ~_Streamer() { }
836 virtual void Next(std::ostream &out)
838 VtStreamOut(*_p++, out);
847 VtArray::_Streamer streamer(
self.
cdata());
848 VtStreamOutArray(&streamer,
self.
size(),
self._GetShapeData(), out);
857 void _DetachIfNotUnique() {
861 _DetachCopyHook(__ARCH_PRETTY_FUNCTION__);
862 auto *newData = _AllocateCopy(_data,
size(),
size());
867 inline bool _IsUnique()
const {
869 (ARCH_LIKELY(!_foreignSource) && _GetNativeRefCount(_data) == 1);
872 inline size_t _CapacityForSize(
size_t sz)
const {
881 value_type *_AllocateNew(
size_t capacity) {
885 sizeof(_ControlBlock) + capacity *
sizeof(value_type));
887 ::new (data) _ControlBlock(1, capacity);
889 return reinterpret_cast<value_type *>(
890 static_cast<_ControlBlock *>(data) + 1);
893 value_type *_AllocateCopy(value_type *src,
size_t newCapacity,
896 value_type *newData = _AllocateNew(newCapacity);
897 std::uninitialized_copy(src, src + numToCopy, newData);
904 if (ARCH_LIKELY(!_foreignSource)) {
906 if (_GetNativeRefCount(_data).fetch_sub(
907 1, std::memory_order_release) == 1) {
908 std::atomic_thread_fence(std::memory_order_acquire);
909 for (value_type *p = _data, *e = _data + _shapeData.totalSize;
913 free(std::addressof(_GetControlBlock(_data)));
919 if (_foreignSource->_refCount.fetch_sub(
920 1, std::memory_order_release) == 1) {
921 std::atomic_thread_fence(std::memory_order_acquire);
922 _foreignSource->_ArraysDetached();
925 _foreignSource =
nullptr;
932 template <
class ELEM>
933 typename std::enable_if<VtIsHashable<ELEM>(),
size_t>::type
935 size_t h = array.
size();
936 for (
auto const &x: array) {
937 boost::hash_combine(h, x);
943 template <
typename T>
948 ARCH_PRAGMA_FORCING_TO_BOOL
949 ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
950 ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
951 VTOPERATOR_CPPSCALAR(+)
952 VTOPERATOR_CPPSCALAR(-)
953 VTOPERATOR_CPPSCALAR(*)
954 VTOPERATOR_CPPSCALAR_DOUBLE(*)
955 VTOPERATOR_CPPSCALAR(/)
956 VTOPERATOR_CPPSCALAR_DOUBLE(/)
957 VTOPERATOR_CPPSCALAR(%)
960 PXR_NAMESPACE_CLOSE_SCOPE
962 #endif // PXR_BASE_VT_ARRAY_H
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
bool operator!=(VtArray const &other) const
Tests two arrays for inequality.
void resize(size_t newSize, FillElemsFn &&fillElems)
Resize this array.
ElementType & operator[](size_t index)
Allows usage of [i].
Pragmas for controlling compiler-specific behaviors.
void reserve(size_t num)
Ensure enough memory is allocated to hold num elements.
const_iterator end() const
Return a const iterator to the end of the array.
PointerIterator< const ElementType > const_iterator
Const iterator type.
friend std::ostream & operator<<(std::ostream &out, const VtArray &self)
Outputs a comma-separated list of the values in the array.
const_reverse_iterator rend() const
Return a const reverse iterator to the start of the array.
const_reference back() const
Return a const reference to the last element in this array.
VtArray(Vt_ArrayForeignDataSource *foreignSrc, ElementType *data, size_t size, bool addRef=true)
Create an array with foreign source.
VtArray & operator=(VtArray &&other)
Move assign from other.
const_reverse_iterator crend() const
Return a const reverse iterator to the start of the array.
VtArray & operator=(std::initializer_list< ELEM > initializerList)
Replace current array contents with those in initializerList.
reverse_iterator rend()
Return a reverse iterator to the start of the array.
Define preprocessor function name macros.
iterator begin()
Return a non-const iterator to the start of the array.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Basic type for a vector of 3 float components.
PointerIterator< const ElementType >::pointer const_pointer
Const pointer type.
Low-level utilities for informing users of various internal and external diagnostic conditions...
VtArray(LegacyInputIterator first, LegacyInputIterator last, typename std::enable_if< !std::is_integral< LegacyInputIterator >::value, void >::type *=nullptr)
Create an array from a pair of iterators.
const_reference front() const
Return a const reference to the first element in this array.
VtArray()
Create an empty array.
const_iterator cend() const
Return a const iterator to the end of the array.
VtArray & operator=(VtArray const &other)
Copy assign from other.
const_iterator begin() const
Return a const iterator to the start of the array.
void resize(size_t newSize)
Resize this array.
VtArray(size_t n, value_type const &value)
Create an array filled with n copies of value.
boost::reverse_iterator< iterator > reverse_iterator
Reverse iterator type.
iterator end()
Returns a non-const iterator to the end of the array.
bool operator==(VtArray const &other) const
Tests two arrays for equality. See also IsIdentical().
void push_back(ElementType const &element)
Appends an element at the end of the array.
const_pointer cdata() const
Return a const pointer to the data held by this array.
void pop_back()
Remove the last element of an array.
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
const_pointer data() const
Return a const pointer to this array's data.
PointerIterator< const ElementType >::reference const_reference
Const reference type.
PointerIterator< ElementType >::reference reference
Reference type.
void emplace_back(Args &&...args)
Initializes a new element at the end of the array.
std::enable_if<!std::is_integral< ForwardIter >::value >::type assign(ForwardIter first, ForwardIter last)
Assign array contents.
const_reference cfront() const
Return a const reference to the first element in this array.
const_iterator cbegin() const
Return a const iterator to the start of the array.
void push_back(ElementType &&element)
Appends an element at the end of the array.
pointer data()
Return a non-const pointer to this array's data.
PointerIterator< ElementType > iterator
Iterator type.
VtArray const & AsConst() const noexcept
Return *this as a const reference.
size_t size() const
Return the total number of elements in this array.
Represents an arbitrary dimensional rectangular container class.
reference back()
Return a reference to the last element in this array.
friend void swap(VtArray &lhs, VtArray &rhs)
Swap array contents.
reverse_iterator rbegin()
Return a non-const reverse iterator to the end of the array.
PointerIterator< ElementType >::pointer pointer
Pointer type.
bool IsIdentical(VtArray const &other) const
Tests if two arrays are identical, i.e.
Defines all the types "TYPED" for which Vt creates a VtTYPEDArray typedef.
Array concept. By default, types are not arrays.
iterator erase(const_iterator pos)
Removes a single element at pos from the array.
VtArray(VtArray &&other)
Move from other.
void swap(VtArray &other)
Swap the contents of this array with other.
const_reference cback() const
Return a const reference to the last element in this array.
VtArray(size_t n)
Create an array filled with n value-initialized elements.
reference front()
Return a non-const reference to the first element in this array.
boost::reverse_iterator< const_iterator > const_reverse_iterator
Reverse const iterator type.
iterator erase(const_iterator first, const_iterator last)
Remove a range of elements [first, last) from the array.
VtArray(std::initializer_list< ELEM > initializerList)
Initialize array from the contents of a initializerList.
size_t capacity() const
Return the number of items this container can grow to hold without triggering a (re)allocation.
void assign(size_t n, const value_type &fill)
Assign array contents.
const_reverse_iterator rbegin() const
Return a const reverse iterator to the end of the array.
ELEM ElementType
Type this array holds.
bool empty() const
Return true if this array contains no elements, false otherwise.
VtArray(VtArray const &other)
Copy other. The new array shares underlying data with other.
void clear()
Equivalent to resize(0).
const_reverse_iterator crbegin() const
Return a const reverse iterator to the end of the array.
void assign(std::initializer_list< ELEM > initializerList)
Assign array contents via intializer list Equivalent to:
ElementType const & operator[](size_t index) const
Allows usage of [i].