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" 34 #include <boost/compressed_pair.hpp> 35 #include <boost/iterator/filter_iterator.hpp> 36 #include <boost/iterator/iterator_facade.hpp> 37 #include <boost/iterator/reverse_iterator.hpp> 41 PXR_NAMESPACE_OPEN_SCOPE
54 bool operator()(
const T& x)
const {
return true; }
64 typedef T PrivateType;
66 static const PublicType& Convert(
const PrivateType& t) {
return t; }
76 template <
typename _Owner,
typename _InnerIterator,
typename _DummyPredicate>
77 class Sdf_ChildrenViewTraits {
84 typedef typename _Owner::value_type value_type;
86 _Predicate() : _owner(NULL) { }
87 _Predicate(
const _Owner* owner) : _owner(owner) { }
89 bool operator()(
const value_type& x)
const 91 return _owner->GetPredicate()(
92 _Owner::Adapter::Convert(x));
100 typedef boost::filter_iterator<_Predicate, _InnerIterator> const_iterator;
105 static const_iterator GetIterator(
const _Owner* owner,
106 const _InnerIterator& i,
109 _InnerIterator end(owner,size);
110 return const_iterator(_Predicate(owner), i, end);
114 static const _InnerIterator& GetBase(
const const_iterator& i)
123 template <
typename _Owner,
typename _InnerIterator>
124 class Sdf_ChildrenViewTraits<_Owner, _InnerIterator,
129 typedef _InnerIterator const_iterator;
131 static const const_iterator& GetIterator(
const _Owner*,
132 const _InnerIterator& i,
size_t size)
137 static const _InnerIterator& GetBase(
const const_iterator& i)
164 template <
typename _ChildPolicy,
165 typename _Predicate =
167 typename _ChildPolicy::ValueType>,
170 typename _ChildPolicy::ValueType> >
175 typedef _Adapter Adapter;
176 typedef _Predicate Predicate;
177 typedef _ChildPolicy ChildPolicy;
178 typedef typename ChildPolicy::KeyPolicy KeyPolicy;
179 typedef Sdf_Children<ChildPolicy> ChildrenType;
181 typedef typename ChildPolicy::KeyType key_type;
182 typedef typename Adapter::PublicType value_type;
191 class _InnerIterator :
192 public boost::iterator_facade<_InnerIterator,
194 std::random_access_iterator_tag,
197 typedef value_type reference;
198 typedef size_t size_type;
199 typedef ptrdiff_t difference_type;
202 _owner(NULL), _pos(0) { }
203 _InnerIterator(
const This* owner,
const size_t& pos) :
204 _owner(owner), _pos(pos) { }
207 friend class boost::iterator_core_access;
209 reference dereference()
const 211 return _owner->_Get(_pos);
214 bool equal(
const _InnerIterator& other)
const 216 return _pos == other._pos;
227 void advance(difference_type n) {
231 difference_type distance_to(
const _InnerIterator& other)
const {
232 return other._pos-_pos;
241 typedef Sdf_ChildrenViewTraits<This, _InnerIterator, Predicate> _Traits;
242 typedef typename _Traits::const_iterator const_iterator;
243 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
244 typedef size_t size_type;
245 typedef ptrdiff_t difference_type;
253 const KeyPolicy& keyPolicy = KeyPolicy()) :
254 _children(layer, path, childrenKey, keyPolicy)
260 const Predicate& predicate,
261 const KeyPolicy& keyPolicy = KeyPolicy()) :
262 _children(layer, path, childrenKey, keyPolicy),
263 _predicate(predicate)
268 _children(other._children),
269 _predicate(other._predicate)
273 template <
class OtherAdapter>
275 OtherAdapter> &other) :
276 _children(other._children),
277 _predicate(other._predicate)
287 _children= other._children;
288 _predicate = other._predicate;
294 _InnerIterator i(
this,0);
295 return _Traits::GetIterator(
this, i, _GetSize());
299 const_iterator
end()
const {
300 _InnerIterator i(
this,_GetSize());
301 return _Traits::GetIterator(
this, i, _GetSize());
307 return const_reverse_iterator(
end());
312 const_reverse_iterator
rend()
const {
313 return const_reverse_iterator(
begin());
318 return std::distance(
begin(),
end());
328 const_iterator i =
begin();
344 const_iterator
find(
const key_type& x)
const {
345 _InnerIterator inner(
this, _children.Find(x));
346 const_iterator iter = _Traits::GetIterator(
this, inner, _GetSize());
353 return _Traits::GetBase(iter) == inner ? iter :
end();
357 const_iterator
find(
const value_type& x)
const {
358 const_iterator i =
find(
key(x));
359 return (i !=
end() && *i == x) ? i :
end();
363 key_type
key(
const const_iterator& x)
const {
368 key_type
key(
const value_type& x)
const {
369 return _children.FindKey(Adapter::Convert(x));
374 return std::vector<value_type>(
begin(),
end());
378 template <
typename V>
381 std::copy(
begin(),
end(), std::inserter(x, x.begin()));
386 std::vector<key_type>
keys()
const {
387 std::vector<key_type> result;
388 result.reserve(
size());
389 for (const_iterator i =
begin(), n =
end(); i != n; ++i) {
390 result.push_back(
key(i));
396 template <
typename V>
398 std::vector<key_type> k =
keys();
399 return V(k.begin(), k.end());
403 template <
typename Dict>
406 for (const_iterator i =
begin(), n =
end(); i != n; ++i) {
407 result.insert(std::make_pair(
key(i), *i));
413 bool has(
const key_type& x)
const {
414 return (_children.Find(x) != _GetSize());
419 bool has(
const value_type& x)
const {
420 return has(
key(Adapter::Convert(x)));
424 size_type
count(
const key_type& x)
const {
430 value_type
get(
const key_type& x)
const {
431 size_t index = _children.Find(x);
432 if (index == _GetSize()) {
440 value_type
get(
const key_type& x,
const value_type& fallback)
const {
441 size_t index = _children.Find(x);
442 if (index == _GetSize()) {
457 return _children.IsEqualTo(other._children);
464 return !_children.IsEqualTo(other._children);
468 bool IsValid()
const {
469 return _children.IsValid();
473 ChildrenType &GetChildren() {
478 const Predicate& GetPredicate()
const {
484 value_type _Get(size_type index)
const {
485 return Adapter::Convert(_children.GetChild(index));
489 size_t _GetSize()
const {
490 return _children.GetSize();
495 ChildrenType _children;
496 Predicate _predicate;
501 template <
class _View,
class _Adapter>
504 typedef _View OriginalView;
506 typename _View::Predicate,
509 static AdaptedView Create(
const OriginalView& view)
516 template <
typename C,
typename P,
typename A>
517 struct Tf_ShouldIterateOverCopy<
SdfChildrenView<C, P, A> > : boost::true_type
520 template <
typename C,
typename P,
typename A>
523 typedef typename Type::const_iterator IteratorType;
524 static IteratorType Begin(Type
const &c) {
return c.
begin(); }
525 static IteratorType End(Type
const &c) {
return c.end(); }
527 template <
typename C,
typename P,
typename A>
530 typedef typename Type::const_reverse_iterator IteratorType;
531 static IteratorType Begin(Type
const &c) {
return c.
rbegin(); }
532 static IteratorType End(Type
const &c) {
return c.rend(); }
535 PXR_NAMESPACE_CLOSE_SCOPE
537 #endif // PXR_USD_SDF_CHILDREN_VIEW_H key_type key(const value_type &x) const
Returns the key for a value.
value_type get(const key_type &x) const
Returns the element with key x or a default constructed value if no such element exists.
const_reverse_iterator rend() const
Returns an const_reverse_iterator pointing to the end of the reversed vector.
Special case adapter that does no conversions.
A simple iterator adapter for STL containers.
std::vector< key_type > keys() const
Returns the keys for all elements, in order.
const_reverse_iterator rbegin() const
Returns an const_reverse_iterator pointing to the beginning of the reversed vector.
size_type size() const
Returns the size of the vector.
value_type back() const
Returns the last element.
bool operator!=(const This &other) const
Compares children for inequality.
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 find(const key_type &x) const
Finds the element with key x.
std::vector< value_type > values() const
Returns the elements, in order.
const_iterator begin() const
Returns an const_iterator pointing to the beginning of the vector.
value_type front() const
Returns the first element.
Token for efficient comparison, assignment, and hashing of known strings.
const_iterator end() const
Returns an const_iterator pointing to the end of the vector.
V values_as() const
Returns the elements, in order.
V keys_as() const
Returns the keys for all elements, in order.
value_type operator[](const key_type &x) const
Returns the element with key x or a default constructed value if no such element exists.
bool operator==(const This &other) const
Compares children for equality.
Helper class to convert a given view of type _View to an adapted view using _Adapter as the adapter c...
A path value used to locate objects in layers or scenegraphs.
bool has(const value_type &x) const
Returns true if an element with the same key as x is in the container.
key_type key(const const_iterator &x) const
Returns the key for an element.
bool empty() const
Returns true if the vector is empty.
Dict items_as() const
Returns the elements as a dictionary.
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.
bool has(const key_type &x) const
Returns true if an element with key x is in the container.
Provides a view onto an object's children.
value_type operator[](size_type n) const
Returns the n'th element.
Special case predicate that always passes.