steel_core/behavior/blocks/redstone/java_hash.rs
1//! Small-map iteration helpers matching the target JDK's `HashMap`.
2
3use steel_utils::BlockPos;
4
5/// Sorts unique positions into Java `HashMap` iteration order while its table
6/// remains at the initial 16 buckets.
7///
8/// Java walks buckets from low to high and preserves insertion order within a
9/// collision chain. Stable sorting by the bucket therefore reproduces that
10/// order exactly.
11pub(super) fn sort_small_map_positions(positions: &mut [BlockPos]) {
12 positions.sort_by_key(|pos| bucket(*pos));
13}
14
15#[must_use]
16pub(super) const fn bucket(pos: BlockPos) -> u32 {
17 let hash = pos
18 .z()
19 .wrapping_mul(31)
20 .wrapping_add(pos.y())
21 .wrapping_mul(31)
22 .wrapping_add(pos.x()) as u32;
23 (hash ^ (hash >> 16)) & 15
24}