HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
connectorMap.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_CONNECTOR_MAP_H
8 #define PXR_EXEC_VDF_CONNECTOR_MAP_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
15 #include "pxr/base/tf/token.h"
16 
17 #include <iterator>
18 #include <utility>
19 
21 
22 /// Maps names to inputs or outputs (a.k.a. connectors.)
23 ///
24 /// VdfConnectorMap is intended to map TfToken names to VdfInput or VdfOutput
25 /// pointers on behalf of VdfNode. The map is responsible for constructing
26 /// Connector objects and retains ownership of the pointed-to objects.
27 ///
28 /// Only a limited subset of the associative container interface is provided
29 /// and lookup is implemented with a linear search. This can be efficient
30 /// for storing small numbers of elements, especially when key comparison is
31 /// quick.
32 ///
33 template <typename Connector>
35 {
36 private:
37  using _Pair = std::pair<TfToken, Connector*>;
39 
40 public:
41  using key_type = TfToken;
42  using mapped_type = Connector*;
43  using value_type = _Pair;
44  using pointer = _Pair*;
45  using reference = _Pair&;
46  using const_reference = const _Pair&;
47  using size_type = typename _Vector::size_type;
53 
54 
55  /// Creates an empty vector.
56  VdfConnectorMap() = default;
57 
58  /// Creates a map that can hold at least \p n elements without
59  /// reallocation.
60  ///
61  explicit VdfConnectorMap(size_t n) {
62  _vec.reserve(n);
63  }
64 
65  /// Destroys all connector instances owned by this map.
67  clear();
68  }
69 
70  /// Returns a const_iterator pointing to the beginning of the map.
72  return _vec.begin();
73  }
74 
75  /// Returns a const_iterator pointing to the end of the map.
76  const_iterator end() const {
77  return _vec.end();
78  }
79 
80  /// Returns a const_reverse_iterator pointing to the beginning of the map.
82  return _vec.rbegin();
83  }
84 
85  /// Returns a const_reverse_iterator pointing to the end of the map.
87  return _vec.rend();
88  }
89 
90  /// Returns the size of the map.
91  size_type size() const {
92  return _vec.size();
93  }
94 
95  /// Returns \c true if the \c map's size is 0.
96  bool empty() const {
97  return _vec.empty();
98  }
99 
100  /// Swaps the contents of this map with \p x.
102  _vec.swap(x._vec);
103  }
104 
105  /// Swaps the contents of \p lhs and \p rhs.
106  friend void swap(VdfConnectorMap& lhs, VdfConnectorMap& rhs) {
107  lhs.swap(rhs);
108  }
109 
110  /// Erases all of the elements.
111  ///
112  /// This destroys all connector instances owned by this map.
113  ///
114  void clear();
115 
116  /// Finds the element with key \p k.
117  const_iterator find(const key_type& k) const;
118 
119  /// Test two maps for equality.
120  bool operator==(const VdfConnectorMap& x) const {
121  return _vec == x._vec;
122  }
123 
124  bool operator!=(const VdfConnectorMap& x) const {
125  return _vec != x._vec;
126  }
127 
128  /// Inserts an element with key \p k if one does not already exist.
129  ///
130  /// If a new element is inserted, a new Connector is constructed by
131  /// forwarding \p args. Otherwise, \p args are not moved.
132  ///
133  /// Returns a pair of (const_iterator, bool) where the iterator points to
134  /// the element in the map for key \k and bool is true if a new element
135  /// was inserted.
136  ///
137  template <typename... Args>
138  std::pair<const_iterator, bool>
139  try_emplace(const key_type& k, Args&&... args);
140 
141 private:
142 
143  _Vector _vec;
144 };
145 
146 
147 template <typename Connector>
150 {
151  const const_iterator endIter = end();
152  for (const_iterator i = begin(); i != endIter; ++i) {
153  if (i->first == k) {
154  return i;
155  }
156  }
157 
158  return endIter;
159 }
160 
161 template <typename Connector>
162 template <typename... Args>
163 std::pair<typename VdfConnectorMap<Connector>::const_iterator, bool>
165 {
166  if (const const_iterator i = find(k); i != end()) {
167  return {i, false};
168  }
169 
170  _vec.emplace_back(k, new Connector(std::forward<Args>(args)...));
171  return {std::prev(end()), true};
172 }
173 
174 template <typename Connector>
175 void
177 {
178  for (const auto &[_, connector] : _vec) {
179  delete connector;
180  }
181  _vec.clear();
182 }
183 
185 
186 #endif
std::pair< const_iterator, bool > try_emplace(const key_type &k, Args &&...args)
const_reverse_iterator rbegin() const
Returns a const_reverse_iterator pointing to the beginning of the map.
Definition: connectorMap.h:81
VdfConnectorMap(size_t n)
Definition: connectorMap.h:61
reverse_iterator rend()
Definition: smallVector.h:683
typename _Vector::const_reverse_iterator const_reverse_iterator
Definition: connectorMap.h:52
reverse_iterator rbegin()
Definition: smallVector.h:666
iterator end()
Definition: smallVector.h:649
void swap(VdfConnectorMap &x)
Swaps the contents of this map with x.
Definition: connectorMap.h:101
const_iterator find(const key_type &k) const
Finds the element with key k.
Definition: connectorMap.h:149
void reserve(size_type newCapacity)
Definition: smallVector.h:410
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
std::ptrdiff_t difference_type
Definition: smallVector.h:51
size_type size() const
Definition: smallVector.h:596
bool empty() const
Definition: smallVector.h:608
bool operator!=(const VdfConnectorMap &x) const
Definition: connectorMap.h:124
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2138
TfToken key_type
Definition: connectorMap.h:41
GLdouble n
Definition: glcorearb.h:2008
Definition: token.h:70
iterator begin()
Definition: smallVector.h:632
GLuint GLuint end
Definition: glcorearb.h:475
size_type size() const
Returns the size of the map.
Definition: connectorMap.h:91
typename _Vector::const_iterator iterator
Definition: connectorMap.h:49
~VdfConnectorMap()
Destroys all connector instances owned by this map.
Definition: connectorMap.h:66
GLint GLenum GLint x
Definition: glcorearb.h:409
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: smallVector.h:182
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:587
friend void swap(VdfConnectorMap &lhs, VdfConnectorMap &rhs)
Swaps the contents of lhs and rhs.
Definition: connectorMap.h:106
typename _Vector::const_iterator const_iterator
Definition: connectorMap.h:51
typename _Vector::const_reverse_iterator reverse_iterator
Definition: connectorMap.h:50
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
**If you just want to fire and args
Definition: thread.h:618
bool empty() const
Returns true if the map's size is 0.
Definition: connectorMap.h:96
std::size_t size_type
Definition: smallVector.h:50
const _Pair * const_iterator
Definition: smallVector.h:180
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the map.
Definition: connectorMap.h:71
const_iterator end() const
Returns a const_iterator pointing to the end of the map.
Definition: connectorMap.h:76
bool operator==(const VdfConnectorMap &x) const
Test two maps for equality.
Definition: connectorMap.h:120
VdfConnectorMap()=default
Creates an empty vector.
void swap(TfSmallVector &rhs)
Definition: smallVector.h:298
const_reverse_iterator rend() const
Returns a const_reverse_iterator pointing to the end of the map.
Definition: connectorMap.h:86
typename _Vector::size_type size_type
Definition: connectorMap.h:47
typename _Vector::difference_type difference_type
Definition: connectorMap.h:48