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/streamOut.h" 41 #include <boost/functional/hash.hpp> 42 #include <boost/iterator_adaptors.hpp> 43 #include <boost/iterator/reverse_iterator.hpp> 52 #include <type_traits> 54 PXR_NAMESPACE_OPEN_SCOPE
58 class Vt_ArrayForeignDataSource
61 explicit Vt_ArrayForeignDataSource(
62 void (*detachedFn)(Vt_ArrayForeignDataSource *
self) =
nullptr,
63 size_t initRefCount = 0)
64 : _refCount(initRefCount)
65 , _detachedFn(detachedFn) {}
68 template <
class T>
friend class VtArray;
70 void _ArraysDetached() {
if (_detachedFn) { _detachedFn(
this); } }
72 std::atomic<size_t> _refCount;
73 void (*_detachedFn)(Vt_ArrayForeignDataSource *
self);
80 Vt_ArrayBase() : _shapeData { 0 }, _foreignSource(
nullptr) {}
82 Vt_ArrayBase(Vt_ArrayForeignDataSource *foreignSrc)
83 : _shapeData { 0 }, _foreignSource(foreignSrc) {}
85 Vt_ArrayBase(Vt_ArrayBase
const &other) =
default;
86 Vt_ArrayBase(Vt_ArrayBase &&other) : Vt_ArrayBase(other) {
87 other._shapeData.clear();
88 other._foreignSource =
nullptr;
91 Vt_ArrayBase &operator=(Vt_ArrayBase
const &other) =
default;
92 Vt_ArrayBase &operator=(Vt_ArrayBase &&other) {
96 other._shapeData.clear();
97 other._foreignSource =
nullptr;
106 struct _ControlBlock {
107 _ControlBlock() : nativeRefCount(0), capacity(0) {}
108 _ControlBlock(
size_t initCount,
size_t initCap)
109 : nativeRefCount(initCount), capacity(initCap) {}
110 mutable std::atomic<size_t> nativeRefCount;
114 _ControlBlock &_GetControlBlock(
void *nativeData) {
116 return *(reinterpret_cast<_ControlBlock *>(nativeData) - 1);
119 _ControlBlock
const &_GetControlBlock(
void *nativeData)
const {
121 return *(reinterpret_cast<_ControlBlock *>(nativeData) - 1);
125 std::atomic<size_t> &_GetNativeRefCount(
void *nativeData)
const {
126 return _GetControlBlock(nativeData).nativeRefCount;
129 size_t &_GetCapacity(
void *nativeData) {
130 return _GetControlBlock(nativeData).capacity;
132 size_t const &_GetCapacity(
void *nativeData)
const {
133 return _GetControlBlock(nativeData).capacity;
136 VT_API
void _DetachCopyHook(
char const *funcName)
const;
138 Vt_ShapeData _shapeData;
139 Vt_ArrayForeignDataSource *_foreignSource;
229 template<
typename ELEM>
276 template <
typename LegacyInputIterator>
277 VtArray(LegacyInputIterator first, LegacyInputIterator last,
278 typename std::enable_if<
279 !std::is_integral<LegacyInputIterator>::value,
280 void>::type* =
nullptr)
286 VtArray(Vt_ArrayForeignDataSource *foreignSrc,
288 : Vt_ArrayBase(foreignSrc)
291 foreignSrc->_refCount.fetch_add(1, std::memory_order_relaxed);
293 _shapeData.totalSize =
size;
298 , _data(other._data) {
302 if (ARCH_LIKELY(!_foreignSource)) {
303 _GetNativeRefCount(_data).fetch_add(1, std::memory_order_relaxed);
306 _foreignSource->_refCount.fetch_add(1, std::memory_order_relaxed);
313 , _data(other._data) {
314 other._data =
nullptr;
318 VtArray(std::initializer_list<ELEM> initializerList)
351 static_cast<Vt_ArrayBase &>(*
this) = std::move(other);
353 other._data =
nullptr;
359 this->
assign(initializerList.begin(), initializerList.end());
431 template <
typename... Args>
434 if (ARCH_UNLIKELY(_shapeData.otherDims[0])) {
439 size_t curSize =
size();
441 _foreignSource || !_IsUnique() || curSize ==
capacity())) {
443 _data, _CapacityForSize(curSize + 1), curSize);
448 ::new (static_cast<void*>(_data + curSize))
value_type(
449 std::forward<Args>(args)...);
451 ++_shapeData.totalSize;
476 if (ARCH_UNLIKELY(_shapeData.otherDims[0])) {
480 _DetachIfNotUnique();
484 --_shapeData.totalSize;
488 size_t size()
const {
return _shapeData.totalSize; }
500 return ARCH_UNLIKELY(_foreignSource) ?
size() : _GetCapacity(_data);
509 return (std::numeric_limits<size_t>::max() -
sizeof(_ControlBlock))
524 _data ? _AllocateCopy(_data, num,
size()) : _AllocateNew(num);
563 return resize(newSize, _Filler());
570 template <
class FillElemsFn>
571 void resize(
size_t newSize, FillElemsFn &&fillElems) {
572 const size_t oldSize =
size();
573 if (oldSize == newSize) {
581 const bool growing = newSize > oldSize;
586 newData = _AllocateNew(newSize);
587 std::forward<FillElemsFn>(fillElems)(newData, newData + newSize);
589 else if (_IsUnique()) {
591 if (newSize > _GetCapacity(_data)) {
592 newData = _AllocateCopy(_data, newSize, oldSize);
595 std::forward<FillElemsFn>(fillElems)(newData + oldSize,
600 for (
auto *cur = newData + newSize,
601 *
end = newData + oldSize; cur !=
end; ++cur) {
608 _AllocateCopy(_data, newSize, growing ? oldSize : newSize);
611 std::forward<FillElemsFn>(fillElems)(newData + oldSize,
617 if (newData != _data) {
622 _shapeData.totalSize = newSize;
639 _shapeData.totalSize = 0;
656 return erase(pos, pos+1);
676 return std::next(
begin(), std::distance(
cbegin(), last));
688 size_t newSize =
size() - std::distance(first, last);
692 value_type* deleteIt = std::move(removeEnd, endIt, removeStart);
693 for (; deleteIt != endIt; ++deleteIt) {
694 deleteIt->~value_type();
696 _shapeData.totalSize = newSize;
704 value_type* newMiddle = std::uninitialized_copy(
705 _data, removeStart, newData);
707 removeEnd, endIt, newMiddle);
710 std::distance(_data, removeStart));
713 _shapeData.totalSize = newSize;
724 template <
class ForwardIter>
725 typename std::enable_if<!std::is_integral<ForwardIter>::value>::type
726 assign(ForwardIter first, ForwardIter last) {
729 std::uninitialized_copy(first, last, b);
731 ForwardIter
const &first, &last;
734 resize(std::distance(first, last), _Copier { first, last });
746 std::uninitialized_fill(b, e, fill);
751 resize(n, _Filler { fill });
759 void assign(std::initializer_list<ELEM> initializerList) {
760 assign(initializerList.begin(), initializerList.end());
767 std::swap(_foreignSource, other._foreignSource);
774 return data()[index];
779 return data()[index];
786 _data == other._data &&
787 _shapeData == other._shapeData &&
788 _foreignSource == other._foreignSource;
794 (*_GetShapeData() == *other._GetShapeData() &&
800 return !(*
this == other);
805 Vt_ShapeData
const *_GetShapeData()
const {
808 Vt_ShapeData *_GetShapeData() {
813 class _Streamer :
public VtStreamOutIterator {
816 virtual ~_Streamer() { }
817 virtual void Next(std::ostream &out)
819 VtStreamOut(*_p++, out);
828 VtArray::_Streamer streamer(
self.
cdata());
829 VtStreamOutArray(&streamer,
self.
size(),
self._GetShapeData(), out);
838 void _DetachIfNotUnique() {
842 _DetachCopyHook(__ARCH_PRETTY_FUNCTION__);
843 auto *newData = _AllocateCopy(_data,
size(),
size());
848 inline bool _IsUnique()
const {
850 (ARCH_LIKELY(!_foreignSource) && _GetNativeRefCount(_data) == 1);
853 inline size_t _CapacityForSize(
size_t sz)
const {
862 value_type *_AllocateNew(
size_t capacity) {
869 ?
sizeof(_ControlBlock) +
capacity *
sizeof(value_type)
870 : std::numeric_limits<size_t>::max();
871 void *
data = ::operator
new(numBytes);
875 return reinterpret_cast<value_type *>(
876 static_cast<_ControlBlock *>(
data) + 1);
879 value_type *_AllocateCopy(value_type *src,
size_t newCapacity,
882 value_type *newData = _AllocateNew(newCapacity);
883 std::uninitialized_copy(src, src + numToCopy, newData);
890 if (ARCH_LIKELY(!_foreignSource)) {
892 if (_GetNativeRefCount(_data).fetch_sub(
893 1, std::memory_order_release) == 1) {
894 std::atomic_thread_fence(std::memory_order_acquire);
895 for (value_type *p = _data, *e = _data + _shapeData.totalSize;
899 ::operator
delete(static_cast<void *>(
900 std::addressof(_GetControlBlock(_data))));
906 if (_foreignSource->_refCount.fetch_sub(
907 1, std::memory_order_release) == 1) {
908 std::atomic_thread_fence(std::memory_order_acquire);
909 _foreignSource->_ArraysDetached();
912 _foreignSource =
nullptr;
921 #define VT_ARRAY_EXTERN_TMPL(r, unused, elem) \ 922 extern template class VtArray< VT_TYPE(elem) >; 923 BOOST_PP_SEQ_FOR_EACH(VT_ARRAY_EXTERN_TMPL, ~, VT_SCALAR_VALUE_TYPES)
925 template <
class ELEM>
926 typename std::enable_if<VtIsHashable<ELEM>(),
size_t>::type
928 size_t h = array.
size();
929 for (
auto const &x: array) {
930 boost::hash_combine(h, x);
936 template <
typename T>
940 #define VTOPERATOR_CPPARRAY(op) \ 943 operator op (VtArray<T> const &lhs, VtArray<T> const &rhs) \ 946 if (!lhs.empty() && !rhs.empty() && lhs.size() != rhs.size()) { \ 947 TF_CODING_ERROR("Non-conforming inputs for operator %s", #op); \ 948 return VtArray<T>(); \ 951 const bool leftEmpty = lhs.size() == 0, rightEmpty = rhs.size() == 0; \ 952 VtArray<T> ret(leftEmpty ? rhs.size() : lhs.size()); \ 953 T zero = VtZero<T>(); \ 955 std::transform(rhs.begin(), rhs.end(), ret.begin(), \ 956 [zero](T const &r) { return T(zero op r); }); \ 958 else if (rightEmpty) { \ 959 std::transform(lhs.begin(), lhs.end(), ret.begin(), \ 960 [zero](T const &l) { return T(l op zero); }); \ 963 std::transform(lhs.begin(), lhs.end(), rhs.begin(), ret.begin(), \ 964 [](T const &l, T const &r) { return T(l op r); }); \ 970 ARCH_PRAGMA_FORCING_TO_BOOL
971 ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
972 ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
974 VTOPERATOR_CPPARRAY(+);
975 VTOPERATOR_CPPARRAY(-);
976 VTOPERATOR_CPPARRAY(*);
977 VTOPERATOR_CPPARRAY(/);
978 VTOPERATOR_CPPARRAY(%);
984 std::transform(a.
begin(), a.
end(), ret.begin(),
985 [](T
const &x) {
return -x; });
993 #define VTOPERATOR_CPPSCALAR_TYPE(op,arraytype,scalartype,rettype) \ 994 template<typename arraytype> \ 996 operator op (scalartype const &scalar, \ 997 VtArray<arraytype> const &vec) { \ 998 VtArray<rettype> ret(vec.size()); \ 999 for (size_t i = 0; i<vec.size(); ++i) { \ 1000 ret[i] = scalar op vec[i]; \ 1004 template<typename arraytype> \ 1006 operator op (VtArray<arraytype> const &vec, \ 1007 scalartype const &scalar) { \ 1008 VtArray<rettype> ret(vec.size()); \ 1009 for (size_t i = 0; i<vec.size(); ++i) { \ 1010 ret[i] = vec[i] op scalar; \ 1015 #define VTOPERATOR_CPPSCALAR(op) \ 1016 VTOPERATOR_CPPSCALAR_TYPE(op,ElemType,ElemType,ElemType) 1021 #define VTOPERATOR_CPPSCALAR_DOUBLE(op) \ 1022 template<typename ElemType> \ 1023 typename boost::disable_if<boost::is_same<ElemType, double>, \ 1024 VtArray<ElemType> >::type \ 1025 operator op (double const &scalar, \ 1026 VtArray<ElemType> const &vec) { \ 1027 VtArray<ElemType> ret(vec.size()); \ 1028 for (size_t i = 0; i<vec.size(); ++i) { \ 1029 ret[i] = scalar op vec[i]; \ 1033 template<typename ElemType> \ 1034 typename boost::disable_if<boost::is_same<ElemType, double>, \ 1035 VtArray<ElemType> >::type \ 1036 operator op (VtArray<ElemType> const &vec, \ 1037 double const &scalar) { \ 1038 VtArray<ElemType> ret(vec.size()); \ 1039 for (size_t i = 0; i<vec.size(); ++i) { \ 1040 ret[i] = vec[i] op scalar; \ 1047 ARCH_PRAGMA_FORCING_TO_BOOL
1048 ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
1049 ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
1050 VTOPERATOR_CPPSCALAR(+)
1051 VTOPERATOR_CPPSCALAR(-)
1052 VTOPERATOR_CPPSCALAR(*)
1053 VTOPERATOR_CPPSCALAR_DOUBLE(*)
1054 VTOPERATOR_CPPSCALAR(/)
1055 VTOPERATOR_CPPSCALAR_DOUBLE(/)
1056 VTOPERATOR_CPPSCALAR(%)
1059 PXR_NAMESPACE_CLOSE_SCOPE
1061 #endif // PXR_BASE_VT_ARRAY_H #define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
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 begin() const
Return a const iterator to the start of the array.
size_t capacity() const
Return the number of items this container can grow to hold without triggering a (re)allocation.
friend std::ostream & operator<<(std::ostream &out, const VtArray &self)
Outputs a comma-separated list of the values in the array.
const_reference cback() 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.
constexpr size_t max_size() const
Return a theoretical maximum size limit for the container.
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.
size_t size() const
Return the total number of elements in this array.
Define preprocessor function name macros.
iterator begin()
Return a non-const iterator to the start of the array.
ElementType const * const_pointer
Const pointer type.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Basic type for a vector of 3 float components.
const_reference cfront() const
Return a const reference to the first element in this array.
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.
VtArray()
Create an empty array.
const_iterator end() const
Return a const iterator to the end of the array.
VtArray & operator=(VtArray const &other)
Copy assign from other.
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 IsIdentical(VtArray const &other) const
Tests if two arrays are identical, i.e.
ElementType const & const_reference
Const reference type.
void push_back(ElementType const &element)
Appends an element at the end of the array.
const_iterator cend() const
Return a const iterator to the end of the array.
void pop_back()
Remove the last element of an array.
ElementType & reference
Reference type.
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
const_reverse_iterator crend() const
Return a const reverse iterator to the start of the array.
const_reverse_iterator crbegin() const
Return a const reverse iterator to the end of the array.
const_iterator cbegin() const
Return a const iterator to the start of the array.
std::enable_if<!std::is_integral< ForwardIter >::value >::type assign(ForwardIter first, ForwardIter last)
Assign array contents.
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.
VtArray const & AsConst() const noexcept
Return *this as a const reference.
const_reverse_iterator rbegin() const
Return a const reverse iterator to the end of the 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.
Defines all the types "TYPED" for which Vt creates a VtTYPEDArray typedef.
ElementType * pointer
Pointer type.
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.
std::enable_if< std::is_same< Half, half >::value, size_t >::type hash_value(const Half &h)
Overload hash_value for half.
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.
ElementType const * const_iterator
Const iterator type.
const_reverse_iterator rend() const
Return a const reverse iterator to the start of the array.
bool empty() const
Return true if this array contains no elements, false otherwise.
VtArray(std::initializer_list< ELEM > initializerList)
Initialize array from the contents of a initializerList.
const_pointer cdata() const
Return a const pointer to the data held by this array.
ElementType * iterator
Iterator type.
const_reference front() const
Return a const reference to the first element in this array.
const_pointer data() const
Return a const pointer to this array's data.
void assign(size_t n, const value_type &fill)
Assign array contents.
ELEM ElementType
Type this array holds.
ElementType const & operator[](size_t index) const
Allows usage of [i].
const_reference back() const
Return a const reference to the last element in this array.
VtArray(VtArray const &other)
Copy other. The new array shares underlying data with other.
void emplace_back(Args &&... args)
Initializes a new element at the end of the array.
bool operator==(VtArray const &other) const
Tests two arrays for equality. See also IsIdentical().
void clear()
Equivalent to resize(0).
void assign(std::initializer_list< ELEM > initializerList)
Assign array contents via intializer list Equivalent to:
bool operator !=(VtArray const &other) const
Tests two arrays for inequality.