From c7aeace7cc67e45076885d868a8fb286c951d2cf Mon Sep 17 00:00:00 2001 From: Ben Arthur Date: Mon, 17 Mar 2025 08:15:00 -0400 Subject: [PATCH 1/5] add 2-arg count method, with tests --- src/mapreduce.jl | 3 ++- test/runtests.jl | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/mapreduce.jl b/src/mapreduce.jl index c6dd0a3..ff89d1e 100644 --- a/src/mapreduce.jl +++ b/src/mapreduce.jl @@ -59,8 +59,9 @@ for fname in [:sum, :prod, :all, :any, :minimum, :maximum] end end +Base.count(v::AbstractDiskArray) = count(identity, v::AbstractDiskArray) function Base.count(f, v::AbstractDiskArray) sum(eachchunk(v)) do chunk count(f, v[chunk...]) end -end \ No newline at end of file +end diff --git a/test/runtests.jl b/test/runtests.jl index 689ec3a..804d562 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1049,3 +1049,9 @@ end @inferred Matrix{Int} map(identity, da) @inferred Matrix{Float64} map(x -> x * 5.0, da) end + +@testset "count" begin + a = ChunkedDiskArray(1:10 .> 3; chunksize=(3, )) + @test count(a) == 7 + @test count(!, a) == 3 +end From e137ee0a40791d51ae35182a5e2862b5946b106a Mon Sep 17 00:00:00 2001 From: Ben Arthur Date: Mon, 17 Mar 2025 12:29:06 -0400 Subject: [PATCH 2/5] add init arg to count method --- src/mapreduce.jl | 7 ++++--- test/runtests.jl | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/mapreduce.jl b/src/mapreduce.jl index ff89d1e..3e98101 100644 --- a/src/mapreduce.jl +++ b/src/mapreduce.jl @@ -59,9 +59,10 @@ for fname in [:sum, :prod, :all, :any, :minimum, :maximum] end end -Base.count(v::AbstractDiskArray) = count(identity, v::AbstractDiskArray) -function Base.count(f, v::AbstractDiskArray) - sum(eachchunk(v)) do chunk +Base.count(v::AbstractDiskArray; init=0) = + count(identity, v::AbstractDiskArray, init=init) +function Base.count(f, v::AbstractDiskArray; init=0) + init .+ sum(eachchunk(v)) do chunk count(f, v[chunk...]) end end diff --git a/test/runtests.jl b/test/runtests.jl index 804d562..23eb91f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1051,7 +1051,8 @@ end end @testset "count" begin - a = ChunkedDiskArray(1:10 .> 3; chunksize=(3, )) - @test count(a) == 7 + a = ChunkedDiskArray(reshape(1:12,3,4) .> 3; chunksize=(3,3)) + @test count(a) == 9 @test count(!, a) == 3 + @test count(a, init=3) == 12 end From 482ce9760c60a097bd61b61d8b6b165d9065cb3d Mon Sep 17 00:00:00 2001 From: Ben Arthur Date: Mon, 17 Mar 2025 13:39:20 -0400 Subject: [PATCH 3/5] Revert "add init arg to count method" This reverts commit e137ee0a40791d51ae35182a5e2862b5946b106a. --- src/mapreduce.jl | 7 +++---- test/runtests.jl | 5 ++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/mapreduce.jl b/src/mapreduce.jl index 3e98101..ff89d1e 100644 --- a/src/mapreduce.jl +++ b/src/mapreduce.jl @@ -59,10 +59,9 @@ for fname in [:sum, :prod, :all, :any, :minimum, :maximum] end end -Base.count(v::AbstractDiskArray; init=0) = - count(identity, v::AbstractDiskArray, init=init) -function Base.count(f, v::AbstractDiskArray; init=0) - init .+ sum(eachchunk(v)) do chunk +Base.count(v::AbstractDiskArray) = count(identity, v::AbstractDiskArray) +function Base.count(f, v::AbstractDiskArray) + sum(eachchunk(v)) do chunk count(f, v[chunk...]) end end diff --git a/test/runtests.jl b/test/runtests.jl index 23eb91f..804d562 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1051,8 +1051,7 @@ end end @testset "count" begin - a = ChunkedDiskArray(reshape(1:12,3,4) .> 3; chunksize=(3,3)) - @test count(a) == 9 + a = ChunkedDiskArray(1:10 .> 3; chunksize=(3, )) + @test count(a) == 7 @test count(!, a) == 3 - @test count(a, init=3) == 12 end From 6c71a8791d512c838b74ce654160304cc56d3374 Mon Sep 17 00:00:00 2001 From: Ben Arthur Date: Mon, 17 Mar 2025 13:43:46 -0400 Subject: [PATCH 4/5] add 1-arg method to sum, prod, etc too --- src/mapreduce.jl | 1 + test/runtests.jl | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/mapreduce.jl b/src/mapreduce.jl index ff89d1e..a28e431 100644 --- a/src/mapreduce.jl +++ b/src/mapreduce.jl @@ -52,6 +52,7 @@ end # Implementation for special cases and if fallback breaks in future julia versions for fname in [:sum, :prod, :all, :any, :minimum, :maximum] + @eval Base.$fname(v::AbstractDiskArray) = Base.$fname(identity, v::AbstractDiskArray) @eval function Base.$fname(f::Function, v::AbstractDiskArray) $fname(eachchunk(v)) do chunk $fname(f, v[chunk...]) diff --git a/test/runtests.jl b/test/runtests.jl index 804d562..78ca9d9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1050,8 +1050,8 @@ end @inferred Matrix{Float64} map(x -> x * 5.0, da) end -@testset "count" begin +@testset "identity function" begin a = ChunkedDiskArray(1:10 .> 3; chunksize=(3, )) - @test count(a) == 7 - @test count(!, a) == 3 + @test count(a) == sum(a) == 7 + @test count(!, a) == sum(!, a) == 3 end From 0056bd1821e561a09a1dbf031e5416f3d8744e37 Mon Sep 17 00:00:00 2001 From: Ben Arthur Date: Wed, 19 Mar 2025 12:48:56 -0400 Subject: [PATCH 5/5] better tests --- Project.toml | 6 +++++- test/runtests.jl | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index e16fa4d..a09a9f8 100644 --- a/Project.toml +++ b/Project.toml @@ -14,13 +14,17 @@ LRUCache = "1" Mmap = "1" OffsetArrays = "1" Statistics = "1.9" +Suppressor = "0.2.8" Test = "1.9" +TraceFuns = "0.2.0" julia = "1.9" [extras] Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb" +TraceFuns = "626f57eb-f6ca-464c-8f04-384fee2a352e" [targets] -test = ["Aqua", "Statistics", "Test"] +test = ["Aqua", "Statistics", "Test", "Suppressor", "TraceFuns"] diff --git a/test/runtests.jl b/test/runtests.jl index 78ca9d9..8ecefa2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,6 +4,7 @@ using DiskArrays.TestTypes using Test using Statistics using Aqua +using TraceFuns, Suppressor # Run with any code changes # using JET @@ -1052,6 +1053,9 @@ end @testset "identity function" begin a = ChunkedDiskArray(1:10 .> 3; chunksize=(3, )) - @test count(a) == sum(a) == 7 - @test count(!, a) == sum(!, a) == 3 + for fname in [:sum, :prod, :all, :any, :minimum, :maximum, :count] + @eval out = @capture_out @trace $fname($a) DiskArrays + @test occursin("DiskGenerator", out) == false + end + @test count(a) + count(!, a) == length(a) end