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> 50 #include <type_traits> 52 PXR_NAMESPACE_OPEN_SCOPE
56 class Vt_ArrayForeignDataSource
59 explicit Vt_ArrayForeignDataSource(
60 void (*detachedFn)(Vt_ArrayForeignDataSource *
self) =
nullptr,
61 size_t initRefCount = 0)
62 : _refCount(initRefCount)
63 , _detachedFn(detachedFn) {}
66 template <
class T>
friend class VtArray;
68 void _ArraysDetached() {
if (_detachedFn) { _detachedFn(
this); } }
70 std::atomic<size_t> _refCount;
71 void (*_detachedFn)(Vt_ArrayForeignDataSource *
self);
78 Vt_ArrayBase() : _shapeData { 0 }, _foreignSource(
nullptr) {}
80 Vt_ArrayBase(Vt_ArrayForeignDataSource *foreignSrc)
81 : _shapeData { 0 }, _foreignSource(foreignSrc) {}
83 Vt_ArrayBase(Vt_ArrayBase
const &other) =
default;
84 Vt_ArrayBase(Vt_ArrayBase &&other) : Vt_ArrayBase(other) {
85 other._shapeData.clear();
86 other._foreignSource =
nullptr;
89 Vt_ArrayBase &operator=(Vt_ArrayBase
const &other) =
default;
90 Vt_ArrayBase &operator=(Vt_ArrayBase &&other) {
94 other._shapeData.clear();
95 other._foreignSource =
nullptr;
104 struct _ControlBlock {
105 _ControlBlock() : nativeRefCount(0), capacity(0) {}
106 _ControlBlock(
size_t initCount,
size_t initCap)
107 : nativeRefCount(initCount), capacity(initCap) {}
108 mutable std::atomic<size_t> nativeRefCount;
112 _ControlBlock &_GetControlBlock(
void *nativeData) {
114 return *(reinterpret_cast<_ControlBlock *>(nativeData) - 1);
117 _ControlBlock
const &_GetControlBlock(
void *nativeData)
const {
119 return *(reinterpret_cast<_ControlBlock *>(nativeData) - 1);
123 std::atomic<size_t> &_GetNativeRefCount(
void *nativeData)
const {
124 return _GetControlBlock(nativeData).nativeRefCount;
127 size_t &_GetCapacity(
void *nativeData) {
128 return _GetControlBlock(nativeData).capacity;
130 size_t const &_GetCapacity(
void *nativeData)
const {
131 return _GetControlBlock(nativeData).capacity;
134 VT_API
void _DetachCopyHook(
char const *funcName)
const;
136 Vt_ShapeData _shapeData;
137 Vt_ArrayForeignDataSource *_foreignSource;
227 template<
typename ELEM>
274 template <
typename LegacyInputIterator>
275 VtArray(LegacyInputIterator first, LegacyInputIterator last,
276 typename std::enable_if<
277 !std::is_integral<LegacyInputIterator>::value,
278 void>::type* =
nullptr)
284 VtArray(Vt_ArrayForeignDataSource *foreignSrc,
286 : Vt_ArrayBase(foreignSrc)
289 foreignSrc->_refCount.fetch_add(1, std::memory_order_relaxed);
291 _shapeData.totalSize =
size;
296 , _data(other._data) {
300 if (ARCH_LIKELY(!_foreignSource)) {
301 _GetNativeRefCount(_data).fetch_add(1, std::memory_order_relaxed);
304 _foreignSource->_refCount.fetch_add(1, std::memory_order_relaxed);
311 , _data(other._data) {
312 other._data =
nullptr;
316 VtArray(std::initializer_list<ELEM> initializerList)
349 static_cast<Vt_ArrayBase &>(*
this) = std::move(other);
351 other._data =
nullptr;
357 this->
assign(initializerList.begin(), initializerList.end());
429 template <
typename... Args>
432 if (ARCH_UNLIKELY(_shapeData.otherDims[0])) {
437 size_t curSize =
size();
439 _foreignSource || !_IsUnique() || curSize ==
capacity())) {
441 _data, _CapacityForSize(curSize + 1), curSize);
446 ::new (static_cast<void*>(_data + curSize))
value_type(
447 std::forward<Args>(args)...);
449 ++_shapeData.totalSize;
474 if (ARCH_UNLIKELY(_shapeData.otherDims[0])) {
478 _DetachIfNotUnique();
482 --_shapeData.totalSize;
486 size_t size()
const {
return _shapeData.totalSize; }
498 return ARCH_UNLIKELY(_foreignSource) ?
size() : _GetCapacity(_data);
512 _data ? _AllocateCopy(_data, num,
size()) : _AllocateNew(num);
551 return resize(newSize, _Filler());
558 template <
class FillElemsFn>
559 void resize(
size_t newSize, FillElemsFn &&fillElems) {
560 const size_t oldSize =
size();
561 if (oldSize == newSize) {
569 const bool growing = newSize > oldSize;
574 newData = _AllocateNew(newSize);
575 std::forward<FillElemsFn>(fillElems)(newData, newData + newSize);
577 else if (_IsUnique()) {
579 if (newSize > _GetCapacity(_data)) {
580 newData = _AllocateCopy(_data, newSize, oldSize);
583 std::forward<FillElemsFn>(fillElems)(newData + oldSize,
588 for (
auto *cur = newData + newSize,
589 *
end = newData + oldSize; cur !=
end; ++cur) {
596 _AllocateCopy(_data, newSize, growing ? oldSize : newSize);
599 std::forward<FillElemsFn>(fillElems)(newData + oldSize,
605 if (newData != _data) {
610 _shapeData.totalSize = newSize;
627 _shapeData.totalSize = 0;
644 return erase(pos, pos+1);
664 return std::next(
begin(), std::distance(
cbegin(), last));
676 size_t newSize =
size() - std::distance(first, last);
680 value_type* deleteIt = std::move(removeEnd, endIt, removeStart);
681 for (; deleteIt != endIt; ++deleteIt) {
682 deleteIt->~value_type();
684 _shapeData.totalSize = newSize;
692 value_type* newMiddle = std::uninitialized_copy(
693 _data, removeStart, newData);
695 removeEnd, endIt, newMiddle);
698 std::distance(_data, removeStart));
701 _shapeData.totalSize = newSize;
712 template <
class ForwardIter>
713 typename std::enable_if<!std::is_integral<ForwardIter>::value>::type
714 assign(ForwardIter first, ForwardIter last) {
717 std::uninitialized_copy(first, last, b);
719 ForwardIter
const &first, &last;
722 resize(std::distance(first, last), _Copier { first, last });
734 std::uninitialized_fill(b, e, fill);
739 resize(n, _Filler { fill });
747 void assign(std::initializer_list<ELEM> initializerList) {
748 assign(initializerList.begin(), initializerList.end());
755 std::swap(_foreignSource, other._foreignSource);
762 return data()[index];
767 return data()[index];
774 _data == other._data &&
775 _shapeData == other._shapeData &&
776 _foreignSource == other._foreignSource;
782 (*_GetShapeData() == *other._GetShapeData() &&
788 return !(*
this == other);
793 Vt_ShapeData
const *_GetShapeData()
const {
796 Vt_ShapeData *_GetShapeData() {
801 class _Streamer :
public VtStreamOutIterator {
804 virtual ~_Streamer() { }
805 virtual void Next(std::ostream &out)
807 VtStreamOut(*_p++, out);
816 VtArray::_Streamer streamer(
self.
cdata());
817 VtStreamOutArray(&streamer,
self.
size(),
self._GetShapeData(), out);
826 void _DetachIfNotUnique() {
830 _DetachCopyHook(__ARCH_PRETTY_FUNCTION__);
831 auto *newData = _AllocateCopy(_data,
size(),
size());
836 inline bool _IsUnique()
const {
838 (ARCH_LIKELY(!_foreignSource) && _GetNativeRefCount(_data) == 1);
841 inline size_t _CapacityForSize(
size_t sz)
const {
850 value_type *_AllocateNew(
size_t capacity) {
854 sizeof(_ControlBlock) +
capacity *
sizeof(value_type));
858 return reinterpret_cast<value_type *>(
859 static_cast<_ControlBlock *>(
data) + 1);
862 value_type *_AllocateCopy(value_type *src,
size_t newCapacity,
865 value_type *newData = _AllocateNew(newCapacity);
866 std::uninitialized_copy(src, src + numToCopy, newData);
873 if (ARCH_LIKELY(!_foreignSource)) {
875 if (_GetNativeRefCount(_data).fetch_sub(
876 1, std::memory_order_release) == 1) {
877 std::atomic_thread_fence(std::memory_order_acquire);
878 for (value_type *p = _data, *e = _data + _shapeData.totalSize;
882 free(std::addressof(_GetControlBlock(_data)));
888 if (_foreignSource->_refCount.fetch_sub(
889 1, std::memory_order_release) == 1) {
890 std::atomic_thread_fence(std::memory_order_acquire);
891 _foreignSource->_ArraysDetached();
894 _foreignSource =
nullptr;
903 #define VT_ARRAY_EXTERN_TMPL(r, unused, elem) \ 904 extern template class VtArray< VT_TYPE(elem) >; 905 BOOST_PP_SEQ_FOR_EACH(VT_ARRAY_EXTERN_TMPL, ~, VT_SCALAR_VALUE_TYPES)
907 template <
class ELEM>
908 typename std::enable_if<VtIsHashable<ELEM>(),
size_t>::type
910 size_t h = array.
size();
911 for (
auto const &x: array) {
912 boost::hash_combine(h, x);
918 template <
typename T>
922 #define VTOPERATOR_CPPARRAY(op) \ 925 operator op (VtArray<T> const &lhs, VtArray<T> const &rhs) \ 928 if (!lhs.empty() && !rhs.empty() && lhs.size() != rhs.size()) { \ 929 TF_CODING_ERROR("Non-conforming inputs for operator %s", #op); \ 930 return VtArray<T>(); \ 933 const bool leftEmpty = lhs.size() == 0, rightEmpty = rhs.size() == 0; \ 934 VtArray<T> ret(leftEmpty ? rhs.size() : lhs.size()); \ 935 T zero = VtZero<T>(); \ 937 std::transform(rhs.begin(), rhs.end(), ret.begin(), \ 938 [zero](T const &r) { return T(zero op r); }); \ 940 else if (rightEmpty) { \ 941 std::transform(lhs.begin(), lhs.end(), ret.begin(), \ 942 [zero](T const &l) { return T(l op zero); }); \ 945 std::transform(lhs.begin(), lhs.end(), rhs.begin(), ret.begin(), \ 946 [](T const &l, T const &r) { return T(l op r); }); \ 952 ARCH_PRAGMA_FORCING_TO_BOOL
953 ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
954 ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
956 VTOPERATOR_CPPARRAY(+);
957 VTOPERATOR_CPPARRAY(-);
958 VTOPERATOR_CPPARRAY(*);
959 VTOPERATOR_CPPARRAY(/);
960 VTOPERATOR_CPPARRAY(%);
966 std::transform(a.
begin(), a.
end(), ret.begin(),
967 [](T
const &x) {
return -x; });
975 #define VTOPERATOR_CPPSCALAR_TYPE(op,arraytype,scalartype,rettype) \ 976 template<typename arraytype> \ 978 operator op (scalartype const &scalar, \ 979 VtArray<arraytype> const &vec) { \ 980 VtArray<rettype> ret(vec.size()); \ 981 for (size_t i = 0; i<vec.size(); ++i) { \ 982 ret[i] = scalar op vec[i]; \ 986 template<typename arraytype> \ 988 operator op (VtArray<arraytype> const &vec, \ 989 scalartype const &scalar) { \ 990 VtArray<rettype> ret(vec.size()); \ 991 for (size_t i = 0; i<vec.size(); ++i) { \ 992 ret[i] = vec[i] op scalar; \ 997 #define VTOPERATOR_CPPSCALAR(op) \ 998 VTOPERATOR_CPPSCALAR_TYPE(op,ElemType,ElemType,ElemType) 1003 #define VTOPERATOR_CPPSCALAR_DOUBLE(op) \ 1004 template<typename ElemType> \ 1005 typename boost::disable_if<boost::is_same<ElemType, double>, \ 1006 VtArray<ElemType> >::type \ 1007 operator op (double const &scalar, \ 1008 VtArray<ElemType> const &vec) { \ 1009 VtArray<ElemType> ret(vec.size()); \ 1010 for (size_t i = 0; i<vec.size(); ++i) { \ 1011 ret[i] = scalar op vec[i]; \ 1015 template<typename ElemType> \ 1016 typename boost::disable_if<boost::is_same<ElemType, double>, \ 1017 VtArray<ElemType> >::type \ 1018 operator op (VtArray<ElemType> const &vec, \ 1019 double const &scalar) { \ 1020 VtArray<ElemType> ret(vec.size()); \ 1021 for (size_t i = 0; i<vec.size(); ++i) { \ 1022 ret[i] = vec[i] op scalar; \ 1029 ARCH_PRAGMA_FORCING_TO_BOOL
1030 ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
1031 ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
1032 VTOPERATOR_CPPSCALAR(+)
1033 VTOPERATOR_CPPSCALAR(-)
1034 VTOPERATOR_CPPSCALAR(*)
1035 VTOPERATOR_CPPSCALAR_DOUBLE(*)
1036 VTOPERATOR_CPPSCALAR(/)
1037 VTOPERATOR_CPPSCALAR_DOUBLE(/)
1038 VTOPERATOR_CPPSCALAR(%)
1041 PXR_NAMESPACE_CLOSE_SCOPE
1043 #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.
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.