7#ifndef PXR_BASE_VT_ARRAY_H
8#define PXR_BASE_VT_ARRAY_H
13#include "pxr/base/vt/api.h"
14#include "pxr/base/vt/hash.h"
15#include "pxr/base/vt/streamOut.h"
23#include "pxr/base/tf/preprocessorUtilsLite.h"
35PXR_NAMESPACE_OPEN_SCOPE
39class Vt_ArrayForeignDataSource
42 explicit Vt_ArrayForeignDataSource(
43 void (*detachedFn)(Vt_ArrayForeignDataSource *self) =
nullptr,
44 size_t initRefCount = 0)
45 : _refCount(initRefCount)
46 , _detachedFn(detachedFn) {}
49 template <
class T>
friend class VtArray;
51 void _ArraysDetached() {
if (_detachedFn) { _detachedFn(
this); } }
53 std::atomic<size_t> _refCount;
54 void (*_detachedFn)(Vt_ArrayForeignDataSource *self);
61 Vt_ArrayBase() : _shapeData { 0 }, _foreignSource(nullptr) {}
63 Vt_ArrayBase(Vt_ArrayForeignDataSource *foreignSrc)
64 : _shapeData { 0 }, _foreignSource(foreignSrc) {}
66 Vt_ArrayBase(Vt_ArrayBase
const &other) =
default;
67 Vt_ArrayBase(Vt_ArrayBase &&other) : Vt_ArrayBase(other) {
68 other._shapeData.clear();
69 other._foreignSource =
nullptr;
72 Vt_ArrayBase &operator=(Vt_ArrayBase
const &other) =
default;
73 Vt_ArrayBase &operator=(Vt_ArrayBase &&other) {
77 other._shapeData.clear();
78 other._foreignSource =
nullptr;
87 struct _ControlBlock {
88 _ControlBlock() : nativeRefCount(0), capacity(0) {}
89 _ControlBlock(
size_t initCount,
size_t initCap)
90 : nativeRefCount(initCount), capacity(initCap) {}
91 mutable std::atomic<size_t> nativeRefCount;
95 _ControlBlock &_GetControlBlock(
void *nativeData) {
97 return *(
reinterpret_cast<_ControlBlock *
>(nativeData) - 1);
100 _ControlBlock
const &_GetControlBlock(
void *nativeData)
const {
102 return *(
reinterpret_cast<_ControlBlock *
>(nativeData) - 1);
106 std::atomic<size_t> &_GetNativeRefCount(
void *nativeData)
const {
107 return _GetControlBlock(nativeData).nativeRefCount;
110 size_t &_GetCapacity(
void *nativeData) {
111 return _GetControlBlock(nativeData).capacity;
113 size_t const &_GetCapacity(
void *nativeData)
const {
114 return _GetControlBlock(nativeData).capacity;
117 VT_API
void _DetachCopyHook(
char const *funcName)
const;
119 Vt_ShapeData _shapeData;
120 Vt_ArrayForeignDataSource *_foreignSource;
210template<
typename ELEM>
216 typedef ELEM value_type;
257 template <
typename LegacyInputIterator>
258 VtArray(LegacyInputIterator first, LegacyInputIterator last,
259 typename std::enable_if<
260 !std::is_integral<LegacyInputIterator>::value,
261 void>::type* =
nullptr)
267 VtArray(Vt_ArrayForeignDataSource *foreignSrc,
269 : Vt_ArrayBase(foreignSrc)
272 foreignSrc->_refCount.fetch_add(1, std::memory_order_relaxed);
274 _shapeData.totalSize =
size;
279 , _data(other._data) {
283 if (ARCH_LIKELY(!_foreignSource)) {
284 _GetNativeRefCount(_data).fetch_add(1, std::memory_order_relaxed);
287 _foreignSource->_refCount.fetch_add(1, std::memory_order_relaxed);
294 , _data(other._data) {
295 other._data =
nullptr;
299 VtArray(std::initializer_list<ELEM> initializerList)
311 explicit VtArray(
size_t n, value_type
const &value)
332 static_cast<Vt_ArrayBase &
>(*this) = std::move(other);
334 other._data =
nullptr;
340 this->
assign(initializerList.begin(), initializerList.end());
412 template <
typename... Args>
415 if (ARCH_UNLIKELY(_shapeData.otherDims[0])) {
420 size_t curSize =
size();
422 _foreignSource || !_IsUnique() || curSize ==
capacity())) {
423 value_type *newData = _AllocateCopy(
424 _data, _CapacityForSize(curSize + 1), curSize);
425 ::new (
static_cast<void*
>(newData + curSize)) value_type(
426 std::forward<Args>(args)...);
431 ::new (
static_cast<void*
>(_data + curSize)) value_type(
432 std::forward<Args>(args)...);
435 ++_shapeData.totalSize;
460 if (ARCH_UNLIKELY(_shapeData.otherDims[0])) {
464 _DetachIfNotUnique();
466 (_data +
size() - 1)->~value_type();
468 --_shapeData.totalSize;
472 size_t size()
const {
return _shapeData.totalSize; }
484 return ARCH_UNLIKELY(_foreignSource) ?
size() : _GetCapacity(_data);
493 return (std::numeric_limits<size_t>::max() -
sizeof(_ControlBlock))
494 /
sizeof(value_type);
507 value_type *newData =
508 _data ? _AllocateCopy(_data, num,
size()) : _AllocateNew(num);
542 return resize(newSize, value_type());
547 void resize(
size_t newSize, value_type
const &value) {
550 std::uninitialized_fill(b, e, value);
556 void resize(
size_t newSize, value_type &value) {
557 return resize(newSize,
const_cast<value_type
const &
>(value));
562 void resize(
size_t newSize, value_type &&value) {
563 return resize(newSize,
const_cast<value_type
const &
>(value));
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;
582 value_type *newData = _data;
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;
631 for (value_type *p = _data, *e = _data +
size(); p != e; ++p) {
639 _shapeData.totalSize = 0;
656 return erase(pos, pos+1);
676 return std::next(
begin(), std::distance(
cbegin(), last));
685 value_type* removeStart = std::next(_data, std::distance(
cbegin(), first));
686 value_type* removeEnd = std::next(_data, std::distance(
cbegin(), last));
687 value_type* endIt = std::next(_data,
size());
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;
703 value_type* newData = _AllocateNew(newSize);
704 value_type* newMiddle = std::uninitialized_copy(
705 _data, removeStart, newData);
706 value_type* newEnd = std::uninitialized_copy(
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 });
743 void assign(
size_t n,
const value_type &fill) {
746 std::uninitialized_fill(b, e, fill);
748 const value_type &fill;
751 resize(n, _Filler { fill });
759 void assign(std::initializer_list<ELEM> initializerList) {
760 assign(initializerList.begin(), initializerList.end());
765 std::swap(_data, other._data);
766 std::swap(_shapeData, other._shapeData);
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() {
816 void operator()(std::ostream &out)
const {
817 VtStreamOut(*_p++, out);
826 VtArray::_Streamer streamer(self.
cdata());
827 VtStreamOutArray(out, self._GetShapeData(), streamer);
836 void _DetachIfNotUnique() {
840 _DetachCopyHook(__ARCH_PRETTY_FUNCTION__);
841 auto *newData = _AllocateCopy(_data,
size(),
size());
846 inline bool _IsUnique()
const {
848 (ARCH_LIKELY(!_foreignSource) && _GetNativeRefCount(_data) == 1);
851 inline size_t _CapacityForSize(
size_t sz)
const {
860 value_type *_AllocateNew(
size_t capacity) {
867 ?
sizeof(_ControlBlock) +
capacity *
sizeof(value_type)
868 : std::numeric_limits<size_t>::max();
869 void *
data = ::operator
new(numBytes);
873 return reinterpret_cast<value_type *
>(
874 static_cast<_ControlBlock *
>(
data) + 1);
877 value_type *_AllocateCopy(value_type *src,
size_t newCapacity,
880 value_type *newData = _AllocateNew(newCapacity);
881 std::uninitialized_copy(src, src + numToCopy, newData);
888 if (ARCH_LIKELY(!_foreignSource)) {
890 if (_GetNativeRefCount(_data).fetch_sub(
891 1, std::memory_order_release) == 1) {
892 std::atomic_thread_fence(std::memory_order_acquire);
893 for (value_type *p = _data, *e = _data + _shapeData.totalSize;
897 ::operator
delete(
static_cast<void *
>(
898 std::addressof(_GetControlBlock(_data))));
904 if (_foreignSource->_refCount.fetch_sub(
905 1, std::memory_order_release) == 1) {
906 std::atomic_thread_fence(std::memory_order_acquire);
907 _foreignSource->_ArraysDetached();
910 _foreignSource =
nullptr;
919#define VT_ARRAY_EXTERN_TMPL(unused, elem) \
920 VT_API_TEMPLATE_CLASS(VtArray< VT_TYPE(elem) >);
921TF_PP_SEQ_FOR_EACH(VT_ARRAY_EXTERN_TMPL, ~, VT_SCALAR_VALUE_TYPES)
923template <
class HashState,
class ELEM>
924inline std::enable_if_t<VtIsHashable<ELEM>()>
927 h.Append(array.
size());
928 h.AppendContiguous(array.
cdata(), array.
size());
932typename std::enable_if<VtIsHashable<ELEM>(),
size_t>::type
942struct Vt_ArrayOpHelp {
943 static T Add(T l, T r) {
return l + r; }
944 static T Sub(T l, T r) {
return l - r; }
945 static T Mul(T l, T r) {
return l * r; }
946 static T Div(T l, T r) {
return l / r; }
947 static T Mod(T l, T r) {
return l % r; }
951struct Vt_ArrayOpHelpScalar {
952 static T Mul(T l,
double r) {
return l * r; }
953 static T Mul(
double l, T r) {
return l * r; }
954 static T Div(T l,
double r) {
return l / r; }
955 static T Div(
double l, T r) {
return l / r; }
961struct Vt_ArrayOpHelp<bool> {
962 static bool Add(
bool l,
bool r) {
return l | r; }
963 static bool Sub(
bool l,
bool r) {
return l ^ r; }
964 static bool Mul(
bool l,
bool r) {
return l & r; }
965 static bool Div(
bool l,
bool r) {
return l; }
966 static bool Mod(
bool l,
bool r) {
return false; }
970struct Vt_ArrayOpHelpScalar<bool> {
971 static bool Mul(
bool l,
double r) {
return l && (r != 0.0); }
972 static bool Mul(
double l,
bool r) {
return (l != 0.0) && r; }
973 static bool Div(
bool l,
double r) {
return (r == 0.0) || l; }
974 static bool Div(
double l,
bool r) {
return !r || (l != 0.0); }
977#define VTOPERATOR_CPPARRAY(op, opName) \
980 operator op (VtArray<T> const &lhs, VtArray<T> const &rhs) \
982 using Op = Vt_ArrayOpHelp<T>; \
984 if (!lhs.empty() && !rhs.empty() && lhs.size() != rhs.size()) { \
985 TF_CODING_ERROR("Non-conforming inputs for operator %s", #op); \
986 return VtArray<T>(); \
989 const bool leftEmpty = lhs.size() == 0, rightEmpty = rhs.size() == 0; \
990 VtArray<T> ret(leftEmpty ? rhs.size() : lhs.size()); \
991 T zero = VtZero<T>(); \
994 rhs.begin(), rhs.end(), ret.begin(), \
995 [zero](T const &r) { return Op:: opName (zero, r); }); \
997 else if (rightEmpty) { \
999 lhs.begin(), lhs.end(), ret.begin(), \
1000 [zero](T const &l) { return Op:: opName (l, zero); }); \
1004 lhs.begin(), lhs.end(), rhs.begin(), ret.begin(), \
1005 [](T const &l, T const &r) { return Op:: opName (l, r); }); \
1011ARCH_PRAGMA_FORCING_TO_BOOL
1012ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
1013ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
1015VTOPERATOR_CPPARRAY(+, Add);
1016VTOPERATOR_CPPARRAY(-, Sub);
1017VTOPERATOR_CPPARRAY(*, Mul);
1018VTOPERATOR_CPPARRAY(/, Div);
1019VTOPERATOR_CPPARRAY(%, Mod);
1025 std::transform(a.
begin(), a.
end(), ret.begin(),
1026 [](T
const &x) { return -x; });
1034#define VTOPERATOR_CPPSCALAR(op,opName) \
1035 template<typename T> \
1036 VtArray<T> operator op (T const &scalar, VtArray<T> const &arr) { \
1037 using Op = Vt_ArrayOpHelp<T>; \
1038 VtArray<T> ret(arr.size()); \
1039 std::transform(arr.begin(), arr.end(), ret.begin(), \
1040 [&scalar](T const &aObj) { \
1041 return Op:: opName (scalar, aObj); \
1045 template<typename T> \
1046 VtArray<T> operator op (VtArray<T> const &arr, T const &scalar) { \
1047 using Op = Vt_ArrayOpHelp<T>; \
1048 VtArray<T> ret(arr.size()); \
1049 std::transform(arr.begin(), arr.end(), ret.begin(), \
1050 [&scalar](T const &aObj) { \
1051 return Op:: opName (aObj, scalar); \
1059#define VTOPERATOR_CPPSCALAR_DOUBLE(op,opName) \
1060 template<typename T> \
1061 std::enable_if_t<!std::is_same<T, double>::value, VtArray<T>> \
1062 operator op (double const &scalar, VtArray<T> const &arr) { \
1063 using Op = Vt_ArrayOpHelpScalar<T>; \
1064 VtArray<T> ret(arr.size()); \
1065 std::transform(arr.begin(), arr.end(), ret.begin(), \
1066 [&scalar](T const &aObj) { \
1067 return Op:: opName (scalar, aObj); \
1071 template<typename T> \
1072 std::enable_if_t<!std::is_same<T, double>::value, VtArray<T>> \
1073 operator op (VtArray<T> const &arr, double const &scalar) { \
1074 using Op = Vt_ArrayOpHelpScalar<T>; \
1075 VtArray<T> ret(arr.size()); \
1076 std::transform(arr.begin(), arr.end(), ret.begin(), \
1077 [&scalar](T const &aObj) { \
1078 return Op:: opName (aObj, scalar); \
1085ARCH_PRAGMA_FORCING_TO_BOOL
1086ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
1087ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
1088VTOPERATOR_CPPSCALAR(+, Add)
1089VTOPERATOR_CPPSCALAR(-, Sub)
1090VTOPERATOR_CPPSCALAR(*, Mul)
1091VTOPERATOR_CPPSCALAR_DOUBLE(*, Mul)
1092VTOPERATOR_CPPSCALAR(/, Div)
1093VTOPERATOR_CPPSCALAR_DOUBLE(/, Div)
1094VTOPERATOR_CPPSCALAR(%, Mod)
1097PXR_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.