Skip to content

Commit 64616d2

Browse files
committed
add some source code
1 parent 8d16954 commit 64616d2

File tree

1 file changed

+142
-1
lines changed

1 file changed

+142
-1
lines changed

src/ZeroDimensionalArrays.jl

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,146 @@
11
module ZeroDimensionalArrays
22

3-
# Write your package code here.
3+
export
4+
ZeroDimensionalArrayImmutable,
5+
ZeroDimensionalArrayMutable,
6+
ZeroDimensionalArrayMutableConstField
7+
8+
struct ZeroDimensionalArrayImmutable{T} <: AbstractArray{T, 0}
9+
v::T
10+
global function new_zero_dimensional_array_immutable(::Type{T}, v) where {T}
11+
new{T}(v)
12+
end
13+
end
14+
15+
mutable struct ZeroDimensionalArrayMutable{T} <: AbstractArray{T, 0}
16+
v::T
17+
global function new_zero_dimensional_array_mutable(::Type{T}, v) where {T}
18+
new{T}(v)
19+
end
20+
global function new_zero_dimensional_array_mutable_undef(::Type{T}) where {T}
21+
new{T}()
22+
end
23+
end
24+
25+
mutable struct ZeroDimensionalArrayMutableConstField{T} <: AbstractArray{T, 0}
26+
const v::T
27+
global function new_zero_dimensional_array_mutable_const_field(::Type{T}, v) where {T}
28+
new{T}(v)
29+
end
30+
end
31+
32+
const ZeroDimensionalArray = Union{
33+
ZeroDimensionalArrayImmutable,
34+
ZeroDimensionalArrayMutable,
35+
ZeroDimensionalArrayMutableConstField,
36+
}
37+
38+
const ZeroDimensionalArrayConstructorFunction = Union{
39+
typeof(new_zero_dimensional_array_immutable),
40+
typeof(new_zero_dimensional_array_mutable),
41+
typeof(new_zero_dimensional_array_mutable_const_field),
42+
}
43+
44+
function type_to_constructor_function(::Type{T}) where {T <: ZeroDimensionalArray}
45+
if T <: ZeroDimensionalArrayImmutable
46+
new_zero_dimensional_array_immutable
47+
elseif T <: ZeroDimensionalArrayMutable
48+
new_zero_dimensional_array_mutable
49+
elseif T <: ZeroDimensionalArrayMutableConstField
50+
new_zero_dimensional_array_mutable_const_field
51+
else
52+
throw(ArgumentError("no such constructor function"))
53+
end
54+
end
55+
56+
Base.@nospecializeinfer function Base.propertynames(
57+
# the `unused` is here because of https://github.com/JuliaLang/julia/issues/44428
58+
(@nospecialize unused::ZeroDimensionalArray),
59+
::Bool = false,
60+
)
61+
()
62+
end
63+
64+
Base.@nospecializeinfer function Base.size(@nospecialize unused::ZeroDimensionalArray)
65+
()
66+
end
67+
68+
function Base.getindex(a::ZeroDimensionalArray)
69+
a.v
70+
end
71+
72+
function Base.setindex!(a::ZeroDimensionalArrayMutable, x)
73+
a.v = x
74+
end
75+
76+
Base.@nospecializeinfer function Base.isassigned(@nospecialize unused::ZeroDimensionalArray)
77+
true
78+
end
79+
80+
Base.@nospecializeinfer function Base.isassigned((@nospecialize unused::ZeroDimensionalArray), i::Int)
81+
isone(i)
82+
end
83+
84+
function Base.only(a::ZeroDimensionalArray)
85+
a[]
86+
end
87+
88+
function convert_from_other_array_to_given_eltype(::Type{Arr}, ::Type{T}, a::AbstractArray{<:Any, 0}) where {Arr <: ZeroDimensionalArray, T}
89+
v = a[]
90+
c = type_to_constructor_function(Arr)
91+
c(T, v)
92+
end
93+
94+
function convert_from_other_array(::Type{Arr}, a::AbstractArray{<:Any, 0}) where {Arr <: ZeroDimensionalArray}
95+
T = eltype(a)
96+
convert_from_other_array_to_given_eltype(Arr, T, a)
97+
end
98+
99+
for Arr (
100+
ZeroDimensionalArrayImmutable,
101+
ZeroDimensionalArrayMutable,
102+
ZeroDimensionalArrayMutableConstField,
103+
)
104+
@eval begin
105+
function Base.convert(::Type{$Arr}, a::AbstractArray{<:Any, 0})
106+
convert_from_other_array($Arr, a)
107+
end
108+
function Base.convert(::Type{$Arr{T}}, a::AbstractArray{<:Any, 0}) where {T}
109+
convert_from_other_array_to_given_eltype($Arr, T, a)
110+
end
111+
function (::Type{$Arr})(a::AbstractArray{<:Any, 0})
112+
convert_from_other_array($Arr, a)
113+
end
114+
function (::Type{$Arr{T}})(a::AbstractArray{<:Any, 0}) where {T}
115+
convert_from_other_array_to_given_eltype($Arr, T, a)
116+
end
117+
end
118+
end
119+
120+
function ZeroDimensionalArrayMutable{T}(::UndefInitializer) where {T}
121+
new_zero_dimensional_array_mutable_undef(T)
122+
end
123+
124+
function ZeroDimensionalArrayMutable{T}(::UndefInitializer, ::Tuple{}) where {T}
125+
new_zero_dimensional_array_mutable_undef(T)
126+
end
127+
128+
function Base.similar((@nospecialize unused::ZeroDimensionalArray), ::Type{T}, ::Tuple{}) where {T}
129+
new_zero_dimensional_array_mutable_undef(T)
130+
end
131+
132+
# TODO:
133+
# ```julia
134+
# function Base.similar((@nospecialize unused::ZeroDimensionalArray), ::Type{T}, size::Tuple{Vararg{Int}}) where {T}
135+
# FixedSizeArrayDefault{T}(undef, size)
136+
# end
137+
# ```
138+
139+
# https://github.com/JuliaLang/julia/issues/51753
140+
if isdefined(Base, :dataids) && hasmethod(Base.dataids, Tuple{ZeroDimensionalArrayMutable{Float32}})
141+
function Base.dataids(a::ZeroDimensionalArray)
142+
Base.dataids(only(a))
143+
end
144+
end
4145

5146
end

0 commit comments

Comments
 (0)