7 #ifndef PXR_USD_SDF_PATH_TABLE_H
8 #define PXR_USD_SDF_PATH_TABLE_H
64 template <
class MappedType>
80 _Entry(
const _Entry&) =
delete;
81 _Entry&
operator=(
const _Entry&) =
delete;
86 , nextSiblingOrParent(
nullptr,
false) {}
89 :
value(std::move(value))
92 , nextSiblingOrParent(
nullptr,
false) {}
96 _Entry *GetNextSibling() {
97 return nextSiblingOrParent.template BitsAs<bool>() ?
98 nextSiblingOrParent.Get() : 0;
102 _Entry
const *GetNextSibling()
const {
103 return nextSiblingOrParent.template BitsAs<bool>() ?
104 nextSiblingOrParent.Get() : 0;
109 _Entry *GetParentLink() {
110 return nextSiblingOrParent.template BitsAs<bool>() ? 0 :
111 nextSiblingOrParent.Get();
115 _Entry
const *GetParentLink()
const {
116 return nextSiblingOrParent.template BitsAs<bool>() ? 0 :
117 nextSiblingOrParent.Get();
122 void SetSibling(_Entry *sibling) {
123 nextSiblingOrParent.Set(sibling,
true);
128 void SetParentLink(_Entry *parent) {
129 nextSiblingOrParent.Set(parent,
false);
133 void AddChild(_Entry *child) {
137 child->SetSibling(firstChild);
139 child->SetParentLink(
this);
143 void RemoveChild(_Entry *child) {
145 if (child == firstChild) {
146 firstChild = child->GetNextSibling();
150 _Entry *prev, *cur = firstChild;
153 cur = prev->GetNextSibling();
154 }
while (cur != child);
155 prev->nextSiblingOrParent = cur->nextSiblingOrParent;
178 typedef std::vector<_Entry *> _BucketVec;
185 template <
class ValType,
class EntryPtr>
200 template <
class OtherVal,
class OtherEntryPtr>
219 template <
class OtherVal,
class OtherEntryPtr>
224 template <
class OtherVal,
class OtherEntryPtr>
226 return !
equal(other);
235 if (EntryPtr sibling =
_entry->GetNextSibling()) {
241 for (EntryPtr p =
_entry->GetParentLink(); p;
242 p = p->GetParentLink()) {
243 if (EntryPtr sibling = p->GetNextSibling()) {
277 template <
class OtherVal,
class OtherEntryPtr>
312 ret._unlinkedEntry.reset(
new _Entry(value,
nullptr));
320 ret._unlinkedEntry.reset(
new _Entry(std::move(
value),
nullptr));
327 return New({ key, mapped });
334 return _unlinkedEntry->value.first;
341 return _unlinkedEntry->value.first;
348 return _unlinkedEntry->value.second;
355 return _unlinkedEntry->value.second;
361 return static_cast<bool>(_unlinkedEntry);
366 explicit operator bool()
const {
373 _unlinkedEntry.reset();
377 std::unique_ptr<_Entry> _unlinkedEntry;
385 : _buckets(other._buckets.
size())
395 if (i._entry->firstChild && !j._entry->firstChild) {
396 j._entry->firstChild =
397 _InsertInTable(i._entry->firstChild->value).first._entry;
400 if (i._entry->nextSiblingOrParent.Get() &&
401 !j._entry->nextSiblingOrParent.Get()) {
402 j._entry->nextSiblingOrParent.Set(
403 _InsertInTable(i._entry->nextSiblingOrParent.
405 i._entry->nextSiblingOrParent.template BitsAs<bool>());
412 : _buckets(std::move(other._buckets))
489 _Entry *
const entry = i._entry;
490 _EraseSubtree(entry);
491 _RemoveFromParent(entry);
492 _EraseFromTable(entry);
500 for (_Entry *e = _buckets[_Hash(path)]; e; e = e->next) {
501 if (e->value.first == path)
513 for (_Entry
const *e = _buckets[_Hash(path)]; e; e = e->next) {
514 if (e->value.first == path)
524 std::pair<iterator, iterator>
526 std::pair<iterator, iterator>
result;
527 result.first =
find(path);
528 result.second = result.first.GetNextSubtree();
535 std::pair<const_iterator, const_iterator>
537 std::pair<const_iterator, const_iterator>
result;
538 result.first =
find(path);
539 result.second = result.first.GetNextSubtree();
549 inline size_t size()
const {
return _size; }
570 _UpdateTreeForNewEntry(result);
586 _UpdateTreeForNewEntry(result);
605 for (
size_t i = 0,
n = _buckets.size(); i !=
n; ++i) {
606 _Entry *entry = _buckets[i];
608 _Entry *next = entry->next;
621 [](
void*& voidEntry) {
622 _Entry *entry =
static_cast<_Entry *
>(voidEntry);
624 _Entry *next = entry->next;
631 _buckets.size(), visitFn);
637 _buckets.swap(other._buckets);
644 std::vector<size_t>
sizes(_buckets.size(), 0u);;
645 for (
size_t i = 0,
n = _buckets.size(); i !=
n; ++i) {
646 for (_Entry *entry = _buckets[i]; entry; entry = entry->next) {
664 for (
iterator i=range.first; i!=range.second; ++i) {
666 i->first.ReplacePrefix(oldName, newName),
670 if (range.first != range.second)
679 template <
typename Callback>
682 [&visitFn](
void*& voidEntry) {
683 _Entry *entry =
static_cast<_Entry *
>(voidEntry);
685 visitFn(std::cref(entry->value.first),
691 _buckets.size(), lambda);
696 template <
typename Callback>
699 [&visitFn](
void*& voidEntry) {
700 const _Entry *entry =
static_cast<const _Entry *
>(voidEntry);
702 visitFn(std::cref(entry->value.first),
703 std::cref(entry->value.second));
709 reinterpret_cast<void **>(const_cast<_Entry**>(_buckets.data())),
710 _buckets.size(), lambda);
722 _Entry *
const newEntry = iresult.first._entry;
723 SdfPath const &parentPath = _GetParentPath(newEntry->value.first);
728 parIter._entry->AddChild(newEntry);
734 template <
class MakeEntryFn>
736 MakeEntryFn &&makeEntry) {
742 _Entry **bucketHead = &(_buckets[_Hash(key)]);
743 for (_Entry *e = *bucketHead; e; e = e->next) {
744 if (e->value.first == key) {
753 bucketHead = &(_buckets[_Hash(key)]);
757 *bucketHead = std::forward<MakeEntryFn>(makeEntry)(*bucketHead);
767 return _InsertInTableImpl(
768 value.first, [&value](_Entry *next) {
769 return new _Entry(value, next);
774 return _InsertInTableImpl(
775 node.GetKey(), [&node](_Entry *next)
mutable {
776 node._unlinkedEntry->next = next;
777 return node._unlinkedEntry.release();
782 void _EraseFromTable(_Entry *entry) {
784 _Entry **cur = &_buckets[_Hash(entry->value.first)];
785 while (*cur != entry)
786 cur = &((*cur)->next);
796 void _EraseSubtree(_Entry *entry) {
798 if (_Entry *
const firstChild = entry->firstChild) {
799 _EraseSubtreeAndSiblings(firstChild);
800 _EraseFromTable(firstChild);
806 void _EraseSubtreeAndSiblings(_Entry *entry) {
808 _EraseSubtree(entry);
811 _Entry *sibling = entry->GetNextSibling();
812 _Entry *nextSibling = sibling ? sibling->GetNextSibling() :
nullptr;
814 _EraseSubtree(sibling);
815 _EraseFromTable(sibling);
816 sibling = nextSibling;
817 nextSibling = sibling ? sibling->GetNextSibling() :
nullptr;
823 void _RemoveFromParent(_Entry *entry) {
828 iterator parIter =
find(_GetParentPath(entry->value.first));
831 parIter._entry->RemoveChild(entry);
838 TfAutoMallocTag2 tag2(
"Sdf",
"SdfPathTable::_Grow");
843 _mask =
std::max(
size_t(7), (_mask << 1) + 1);
844 _BucketVec newBuckets(_mask + 1);
847 for (
size_t i = 0,
n = _buckets.size(); i !=
n; ++i) {
848 _Entry *elem = _buckets[i];
851 _Entry *next = elem->next;
854 _Entry *&m = newBuckets[_Hash(elem->value.first)];
866 _buckets.swap(newBuckets);
870 bool _IsTooFull()
const {
871 return _size > _buckets.size();
875 inline size_t _Hash(
SdfPath const &path)
const {
888 #endif // PXR_USD_SDF_PATH_TABLE_H
void ParallelForEach(Callback const &visitFn) const
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
pointer operator->() const
const_iterator begin() const
Return a const_iterator to the start of the table.
_IterBoolPair insert(NodeHandle &&node)
static SDF_API const SdfPath & AbsoluteRootPath()
~SdfPathTable()
Destructor.
key_type & GetMutableKey()
SdfPathTable()
Default constructor.
SdfPathTable & operator=(SdfPathTable const &other)
Copy assignment.
const_iterator end() const
Return a const_iterator denoting the end of the table.
bool operator!=(Iterator< OtherVal, OtherEntryPtr > const &other) const
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
const_iterator find(SdfPath const &path) const
SdfPathTable & operator=(SdfPathTable &&other)
Move assignment.
GLsizei const GLfloat * value
GLsizei const GLchar *const * path
mapped_type const & GetMapped() const
Iterator GetNextSubtree() const
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
void ParallelForEach(Callback const &visitFn)
void UpdateForRename(const SdfPath &oldName, const SdfPath &newName)
**But if you need a result
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
static NodeHandle New(value_type &&value)
Iterator(Iterator< OtherVal, OtherEntryPtr > const &other)
Copy constructor (also allows for converting non-const to const).
static NodeHandle New(key_type const &key, mapped_type const &mapped)
size_t GetHash() const
Equality operator.
iterator find(SdfPath const &path)
mapped_type & GetMutableMapped()
#define __ARCH_PRETTY_FUNCTION__
std::forward_iterator_tag iterator_category
std::ptrdiff_t difference_type
void erase(iterator const &i)
bool operator==(Iterator< OtherVal, OtherEntryPtr > const &other) const
static NodeHandle New(value_type const &value)
SdfPathTable(SdfPathTable const &other)
Copy constructor.
std::pair< iterator, iterator > FindSubtreeRange(SdfPath const &path)
key_type const & GetKey() const
Iterator< const value_type, const _Entry * > const_iterator
iterator begin()
Return an iterator to the start of the table.
reference operator*() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
size_t size() const
Return the number of elements in the table.
bool equal(Iterator< OtherVal, OtherEntryPtr > const &other) const
size_t count(SdfPath const &path) const
Return 1 if there is an element for path in the table, otherwise 0.
SdfPathTable(SdfPathTable &&other)
Move constructor.
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
#define PXR_NAMESPACE_CLOSE_SCOPE
std::pair< const_iterator, const_iterator > FindSubtreeRange(SdfPath const &path) const
SDF_API SdfPath GetParentPath() const
bool erase(SdfPath const &path)
mapped_type & operator[](SdfPath const &path)
PXR_NAMESPACE_OPEN_SCOPE SDF_API void Sdf_VisitPathTableInParallel(void **, size_t, TfFunctionRef< void(void *&)>)
bool empty() const
Return true if this table is empty.
std::pair< iterator, bool > _IterBoolPair
Result type for insert().
std::pair< key_type, mapped_type > value_type
Iterator< value_type, _Entry * > iterator
_IterBoolPair insert(value_type const &value)
std::vector< size_t > GetBucketSizes() const
Return a vector of the count of elements in each bucket.
ValType & dereference() const
void swap(SdfPathTable &other)
Swap this table's contents with other.
iterator end()
Return an iterator denoting the end of the table.