@@ -278,10 +278,15 @@ Base._reshape(parent::AbstractFill, dims::Tuple{Integer,Vararg{Integer}}) = fill
278
278
# Resolves ambiguity error with `_reshape(v::AbstractArray{T, 1}, dims::Tuple{Int})`
279
279
Base. _reshape (parent:: AbstractFill{T, 1, Axes} , dims:: Tuple{Int} ) where {T, Axes} = fill_reshape (parent, dims... )
280
280
281
- for (Typ, funcs, func) in ((:Zeros , :zeros , :zero ), (:Ones , :ones , :one ))
281
+ for (AbsTyp, Typ, funcs, func) in ((:AbstractZeros , : Zeros , :zeros , :zero ), (:AbstractOnes , :Ones , :ones , :one ))
282
282
@eval begin
283
+ abstract type $ AbsTyp{T, N, Axes} <: AbstractFill{T, N, Axes} end
284
+ $ (Symbol (AbsTyp," Vector" )){T} = $ AbsTyp{T,1 }
285
+ $ (Symbol (AbsTyp," Matrix" )){T} = $ AbsTyp{T,2 }
286
+ $ (Symbol (AbsTyp," VecOrMat" )){T} = Union{$ (Symbol (AbsTyp," Vector" )){T},$ (Symbol (AbsTyp," Matrix" ))}
287
+
283
288
""" `$($ Typ) {T, N, Axes} <: AbstractFill{T, N, Axes}` (lazy `$($ funcs) ` with axes)"""
284
- struct $ Typ{T, N, Axes} <: AbstractFill {T, N, Axes}
289
+ struct $ Typ{T, N, Axes} <: $AbsTyp {T, N, Axes}
285
290
axes:: Axes
286
291
@inline $ Typ {T,N,Axes} (sz:: Axes ) where Axes<: Tuple{Vararg{AbstractUnitRange,N}} where {T, N} =
287
292
new {T,N,Axes} (sz)
@@ -312,29 +317,29 @@ for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
312
317
@inline $ Typ (:: Type{T} , m... ) where T = $ Typ {T} (m... )
313
318
314
319
@inline axes (Z:: $Typ ) = Z. axes
315
- @inline size (Z:: $Typ ) = length .(Z . axes)
316
- @inline getindex_value (Z:: $Typ {T} ) where T = $ func (T)
320
+ @inline size (Z:: $AbsTyp ) = length .(axes (Z) )
321
+ @inline getindex_value (Z:: $AbsTyp {T} ) where T = $ func (T)
317
322
318
- AbstractArray {T} (F:: $Typ ) where T = $ Typ {T} (F . axes)
319
- AbstractArray {T,N} (F:: $Typ {V,N} ) where {T,V,N} = $ Typ {T} (F . axes)
323
+ AbstractArray {T} (F:: $AbsTyp ) where T = $ Typ {T} (axes (F) )
324
+ AbstractArray {T,N} (F:: $AbsTyp {V,N} ) where {T,V,N} = $ Typ {T} (axes (F) )
320
325
321
- copy (F:: $Typ ) = F
326
+ copy (F:: $AbsTyp ) = F
322
327
323
- getindex (F:: $Typ {T,0} ) where T = getindex_value (F)
328
+ getindex (F:: $AbsTyp {T,0} ) where T = getindex_value (F)
324
329
325
330
promote_rule (:: Type{$Typ{T, N, Axes}} , :: Type{$Typ{V, N, Axes}} ) where {T,V,N,Axes} = $ Typ{promote_type (T,V),N,Axes}
326
- function convert (:: Type{$ Typ{T,N,Axes}} , A:: $Typ {V,N,Axes} ) where {T,V,N,Axes}
331
+ function convert (:: Type{Typ} , A:: $AbsTyp {V,N,Axes} ) where {T,V,N,Axes,Typ <: $AbsTyp{T,N,Axes} }
327
332
convert (T, getindex_value (A)) # checks that the types are convertible
328
- $ Typ {T,N,Axes} (axes (A))
333
+ Typ (axes (A))
329
334
end
330
- convert (:: Type{$Typ{T,N}} , A:: $Typ {V,N,Axes} ) where {T,V,N,Axes} = convert ($ Typ{T,N,Axes}, A)
331
- convert (:: Type{$Typ{T}} , A:: $Typ {V,N,Axes} ) where {T,V,N,Axes} = convert ($ Typ{T,N,Axes}, A)
332
- function convert (:: Type{$ Typ{T,N,Axes}} , A:: AbstractFill{V,N} ) where {T,V,N,Axes}
335
+ convert (:: Type{$Typ{T,N}} , A:: $AbsTyp {V,N,Axes} ) where {T,V,N,Axes} = convert ($ Typ{T,N,Axes}, A)
336
+ convert (:: Type{$Typ{T}} , A:: $AbsTyp {V,N,Axes} ) where {T,V,N,Axes} = convert ($ Typ{T,N,Axes}, A)
337
+ function convert (:: Type{Typ} , A:: AbstractFill{V,N} ) where {T,V,N,Axes,Typ <: $AbsTyp{T,N,Axes} }
333
338
axes (A) isa Axes || throw (ArgumentError (" cannot convert, as axes of array are not $Axes " ))
334
339
val = getindex_value (A)
335
340
y = convert (T, val)
336
341
y == $ func (T) || throw (ArgumentError (string (" cannot convert an array containinig $val to " , $ Typ)))
337
- $ Typ {T,N,Axes} (axes (A))
342
+ Typ (axes (A))
338
343
end
339
344
function convert (:: Type{$Typ{T,N}} , A:: AbstractFill{<:Any,N} ) where {T,N}
340
345
convert ($ Typ{T,N,typeof (axes (A))}, A)
@@ -517,7 +522,7 @@ Base.Array{T,N}(F::AbstractFill{V,N}) where {T,V,N} =
517
522
convert (Array{T,N}, fill (convert (T, getindex_value (F)), size (F)))
518
523
519
524
# These are in case `zeros` or `ones` are ever faster than `fill`
520
- for (Typ, funcs, func) in ((:Zeros , :zeros , :zero ), (:Ones , :ones , :one ))
525
+ for (Typ, funcs, func) in ((:AbstractZeros , :zeros , :zero ), (:AbstractOnes , :ones , :one ))
521
526
@eval begin
522
527
Base. Array {T,N} (F:: $Typ{V,N} ) where {T,V,N} = $ funcs (T,size (F))
523
528
end
@@ -562,16 +567,16 @@ end
562
567
# These methods are necessary to deal with infinite arrays
563
568
sum (x:: AbstractFill ) = getindex_value (x)* length (x)
564
569
sum (f, x:: AbstractFill ) = length (x) * f (getindex_value (x))
565
- sum (x:: Zeros ) = getindex_value (x)
570
+ sum (x:: AbstractZeros ) = getindex_value (x)
566
571
567
572
# needed to support infinite case
568
573
steprangelen (st... ) = StepRangeLen (st... )
569
574
cumsum (x:: AbstractFill{<:Any,1} ) = steprangelen (getindex_value (x), getindex_value (x), length (x))
570
575
571
- cumsum (x:: ZerosVector ) = x
572
- cumsum (x:: ZerosVector {Bool} ) = x
573
- cumsum (x:: OnesVector {II} ) where II<: Integer = convert (AbstractVector{II}, oneto (length (x)))
574
- cumsum (x:: OnesVector {Bool} ) = oneto (length (x))
576
+ cumsum (x:: AbstractZerosVector ) = x
577
+ cumsum (x:: AbstractZerosVector {Bool} ) = x
578
+ cumsum (x:: AbstractOnesVector {II} ) where II<: Integer = convert (AbstractVector{II}, oneto (length (x)))
579
+ cumsum (x:: AbstractOnesVector {Bool} ) = oneto (length (x))
575
580
576
581
577
582
# ########
@@ -591,8 +596,9 @@ allunique(x::AbstractFill) = length(x) < 2
591
596
# zero
592
597
# ########
593
598
594
- zero (r:: Zeros{T,N} ) where {T,N} = r
595
- zero (r:: Ones{T,N} ) where {T,N} = Zeros {T,N} (r. axes)
599
+ zero (r:: AbstractZeros{T,N} ) where {T,N} = r
600
+ # TODO : Make this required?
601
+ zero (r:: AbstractOnes{T,N} ) where {T,N} = Zeros {T,N} (axes (r))
596
602
zero (r:: Fill{T,N} ) where {T,N} = Zeros {T,N} (r. axes)
597
603
598
604
# ########
@@ -641,8 +647,8 @@ all(f::Function, x::AbstractFill) = isempty(x) || all(f(getindex_value(x)))
641
647
any (x:: AbstractFill ) = any (identity, x)
642
648
all (x:: AbstractFill ) = all (identity, x)
643
649
644
- count (x:: Ones {Bool} ) = length (x)
645
- count (x:: Zeros {Bool} ) = 0
650
+ count (x:: AbstractOnes {Bool} ) = length (x)
651
+ count (x:: AbstractZeros {Bool} ) = 0
646
652
count (f, x:: AbstractFill ) = f (getindex_value (x)) ? length (x) : 0
647
653
648
654
# ########
671
677
# #
672
678
# print
673
679
# #
674
- Base. replace_in_print_matrix (:: Zeros , :: Integer , :: Integer , s:: AbstractString ) =
680
+ Base. replace_in_print_matrix (:: AbstractZeros , :: Integer , :: Integer , s:: AbstractString ) =
675
681
Base. replace_with_centered_mark (s)
676
682
677
683
# following support blocked fill array printing via
@@ -704,7 +710,7 @@ function Base.show(io::IO, ::MIME"text/plain", x::Union{Eye, AbstractFill})
704
710
return show (io, x)
705
711
end
706
712
summary (io, x)
707
- if x isa Union{Zeros, Ones , Eye}
713
+ if x isa Union{AbstractZeros, AbstractOnes , Eye}
708
714
# then no need to print entries
709
715
elseif length (x) > 1
710
716
print (io, " , with entries equal to " , getindex_value (x))
716
722
function Base. show (io:: IO , x:: AbstractFill ) # for example (Fill(π,3),)
717
723
print (io, nameof (typeof (x)))
718
724
sz = size (x)
719
- args = if x isa Union{Zeros, Ones }
725
+ args = if x isa Union{AbstractZeros, AbstractOnes }
720
726
T = eltype (x)
721
727
if T != Float64
722
728
print (io," {" , T, " }" )
0 commit comments