Skip to content

Commit 4073545

Browse files
Merge pull request #2156 from OceanParcels/hashfunction_for_testing_in_v3
Adding the round_and_hash_float_array also to v3
2 parents fb88390 + 024bfb1 commit 4073545

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

tests/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""General helper functions and utilies for test suite."""
22

3+
import struct
34
from pathlib import Path
45

56
import numpy as np
@@ -116,3 +117,18 @@ def create_fieldset_zeros_simple(xdim=40, ydim=100, withtime=False):
116117

117118
def assert_empty_folder(path: Path):
118119
assert [p.name for p in path.iterdir()] == []
120+
121+
122+
def round_and_hash_float_array(arr, decimals=6):
123+
arr = np.round(arr, decimals=decimals)
124+
125+
# Adapted from https://cs.stackexchange.com/a/37965
126+
h = 1
127+
for f in arr:
128+
# Mimic Float.floatToIntBits: converts float to 4-byte binary, then interprets as int
129+
float_as_int = struct.unpack("!i", struct.pack("!f", f))[0]
130+
h = 31 * h + float_as_int
131+
132+
# Mimic Java's HashMap hash transformation
133+
h ^= (h >> 20) ^ (h >> 12)
134+
return h ^ (h >> 7) ^ (h >> 4)

0 commit comments

Comments
 (0)