diff --git a/src/mapreduce.jl b/src/mapreduce.jl index 245983e..be6dfa4 100644 --- a/src/mapreduce.jl +++ b/src/mapreduce.jl @@ -47,3 +47,14 @@ macro implement_mapreduce(t) end end end + + +# optimized implementation for special cases + +for fname in [:sum,:prod,:count,:all,:any,:minimum,:maximum] + @eval function Base.$fname(v::AbstractDiskArray) + $fname(eachchunk(v)) do chunk + $fname(v[chunk...]) + end + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 49cbc1c..bfd8fef 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -907,10 +907,23 @@ end @test getindex_count(A) == 0 end +@testset "optimization for associative reductions" begin + A = rand(1:10,30,30) + DA = AccessCountDiskArray(A,chunksize=(2,2)) + for fun in (sum, prod, maximum, minimum) + @test fun(A) == fun(DA) + end + + Ab = rand(Bool,30,30) + DAb = AccessCountDiskArray(Ab,chunksize=(2,2)) + for fun in (count, all, any) + @test fun(Ab) == fun(DAb) + end +end # @test offsets == [[1:1,2:3,4:4],[5:5,6:6,7:7],[8:8,9:9]] # inds = [1,1,1,3,5,6,6,7,10,13,16,16,19,20] # readranges, offsets = find_subranges_sorted(inds,false) # @test readranges == [1:1, 3:3, 5:7, 10:10, 13:13, 16:16, 19:20] -# @test offsets == [[1:3], [4:4], [5:5,6:7,8:8], [9:9], [10:10], [11:12], [13:13,14:14]] \ No newline at end of file +# @test offsets == [[1:3], [4:4], [5:5,6:7,8:8], [9:9], [10:10], [11:12], [13:13,14:14]]