|
1 | 1 | """ |
2 | | - MArray{Size, T, L}() |
3 | | - MArray{Size, T, L}(x::NTuple{L, T}) |
4 | | - MArray{Size, T, L}(x1, x2, x3, ...) |
| 2 | + MArray{S, T, L}() |
| 3 | + MArray{S, T, L}(x::NTuple{L, T}) |
| 4 | + MArray{S, T, L}(x1, x2, x3, ...) |
| 5 | +
|
5 | 6 |
|
6 | 7 | Construct a statically-sized, mutable array `MArray`. The data may optionally be |
7 | | -provided upon construction and can be mutated later. The `Size` parameter is a |
8 | | -Tuple specifying the dimensions of the array. The `L` parameter is the `length` |
9 | | -of the array and is always equal to `prod(S)`. Constructors may drop the `L` and |
10 | | -`T` parameters if they are inferrable from the input (e.g. `L` is always |
11 | | -inferrable from `Size`). |
| 8 | +provided upon construction and cannot be mutated later. The `S` parameter is a Tuple-type |
| 9 | +specifying the dimensions, or size, of the array - such as `Tuple{3,4,5}` for a 3×4×5-sized |
| 10 | +array. The `L` parameter is the `length` of the array and is always equal to `prod(S)`. |
| 11 | +Constructors may drop the `L` and `T` parameters if they are inferrable from the input |
| 12 | +(e.g. `L` is always inferrable from `S`). |
12 | 13 |
|
13 | | - MArray{Size}(a::Array) |
| 14 | + MArray{S}(a::Array) |
14 | 15 |
|
15 | | -Construct a statically-sized, mutable array of dimensions `Size` using the data from |
16 | | -`a`. The `Size` parameter is mandatory since the size of `a` is unknown to the |
17 | | -compiler (the element type may optionally also be specified). |
| 16 | +Construct a statically-sized, mutable array of dimensions `S` (expressed as a `Tuple{...}`) |
| 17 | +using the data from `a`. The `S` parameter is mandatory since the size of `a` is unknown to |
| 18 | +the compiler (the element type may optionally also be specified). |
18 | 19 | """ |
19 | | -type MArray{Size, T, N, L} <: StaticArray{T, N} |
| 20 | +type MArray{S <: Tuple, T, N, L} <: StaticArray{S, T, N} |
20 | 21 | data::NTuple{L,T} |
21 | 22 |
|
22 | | - function (::Type{MArray{Size,T,N,L}}){Size,T,N,L}(x::NTuple{L,T}) |
23 | | - check_array_parameters(Size, T, Val{N}, Val{L}) |
24 | | - new{Size,T,N,L}(x) |
| 23 | + function (::Type{MArray{S,T,N,L}}){S,T,N,L}(x::NTuple{L,T}) |
| 24 | + check_array_parameters(S, T, Val{N}, Val{L}) |
| 25 | + new{S,T,N,L}(x) |
25 | 26 | end |
26 | 27 |
|
27 | | - function (::Type{MArray{Size,T,N,L}}){Size,T,N,L}(x::NTuple{L,Any}) |
28 | | - check_array_parameters(Size, T, Val{N}, Val{L}) |
29 | | - new{Size,T,N,L}(convert_ntuple(T, x)) |
| 28 | + function (::Type{MArray{S,T,N,L}}){S,T,N,L}(x::NTuple{L,Any}) |
| 29 | + check_array_parameters(S, T, Val{N}, Val{L}) |
| 30 | + new{S,T,N,L}(convert_ntuple(T, x)) |
30 | 31 | end |
31 | 32 |
|
32 | | - function (::Type{MArray{Size,T,N,L}}){Size,T,N,L}() |
33 | | - check_array_parameters(Size, T, Val{N}, Val{L}) |
34 | | - new{Size,T,N,L}() |
| 33 | + function (::Type{MArray{S,T,N,L}}){S,T,N,L}() |
| 34 | + check_array_parameters(S, T, Val{N}, Val{L}) |
| 35 | + new{S,T,N,L}() |
35 | 36 | end |
36 | 37 | end |
37 | 38 |
|
38 | | -@generated function (::Type{MArray{Size,T,N}}){Size,T,N}(x::Tuple) |
| 39 | +@generated function (::Type{MArray{S,T,N}}){S,T,N}(x::Tuple) |
39 | 40 | return quote |
40 | 41 | $(Expr(:meta, :inline)) |
41 | | - MArray{Size,T,N,$(tuple_prod(Size))}(x) |
| 42 | + MArray{S,T,N,$(tuple_prod(S))}(x) |
42 | 43 | end |
43 | 44 | end |
44 | 45 |
|
45 | | -@generated function (::Type{MArray{Size,T}}){Size,T}(x::Tuple) |
| 46 | +@generated function (::Type{MArray{S,T}}){S,T}(x::Tuple) |
46 | 47 | return quote |
47 | 48 | $(Expr(:meta, :inline)) |
48 | | - MArray{Size,T,$(tuple_length(Size)),$(tuple_prod(Size))}(x) |
| 49 | + MArray{S,T,$(tuple_length(S)),$(tuple_prod(S))}(x) |
49 | 50 | end |
50 | 51 | end |
51 | 52 |
|
52 | | -@generated function (::Type{MArray{Size}}){Size, T <: Tuple}(x::T) |
| 53 | +@generated function (::Type{MArray{S}}){S, T <: Tuple}(x::T) |
53 | 54 | return quote |
54 | 55 | $(Expr(:meta, :inline)) |
55 | | - MArray{Size,$(promote_tuple_eltype(T)),$(tuple_length(Size)),$(tuple_prod(Size))}(x) |
| 56 | + MArray{S,$(promote_tuple_eltype(T)),$(tuple_length(S)),$(tuple_prod(S))}(x) |
56 | 57 | end |
57 | 58 | end |
58 | 59 |
|
59 | | -@generated function (::Type{MArray{Size,T,N}}){Size,T,N}() |
| 60 | +@generated function (::Type{MArray{S,T,N}}){S,T,N}() |
60 | 61 | return quote |
61 | 62 | $(Expr(:meta, :inline)) |
62 | | - MArray{Size, T, N, $(tuple_prod(Size))}() |
| 63 | + MArray{S, T, N, $(tuple_prod(S))}() |
63 | 64 | end |
64 | 65 | end |
65 | 66 |
|
66 | | -@generated function (::Type{MArray{Size,T}}){Size,T}() |
| 67 | +@generated function (::Type{MArray{S,T}}){S,T}() |
67 | 68 | return quote |
68 | 69 | $(Expr(:meta, :inline)) |
69 | | - MArray{Size, T, $(tuple_length(Size)), $(tuple_prod(Size))}() |
| 70 | + MArray{S, T, $(tuple_length(S)), $(tuple_prod(S))}() |
70 | 71 | end |
71 | 72 | end |
72 | 73 |
|
73 | 74 | @inline MArray(a::StaticArray) = MArray{size_tuple(typeof(a))}(Tuple(a)) |
74 | 75 |
|
| 76 | +# Simplified show for the type |
| 77 | +show(io::IO, ::Type{MArray{S, T, N}}) where {S, T, N} = print(io, "MArray{$S,$T,$N}") |
| 78 | + |
75 | 79 | # Some more advanced constructor-like functions |
76 | 80 | @inline one(::Type{MArray{S}}) where {S} = one(MArray{S,Float64,tuple_length(S)}) |
77 | 81 | @inline eye(::Type{MArray{S}}) where {S} = eye(MArray{S,Float64,tuple_length(S)}) |
|
82 | 86 | ## MArray methods ## |
83 | 87 | #################### |
84 | 88 |
|
85 | | -@pure Size{S}(::Type{MArray{S}}) = Size(S) |
86 | | -@pure Size{S,T}(::Type{MArray{S,T}}) = Size(S) |
87 | | -@pure Size{S,T,N}(::Type{MArray{S,T,N}}) = Size(S) |
88 | | -@pure Size{S,T,N,L}(::Type{MArray{S,T,N,L}}) = Size(S) |
89 | | - |
90 | 89 | function getindex(v::MArray, i::Int) |
91 | 90 | Base.@_inline_meta |
92 | 91 | v.data[i] |
93 | 92 | end |
94 | 93 |
|
95 | | -@propagate_inbounds setindex!{S,T}(v::MArray{S,T}, val, i::Int) = setindex!(v, convert(T, val), i) |
96 | | -@inline function setindex!{S,T}(v::MArray{S,T}, val::T, i::Int) |
| 94 | +@inline function setindex!(v::MArray, val, i::Int) |
97 | 95 | @boundscheck if i < 1 || i > length(v) |
98 | 96 | throw(BoundsError()) |
99 | 97 | end |
100 | 98 |
|
| 99 | + T = eltype(v) |
101 | 100 | if isbits(T) |
102 | | - unsafe_store!(Base.unsafe_convert(Ptr{T}, Base.data_pointer_from_objref(v)), val, i) |
| 101 | + unsafe_store!(Base.unsafe_convert(Ptr{T}, Base.data_pointer_from_objref(v)), convert(T, val), i) |
103 | 102 | else |
104 | 103 | # This one is unsafe (#27) |
105 | 104 | # unsafe_store!(Base.unsafe_convert(Ptr{Ptr{Void}}, Base.data_pointer_from_objref(v.data)), Base.data_pointer_from_objref(val), i) |
|
111 | 110 |
|
112 | 111 | @inline Tuple(v::MArray) = v.data |
113 | 112 |
|
114 | | -@inline function Base.unsafe_convert{Size,T}(::Type{Ptr{T}}, a::MArray{Size,T}) |
| 113 | +@inline function Base.unsafe_convert{S,T}(::Type{Ptr{T}}, a::MArray{S,T}) |
115 | 114 | Base.unsafe_convert(Ptr{T}, Base.data_pointer_from_objref(a)) |
116 | 115 | end |
117 | 116 |
|
|
0 commit comments