Skip to content

Make hash of OffsetInteger consistent with isequal - #291

Open
KainanP wants to merge 1 commit into
JuliaGeometry:masterfrom
KainanP:offsetinteger-hash-288
Open

KainanP wants to merge 1 commit into
JuliaGeometry:masterfrom
KainanP:offsetinteger-hash-288

Conversation

@KainanP

@KainanP KainanP commented Dec 6, 2026

Copy link
Copy Markdown

Fixes #288

==/isequal compare OffsetIntegers by value, also across different offsets and against plain integers (through promote_rule). hash instead mixed the raw stored integer with the offset, so equal values could hash differently:

julia> isequal(ZeroIndex(123), OneIndex(123)), hash(ZeroIndex(123)) == hash(OneIndex(123))
(true, false)

julia> isequal(OneIndex(123), 123), hash(OneIndex(123)) == hash(123)
(true, false)

This breaks hashed collections: Dict(ZeroIndex(10) => :a)[OneIndex(10)] throws a KeyError, and Set{Any}((ZeroIndex(1), OneIndex(1), 1)) has 3 elements.

The method is now hash(value(o), h), so an OffsetInteger hashes like the integer it is equal to. It is a one-line change in src/offsetintegers.jl.

One side effect: since the hashes now match, a Dict or Set holding an OffsetInteger and an equal Float64 has to call isequal on the pair, and that throws, because OneIndex(1) == 1.0 already throws on master ("promotion of types OneIndex{Int64} and Float64 failed to change any arguments"). Before, Set{Any}(Any[OneIndex(1), 1.0]) silently kept both elements. Bool behaves the same until #289 is merged. Comparison with floats is a separate problem, so I left it out of this PR.

Tests: a new block in the Offsetintegers testset checks isequal(a, b) and hash(a) == hash(b) (with and without a seed) for different offsets, element types (Int, GLIndex, Int8 vs UInt8) and plain integers, plus Set and Dict lookups. On master it gives 14 failures and 1 error; with the change it passes on Julia 1.13.4 and 1.10.14. The full Pkg.test() passes locally on Julia 1.13.4 (1351 pass, plus the 2 existing @test_broken).

This doesn't touch the lines changed by #289 or #290. I checked that it applies cleanly on top of both and that the full test suite still passes there.

This PR was written with an AI assistant (Claude Code).

This merges cleanly with #289 and #290. If you'd rather fold the hash change into your series, feel free to close this one.

🤖 Generated with Claude Code

`==`/`isequal` compare `OffsetInteger`s by value, also across different
offsets and against plain integers (via `promote_rule`), but `hash` mixed
the raw stored integer with the offset. So `isequal(ZeroIndex(123),
OneIndex(123))` was `true` while their hashes differed, which breaks
`Dict`/`Set` lookups. Hash the value instead, like the integer it equals.

Fixes JuliaGeometry#288

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistency between the definitions of == and hash for OffsetInteger

1 participant