7#ifndef PXR_USD_SDF_CHILDREN_VIEW_H
8#define PXR_USD_SDF_CHILDREN_VIEW_H
13#include "pxr/usd/sdf/api.h"
14#include "pxr/usd/sdf/children.h"
20PXR_NAMESPACE_OPEN_SCOPE
33 bool operator()(
const T& x)
const {
return true; }
43 typedef T PrivateType;
45 static const PublicType& Convert(
const PrivateType& t) {
return t; }
55template <
typename _Owner,
typename _InnerIterator,
typename _DummyPredicate>
56class Sdf_ChildrenViewTraits {
61 class _FilterIterator {
63 using iterator_category = std::forward_iterator_tag;
64 using value_type =
typename _InnerIterator::value_type;
65 using reference =
typename _InnerIterator::reference;
66 using pointer =
typename _InnerIterator::pointer;
67 using difference_type =
typename _InnerIterator::difference_type;
69 _FilterIterator() =
default;
70 _FilterIterator(
const _Owner* owner,
71 const _InnerIterator& underlyingIterator,
72 const _InnerIterator& end) :
74 _underlyingIterator(underlyingIterator),
79 reference operator*()
const {
80 return *_underlyingIterator;
83 pointer operator->()
const {
84 return _underlyingIterator.operator->();
87 _FilterIterator& operator++() {
89 ++_underlyingIterator;
94 _FilterIterator operator++(
int) {
96 _FilterIterator result(*
this);
97 ++_underlyingIterator;
102 bool operator==(
const _FilterIterator& other)
const {
103 return _underlyingIterator == other._underlyingIterator;
106 bool operator!=(
const _FilterIterator& other)
const {
107 return _underlyingIterator != other._underlyingIterator;
110 const _InnerIterator& GetBase()
const {
return _underlyingIterator; }
114 bool _ShouldFilter(
const value_type& x)
const
116 return !_owner->GetPredicate()(
117 _Owner::Adapter::Convert(x));
122 while (_underlyingIterator != _end &&
123 _ShouldFilter(*_underlyingIterator)) {
124 ++_underlyingIterator;
128 const _Owner* _owner =
nullptr;
129 _InnerIterator _underlyingIterator;
134 using const_iterator = _FilterIterator;
139 static const_iterator GetIterator(
const _Owner* owner,
140 const _InnerIterator& i,
143 _InnerIterator end(owner,size);
144 return const_iterator(owner, i, end);
148 static const _InnerIterator& GetBase(
const const_iterator& i)
157template <
typename _Owner,
typename _InnerIterator>
158class Sdf_ChildrenViewTraits<_Owner, _InnerIterator,
163 typedef _InnerIterator const_iterator;
165 static const const_iterator& GetIterator(
const _Owner*,
166 const _InnerIterator& i,
size_t size)
171 static const _InnerIterator& GetBase(
const const_iterator& i)
198template <
typename _ChildPolicy,
199 typename _Predicate =
201 typename _ChildPolicy::ValueType>,
204 typename _ChildPolicy::ValueType> >
209 typedef _Adapter Adapter;
210 typedef _Predicate Predicate;
211 typedef _ChildPolicy ChildPolicy;
212 typedef typename ChildPolicy::KeyPolicy KeyPolicy;
213 typedef Sdf_Children<ChildPolicy> ChildrenType;
215 typedef typename ChildPolicy::KeyType key_type;
216 typedef typename Adapter::PublicType value_type;
225 class _InnerIterator {
228 SdfChildrenView::value_type* operator->() {
return &_value; }
231 explicit _PtrProxy(
const SdfChildrenView::value_type& value)
233 SdfChildrenView::value_type _value;
236 using iterator_category = std::random_access_iterator_tag;
237 using value_type = SdfChildrenView::value_type;
238 using reference = value_type;
239 using pointer = _PtrProxy;
240 using difference_type = std::ptrdiff_t;
242 _InnerIterator() =
default;
243 _InnerIterator(
const This* owner,
const size_t& pos) :
244 _owner(owner), _pos(pos) { }
246 reference operator*()
const {
return dereference(); }
247 pointer operator->()
const {
return pointer(dereference()); }
248 reference operator[](
const difference_type index)
const {
249 _InnerIterator advanced(*
this);
250 advanced.advance(index);
251 return advanced.dereference();
254 difference_type operator-(
const _InnerIterator& other)
const {
255 return -distance_to(other);
258 _InnerIterator& operator++() {
263 _InnerIterator& operator--() {
268 _InnerIterator operator++(
int) {
269 _InnerIterator result(*
this);
274 _InnerIterator operator--(
int) {
275 _InnerIterator result(*
this);
280 _InnerIterator operator+(
const difference_type increment)
const {
281 _InnerIterator result(*
this);
282 result.advance(increment);
286 _InnerIterator operator-(
const difference_type decrement)
const {
287 _InnerIterator result(*
this);
288 result.advance(-decrement);
292 _InnerIterator& operator+=(
const difference_type increment) {
297 _InnerIterator& operator-=(
const difference_type decrement) {
302 bool operator==(
const _InnerIterator& other)
const {
306 bool operator!=(
const _InnerIterator& other)
const {
307 return !equal(other);
310 bool operator<(
const _InnerIterator& other)
const {
312 return _pos < other._pos;
315 bool operator<=(
const _InnerIterator& other)
const {
317 return _pos <= other._pos;
320 bool operator>(
const _InnerIterator& other)
const {
322 return _pos > other._pos;
325 bool operator>=(
const _InnerIterator& other)
const {
327 return _pos >= other._pos;
332 reference dereference()
const
334 return _owner->_Get(_pos);
337 bool equal(
const _InnerIterator& other)
const
339 return _pos == other._pos;
350 void advance(difference_type n) {
354 difference_type distance_to(
const _InnerIterator& other)
const {
355 return other._pos-_pos;
359 const This* _owner =
nullptr;
364 typedef Sdf_ChildrenViewTraits<This, _InnerIterator, Predicate> _Traits;
365 typedef typename _Traits::const_iterator const_iterator;
366 typedef Tf_ProxyReferenceReverseIterator<const_iterator> const_reverse_iterator;
367 typedef size_t size_type;
368 typedef ptrdiff_t difference_type;
376 const KeyPolicy& keyPolicy = KeyPolicy()) :
377 _children(layer, path, childrenKey, keyPolicy)
383 const Predicate& predicate,
384 const KeyPolicy& keyPolicy = KeyPolicy()) :
385 _children(layer, path, childrenKey, keyPolicy),
386 _predicate(predicate)
391 _children(other._children),
392 _predicate(other._predicate)
396 template <
class OtherAdapter>
398 OtherAdapter> &other) :
399 _children(other._children),
400 _predicate(other._predicate)
410 _children= other._children;
411 _predicate = other._predicate;
417 _InnerIterator i(
this,0);
418 return _Traits::GetIterator(
this, i, _GetSize());
422 const_iterator
end()
const {
423 _InnerIterator i(
this,_GetSize());
424 return _Traits::GetIterator(
this, i, _GetSize());
430 return const_reverse_iterator(
end());
435 const_reverse_iterator
rend()
const {
436 return const_reverse_iterator(
begin());
441 return std::distance(
begin(),
end());
451 const_iterator i =
begin();
467 const_iterator
find(
const key_type& x)
const {
468 _InnerIterator inner(
this, _children.Find(x));
469 const_iterator iter = _Traits::GetIterator(
this, inner, _GetSize());
476 return _Traits::GetBase(iter) == inner ? iter :
end();
480 const_iterator
find(
const value_type& x)
const {
481 const_iterator i =
find(
key(x));
482 return (i !=
end() && *i == x) ? i :
end();
486 key_type
key(
const const_iterator& x)
const {
491 key_type
key(
const value_type& x)
const {
492 return _children.FindKey(Adapter::Convert(x));
497 return std::vector<value_type>(
begin(),
end());
501 template <
typename V>
504 std::copy(
begin(),
end(), std::inserter(x, x.begin()));
509 std::vector<key_type>
keys()
const {
510 std::vector<key_type> result;
511 result.reserve(
size());
512 for (const_iterator i =
begin(), n =
end(); i != n; ++i) {
513 result.push_back(
key(i));
519 template <
typename V>
521 std::vector<key_type> k =
keys();
522 return V(k.begin(), k.end());
526 template <
typename Dict>
529 for (const_iterator i =
begin(), n =
end(); i != n; ++i) {
530 result.insert(std::make_pair(
key(i), *i));
536 bool has(
const key_type& x)
const {
537 return (_children.Find(x) != _GetSize());
542 bool has(
const value_type& x)
const {
543 return has(
key(Adapter::Convert(x)));
547 size_type
count(
const key_type& x)
const {
553 value_type
get(
const key_type& x)
const {
554 size_t index = _children.Find(x);
555 if (index == _GetSize()) {
563 value_type
get(
const key_type& x,
const value_type& fallback)
const {
564 size_t index = _children.Find(x);
565 if (index == _GetSize()) {
580 return _children.IsEqualTo(other._children);
587 return !_children.IsEqualTo(other._children);
591 bool IsValid()
const {
592 return _children.IsValid();
596 ChildrenType &GetChildren() {
601 const Predicate& GetPredicate()
const {
607 value_type _Get(size_type index)
const {
608 return Adapter::Convert(_children.GetChild(index));
612 size_t _GetSize()
const {
613 return _children.GetSize();
618 ChildrenType _children;
619 Predicate _predicate;
624template <
class _View,
class _Adapter>
627 typedef _View OriginalView;
629 typename _View::Predicate,
632 static AdaptedView Create(
const OriginalView& view)
639template <
typename C,
typename P,
typename A>
640struct Tf_ShouldIterateOverCopy<
SdfChildrenView<C, P, A> > : std::true_type
643template <
typename C,
typename P,
typename A>
646 typedef typename Type::const_iterator IteratorType;
647 static IteratorType Begin(Type
const &c) {
return c.
begin(); }
648 static IteratorType End(Type
const &c) {
return c.end(); }
650template <
typename C,
typename P,
typename A>
653 typedef typename Type::const_reverse_iterator IteratorType;
654 static IteratorType Begin(Type
const &c) {
return c.
rbegin(); }
655 static IteratorType End(Type
const &c) {
return c.rend(); }
658PXR_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...