HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
10 ///\file
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 
23 
24 ///////////////////////////////////////////////////////////////////////////////
25 ///
26 /// \class Ef_VectorKey
27 ///
28 /// \brief This class wraps a VdfVector adding equality comparison and hashing
29 /// capabilities to the vector, without requiring all types stored in
30 /// VdfVector to implement the corresponding operators. Only the types
31 /// wrapped in Ef_VectorKey must provide these operators.
32 ///
33 /// This is an abstract base class.
34 ///
36 {
37 public:
38  /// The type which must be used to store Ef_VectorKeys, for example as
39  /// keys in a hash map.
40  ///
41  using StoredType = std::shared_ptr<Ef_VectorKey>;
42 
43  /// Non-copyable.
44  ///
45  Ef_VectorKey(const Ef_VectorKey&) = delete;
46  Ef_VectorKey& operator=(const Ef_VectorKey&) = delete;
47 
48  /// Destructor.
49  ///
50  EF_API
51  virtual ~Ef_VectorKey();
52 
53  /// Returns the wrapped VdfVector.
54  ///
55  const VdfVector &GetValue() const {
56  return _value;
57  }
58 
59  /// Generates a hash from the VdfVector.
60  ///
61  /// This must be implemented by the derived Ef_TypedVectorKey.
62  ///
63  virtual size_t CreateHash() const = 0;
64 
65  /// Equality compares this Ef_VectorKey with another one.
66  ///
67  /// This must be implemented by the derived Ef_TypedVectorKey.
68  ///
69  virtual bool IsEqual(const Ef_VectorKey &) const = 0;
70 
71  /// Equality operator.
72  ///
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 
81  /// Hashing.
82  ///
83  template <class HashState>
84  friend void TfHashAppend(HashState &h, const Ef_VectorKey::StoredType &v) {
85  h.Append(v->CreateHash());
86  }
87 
88  /// Equality compare functor.
89  ///
90  struct Equal {
91  size_t operator()(const StoredType &lhs, const StoredType &rhs) const {
92  return lhs->IsEqual(*rhs);
93  }
94  };
95 
96  /// Type of a hash map with Ef_VectorKey as key.
97  ///
98  template < typename T >
99  struct Map {
100  using Type = TfHashMap<
102  };
103 
104 protected:
105  // Constructor.
106  explicit Ef_VectorKey(const VdfVector &value) :
107  _value(value)
108  {}
109 
110  // The wrapped VdfVector.
112 
113 };
114 
115 
116 ///////////////////////////////////////////////////////////////////////////////
117 ///
118 /// \class Ef_TypedVectorKey
119 ///
120 /// \brief The derived Ef_VectorKey type, which implements the methods for
121 /// generating hashes and equality comparing Ef_VectorKeys with wrapped
122 /// VdfVectors of type T.
123 ///
124 template < typename T >
125 class Ef_TypedVectorKey final : public Ef_VectorKey
126 {
127 public:
128 
129  /// Constructor.
130  ///
131  explicit Ef_TypedVectorKey(const VdfVector &value) :
132  Ef_VectorKey(value)
133  {}
134 
135  /// Non-copyable.
136  ///
137  Ef_TypedVectorKey(const Ef_TypedVectorKey&) = delete;
139 
140  /// Implementation of the method that generates a hash from the
141  /// VdfVector holding data of type T.
142  ///
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 
152  /// Implementation of the method that equality compares two Ef_VectorKeys
153  /// of type T.
154  ///
155  /// Note, that if two Ef_VectorKeys do not hold the same type T, they
156  /// will be considered unequal by design.
157  ///
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 
189 
190 #endif
bool operator!=(const Ef_VectorKey &rhs) const
Definition: vectorKey.h:77
The derived Ef_VectorKey type, which implements the methods for generating hashes and equality compar...
Definition: vectorKey.h:125
const GLdouble * v
Definition: glcorearb.h:837
Ef_VectorKey(const VdfVector &value)
Definition: vectorKey.h:106
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
This class wraps a VdfVector adding equality comparison and hashing capabilities to the vector...
Definition: vectorKey.h:35
const VdfVector & GetValue() const
Definition: vectorKey.h:55
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
#define EF_API_TYPE
Definition: api.h:26
Definition: hash.h:472
std::shared_ptr< Ef_VectorKey > StoredType
Definition: vectorKey.h:41
friend void TfHashAppend(HashState &h, const Ef_VectorKey::StoredType &v)
Definition: vectorKey.h:84
virtual bool IsEqual(const Ef_VectorKey &rhs) const
Definition: vectorKey.h:158
Ef_TypedVectorKey & operator=(const Ef_TypedVectorKey &)=delete
Ef_TypedVectorKey(const VdfVector &value)
Definition: vectorKey.h:131
size_t operator()(const StoredType &lhs, const StoredType &rhs) const
Definition: vectorKey.h:91
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
ReadAccessor< TYPE > GetReadAccessor() const
Definition: vector.h:521
LeafData & operator=(const LeafData &)=delete
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
size_t GetNumValues() const
Definition: vector.h:489
virtual size_t CreateHash() const
Definition: vectorKey.h:143
bool operator==(const Ef_VectorKey &rhs) const
Definition: vectorKey.h:73
const VdfVector _value
Definition: vectorKey.h:111
#define EF_API
Definition: api.h:25