7#ifndef PXR_USD_SDF_PATH_TABLE_H
8#define PXR_USD_SDF_PATH_TABLE_H
11#include "pxr/usd/sdf/api.h"
12#include "pxr/usd/sdf/path.h"
13#include "pxr/base/tf/pointerAndBits.h"
14#include "pxr/base/tf/functionRef.h"
20PXR_NAMESPACE_OPEN_SCOPE
24void Sdf_VisitPathTableInParallel(
void **,
size_t,
TfFunctionRef<
void(
void*&)>);
64template <
class MappedType>
70 typedef MappedType mapped_type;
71 typedef std::pair<key_type, mapped_type> value_type;
80 _Entry(
const _Entry&) =
delete;
81 _Entry& operator=(
const _Entry&) =
delete;
82 _Entry(value_type
const &value, _Entry *n)
86 , nextSiblingOrParent(
nullptr,
false) {}
88 _Entry(value_type &&value, _Entry *n)
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;
184 template <
class,
class>
friend class Iterator;
185 template <
class ValType,
class EntryPtr>
189 using iterator_category = std::forward_iterator_tag;
190 using value_type = ValType;
191 using reference = ValType&;
192 using pointer = ValType*;
193 using difference_type = std::ptrdiff_t;
197 Iterator() =
default;
200 template <
class OtherVal,
class OtherEntryPtr>
201 Iterator(Iterator<OtherVal, OtherEntryPtr>
const &other)
202 : _entry(other._entry)
205 reference operator*()
const {
return dereference(); }
206 pointer operator->()
const {
return &(dereference()); }
208 Iterator& operator++() {
213 Iterator operator++(
int) {
214 Iterator result(*
this);
219 template <
class OtherVal,
class OtherEntryPtr>
220 bool operator==(Iterator<OtherVal, OtherEntryPtr>
const &other)
const {
224 template <
class OtherVal,
class OtherEntryPtr>
225 bool operator!=(Iterator<OtherVal, OtherEntryPtr>
const &other)
const {
226 return !equal(other);
232 Iterator GetNextSubtree()
const {
235 if (EntryPtr sibling = _entry->GetNextSibling()) {
237 result._entry = sibling;
241 for (EntryPtr p = _entry->GetParentLink(); p;
242 p = p->GetParentLink()) {
243 if (EntryPtr sibling = p->GetNextSibling()) {
244 result._entry = sibling;
255 bool HasChild()
const {
256 return bool(_entry->firstChild);
261 template <
class,
class>
friend class Iterator;
263 explicit Iterator(EntryPtr entry)
269 inline void increment() {
271 _entry = _entry->firstChild ? _entry->firstChild :
272 GetNextSubtree()._entry;
277 template <
class OtherVal,
class OtherEntryPtr>
278 bool equal(Iterator<OtherVal, OtherEntryPtr>
const &other)
const {
279 return _entry == other._entry;
283 ValType &dereference()
const {
284 return _entry->value;
291 typedef Iterator<value_type, _Entry *> iterator;
292 typedef Iterator<const value_type, const _Entry *> const_iterator;
310 New(value_type
const &value) {
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())
391 for (const_iterator i = other.
begin(),
end = other.
end();
393 iterator j = _InsertInTable(*i).first;
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.
404 Get()->value).first._entry,
405 i._entry->nextSiblingOrParent.template BitsAs<bool>());
412 : _buckets(
std::move(other._buckets))
462 const_iterator
end()
const {
463 return const_iterator(0);
473 iterator i =
find(path);
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)
515 return const_iterator(e);
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);
596 return insert(value_type(path, mapped_type())).first->second;
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;
630 Sdf_VisitPathTableInParallel(
reinterpret_cast<void **
>(_buckets.data()),
631 _buckets.size(), visitFn);
637 _buckets.swap(other._buckets);
638 std::swap(_size, other._size);
639 std::swap(_mask, other._mask);
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),
686 std::ref(entry->value.second));
690 Sdf_VisitPathTableInParallel(
reinterpret_cast<void **
>(_buckets.data()),
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));
707 Sdf_VisitPathTableInParallel(
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);
726 insert(value_type(parentPath, mapped_type())).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);
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 {
876 return path.GetHash() & _mask;
886PXR_NAMESPACE_CLOSE_SCOPE
A path value used to locate objects in layers or scenegraphs.
SDF_API SdfPath GetParentPath() const
Return the path that identifies this path's namespace parent.
static SDF_API const SdfPath & AbsoluteRootPath()
The absolute path representing the top of the namespace hierarchy.
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
A mapping from SdfPath to MappedType, somewhat similar to map<SdfPath, MappedType> and TfHashMap<SdfP...
void ParallelForEach(Callback const &visitFn)
ParallelForEach: parallel iteration over all of the key-value pairs in the path table.
void erase(iterator const &i)
Remove the element pointed to by i from the table as well as all elements whose paths are prefixed by...
mapped_type & operator[](SdfPath const &path)
Shorthand for the following, where t is an SdfPathTable<T>.
iterator find(SdfPath const &path)
Return an iterator to the element corresponding to path, or end() if there is none.
size_t size() const
Return the number of elements in the table.
void ParallelForEach(Callback const &visitFn) const
ParallelForEach: const version, runnable on a const path table and taking a (const SdfPath&,...
const_iterator begin() const
Return a const_iterator to the start of the table.
_IterBoolPair insert(value_type const &value)
Insert value into the table, and additionally insert default entries for all ancestral paths of value...
_IterBoolPair insert(NodeHandle &&node)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool empty() const
Return true if this table is empty.
std::pair< iterator, bool > _IterBoolPair
Result type for insert().
~SdfPathTable()
Destructor.
SdfPathTable(SdfPathTable const &other)
Copy constructor.
void ClearInParallel()
Equivalent to clear(), but destroy contained objects in parallel.
std::pair< iterator, iterator > FindSubtreeRange(SdfPath const &path)
Return a pair of iterators [b, e), describing the maximal range such that for all i in the range,...
std::vector< size_t > GetBucketSizes() const
Return a vector of the count of elements in each bucket.
const_iterator find(SdfPath const &path) const
Return a const_iterator to the element corresponding to path, or end() if there is none.
bool erase(SdfPath const &path)
Remove the element with path path from the table as well as all elements whose paths are prefixed by ...
void swap(SdfPathTable &other)
Swap this table's contents with other.
void clear()
Remove all elements from the table, leaving size() == 0.
SdfPathTable & operator=(SdfPathTable const &other)
Copy assignment.
iterator end()
Return an iterator denoting the end of the table.
const_iterator end() const
Return a const_iterator denoting the end of the table.
iterator begin()
Return an iterator to the start of the table.
SdfPathTable & operator=(SdfPathTable &&other)
Move assignment.
void UpdateForRename(const SdfPath &oldName, const SdfPath &newName)
Replaces all prefixes from oldName to newName.
size_t count(SdfPath const &path) const
Return 1 if there is an element for path in the table, otherwise 0.
std::pair< const_iterator, const_iterator > FindSubtreeRange(SdfPath const &path) const
Return a pair of const_iterators [b, e), describing the maximal range such that for all i in the rang...
SdfPathTable(SdfPathTable &&other)
Move constructor.
SdfPathTable()
Default constructor.
This class provides a non-owning reference to a type-erased callable object with a specified signatur...
This class stores a T * and a small integer in the space of a T *.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
A handle owning a path table node that may be used to "reserve" a stable memory location for key & ma...
static NodeHandle New(key_type const &key, mapped_type const &mapped)
This is an overloaded member function, provided for convenience. It differs from the above function o...
mapped_type const & GetMapped() const
Return a const reference to this NodeHandle's mapped object.
key_type & GetMutableKey()
Return a mutable reference to this NodeHandle's key.
key_type const & GetKey() const
Return a const reference to this NodeHandle's key.
static NodeHandle New(value_type &&value)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool IsValid() const
Return true if this NodeHandle owns a path table entry, false otherwise.
void reset()
Delete any owned path table entry.
mapped_type & GetMutableMapped()
Return a mutable reference to this NodeHandle's mapped object.
static NodeHandle New(value_type const &value)
Create a new NodeHandle for a table entry.