HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GA_AttributeHash.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: GA_AttributeHash.h (GA Library, C++)
7  *
8  * COMMENTS: Class to act as a hash key for hashing on attribute values
9  * and group memberships of an element.
10  */
11 
12 #ifndef __GA_AttributeHash_h__
13 #define __GA_AttributeHash_h__
14 
15 #include "GA_API.h"
16 #include "GA_Types.h"
17 #include "GA_AIFCompare.h"
18 #include "GA_IndexMap.h"
19 #include <UT/UT_Map.h>
20 #include <SYS/SYS_PointerHash.h>
21 
23 {
24 public:
25  GA_AttributeHash(const GA_IndexMap &indexmap,
26  const GA_Offset offset,
27  const GA_Attribute *const *attribs,
28  const GA_Size nattribs,
29  const GA_ElementGroup *const *groups = NULL,
30  const GA_Size ngroups = 0)
31  : myIndexMap(indexmap)
32  , myOffset(offset)
33  , myAttribs(attribs)
34  , myNAttribs(nattribs)
35  , myGroups(groups)
36  , myNGroups(ngroups)
37  , myHash(computeHash())
38  {}
40  : myIndexMap(that.myIndexMap)
41  , myOffset(that.myOffset)
42  , myAttribs(that.myAttribs)
43  , myNAttribs(that.myNAttribs)
44  , myGroups(that.myGroups)
45  , myNGroups(that.myNGroups)
46  , myHash(that.myHash)
47  {}
48 
49  bool operator==(const GA_AttributeHash &that) const
50  {
51  if (!myNGroups && !myNAttribs)
52  return true;
53 
54  // The group handling doesn't work properly for
55  // elements in different details.
56  UT_ASSERT_P(&myIndexMap.getDetail() == &that.myIndexMap.getDetail() ||
57  (myNGroups == 0 && that.myNGroups == 0));
58 
59  if (&myIndexMap.getDetail() == &that.myIndexMap.getDetail())
60  {
61  // Check group membership
62  if (myNGroups != that.myNGroups)
63  return false;
64 
65  for (GA_Size i = 0; i < myNGroups; ++i)
66  {
67  if (myGroups[i] != that.myGroups[i])
68  return false;
69  }
70  }
71 
72  UT_ASSERT_P(myNAttribs == that.myNAttribs);
73 
74  // Check attributes
75  const GA_Offset off0 = myOffset;
76  const GA_Offset off1 = that.myOffset;
77  for (GA_Size i = 0; i < myNAttribs; ++i)
78  {
79  const GA_Attribute *attribute = myAttribs[i];
80  const GA_AIFCompare *compare = attribute->getAIFCompare();
81  bool result;
82  if (compare && (!compare->isEqual(
83  result, *attribute, off0, *that.myAttribs[i], off1) || !result))
84  return false;
85  }
86  return true;
87  }
88 
89  const size_t &hash() const { return myHash; }
90 
91 private:
92  size_t computeHash() const {
93  if (!myNGroups && !myNAttribs)
94  return 0;
95 
96  size_t h = 0;
97 
98  // Apply group membership
99  for (GA_Size i = 0; i < myNGroups; ++i)
100  {
101  h ^= SYSwang_inthash(SYSpointerHash(myGroups[i]));
102  }
103 
104  // Apply attributes
105  const GA_Offset off = myOffset;
106  for (GA_Size i = 0; i < myNAttribs; ++i)
107  {
108  const GA_Attribute *attribute = myAttribs[i];
109  const GA_AIFCompare *compare = attribute->getAIFCompare();
110  if (compare)
111  h ^= SYSwang_inthash(compare->hash(*attribute, off));
112  }
113  return h;
114  }
115 private:
116  const GA_Offset myOffset;
117  const GA_IndexMap &myIndexMap;
118  const GA_Attribute *const *myAttribs;
119  const GA_Size myNAttribs;
120  const GA_ElementGroup *const *myGroups;
121  const GA_Size myNGroups;
122  const size_t myHash;
123 };
124 
126 {
127  inline size_t operator()(const GA_AttributeHash &key) const
128  {
129  return key.hash();
130  }
131 };
132 
133 // These class declarations act just like templated typedefs,
134 // which unfortunately, C++ lacks.
135 template <typename ToType>
136 class GA_AttributeHashMap : public UT_Map<GA_AttributeHash, ToType, GA_AttributeHashFunctor >
137 { };
138 
139 #endif
A class to manage an ordered array which has fixed offset handles.
Definition: GA_IndexMap.h:63
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
virtual uint hash(const GA_Attribute &a, const GA_Offset i) const
Definition: GA_AIFCompare.h:56
Unsorted map container.
Definition: UT_Map.h:109
CompareResults OIIO_API compare(const ImageBuf &A, const ImageBuf &B, float failthresh, float warnthresh, float failrelative, float warnrelative, ROI roi={}, int nthreads=0)
size_t operator()(const GA_AttributeHash &key) const
**But if you need a result
Definition: thread.h:622
GA_Detail & getDetail() const
Access the detail this index map belongs to.
Definition: GA_IndexMap.h:70
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:236
virtual const GA_AIFCompare * getAIFCompare() const
Return the attribute's comparison interface or NULL.
GA_Size GA_Offset
Definition: GA_Types.h:646
GLintptr offset
Definition: glcorearb.h:665
const size_t & hash() const
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
Attribute Interface class to perform comparisons on attributes.
Definition: GA_AIFCompare.h:27
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
virtual bool isEqual(bool &result, const GA_Attribute &a, GA_Offset ai, const GA_Attribute &b, GA_Offset bi) const =0
OIIO_API bool attribute(string_view name, TypeDesc type, const void *val)
GA_AttributeHash(const GA_AttributeHash &that)
bool operator==(const GA_AttributeHash &that) const
GA_AttributeHash(const GA_IndexMap &indexmap, const GA_Offset offset, const GA_Attribute *const *attribs, const GA_Size nattribs, const GA_ElementGroup *const *groups=NULL, const GA_Size ngroups=0)