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"
40#include "pxr/base/tf/preprocessorUtilsLite.h"
52PXR_NAMESPACE_OPEN_SCOPE
56class 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;
227template<
typename ELEM>
233 typedef ELEM value_type;
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)
328 explicit VtArray(
size_t n, value_type
const &value)
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())) {
440 value_type *newData = _AllocateCopy(
441 _data, _CapacityForSize(curSize + 1), curSize);
442 ::new (
static_cast<void*
>(newData + curSize)) value_type(
443 std::forward<Args>(args)...);
448 ::new (
static_cast<void*
>(_data + curSize)) value_type(
449 std::forward<Args>(args)...);
452 ++_shapeData.totalSize;
477 if (ARCH_UNLIKELY(_shapeData.otherDims[0])) {
481 _DetachIfNotUnique();
483 (_data +
size() - 1)->~value_type();
485 --_shapeData.totalSize;
489 size_t size()
const {
return _shapeData.totalSize; }
501 return ARCH_UNLIKELY(_foreignSource) ?
size() : _GetCapacity(_data);
510 return (std::numeric_limits<size_t>::max() -
sizeof(_ControlBlock))
511 /
sizeof(value_type);
524 value_type *newData =
525 _data ? _AllocateCopy(_data, num,
size()) : _AllocateNew(num);
559 return resize(newSize, value_type());
564 void resize(
size_t newSize, value_type
const &value) {
567 std::uninitialized_fill(b, e, value);
573 void resize(
size_t newSize, value_type &value) {
574 return resize(newSize,
const_cast<value_type
const &
>(value));
579 void resize(
size_t newSize, value_type &&value) {
580 return resize(newSize,
const_cast<value_type
const &
>(value));
587 template <
class FillElemsFn>
588 void resize(
size_t newSize, FillElemsFn &&fillElems) {
589 const size_t oldSize =
size();
590 if (oldSize == newSize) {
598 const bool growing = newSize > oldSize;
599 value_type *newData = _data;
603 newData = _AllocateNew(newSize);
604 std::forward<FillElemsFn>(fillElems)(newData, newData + newSize);
606 else if (_IsUnique()) {
608 if (newSize > _GetCapacity(_data)) {
609 newData = _AllocateCopy(_data, newSize, oldSize);
612 std::forward<FillElemsFn>(fillElems)(newData + oldSize,
617 for (
auto *cur = newData + newSize,
618 *
end = newData + oldSize; cur !=
end; ++cur) {
625 _AllocateCopy(_data, newSize, growing ? oldSize : newSize);
628 std::forward<FillElemsFn>(fillElems)(newData + oldSize,
634 if (newData != _data) {
639 _shapeData.totalSize = newSize;
648 for (value_type *p = _data, *e = _data +
size(); p != e; ++p) {
656 _shapeData.totalSize = 0;
673 return erase(pos, pos+1);
693 return std::next(
begin(), std::distance(
cbegin(), last));
702 value_type* removeStart = std::next(_data, std::distance(
cbegin(), first));
703 value_type* removeEnd = std::next(_data, std::distance(
cbegin(), last));
704 value_type* endIt = std::next(_data,
size());
705 size_t newSize =
size() - std::distance(first, last);
709 value_type* deleteIt = std::move(removeEnd, endIt, removeStart);
710 for (; deleteIt != endIt; ++deleteIt) {
711 deleteIt->~value_type();
713 _shapeData.totalSize = newSize;
720 value_type* newData = _AllocateNew(newSize);
721 value_type* newMiddle = std::uninitialized_copy(
722 _data, removeStart, newData);
723 value_type* newEnd = std::uninitialized_copy(
724 removeEnd, endIt, newMiddle);
727 std::distance(_data, removeStart));
730 _shapeData.totalSize = newSize;
741 template <
class ForwardIter>
742 typename std::enable_if<!std::is_integral<ForwardIter>::value>::type
743 assign(ForwardIter first, ForwardIter last) {
746 std::uninitialized_copy(first, last, b);
748 ForwardIter
const &first, &last;
751 resize(std::distance(first, last), _Copier { first, last });
760 void assign(
size_t n,
const value_type &fill) {
763 std::uninitialized_fill(b, e, fill);
765 const value_type &fill;
768 resize(n, _Filler { fill });
776 void assign(std::initializer_list<ELEM> initializerList) {
777 assign(initializerList.begin(), initializerList.end());
782 std::swap(_data, other._data);
783 std::swap(_shapeData, other._shapeData);
784 std::swap(_foreignSource, other._foreignSource);
791 return data()[index];
796 return data()[index];
803 _data == other._data &&
804 _shapeData == other._shapeData &&
805 _foreignSource == other._foreignSource;
811 (*_GetShapeData() == *other._GetShapeData() &&
817 return !(*
this == other);
822 Vt_ShapeData
const *_GetShapeData()
const {
825 Vt_ShapeData *_GetShapeData() {
833 void operator()(std::ostream &out)
const {
834 VtStreamOut(*_p++, out);
843 VtArray::_Streamer streamer(self.
cdata());
844 VtStreamOutArray(out, self._GetShapeData(), streamer);
853 void _DetachIfNotUnique() {
857 _DetachCopyHook(__ARCH_PRETTY_FUNCTION__);
858 auto *newData = _AllocateCopy(_data,
size(),
size());
863 inline bool _IsUnique()
const {
865 (ARCH_LIKELY(!_foreignSource) && _GetNativeRefCount(_data) == 1);
868 inline size_t _CapacityForSize(
size_t sz)
const {
877 value_type *_AllocateNew(
size_t capacity) {
884 ?
sizeof(_ControlBlock) +
capacity *
sizeof(value_type)
885 : std::numeric_limits<size_t>::max();
886 void *
data = ::operator
new(numBytes);
890 return reinterpret_cast<value_type *
>(
891 static_cast<_ControlBlock *
>(
data) + 1);
894 value_type *_AllocateCopy(value_type *src,
size_t newCapacity,
897 value_type *newData = _AllocateNew(newCapacity);
898 std::uninitialized_copy(src, src + numToCopy, newData);
905 if (ARCH_LIKELY(!_foreignSource)) {
907 if (_GetNativeRefCount(_data).fetch_sub(
908 1, std::memory_order_release) == 1) {
909 std::atomic_thread_fence(std::memory_order_acquire);
910 for (value_type *p = _data, *e = _data + _shapeData.totalSize;
914 ::operator
delete(
static_cast<void *
>(
915 std::addressof(_GetControlBlock(_data))));
921 if (_foreignSource->_refCount.fetch_sub(
922 1, std::memory_order_release) == 1) {
923 std::atomic_thread_fence(std::memory_order_acquire);
924 _foreignSource->_ArraysDetached();
927 _foreignSource =
nullptr;
936#define VT_ARRAY_EXTERN_TMPL(unused, elem) \
937 VT_API_TEMPLATE_CLASS(VtArray< VT_TYPE(elem) >);
938TF_PP_SEQ_FOR_EACH(VT_ARRAY_EXTERN_TMPL, ~, VT_SCALAR_VALUE_TYPES)
940template <
class HashState,
class ELEM>
941inline std::enable_if_t<VtIsHashable<ELEM>()>
944 h.Append(array.
size());
945 h.AppendContiguous(array.
cdata(), array.
size());
949typename std::enable_if<VtIsHashable<ELEM>(),
size_t>::type
959struct Vt_ArrayOpHelp {
960 static T Add(T l, T r) {
return l + r; }
961 static T Sub(T l, T r) {
return l - r; }
962 static T Mul(T l, T r) {
return l * r; }
963 static T Div(T l, T r) {
return l / r; }
964 static T Mod(T l, T r) {
return l % r; }
968struct Vt_ArrayOpHelpScalar {
969 static T Mul(T l,
double r) {
return l * r; }
970 static T Mul(
double l, T r) {
return l * r; }
971 static T Div(T l,
double r) {
return l / r; }
972 static T Div(
double l, T r) {
return l / r; }
978struct Vt_ArrayOpHelp<bool> {
979 static bool Add(
bool l,
bool r) {
return l | r; }
980 static bool Sub(
bool l,
bool r) {
return l ^ r; }
981 static bool Mul(
bool l,
bool r) {
return l & r; }
982 static bool Div(
bool l,
bool r) {
return l; }
983 static bool Mod(
bool l,
bool r) {
return false; }
987struct Vt_ArrayOpHelpScalar<bool> {
988 static bool Mul(
bool l,
double r) {
return l && (r != 0.0); }
989 static bool Mul(
double l,
bool r) {
return (l != 0.0) && r; }
990 static bool Div(
bool l,
double r) {
return (r == 0.0) || l; }
991 static bool Div(
double l,
bool r) {
return !r || (l != 0.0); }
994#define VTOPERATOR_CPPARRAY(op, opName) \
997 operator op (VtArray<T> const &lhs, VtArray<T> const &rhs) \
999 using Op = Vt_ArrayOpHelp<T>; \
1001 if (!lhs.empty() && !rhs.empty() && lhs.size() != rhs.size()) { \
1002 TF_CODING_ERROR("Non-conforming inputs for operator %s", #op); \
1003 return VtArray<T>(); \
1006 const bool leftEmpty = lhs.size() == 0, rightEmpty = rhs.size() == 0; \
1007 VtArray<T> ret(leftEmpty ? rhs.size() : lhs.size()); \
1008 T zero = VtZero<T>(); \
1011 rhs.begin(), rhs.end(), ret.begin(), \
1012 [zero](T const &r) { return Op:: opName (zero, r); }); \
1014 else if (rightEmpty) { \
1016 lhs.begin(), lhs.end(), ret.begin(), \
1017 [zero](T const &l) { return Op:: opName (l, zero); }); \
1021 lhs.begin(), lhs.end(), rhs.begin(), ret.begin(), \
1022 [](T const &l, T const &r) { return Op:: opName (l, r); }); \
1028ARCH_PRAGMA_FORCING_TO_BOOL
1029ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
1030ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
1032VTOPERATOR_CPPARRAY(+, Add);
1033VTOPERATOR_CPPARRAY(-, Sub);
1034VTOPERATOR_CPPARRAY(*, Mul);
1035VTOPERATOR_CPPARRAY(/, Div);
1036VTOPERATOR_CPPARRAY(%, Mod);
1042 std::transform(a.
begin(), a.
end(), ret.begin(),
1043 [](T
const &x) { return -x; });
1051#define VTOPERATOR_CPPSCALAR(op,opName) \
1052 template<typename T> \
1053 VtArray<T> operator op (T const &scalar, VtArray<T> const &arr) { \
1054 using Op = Vt_ArrayOpHelp<T>; \
1055 VtArray<T> ret(arr.size()); \
1056 std::transform(arr.begin(), arr.end(), ret.begin(), \
1057 [&scalar](T const &aObj) { \
1058 return Op:: opName (scalar, aObj); \
1062 template<typename T> \
1063 VtArray<T> operator op (VtArray<T> const &arr, T const &scalar) { \
1064 using Op = Vt_ArrayOpHelp<T>; \
1065 VtArray<T> ret(arr.size()); \
1066 std::transform(arr.begin(), arr.end(), ret.begin(), \
1067 [&scalar](T const &aObj) { \
1068 return Op:: opName (aObj, scalar); \
1076#define VTOPERATOR_CPPSCALAR_DOUBLE(op,opName) \
1077 template<typename T> \
1078 std::enable_if_t<!std::is_same<T, double>::value, VtArray<T>> \
1079 operator op (double const &scalar, VtArray<T> const &arr) { \
1080 using Op = Vt_ArrayOpHelpScalar<T>; \
1081 VtArray<T> ret(arr.size()); \
1082 std::transform(arr.begin(), arr.end(), ret.begin(), \
1083 [&scalar](T const &aObj) { \
1084 return Op:: opName (scalar, aObj); \
1088 template<typename T> \
1089 std::enable_if_t<!std::is_same<T, double>::value, VtArray<T>> \
1090 operator op (VtArray<T> const &arr, double const &scalar) { \
1091 using Op = Vt_ArrayOpHelpScalar<T>; \
1092 VtArray<T> ret(arr.size()); \
1093 std::transform(arr.begin(), arr.end(), ret.begin(), \
1094 [&scalar](T const &aObj) { \
1095 return Op:: opName (aObj, scalar); \
1102ARCH_PRAGMA_FORCING_TO_BOOL
1103ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
1104ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
1105VTOPERATOR_CPPSCALAR(+, Add)
1106VTOPERATOR_CPPSCALAR(-, Sub)
1107VTOPERATOR_CPPSCALAR(*, Mul)
1108VTOPERATOR_CPPSCALAR_DOUBLE(*, Mul)
1109VTOPERATOR_CPPSCALAR(/, Div)
1110VTOPERATOR_CPPSCALAR_DOUBLE(/, Div)
1111VTOPERATOR_CPPSCALAR(%, Mod)
1114PXR_NAMESPACE_CLOSE_SCOPE
Low-level utilities for informing users of various internal and external diagnostic conditions.
Defines all the types "TYPED" for which Vt creates a VtTYPEDArray typedef.
A user-extensible hashing mechanism for use with runtime hash tables.
Represents an arbitrary dimensional rectangular container class.
VtArray(Vt_ArrayForeignDataSource *foreignSrc, ElementType *data, size_t size, bool addRef=true)
Create an array with foreign source.
ELEM ElementType
Type this array holds.
VtArray const & AsConst() const noexcept
Return *this as a const reference.
VtArray & operator=(VtArray &&other)
Move assign from other.
VtArray & operator=(std::initializer_list< ELEM > initializerList)
Replace current array contents with those in initializerList.
friend void swap(VtArray &lhs, VtArray &rhs)
Swap array contents.
bool IsIdentical(VtArray const &other) const
Tests if two arrays are identical, i.e.
friend std::ostream & operator<<(std::ostream &out, const VtArray &self)
Outputs a comma-separated list of the values in the array.
bool operator!=(VtArray const &other) const
Tests two arrays for inequality.
VtArray(std::initializer_list< ELEM > initializerList)
Initialize array from the contents of a initializerList.
VtArray(VtArray const &other)
Copy other. The new array shares underlying data with other.
VtArray(size_t n)
Create an array filled with n value-initialized elements.
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.
ElementType & operator[](size_t index)
Allows usage of [i].
VtArray()
Create an empty array.
VtArray(VtArray &&other)
Move from other.
ElementType const & operator[](size_t index) const
Allows usage of [i].
VtArray & operator=(VtArray const &other)
Copy assign from other.
bool operator==(VtArray const &other) const
Tests two arrays for equality. See also IsIdentical().
VtArray(size_t n, value_type const &value)
Create an array filled with n copies of value.
Define preprocessor function name macros.
void resize(size_t newSize, value_type &value)
Resize this array.
void assign(std::initializer_list< ELEM > initializerList)
Assign array contents via intializer list Equivalent to:
void pop_back()
Remove the last element of an array.
ElementType & reference
Reference type.
const_reverse_iterator rend() const
Return a const reverse iterator to the start of the array.
const_reference front() const
Return a const reference to the first element in this array.
const_pointer cdata() const
Return a const pointer to the data held by this array.
ElementType * iterator
Iterator type.
size_t size() const
Return the total number of elements in this array.
const_iterator begin() const
Return a const iterator to the start of the array.
iterator erase(const_iterator pos)
Removes a single element at pos from the array.
constexpr size_t max_size() const
Return a theoretical maximum size limit for the container.
const_pointer data() const
Return a const pointer to this array's data.
pointer data()
Return a non-const pointer to this array's data.
void reserve(size_t num)
Ensure enough memory is allocated to hold num elements.
const_iterator cbegin() const
Return a const iterator to the start of the array.
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse const iterator type.
void swap(VtArray &other)
Swap the contents of this array with other.
void push_back(ElementType const &element)
Appends an element at the end of the array.
ElementType * pointer
Pointer type.
ElementType const * const_iterator
Const iterator type.
void assign(size_t n, const value_type &fill)
Assign array contents.
const_reference back() const
Return a const reference to the last element in this array.
ElementType const & const_reference
Const reference type.
bool empty() const
Return true if this array contains no elements, false otherwise.
reverse_iterator rend()
Return a reverse iterator to the start of the array.
reference front()
Return a non-const reference to the first element in this array.
std::enable_if<!std::is_integral< ForwardIter >::value >::type assign(ForwardIter first, ForwardIter last)
Assign array contents.
const_reference cback() const
Return a const reference to the last element in this array.
size_t capacity() const
Return the number of items this container can grow to hold without triggering a (re)allocation.
void resize(size_t newSize)
Resize this array.
void resize(size_t newSize, value_type &&value)
Resize this array.
const_reverse_iterator crbegin() const
Return a const reverse iterator to the end of the array.
ElementType const * const_pointer
Const pointer type.
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator type.
const_iterator cend() const
Return a const iterator to the end of the array.
iterator erase(const_iterator first, const_iterator last)
Remove a range of elements [first, last) from the array.
void resize(size_t newSize, value_type const &value)
Resize this array.
const_reverse_iterator crend() const
Return a const reverse iterator to the start of the array.
void emplace_back(Args &&... args)
Initializes a new element at the end of the array.
void clear()
Equivalent to resize(0).
iterator end()
Returns a non-const iterator to the end of the array.
const_iterator end() const
Return a const iterator to the end of the array.
reverse_iterator rbegin()
Return a non-const reverse iterator to the end of the array.
void push_back(ElementType &&element)
Appends an element at the end of the array.
iterator begin()
Return a non-const iterator to the start of the array.
void resize(size_t newSize, FillElemsFn &&fillElems)
Resize this array.
const_reference cfront() const
Return a const reference to the first element in this array.
const_reverse_iterator rbegin() const
Return a const reverse iterator to the end of the array.
reference back()
Return a reference to the last element in this array.
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
size_t hash_value(const half h)
Overload hash_value for half.
Pragmas for controlling compiler-specific behaviors.
Array concept. By default, types are not arrays.