Viskores  1.0
Hash.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_Hash_h
19 #define viskores_Hash_h
20 
21 #include <viskores/TypeTraits.h>
22 #include <viskores/Types.h>
23 #include <viskores/VecTraits.h>
24 
25 namespace viskores
26 {
27 
29 
30 namespace detail
31 {
32 
33 static constexpr viskores::HashType FNV1A_OFFSET = 2166136261;
34 static constexpr viskores::HashType FNV1A_PRIME = 16777619;
35 
38 template <typename InVecType>
39 VISKORES_EXEC_CONT inline viskores::HashType HashFNV1a32(const InVecType& inVec)
40 {
41  using Traits = viskores::VecTraits<InVecType>;
42  const viskores::IdComponent numComponents = Traits::GetNumberOfComponents(inVec);
43 
44  viskores::HashType hash = FNV1A_OFFSET;
45  for (viskores::IdComponent index = 0; index < numComponents; index++)
46  {
47  viskores::HashType dataBits =
48  static_cast<viskores::HashType>(Traits::GetComponent(inVec, index));
49  hash = (hash * FNV1A_PRIME) ^ dataBits;
50  }
51 
52  return hash;
53 }
54 
57 template <typename InVecType>
58 VISKORES_EXEC_CONT inline viskores::HashType HashFNV1a64(const InVecType& inVec)
59 {
60  using Traits = viskores::VecTraits<InVecType>;
61  const viskores::IdComponent numComponents = Traits::GetNumberOfComponents(inVec);
62 
63  viskores::HashType hash = FNV1A_OFFSET;
64  for (viskores::IdComponent index = 0; index < numComponents; index++)
65  {
66  viskores::UInt64 allDataBits =
67  static_cast<viskores::UInt64>(Traits::GetComponent(inVec, index));
68  viskores::HashType upperDataBits =
69  static_cast<viskores::HashType>((allDataBits & 0xFFFFFFFF00000000L) >> 32);
70  hash = (hash * FNV1A_PRIME) ^ upperDataBits;
71  viskores::HashType lowerDataBits =
72  static_cast<viskores::HashType>(allDataBits & 0x00000000FFFFFFFFL);
73  hash = (hash * FNV1A_PRIME) ^ lowerDataBits;
74  }
75 
76  return hash;
77 }
78 
79 // If you get a compile error saying that there is no implementation of the class HashChooser,
80 // then you have tried to make a hash from an invalid type (like a float).
81 template <typename NumericTag, std::size_t DataSize>
82 struct HashChooser;
83 
84 template <>
85 struct HashChooser<viskores::TypeTraitsIntegerTag, 4>
86 {
87  template <typename InVecType>
88  VISKORES_EXEC_CONT static viskores::HashType Hash(const InVecType& inVec)
89  {
90  return viskores::detail::HashFNV1a32(inVec);
91  }
92 };
93 
94 template <>
95 struct HashChooser<viskores::TypeTraitsIntegerTag, 8>
96 {
97  template <typename InVecType>
98  VISKORES_EXEC_CONT static viskores::HashType Hash(const InVecType& inVec)
99  {
100  return viskores::detail::HashFNV1a64(inVec);
101  }
102 };
103 
104 } // namespace detail
105 
116 template <typename InVecType>
117 VISKORES_EXEC_CONT inline viskores::HashType Hash(const InVecType& inVec)
118 {
120  using ComponentType = typename VecTraits::ComponentType;
121  using ComponentTraits = viskores::TypeTraits<ComponentType>;
122  using Chooser = detail::HashChooser<typename ComponentTraits::NumericTag, sizeof(ComponentType)>;
123  return Chooser::Hash(inVec);
124 }
125 
126 } // namespace viskores
127 
128 #endif //viskores_Hash_h
Types.h
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
TypeTraits.h
viskores::TypeTraits
The TypeTraits class provides helpful compile-time information about the basic types used in Viskores...
Definition: TypeTraits.h:69
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:69
viskores::UInt64
unsigned long long UInt64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:215
viskores::Hash
viskores::HashType Hash(const InVecType &inVec)
Returns a 32-bit hash on a group of integer-type values.
Definition: Hash.h:117
viskores::VecTraits::ComponentType
T ComponentType
Type of the components in the vector.
Definition: VecTraits.h:79
viskores::HashType
viskores::UInt32 HashType
Definition: Hash.h:28
viskores::UInt32
uint32_t UInt32
Base type to use for 32-bit unsigned integer numbers.
Definition: Types.h:193
viskores::TypeTraitsIntegerTag
Tag used to identify types that store integer numbers.
Definition: TypeTraits.h:44
VecTraits.h