From 42a1b1099118764330ba683a60c04bde9c30a12e Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 1 Sep 2026 00:15:28 +0400 Subject: [PATCH] Define hash for PointSpace and DiracSpace Space defines == (Space.jl:165) as spacescompatible && equal domains, but no matching hash, so these spaces fell back to the identity-based default. Two equal-valued spaces therefore hashed differently and could not be used as Set elements or Dict keys: julia> PointSpace(1:2) == PointSpace(1:2) true julia> hash(PointSpace(1:2)) == hash(PointSpace(1:2)) false For both types spacescompatible reduces to comparing the sorted points, and domain is derived from the same field, so hashing the points reproduces == exactly. The type is mixed in so PointSpace and DiracSpace with equal points do not collide, matching == which is only defined within each type. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01DZeBMyvW7qYiDfC7Tkb1vT --- src/Spaces/DiracSpace.jl | 2 ++ test/SpacesTest.jl | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Spaces/DiracSpace.jl b/src/Spaces/DiracSpace.jl index e0e10f54..41fdcbe3 100644 --- a/src/Spaces/DiracSpace.jl +++ b/src/Spaces/DiracSpace.jl @@ -30,6 +30,8 @@ for TYP in (:DiracSpace,:PointSpace) spacescompatible(a::$TYP,b::$TYP) = a.points == b.points canonicalspace(a::$TYP) = a + Base.hash(a::$TYP, h::UInt) = hash(a.points, hash($TYP, h)) + union_rule(a::$TYP,b::$TYP) = $TYP(sort(union(a.points,b.points))) function coefficients(cfs::AbstractVector,fromspace::$TYP,tospace::$TYP) diff --git a/test/SpacesTest.jl b/test/SpacesTest.jl index 6c520c1c..e2db7f81 100644 --- a/test/SpacesTest.jl +++ b/test/SpacesTest.jl @@ -213,6 +213,24 @@ using Test f = Fun(PointSpace(1:4), ones(4)) @test ApproxFunBase.isconstantfun(f) end + + @testset "hash" begin + DS = ApproxFunBase.DiracSpace + for T in (PointSpace, DS) + a, b = T(1:2), T(1:2) + @test a == b + @test hash(a) == hash(b) + @test isequal(a, b) + @test length(Set([a, b])) == 1 + @test Dict(a => 1)[b] == 1 + @test Set([T(1:2), T(3:4)]) == Set([T(3:4), T(1:2)]) + @test hash(T(1:2)) != hash(T(3:4)) + @test T([1, 2]) == T([1.0, 2.0]) + @test hash(T([1, 2])) == hash(T([1.0, 2.0])) + end + @test PointSpace(1:2) != DS(1:2) + @test hash(PointSpace(1:2)) != hash(DS(1:2)) + end end @testset "DiracSpace" begin