Loading...
Searching...
No Matches
vectorKey.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_EF_VECTOR_KEY_H
8#define PXR_EXEC_EF_VECTOR_KEY_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/ef/api.h"
15
16#include "pxr/base/tf/hash.h"
17#include "pxr/base/tf/hashmap.h"
18#include "pxr/exec/vdf/vector.h"
19
20#include <memory>
21
22PXR_NAMESPACE_OPEN_SCOPE
23
35class EF_API_TYPE Ef_VectorKey
36{
37public:
41 using StoredType = std::shared_ptr<Ef_VectorKey>;
42
45 Ef_VectorKey(const Ef_VectorKey&) = delete;
46 Ef_VectorKey& operator=(const Ef_VectorKey&) = delete;
47
50 EF_API
51 virtual ~Ef_VectorKey();
52
55 const VdfVector &GetValue() const {
56 return _value;
57 }
58
63 virtual size_t CreateHash() const = 0;
64
69 virtual bool IsEqual(const Ef_VectorKey &) const = 0;
70
73 bool operator==(const Ef_VectorKey &rhs) const {
74 return IsEqual(rhs);
75 }
76
77 bool operator!=(const Ef_VectorKey &rhs) const {
78 return !(*this == rhs);
79 }
80
83 template <class HashState>
84 friend void TfHashAppend(HashState &h, const Ef_VectorKey::StoredType &v) {
85 h.Append(v->CreateHash());
86 }
87
90 struct Equal {
91 size_t operator()(const StoredType &lhs, const StoredType &rhs) const {
92 return lhs->IsEqual(*rhs);
93 }
94 };
95
98 template < typename T >
99 struct Map {
100 using Type = TfHashMap<
102 };
103
104protected:
105 // Constructor.
106 explicit Ef_VectorKey(const VdfVector &value) :
107 _value(value)
108 {}
109
110 // The wrapped VdfVector.
111 const VdfVector _value;
112
113};
114
115
124template < typename T >
125class Ef_TypedVectorKey final : public Ef_VectorKey
126{
127public:
128
131 explicit Ef_TypedVectorKey(const VdfVector &value) :
132 Ef_VectorKey(value)
133 {}
134
138 Ef_TypedVectorKey &operator=(const Ef_TypedVectorKey&) = delete;
139
143 virtual size_t CreateHash() const {
145 size_t hash = TfHash::Combine(a.GetNumValues());
146 for (size_t i = 0; i < a.GetNumValues(); ++i) {
147 hash = TfHash::Combine(hash, a[i]);
148 }
149 return hash;
150 }
151
158 virtual bool IsEqual(const Ef_VectorKey &rhs) const {
159 if (const Ef_TypedVectorKey<T> *rhsDerived =
160 dynamic_cast<const Ef_TypedVectorKey<T>*>(&rhs)) {
161
162 // Get the accessor to this vector
164
165 // Get the accessor to the right-hand-side vector
166 const VdfVector &rhsV = rhsDerived->_value;
167 VdfVector::ReadAccessor<T> rhsA = rhsV.GetReadAccessor<T>();
168
169 // Compare each stored element. If two vectors do not hold
170 // the same number of elements, they are considered not equal.
171 if (a.GetNumValues() == rhsA.GetNumValues()) {
172 for (size_t i = 0; i < a.GetNumValues(); ++i) {
173 if (a[i] != rhsA[i]) {
174 return false;
175 }
176 }
177 return true;
178 }
179 }
180
181 // The two Ef_VectorKeys do not hold the same type T or their sizes do
182 // not match, so we consider them unequal.
183 return false;
184 }
185
186};
187
188PXR_NAMESPACE_CLOSE_SCOPE
189
190#endif
The derived Ef_VectorKey type, which implements the methods for generating hashes and equality compar...
Definition: vectorKey.h:126
virtual size_t CreateHash() const
Implementation of the method that generates a hash from the VdfVector holding data of type T.
Definition: vectorKey.h:143
virtual bool IsEqual(const Ef_VectorKey &rhs) const
Implementation of the method that equality compares two Ef_VectorKeys of type T.
Definition: vectorKey.h:158
Ef_TypedVectorKey(const Ef_TypedVectorKey &)=delete
Non-copyable.
Ef_TypedVectorKey(const VdfVector &value)
Constructor.
Definition: vectorKey.h:131
This class wraps a VdfVector adding equality comparison and hashing capabilities to the vector,...
Definition: vectorKey.h:36
virtual size_t CreateHash() const =0
Generates a hash from the VdfVector.
std::shared_ptr< Ef_VectorKey > StoredType
The type which must be used to store Ef_VectorKeys, for example as keys in a hash map.
Definition: vectorKey.h:41
friend void TfHashAppend(HashState &h, const Ef_VectorKey::StoredType &v)
Hashing.
Definition: vectorKey.h:84
bool operator==(const Ef_VectorKey &rhs) const
Equality operator.
Definition: vectorKey.h:73
virtual EF_API ~Ef_VectorKey()
Destructor.
Ef_VectorKey(const Ef_VectorKey &)=delete
Non-copyable.
virtual bool IsEqual(const Ef_VectorKey &) const =0
Equality compares this Ef_VectorKey with another one.
const VdfVector & GetValue() const
Returns the wrapped VdfVector.
Definition: vectorKey.h:55
Type of a hash map with Ef_VectorKey as key.
Definition: vectorKey.h:99
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:472
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
A read-only accessor for low-level acces to the contents of the VdfVector.
Definition: vector.h:469
size_t GetNumValues() const
Returns the size of the vector, i.e.
Definition: vector.h:482
This class is used to abstract away knowledge of the cache data used for each node.
Definition: vector.h:56
ReadAccessor< TYPE > GetReadAccessor() const
GetReadAccessor() allows low level read-only access to the content of of the VdfVector via the Vdf_Ve...
Definition: vector.h:514
Equality compare functor.
Definition: vectorKey.h:90