24#ifndef PXR_USD_SDF_CHILDREN_VIEW_H
25#define PXR_USD_SDF_CHILDREN_VIEW_H
30#include "pxr/usd/sdf/api.h"
31#include "pxr/usd/sdf/children.h"
37PXR_NAMESPACE_OPEN_SCOPE
50 bool operator()(
const T& x)
const {
return true; }
60 typedef T PrivateType;
62 static const PublicType& Convert(
const PrivateType& t) {
return t; }
72template <
typename _Owner,
typename _InnerIterator,
typename _DummyPredicate>
73class Sdf_ChildrenViewTraits {
78 class _FilterIterator {
80 using iterator_category = std::forward_iterator_tag;
81 using value_type =
typename _InnerIterator::value_type;
82 using reference =
typename _InnerIterator::reference;
83 using pointer =
typename _InnerIterator::pointer;
84 using difference_type =
typename _InnerIterator::difference_type;
86 _FilterIterator() =
default;
87 _FilterIterator(
const _Owner* owner,
88 const _InnerIterator& underlyingIterator,
89 const _InnerIterator& end) :
91 _underlyingIterator(underlyingIterator),
96 reference operator*()
const {
97 return *_underlyingIterator;
100 pointer operator->()
const {
101 return _underlyingIterator.operator->();
104 _FilterIterator& operator++() {
106 ++_underlyingIterator;
111 _FilterIterator operator++(
int) {
113 _FilterIterator result(*
this);
114 ++_underlyingIterator;
119 bool operator==(
const _FilterIterator& other)
const {
120 return _underlyingIterator == other._underlyingIterator;
123 bool operator!=(
const _FilterIterator& other)
const {
124 return _underlyingIterator != other._underlyingIterator;
127 const _InnerIterator& GetBase()
const {
return _underlyingIterator; }
131 bool _ShouldFilter(
const value_type& x)
const
133 return !_owner->GetPredicate()(
134 _Owner::Adapter::Convert(x));
139 while (_underlyingIterator != _end &&
140 _ShouldFilter(*_underlyingIterator)) {
141 ++_underlyingIterator;
145 const _Owner* _owner =
nullptr;
146 _InnerIterator _underlyingIterator;
151 using const_iterator = _FilterIterator;
156 static const_iterator GetIterator(
const _Owner* owner,
157 const _InnerIterator& i,
160 _InnerIterator end(owner,size);
161 return const_iterator(owner, i, end);
165 static const _InnerIterator& GetBase(
const const_iterator& i)
174template <
typename _Owner,
typename _InnerIterator>
175class Sdf_ChildrenViewTraits<_Owner, _InnerIterator,
180 typedef _InnerIterator const_iterator;
182 static const const_iterator& GetIterator(
const _Owner*,
183 const _InnerIterator& i,
size_t size)
188 static const _InnerIterator& GetBase(
const const_iterator& i)
215template <
typename _ChildPolicy,
216 typename _Predicate =
218 typename _ChildPolicy::ValueType>,
221 typename _ChildPolicy::ValueType> >
226 typedef _Adapter Adapter;
227 typedef _Predicate Predicate;
228 typedef _ChildPolicy ChildPolicy;
229 typedef typename ChildPolicy::KeyPolicy KeyPolicy;
230 typedef Sdf_Children<ChildPolicy> ChildrenType;
232 typedef typename ChildPolicy::KeyType key_type;
233 typedef typename Adapter::PublicType value_type;
242 class _InnerIterator {
245 SdfChildrenView::value_type* operator->() {
return &_value; }
248 explicit _PtrProxy(
const SdfChildrenView::value_type& value)
250 SdfChildrenView::value_type _value;
253 using iterator_category = std::random_access_iterator_tag;
254 using value_type = SdfChildrenView::value_type;
255 using reference = value_type;
256 using pointer = _PtrProxy;
257 using difference_type = std::ptrdiff_t;
259 _InnerIterator() =
default;
260 _InnerIterator(
const This* owner,
const size_t& pos) :
261 _owner(owner), _pos(pos) { }
263 reference operator*()
const {
return dereference(); }
264 pointer operator->()
const {
return pointer(dereference()); }
265 reference operator[](
const difference_type index)
const {
266 _InnerIterator advanced(*
this);
267 advanced.advance(index);
268 return advanced.dereference();
271 difference_type operator-(
const _InnerIterator& other)
const {
272 return -distance_to(other);
275 _InnerIterator& operator++() {
280 _InnerIterator& operator--() {
285 _InnerIterator operator++(
int) {
286 _InnerIterator result(*
this);
291 _InnerIterator operator--(
int) {
292 _InnerIterator result(*
this);
297 _InnerIterator operator+(
const difference_type increment)
const {
298 _InnerIterator result(*
this);
299 result.advance(increment);
303 _InnerIterator operator-(
const difference_type decrement)
const {
304 _InnerIterator result(*
this);
305 result.advance(-decrement);
309 _InnerIterator& operator+=(
const difference_type increment) {
314 _InnerIterator& operator-=(
const difference_type decrement) {
319 bool operator==(
const _InnerIterator& other)
const {
323 bool operator!=(
const _InnerIterator& other)
const {
324 return !equal(other);
327 bool operator<(
const _InnerIterator& other)
const {
329 return _pos < other._pos;
332 bool operator<=(
const _InnerIterator& other)
const {
334 return _pos <= other._pos;
337 bool operator>(
const _InnerIterator& other)
const {
339 return _pos > other._pos;
342 bool operator>=(
const _InnerIterator& other)
const {
344 return _pos >= other._pos;
349 reference dereference()
const
351 return _owner->_Get(_pos);
354 bool equal(
const _InnerIterator& other)
const
356 return _pos == other._pos;
367 void advance(difference_type n) {
371 difference_type distance_to(
const _InnerIterator& other)
const {
372 return other._pos-_pos;
376 const This* _owner =
nullptr;
381 typedef Sdf_ChildrenViewTraits<This, _InnerIterator, Predicate> _Traits;
382 typedef typename _Traits::const_iterator const_iterator;
383 typedef Tf_ProxyReferenceReverseIterator<const_iterator> const_reverse_iterator;
384 typedef size_t size_type;
385 typedef ptrdiff_t difference_type;
393 const KeyPolicy& keyPolicy = KeyPolicy()) :
394 _children(layer, path, childrenKey, keyPolicy)
400 const Predicate& predicate,
401 const KeyPolicy& keyPolicy = KeyPolicy()) :
402 _children(layer, path, childrenKey, keyPolicy),
403 _predicate(predicate)
408 _children(other._children),
409 _predicate(other._predicate)
413 template <
class OtherAdapter>
415 OtherAdapter> &other) :
416 _children(other._children),
417 _predicate(other._predicate)
427 _children= other._children;
428 _predicate = other._predicate;
434 _InnerIterator i(
this,0);
435 return _Traits::GetIterator(
this, i, _GetSize());
439 const_iterator
end()
const {
440 _InnerIterator i(
this,_GetSize());
441 return _Traits::GetIterator(
this, i, _GetSize());
447 return const_reverse_iterator(
end());
452 const_reverse_iterator
rend()
const {
453 return const_reverse_iterator(
begin());
458 return std::distance(
begin(),
end());
468 const_iterator i =
begin();
484 const_iterator
find(
const key_type& x)
const {
485 _InnerIterator inner(
this, _children.Find(x));
486 const_iterator iter = _Traits::GetIterator(
this, inner, _GetSize());
493 return _Traits::GetBase(iter) == inner ? iter :
end();
497 const_iterator
find(
const value_type& x)
const {
498 const_iterator i =
find(
key(x));
499 return (i !=
end() && *i == x) ? i :
end();
503 key_type
key(
const const_iterator& x)
const {
508 key_type
key(
const value_type& x)
const {
509 return _children.FindKey(Adapter::Convert(x));
514 return std::vector<value_type>(
begin(),
end());
518 template <
typename V>
521 std::copy(
begin(),
end(), std::inserter(x, x.begin()));
526 std::vector<key_type>
keys()
const {
527 std::vector<key_type> result;
528 result.reserve(
size());
529 for (const_iterator i =
begin(), n =
end(); i != n; ++i) {
530 result.push_back(
key(i));
536 template <
typename V>
538 std::vector<key_type> k =
keys();
539 return V(k.begin(), k.end());
543 template <
typename Dict>
546 for (const_iterator i =
begin(), n =
end(); i != n; ++i) {
547 result.insert(std::make_pair(
key(i), *i));
553 bool has(
const key_type& x)
const {
554 return (_children.Find(x) != _GetSize());
559 bool has(
const value_type& x)
const {
560 return has(
key(Adapter::Convert(x)));
564 size_type
count(
const key_type& x)
const {
570 value_type
get(
const key_type& x)
const {
571 size_t index = _children.Find(x);
572 if (index == _GetSize()) {
580 value_type
get(
const key_type& x,
const value_type& fallback)
const {
581 size_t index = _children.Find(x);
582 if (index == _GetSize()) {
597 return _children.IsEqualTo(other._children);
604 return !_children.IsEqualTo(other._children);
608 bool IsValid()
const {
609 return _children.IsValid();
613 ChildrenType &GetChildren() {
618 const Predicate& GetPredicate()
const {
624 value_type _Get(size_type index)
const {
625 return Adapter::Convert(_children.GetChild(index));
629 size_t _GetSize()
const {
630 return _children.GetSize();
635 ChildrenType _children;
636 Predicate _predicate;
641template <
class _View,
class _Adapter>
644 typedef _View OriginalView;
646 typename _View::Predicate,
649 static AdaptedView Create(
const OriginalView& view)
656template <
typename C,
typename P,
typename A>
657struct Tf_ShouldIterateOverCopy<
SdfChildrenView<C, P, A> > : std::true_type
660template <
typename C,
typename P,
typename A>
663 typedef typename Type::const_iterator IteratorType;
664 static IteratorType Begin(Type
const &c) {
return c.
begin(); }
665 static IteratorType End(Type
const &c) {
return c.end(); }
667template <
typename C,
typename P,
typename A>
670 typedef typename Type::const_reverse_iterator IteratorType;
671 static IteratorType Begin(Type
const &c) {
return c.
rbegin(); }
672 static IteratorType End(Type
const &c) {
return c.rend(); }
675PXR_NAMESPACE_CLOSE_SCOPE
A simple iterator adapter for STL containers.
Provides a view onto an object's children.
std::vector< value_type > values() const
Returns the elements, in order.
const_reverse_iterator rend() const
Returns an const_reverse_iterator pointing to the end of the reversed vector.
std::vector< key_type > keys() const
Returns the keys for all elements, in order.
bool has(const key_type &x) const
Returns true if an element with key x is in the container.
key_type key(const const_iterator &x) const
Returns the key for an element.
value_type front() const
Returns the first element.
value_type get(const key_type &x, const value_type &fallback) const
Returns the element with key x or the fallback if no such element exists.
const_iterator begin() const
Returns an const_iterator pointing to the beginning of the vector.
value_type get(const key_type &x) const
Returns the element with key x or a default constructed value if no such element exists.
value_type operator[](const key_type &x) const
Returns the element with key x or a default constructed value if no such element exists.
size_type size() const
Returns the size of the vector.
bool empty() const
Returns true if the vector is empty.
const_iterator find(const key_type &x) const
Finds the element with key x.
value_type back() const
Returns the last element.
V keys_as() const
Returns the keys for all elements, in order.
Dict items_as() const
Returns the elements as a dictionary.
value_type operator[](size_type n) const
Returns the n'th element.
key_type key(const value_type &x) const
Returns the key for a value.
V values_as() const
Returns the elements, in order.
bool has(const value_type &x) const
Returns true if an element with the same key as x is in the container.
bool operator==(const This &other) const
Compares children for equality.
bool operator!=(const This &other) const
Compares children for inequality.
const_iterator end() const
Returns an const_iterator pointing to the end of the vector.
size_type count(const key_type &x) const
Returns the number of elements with key x in the container.
const_iterator find(const value_type &x) const
Finds element x, if present in this view.
const_reverse_iterator rbegin() const
Returns an const_reverse_iterator pointing to the beginning of the reversed vector.
Special case adapter that does no conversions.
Special case predicate that always passes.
A path value used to locate objects in layers or scenegraphs.
Token for efficient comparison, assignment, and hashing of known strings.
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Helper class to convert a given view of type _View to an adapted view using _Adapter as the adapter c...