Loading...
Searching...
No Matches
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
11
12#include "pxr/pxr.h"
13
15#include "pxr/base/tf/token.h"
16
17#include <iterator>
18#include <utility>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
33template <typename Connector>
35{
36private:
37 using _Pair = std::pair<TfToken, Connector*>;
39
40public:
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;
48 using difference_type = typename _Vector::difference_type;
49 using iterator = typename _Vector::const_iterator;
50 using reverse_iterator = typename _Vector::const_reverse_iterator;
51 using const_iterator = typename _Vector::const_iterator;
52 using const_reverse_iterator = typename _Vector::const_reverse_iterator;
53
54
56 VdfConnectorMap() = default;
57
61 explicit VdfConnectorMap(size_t n) {
62 _vec.reserve(n);
63 }
64
67 clear();
68 }
69
71 const_iterator begin() const {
72 return _vec.begin();
73 }
74
76 const_iterator end() const {
77 return _vec.end();
78 }
79
81 const_reverse_iterator rbegin() const {
82 return _vec.rbegin();
83 }
84
86 const_reverse_iterator rend() const {
87 return _vec.rend();
88 }
89
91 size_type size() const {
92 return _vec.size();
93 }
94
96 bool empty() const {
97 return _vec.empty();
98 }
99
102 _vec.swap(x._vec);
103 }
104
106 friend void swap(VdfConnectorMap& lhs, VdfConnectorMap& rhs) {
107 lhs.swap(rhs);
108 }
109
114 void clear();
115
117 const_iterator find(const key_type& k) const;
118
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
137 template <typename... Args>
138 std::pair<const_iterator, bool>
139 try_emplace(const key_type& k, Args&&... args);
140
141private:
142
143 _Vector _vec;
144};
145
146
147template <typename Connector>
148typename VdfConnectorMap<Connector>::const_iterator
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
161template <typename Connector>
162template <typename... Args>
163std::pair<typename VdfConnectorMap<Connector>::const_iterator, bool>
164VdfConnectorMap<Connector>::try_emplace(const key_type& k, Args&&... args)
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
174template <typename Connector>
175void
177{
178 for (const auto &[_, connector] : _vec) {
179 delete connector;
180 }
181 _vec.clear();
182}
183
184PXR_NAMESPACE_CLOSE_SCOPE
185
186#endif
void reserve(size_type newCapacity)
Reserve storage for newCapacity entries.
Definition: smallVector.h:410
size_type size() const
Returns the current size of the vector.
Definition: smallVector.h:596
bool empty() const
Returns true if this vector is empty.
Definition: smallVector.h:608
void swap(TfSmallVector &rhs)
Swap two vector instances.
Definition: smallVector.h:298
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Maps names to inputs or outputs (a.k.a.
Definition: connectorMap.h:35
const_reverse_iterator rend() const
Returns a const_reverse_iterator pointing to the end of the map.
Definition: connectorMap.h:86
friend void swap(VdfConnectorMap &lhs, VdfConnectorMap &rhs)
Swaps the contents of lhs and rhs.
Definition: connectorMap.h:106
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the map.
Definition: connectorMap.h:71
VdfConnectorMap()=default
Creates an empty vector.
size_type size() const
Returns the size of the map.
Definition: connectorMap.h:91
bool empty() const
Returns true if the map's size is 0.
Definition: connectorMap.h:96
~VdfConnectorMap()
Destroys all connector instances owned by this map.
Definition: connectorMap.h:66
void swap(VdfConnectorMap &x)
Swaps the contents of this map with x.
Definition: connectorMap.h:101
VdfConnectorMap(size_t n)
Creates a map that can hold at least n elements without reallocation.
Definition: connectorMap.h:61
const_iterator find(const key_type &k) const
Finds the element with key k.
Definition: connectorMap.h:149
std::pair< const_iterator, bool > try_emplace(const key_type &k, Args &&... args)
Inserts an element with key k if one does not already exist.
bool operator==(const VdfConnectorMap &x) const
Test two maps for equality.
Definition: connectorMap.h:120
void clear()
Erases all of the elements.
Definition: connectorMap.h:176
const_iterator end() const
Returns a const_iterator pointing to the end of the map.
Definition: connectorMap.h:76
const_reverse_iterator rbegin() const
Returns a const_reverse_iterator pointing to the beginning of the map.
Definition: connectorMap.h:81
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...