I'm looking for advice on building a better hash value to reduce collisions for an array expected to hold 100,000 vertices.
The hash property is part of a struct designed to hold vertex data, each vertex would hold 8 float values. The float values can be negative and extend 15 spaces passed the decimal point.
I made two computed hashValue properties
First Version
The first version creates a String from the 8 properties of the Vertex struct, and returns the hash value of the string.
var hashValue: Int {
return "\(self.x),\(self.y),\(self.z),\(self.nx),\(self.ny),\(self.nz),\(self.s),\(self.t),".hashValue
}
Second Version
The second version loops through all the properties in Vertex struct. Multiplies to move the decimal place 15 spaces to the right, then makes the value absolute before finally changing the value's type from Float to Int and adding to the hash total.
var hashValue: Int {
let array = [self.x, self.y, self.z, self.nx, self.ny, self.nz, self.s, self.t]
var hash = 0
for number in array {
let absolute = abs(number * 1e15)
let int = Int(absolute)
hash += int
}
return hash
}
Both versions ended up producing similar hash collision counts. I was hoping to reduce it further.
All the examples I've seen regarding hash values have been structs with two properties like this.
var hashValue: Int {
return x.hashValue ^ y.hashValue
}
I'm not sure if my approach to creating the hash value for a struct with 8 properties is the correct way to go. Any advice would be appreciated, thanks for reading.
Just wanted to clarify, this hash value is for the vertices stored in the array, not the array itself. Sorry for any confusion.