Skip to content

Commit f064e1b

Browse files
authored
forward repeat(A, cts..) to repeat(A; kw..) (#49830)
This would mean that custom array types that seek to extend `repeat` only need to add the method `repeat(A::CustomArray; kw...)`
1 parent 6db867b commit f064e1b

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

base/abstractarraymath.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ julia> repeat([1, 2, 3], 2, 3)
353353
```
354354
"""
355355
function repeat(A::AbstractArray, counts...)
356-
return _RepeatInnerOuter.repeat(A, outer=counts)
356+
return repeat(A, outer=counts)
357357
end
358358

359359
"""

test/abstractarray.jl

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ using .Main.InfiniteArrays
88
isdefined(Main, :StructArrays) || @eval Main include("testhelpers/StructArrays.jl")
99
using .Main.StructArrays
1010

11+
isdefined(Main, :FillArrays) || @eval Main include("testhelpers/FillArrays.jl")
12+
using .Main.FillArrays
13+
1114
A = rand(5,4,3)
1215
@testset "Bounds checking" begin
1316
@test checkbounds(Bool, A, 1, 1, 1) == true
@@ -1936,3 +1939,14 @@ f45952(x) = [x;;]
19361939
@test_throws "invalid index: true of type Bool" isassigned(A, 1, true)
19371940
@test_throws "invalid index: true of type Bool" isassigned(A, true)
19381941
end
1942+
1943+
@testset "repeat for FillArrays" begin
1944+
f = FillArrays.Fill(3, (4,))
1945+
@test repeat(f, 2) === FillArrays.Fill(3, (8,))
1946+
@test repeat(f, 2, 3) === FillArrays.Fill(3, (8, 3))
1947+
@test repeat(f, inner=(1,2), outer=(3,1)) === repeat(f, 3, 2) === FillArrays.Fill(3, (12,2))
1948+
f = FillArrays.Fill(3, (4, 2))
1949+
@test repeat(f, 2, 3) === FillArrays.Fill(3, (8, 6))
1950+
@test repeat(f, 2, 3, 4) === FillArrays.Fill(3, (8, 6, 4))
1951+
@test repeat(f, inner=(1,2), outer=(3,1)) === FillArrays.Fill(3, (12, 4))
1952+
end

test/testhelpers/FillArrays.jl

+27
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,31 @@ end
3232
Base.show(io::IO, F::Fill) = print(io, "Fill($(F.value), $(F.size))")
3333
Base.show(io::IO, ::MIME"text/plain", F::Fill) = show(io, F)
3434

35+
_first_or_one(t::Tuple) = t[1]
36+
_first_or_one(t::Tuple{}) = 1
37+
38+
_match_size(sz::Tuple{}, inner::Tuple{}, outer::Tuple{}) = ()
39+
function _match_size(sz::Tuple, inner::Tuple, outer::Tuple)
40+
t1 = (_first_or_one(sz), _first_or_one(inner), _first_or_one(outer))
41+
t2 = _match_size(sz[2:end], inner[2:end], outer[2:end])
42+
(t1, t2...)
43+
end
44+
45+
function _repeat_size(sz::Tuple, inner::Tuple, outer::Tuple)
46+
t = _match_size(sz, inner, outer)
47+
map(*, getindex.(t, 1), getindex.(t, 2), getindex.(t, 3))
48+
end
49+
50+
function Base.repeat(A::Fill; inner=ntuple(x->1, ndims(A)), outer=ntuple(x->1, ndims(A)))
51+
Base.require_one_based_indexing(A)
52+
length(inner) >= ndims(A) ||
53+
throw(ArgumentError("number of inner repetitions $(length(inner)) cannot be "*
54+
"less than number of dimensions of input array $(ndims(A))"))
55+
length(outer) >= ndims(A) ||
56+
throw(ArgumentError("number of outer repetitions $(length(outer)) cannot be "*
57+
"less than number of dimensions of input array $(ndims(A))"))
58+
sz = _repeat_size(size(A), Tuple(inner), Tuple(outer))
59+
Fill(getindex_value(A), sz)
60+
end
61+
3562
end

0 commit comments

Comments
 (0)