HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
maskMemoizer.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_MASK_MEMOIZER_H
8 #define PXR_EXEC_VDF_MASK_MEMOIZER_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/mask.h"
15 
16 #include "pxr/base/tf/hash.h"
17 
18 #include <utility>
19 
21 
22 /// Memoizes the results of mask append (union) operations.
23 ///
24 /// MapType must provide find and insert methods compatible with the standard
25 /// library associative container API. MapType must accept Key, Mapped, Hash
26 /// and Eq types as its first four template parameters with the same meaning
27 /// as the standard unordered associative containers. References returned by
28 /// Append have that same invalidation policy as the MapType. For example,
29 /// references from VdfMaskMemoizer<TfHashMap> are valid for the lifetime of
30 /// the memoizer but calling VdfMaskMemoizer<flat_map>::Append invalidates
31 /// *all* references.
32 template <template <typename...> class MapType>
34 {
35 public:
36  /// Append \p lhs and \p rhs and return the result.
37  /// Returns a cached result, if available.
38  const VdfMask &Append(const VdfMask &lhs, const VdfMask &rhs) {
39  _Key key{lhs, rhs};
40 
41  typename _Cache::iterator it = _appended.find(key);
42  if (it != _appended.end()) {
43  return it->second;
44  }
45 
46  return _appended.insert({std::move(key), lhs | rhs}).first->second;
47  }
48 
49 private:
50 
51  // Operations are keyed off of the lhs and rhs operands.
52  using _Key = std::pair<VdfMask, VdfMask>;
53 
54  // Produce a combination of lhs and rhs hash values as the hash for the
55  // key.
56  struct _Hash {
57  size_t operator()(const _Key &v) const {
58  return TfHash::Combine(v.first.GetHash(), v.second.GetHash());
59  }
60  };
61 
62  // The cache for append operations.
63  using _Cache = MapType<_Key, VdfMask, _Hash>;
64  _Cache _appended;
65 };
66 
68 
69 #endif
GLint first
Definition: glcorearb.h:405
const GLdouble * v
Definition: glcorearb.h:837
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:36
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
const VdfMask & Append(const VdfMask &lhs, const VdfMask &rhs)
Definition: maskMemoizer.h:38
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74