src/ZipArrays.jl

Wed, 22 Dec 2021 11:13:38 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 22 Dec 2021 11:13:38 +0200
changeset 37
f8be66557e0f
parent 36
6dfa8001eed2
permissions
-rw-r--r--

Planar finite elements, simple linear solvers for fixed dimensions

36
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 #######################################
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Simultaneous view to multiple arrays
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 #######################################
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 """
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 Implements a type for zipped arrays; see `ZipArray`, `AbstractZipArray`,
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 `ZipVector`, and `AbstractZipVector`.
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 """
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 module ZipArrays
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 export ZipArray,
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 ZipVector,
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 AbstractZipArray,
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 AbstractZipVector
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 """
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 `ZipArray{S,T,N,A,B}`
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 `N`-dimensional array of element type `Tuple{S,T}`, the component of the tuple stored in separate
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 arrays of type `A <: AbstractArray{S,N}` and `B <: AbstractArray{T,N}`.
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 """
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 struct ZipArray{S,T,N,A <: AbstractArray{S,N}, B <: AbstractArray{T,N}} <: AbstractArray{Tuple{S,T}, N}
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 a :: A
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 b :: B
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 function ZipArray(a :: A, b :: B) where {S,T,N,A <: AbstractArray{S,N}, B <: AbstractArray{T,N}}
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 @assert(IndexStyle(A)==IndexStyle(B))
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 @assert(size(a)==size(b))
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 return new{S,T,N,A,B}(a, b)
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 end
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 end
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 """
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 `AbstractZipArray{S,T,N}`
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 A way to refer to a `ZipArray` without specifying the container types.
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 """
37
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 36
diff changeset
39 const AbstractZipArray{S,T,N} = ZipArray{S,T,N,A,B} where {A <: AbstractArray{S,N}, B <: AbstractArray{T,N}}
36
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 """
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 `ZipVector{S,T,A,B}`
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 One-dimensional `ZipArray`.
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 """
37
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 36
diff changeset
46 const ZipVector{S,T,A,B} = ZipArray{S,T,1,A,B}
36
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 """
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 `AbstractZipVector{S,T}`
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 One-dimensional `AbstractZipArray`.
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 """
37
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 36
diff changeset
53 const AbstractZipVector{S,T} = AbstractZipArray{S,T,1}
36
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 function Base.IndexStyle( :: Type{<:ZipArray{S,T,N,A,B}}) where {S,T,N,A,B}
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 return IndexStyle(A)
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 end
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 @inline function Base.size(z :: AbstractZipArray{S,T,N}) where {S,T,N}
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 return size(z.a)
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 end
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 @inline function Base.getindex(z :: AbstractZipArray{S,T,N}, i) where {S,T,N}
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 return (getindex(z.a, i), getindex(z.b, i))
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66 end
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68 @inline function Base.setindex!(z :: AbstractZipArray{S,T,N}, (s, t) :: Tuple{S,T}, i) where {S,T,N}
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 return (setindex!(z.a, s, i), setindex!(z.b, t, i))
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70 end
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72 @inline function Base.pop!(z :: AbstractZipVector{S,T}) where {S,T}
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73 s = pop!(z.a)
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74 t = pop!(z.b)
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75 return (s, t)
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 end
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
78 @inline function Base.push!(z :: AbstractZipVector{S,T}, items :: Vararg{Tuple{S,T},M}) where {S,T,M}
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
79 push!(z.a, s for (s, _) ∈ items)
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80 push!(z.b, t for (_, t) ∈ items)
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 end
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 end # module
6dfa8001eed2 Implement ZipArrays
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84

mercurial