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 
22 {
23 public:
24  GA_AttributeHash(const GA_IndexMap &indexmap,
25  const GA_Offset offset,
26  const GA_Attribute *const *attribs,
27  const GA_Size nattribs,
28  const GA_ElementGroup *const *groups = NULL,
29  const GA_Size ngroups = 0)
30  : myIndexMap(indexmap)
31  , myOffset(offset)
32  , myAttribs(attribs)
33  , myNAttribs(nattribs)
34  , myGroups(groups)
35  , myNGroups(ngroups)
36  , myHash(computeHash())
37  {}
39  : myIndexMap(that.myIndexMap)
40  , myOffset(that.myOffset)
41  , myAttribs(that.myAttribs)
42  , myNAttribs(that.myNAttribs)
43  , myGroups(that.myGroups)
44  , myNGroups(that.myNGroups)
45  , myHash(that.myHash)
46  {}
47 
48  bool operator==(const GA_AttributeHash &that) const
49  {
50  if (!myNGroups && !myNAttribs)
51  return true;
52 
53  // The group handling doesn't work properly for
54  // elements in different details.
55  UT_ASSERT_P(&myIndexMap.getDetail() == &that.myIndexMap.getDetail() ||
56  (myNGroups == 0 && that.myNGroups == 0));
57 
58  if (&myIndexMap.getDetail() == &that.myIndexMap.getDetail())
59  {
60  // Check group membership
61  if (myNGroups != that.myNGroups)
62  return false;
63 
64  for (GA_Size i = 0; i < myNGroups; ++i)
65  {
66  if (myGroups[i] != that.myGroups[i])
67  return false;
68  }
69  }
70 
71  UT_ASSERT_P(myNAttribs == that.myNAttribs);
72 
73  // Check attributes
74  const GA_Offset off0 = myOffset;
75  const GA_Offset off1 = that.myOffset;
76  for (GA_Size i = 0; i < myNAttribs; ++i)
77  {
78  const GA_Attribute *attribute = myAttribs[i];
79  const GA_AIFCompare *compare = attribute->getAIFCompare();
80  bool result;
81  if (compare && (!compare->isEqual(
82  result, *attribute, off0, *that.myAttribs[i], off1) || !result))
83  return false;
84  }
85  return true;
86  }
87 
88  const size_t &hash() const { return myHash; }
89 
90 private:
91  size_t computeHash() const {
92  if (!myNGroups && !myNAttribs)
93  return 0;
94 
95  size_t h = 0;
96 
97  // Apply group membership
98  for (GA_Size i = 0; i < myNGroups; ++i)
99  {
100  h ^= SYSwang_inthash(SYSpointerHash(myGroups[i]));
101  }
102 
103  // Apply attributes
104  const GA_Offset off = myOffset;
105  for (GA_Size i = 0; i < myNAttribs; ++i)
106  {
107  const GA_Attribute *attribute = myAttribs[i];
108  const GA_AIFCompare *compare = attribute->getAIFCompare();
109  if (compare)
110  h ^= SYSwang_inthash(compare->hash(*attribute, off));
111  }
112  return h;
113  }
114 private:
115  const GA_Offset myOffset;
116  const GA_IndexMap &myIndexMap;
117  const GA_Attribute *const *myAttribs;
118  const GA_Size myNAttribs;
119  const GA_ElementGroup *const *myGroups;
120  const GA_Size myNGroups;
121  const size_t myHash;
122 };
123 
125 {
126  inline size_t operator()(const GA_AttributeHash &key) const
127  {
128  return key.hash();
129  }
130 };
131 
132 // These class declarations act just like templated typedefs,
133 // which unfortunately, C++ lacks.
134 template <typename ToType>
135 class GA_AttributeHashMap : public UT_Map<GA_AttributeHash, ToType, GA_AttributeHashFunctor >
136 { };
137 
138 #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:107
size_t operator()(const GA_AttributeHash &key) const
**But if you need a result
Definition: thread.h:613
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:235
virtual const GA_AIFCompare * getAIFCompare() const
Return the attribute's comparison interface or NULL.
GA_Size GA_Offset
Definition: GA_Types.h:641
CompareResults OIIO_API compare(const ImageBuf &A, const ImageBuf &B, float failthresh, float warnthresh, ROI roi={}, int nthreads=0)
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)