Skip to content

Add equals_layout hook for == on LayoutArrays - #284

Merged
dlfivefifty merged 2 commits into
masterfrom
equals_layout
Oct 7, 2026
Merged

dlfivefifty merged 2 commits into
masterfrom
equals_layout

Conversation

@dlfivefifty

Copy link
Copy Markdown
Member

Adds a layout hook for equality, alongside mapreduce_layout and copyto!_layout:

equals_layout(_, _, A, B) = invoke(==, Tuple{AbstractArray,AbstractArray}, A, B)
equals_layout(A, B) = equals_layout(MemoryLayout(A), MemoryLayout(B), A, B)
==(A::LayoutArray, B::AbstractArray) = equals_layout(A, B)
# ... plus the mirrored and SubArray{<:Any,<:Any,<:LayoutArray} variants

== routes through the hook whenever either argument is a LayoutArray or a view of one. The default falls back to Base's generic AbstractArray comparison, so behaviour doesn't change unless a layout opts in. Layouts can then specialise equality, for example to compare structured data directly rather than entry by entry.

Notes

  • The SubArray overloads don't constrain the dimension, so comparing views of different dimensions isn't ambiguous.
  • The new methods are ambiguous with SparseArrays.ReadOnly's ==. ArrayLayoutsSparseArraysExt adds resolving methods, guarded by isdefined(SparseArrays, :ReadOnly).
  • Test.detect_ambiguities over ArrayLayouts, Base, LinearAlgebra, FillArrays and SparseArrays reports no == ambiguities.
  • Downstream packages that define ==(::AbstractArray, ::SomeType) or its mirror may see new ambiguities when one side is a LayoutArray.

Tests

A new == testset in test/test_layoutarray.jl covers comparisons in both directions between layout arrays, plain arrays and views. It also includes size mismatches and a custom layout that overloads equals_layout. The full test suite passes locally.

🤖 Generated with Claude Code

`==` with a LayoutArray (or a view of one) on either side now reduces to
`equals_layout(MemoryLayout(A), MemoryLayout(B), A, B)`, which can be
overloaded per layout. The default falls back to the generic
AbstractArray implementation in Base, so behaviour is unchanged unless a
layout opts in.

Also resolves the resulting ambiguities with SparseArrays.ReadOnly in the
SparseArrays extension.

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

codecov Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.33%. Comparing base (be85225) to head (3f26a0e).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #284      +/-   ##
==========================================
+ Coverage   90.26%   90.33%   +0.06%     
==========================================
  Files          12       12              
  Lines        1973     1987      +14     
==========================================
+ Hits         1781     1795      +14     
  Misses        192      192              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dlfivefifty
dlfivefifty merged commit 19a94cb into master Oct 7, 2026
20 checks passed
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.

1 participant