Skip to content

Commit 1d62d52

Browse files
authored
Merge pull request #4 from JuliaArrays/teh/of_eltype
Add `of_eltype`, for lazy element type conversions
2 parents d5b8d5c + 0085b0b commit 1d62d52

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,33 @@ julia> b = mappedarray(abs, a)
138138
0.486558 1.27959 1.59661 1.05867 2.06828
139139
0.315976 0.188828 0.567672 0.405086 1.06983
140140
```
141+
142+
### of_eltype
143+
144+
This package defines a convenience method, `of_eltype`, which
145+
"lazily-converts" arrays to a specific `eltype`. (It works simply by
146+
defining `convert` functions for both `f` and `finv`.)
147+
148+
Using `of_eltype` you can "convert" a series of arrays to a chosen element type:
149+
150+
```julia
151+
julia> arrays = (rand(2,2), rand(Int,2,2), [0x01 0x03; 0x02 0x04])
152+
(
153+
[0.541018 0.223392; 0.341264 0.022014],
154+
155+
[2437062103055434647 4726011606246170825; -4226911569217925847 -8715663020460318733],
156+
157+
UInt8[0x01 0x03; 0x02 0x04])
158+
159+
julia> arraysT = map(A->of_eltype(Float64, A), arrays)
160+
(
161+
[0.541018 0.223392; 0.341264 0.022014],
162+
163+
[2.43706e18 4.72601e18; -4.22691e18 -8.71566e18],
164+
165+
[1.0 3.0; 2.0 4.0])
166+
```
167+
168+
This construct is inferrable (type-stable), so it can be a useful
169+
means to "coerce" arrays to a common type. This can sometimes solve
170+
type-stability problems without requiring that one copy the data.

src/MappedArrays.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module MappedArrays
22

33
using Base: @propagate_inbounds
44

5-
export mappedarray
5+
export mappedarray, of_eltype
66

77
abstract AbstractMappedArray{T,N} <: AbstractArray{T,N}
88

@@ -36,6 +36,14 @@ function mappedarray{T,N}(f_finv::Tuple{Any,Any}, data::AbstractArray{T,N})
3636
MappedArray{typeof(f(one(T))),N,typeof(data),typeof(f),typeof(finv)}(f, finv, data)
3737
end
3838

39+
"""
40+
of_eltype(T, A)
41+
42+
creates a view of `A` that lazily-converts the element type to `T`.
43+
"""
44+
of_eltype{S,T}(::Type{T}, data::AbstractArray{S}) = mappedarray((x->convert(T,x), y->convert(S,y)), data)
45+
of_eltype{T}(::Type{T}, data::AbstractArray{T}) = data
46+
3947
Base.parent(A::AbstractMappedArray) = A.data
4048
Base.size(A::AbstractMappedArray) = size(A.data)
4149
parenttype{T,N,A,F}(::Type{ReadonlyMappedArray{T,N,A,F}}) = A

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ for i = 1:4
4444
end
4545
b[2,1] = 10/255
4646
@test a[2,1] == 0x0a
47+
48+
a = [0.1 0.3; 0.2 0.4]
49+
b = @inferred(of_eltype(UFixed8, a))
50+
@test b[1,1] === UFixed8(0.1)
51+
b[2,1] = UFixed8(0.5)
52+
@test a[2,1] == UFixed8(0.5)
53+
@test !(b === a)
54+
b = of_eltype(Float64, a)
55+
@test b === a

0 commit comments

Comments
 (0)