-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathZeroDimensionalArrays.jl
More file actions
170 lines (144 loc) · 4.53 KB
/
ZeroDimensionalArrays.jl
File metadata and controls
170 lines (144 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
module ZeroDimensionalArrays
export
ZeroDimArray,
Box,
BoxConst
struct ZeroDimArray{T} <: AbstractArray{T, 0}
v::T
global function new_zero_dimensional_array_immutable(::Type{T}, v) where {T}
new{T}(v)
end
end
mutable struct Box{T} <: AbstractArray{T, 0}
v::T
global function new_zero_dimensional_array_mutable(::Type{T}, v) where {T}
new{T}(v)
end
global function new_zero_dimensional_array_mutable_undef(::Type{T}) where {T}
new{T}()
end
end
mutable struct BoxConst{T} <: AbstractArray{T, 0}
const v::T
global function new_zero_dimensional_array_mutable_const_field(::Type{T}, v) where {T}
new{T}(v)
end
end
const ZeroDimensionalArrayCanNotMutateElement = Union{
ZeroDimArray,
BoxConst,
}
const ZeroDimensionalArray = Union{
ZeroDimensionalArrayCanNotMutateElement,
Box,
}
function type_to_constructor_function(::Type{T}) where {T <: ZeroDimensionalArray}
local ret
if T <: ZeroDimArray
ret = new_zero_dimensional_array_immutable
elseif T <: Box
ret = new_zero_dimensional_array_mutable
elseif T <: BoxConst
ret = new_zero_dimensional_array_mutable_const_field
end
ret
end
Base.@nospecializeinfer function Base.propertynames(
# the `unused` is here because of https://github.com/JuliaLang/julia/issues/44428
(@nospecialize unused::ZeroDimensionalArray),
::Bool = false,
)
()
end
Base.@nospecializeinfer function Base.size(@nospecialize unused::ZeroDimensionalArray)
()
end
function Base.getindex(a::ZeroDimensionalArray)
a.v
end
# This method is redundant for correctness, but adding it helps achieve constprop, which
# helps `only(::ZeroDimensionalArray)` and `last(::ZeroDimensionalArray)`, for example.
Base.@constprop :aggressive function Base.getindex(a::ZeroDimensionalArray, i::Int)
if !isone(i)
throw(BoundsError())
end
a[]
end
function Base.setindex!(a::Box, x)
a.v = x
end
Base.@nospecializeinfer function Base.isassigned((@nospecialize unused::ZeroDimensionalArray), i::Vararg{Integer})
all(isone, i)
end
function Base.iterate(a::ZeroDimensionalArray)
(a[], nothing)
end
Base.@nospecializeinfer function Base.iterate((@nospecialize a::ZeroDimensionalArray), @nospecialize state::Any)
nothing
end
function Base.iterate(r::Iterators.Reverse{<:ZeroDimensionalArray})
a = Iterators.reverse(r)
iterate(a)
end
Base.@nospecializeinfer function Base.iterate((@nospecialize a::Iterators.Reverse{<:ZeroDimensionalArray}), @nospecialize state::Any)
nothing
end
function construct_given_eltype(::Type{Arr}, ::Type{T}, v) where {Arr <: ZeroDimensionalArray, T}
c = type_to_constructor_function(Arr)
c(T, v)
end
function construct(::Type{Arr}, v) where {Arr <: ZeroDimensionalArray}
T = typeof(v)
construct_given_eltype(Arr, T, v)
end
function convert_from_other_array_to_given_eltype(::Type{Arr}, ::Type{T}, a::AbstractArray{<:Any, 0}) where {Arr <: ZeroDimensionalArray, T}
v = a[]
construct_given_eltype(Arr, T, v)
end
function convert_from_other_array(::Type{Arr}, a::AbstractArray{<:Any, 0}) where {Arr <: ZeroDimensionalArray}
T = eltype(a)
convert_from_other_array_to_given_eltype(Arr, T, a)
end
function Base.copy(a::ZeroDimensionalArray)
Arr = typeof(a)
convert_from_other_array(Arr, a)
end
for Arr ∈ (
ZeroDimArray,
Box,
BoxConst,
)
@eval begin
function Base.convert(::Type{$Arr}, a::AbstractArray{<:Any, 0})
convert_from_other_array($Arr, a)
end
function Base.convert(::Type{$Arr{T}}, a::AbstractArray{<:Any, 0}) where {T}
convert_from_other_array_to_given_eltype($Arr, T, a)
end
function (::Type{$Arr})(v)
construct($Arr, v)
end
function (::Type{$Arr{T}})(v) where {T}
construct_given_eltype($Arr, T, v)
end
end
end
function Box{T}() where {T}
new_zero_dimensional_array_mutable_undef(T)
end
function Base.similar((@nospecialize unused::ZeroDimensionalArray), ::Type{T}, ::Tuple{}) where {T}
new_zero_dimensional_array_mutable_undef(T)
end
# TODO:
# ```julia
# function Base.similar((@nospecialize unused::ZeroDimensionalArray), ::Type{T}, size::Tuple{Vararg{Int}}) where {T}
# FixedSizeArrayDefault{T}(undef, size)
# end
# ```
# https://github.com/JuliaLang/julia/issues/51753
if isdefined(Base, :dataids) && hasmethod(Base.dataids, Tuple{Box{Float32}})
Base.@nospecializeinfer function Base.dataids((@nospecialize a::ZeroDimensionalArrayCanNotMutateElement),)
()
end
end
end