HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nodeSet.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_EXEC_VDF_NODE_SET_H
8 #define PXR_EXEC_VDF_NODE_SET_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
15 #include "pxr/exec/vdf/node.h"
16 
17 #include "pxr/base/tf/bits.h"
18 
20 
21 /// Class that efficiently stores a set of VdfNodes.
22 ///
23 /// Uses node indices to identify nodes, for efficient storage.
24 ///
25 class VdfNodeSet {
26 public:
27 
28  /// Default constructor
29  ///
30  VdfNodeSet() = default;
31 
32  /// Is this set empty?
33  ///
34  bool IsEmpty() const {
35  return _bits.GetSize() == 0 || _bits.GetNumSet() == 0;
36  }
37 
38  /// Get the number of elements contained in this set.
39  ///
40  size_t GetSize() const {
41  return _bits.GetNumSet();
42  }
43 
44  /// Clears the node set.
45  ///
46  /// Note that unlike on STL containers, this method also reclaims memory.
47  ///
48  VDF_API
49  void Clear();
50 
51  /// Returns \c true if \p node is in the set.
52  ///
53  bool Contains(const VdfNode &node) const {
54  return Contains(VdfNode::GetIndexFromId(node.GetId()));
55  }
56 
57  /// Returns \c true if the node with the given \p index is in the set.
58  ///
59  bool Contains(const VdfIndex index) const {
60  return index < _bits.GetSize() ? _bits.IsSet(index) : false;
61  }
62 
63  /// Inserts \p node into the set.
64  ///
65  inline void Insert(const VdfNode &node);
66 
67  /// Inserts another \p nodeSet into this set.
68  ///
69  VDF_API
70  void Insert(const VdfNodeSet &rhs);
71 
72  /// Removes \p node from the set.
73  ///
74  /// Returns true if \p node was contained in the set.
75  ///
76  VDF_API
77  bool Remove(const VdfNode &node);
78 
79  /// Iterator types.
80  ///
83 
84  /// Returns an iterator at the beginning of the iterable range.
85  ///
87  return _bits.GetAllSetView().begin();
88  }
89 
90  /// Returns an iterator at the end of the iterable range.
91  ///
92  const_iterator end() const {
93  return _bits.GetAllSetView().end();
94  }
95 
96  /// Swaps two VdfNodeSet instances.
97  ///
98  friend void swap(VdfNodeSet &lhs, VdfNodeSet &rhs) {
99  lhs._bits.Swap(rhs._bits);
100  }
101 
102 private:
103 
104  // Grow the underlying storage to accomodate at least the number of
105  // specified indices.
106  VDF_API
107  void _Grow(size_t size);
108 
109  // The bit set representing nodes included in the set. The size of this
110  // bit set denotes the capacity.
111  TfBits _bits;
112 
113 };
114 
115 void
117 {
118  // Make sure to grow the bitset to accomodate the corresponding index of
119  // the specified node.
121  if (index >= _bits.GetSize()) {
122  _Grow(index + 1);
123  }
124 
125  // Set the index.
126  _bits.Set(index);
127 }
128 
130 
131 #endif
VdfNodeSet()=default
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
VDF_API void Clear()
Definition: node.h:52
const_iterator end() const
Definition: bits.h:880
void Swap(TfBits &rhs)
Definition: bits.h:261
#define VDF_API
Definition: api.h:25
const_iterator begin() const
Definition: nodeSet.h:86
bool IsEmpty() const
Definition: nodeSet.h:34
Fast bit array that keeps track of the number of bits set and can find the next set in a timely manne...
Definition: bits.h:48
size_t GetSize() const
Definition: nodeSet.h:40
VdfId GetId() const
Definition: node.h:116
VDF_API bool Remove(const VdfNode &node)
const_iterator end() const
Definition: nodeSet.h:92
size_t GetSize() const
Definition: bits.h:475
bool Contains(const VdfNode &node) const
Definition: nodeSet.h:53
static VdfIndex GetIndexFromId(const VdfId id)
Definition: node.h:123
const_iterator begin() const
Definition: bits.h:870
TfBits::View< TfBits::AllSet >::const_iterator const_iterator
Definition: nodeSet.h:82
size_t GetNumSet() const
Definition: bits.h:520
GLsizeiptr size
Definition: glcorearb.h:664
void Set(size_t index)
Definition: bits.h:377
friend void swap(VdfNodeSet &lhs, VdfNodeSet &rhs)
Definition: nodeSet.h:98
void Insert(const VdfNode &node)
Definition: nodeSet.h:116
GLuint index
Definition: glcorearb.h:786
AllSetView GetAllSetView() const
Definition: bits.h:914
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
bool Contains(const VdfIndex index) const
Definition: nodeSet.h:59
bool IsSet(size_t index) const
Definition: bits.h:412
TfBits::View< TfBits::AllSet >::const_iterator iterator
Definition: nodeSet.h:81
uint32_t VdfIndex
The index type for Vdf objects.
Definition: types.h:110