Skip to content

Commit

Permalink
Use uint64_t for Vector.hash() function
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanhogg committed Nov 20, 2024
1 parent d17eb35 commit 01b5e47
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/flitter/language/noise.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import cython

from libc.math cimport floor
from libc.stdint cimport int64_t
from libc.stdint cimport int64_t, uint64_t

from .functions cimport uniform, shuffle
from ..model cimport Vector, null_
Expand Down Expand Up @@ -1643,8 +1643,8 @@ cdef Vector _noise(Vector perm, list args):
return result


cdef Vector get_perm(Vector seed, int64_t i):
cdef int64_t seed_hash = seed.hash(True) ^ <int64_t>i
cdef Vector get_perm(Vector seed, uint64_t i):
cdef uint64_t seed_hash = seed.hash(True) ^ i
cdef uniform prng
cdef Vector perm = <Vector>PermCache.get(seed_hash)
if perm is None:
Expand Down
6 changes: 3 additions & 3 deletions src/flitter/model.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ cdef class Vector:
cdef tuple objects
cdef double* numbers
cdef double[16] _numbers
cdef int64_t _hash
cdef uint64_t _hash

@staticmethod
cdef Vector _coerce(object other)
Expand All @@ -59,7 +59,7 @@ cdef class Vector:
cdef double as_double(self) noexcept
cdef int64_t as_integer(self) noexcept
cdef str as_string(self)
cpdef int64_t hash(self, bint floor_floats)
cpdef uint64_t hash(self, bint floor_floats)
cpdef object match(self, int64_t n=?, type t=?, default=?)
cdef str repr(self)
cdef Vector neg(self)
Expand Down Expand Up @@ -200,7 +200,7 @@ cdef class Node:
cdef bint _attributes_shared
cdef tuple _children

cdef int64_t hash(self)
cdef uint64_t hash(self)
cpdef Node copy(self)
cpdef void add_tag(self, str tag)
cpdef void set_attribute(self, str name, Vector value)
Expand Down
10 changes: 5 additions & 5 deletions src/flitter/model.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,9 @@ cdef class Vector:
yield self.numbers[i]

def __hash__(self):
return self.hash(False)
return <int64_t>self.hash(False)

cpdef int64_t hash(self, bint floor_floats):
cpdef uint64_t hash(self, bint floor_floats):
if not floor_floats and self._hash:
return self._hash
cdef uint64_t y, _hash = HASH_START
Expand Down Expand Up @@ -2186,9 +2186,9 @@ cdef class Node:
self._children = children

def __hash__(self):
return self.hash()
return <int64_t>self.hash()

cdef int64_t hash(self):
cdef uint64_t hash(self):
cdef uint64_t _hash = HASH_START
_hash = HASH_UPDATE(_hash, HASH_STRING(self.kind))
if self._tags is not None:
Expand All @@ -2207,7 +2207,7 @@ cdef class Node:
if self._children is not ():
for child in self._children:
_hash = HASH_UPDATE(_hash, (<Node>child).hash())
return <int64_t>_hash
return _hash

@property
def children(self):
Expand Down
4 changes: 2 additions & 2 deletions src/flitter/render/window/models.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ cdef class Transform(UnaryOperation):
cdef Model _get(Model original, Matrix44 transform_matrix):
if transform_matrix.eq(IdentityTransform) is true_:
return original
cdef str name = f'{original.name}@{hex(transform_matrix.hash(False))[3:]}'
cdef str name = f'{original.name}@{hex(transform_matrix.hash(False))[2:]}'
cdef Transform model = <Transform>ModelCache.get(name, None)
if model is None:
model = Transform.__new__(Transform)
Expand Down Expand Up @@ -561,7 +561,7 @@ cdef class Slice(UnaryOperation):
cdef Slice _get(Model original, Vector origin, Vector normal):
if origin.numbers == NULL or origin.length != 3 or normal.numbers == NULL or normal.length != 3:
return None
cdef str name = f'slice({original.name}, {hex(origin.hash(False) ^ normal.hash(False))[3:]})'
cdef str name = f'slice({original.name}, {hex(origin.hash(False) ^ normal.hash(False))[2:]})'
cdef Slice model = <Slice>ModelCache.get(name, None)
if model is None:
model = Slice.__new__(Slice)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@ def test_hash(self):
self.assertIsNotNone(hash(Vector(test_class))) # just check it works, value will not be stable

def test_hash_floor_floats(self):
self.assertEqual(null.hash(True), hash(null))
self.assertEqual(Vector(0.1).hash(True), hash(Vector(0)))
self.assertEqual(Vector("Hello world!").hash(True), hash(Vector("Hello world!")))
self.assertEqual(Vector(["foo", 1]).hash(True), hash(Vector(["foo", 1.0])))
self.assertEqual(Vector(["foo", 1.1]).hash(True), hash(Vector(["foo", 1.0])))
self.assertEqual(null.hash(True), null.hash(False))
self.assertEqual(Vector(0.1).hash(True), Vector(0).hash(True))
self.assertEqual(Vector("Hello world!").hash(True), Vector("Hello world!").hash(False))
self.assertEqual(Vector(["foo", 1]).hash(True), Vector(["foo", 1.0]).hash(True))
self.assertEqual(Vector(["foo", 1.1]).hash(True), Vector(["foo", 1.0]).hash(True))

def test_hash_uniformity(self):
from scipy.stats import kstest
Expand Down

0 comments on commit 01b5e47

Please sign in to comment.