Skip to content

Commit 182f50e

Browse files
committed
add some convenient functions (#11)
1 parent e6956cd commit 182f50e

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/blockarray.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ can be very fast since no copying of data is needed.
1313
1414
In the type definition, `R` defines the array type that each block has, for example `Matrix{Float64}.
1515
"""
16-
immutable BlockArray{T, N, R} <: AbstractBlockArray{T, N}
16+
immutable BlockArray{T, N, R <: AbstractArray} <: AbstractBlockArray{T, N}
1717
blocks::Array{R, N}
1818
block_sizes::BlockSizes{N}
1919
function BlockArray(blocks::Array{R, N}, block_sizes::BlockSizes{N})
@@ -201,9 +201,29 @@ end
201201
@nloops $N i i->(1:length(block_sizes[i])) begin
202202
block_index = @ntuple $N i
203203
indices = globalrange(block_sizes, block_index)
204-
arr[indices...] = block_array[Block(block_index...)]
204+
arr[indices...] = getblock(block_array, block_index...)
205205
end
206206

207207
return arr
208208
end
209209
end
210+
211+
@generated function Base.copy!{T, N, R <: AbstractArray}(block_array::BlockArray{T, N, R}, arr::R)
212+
return quote
213+
block_sizes = block_array.block_sizes
214+
215+
@nloops $N i i->(1:length(block_sizes[i])) begin
216+
block_index = @ntuple $N i
217+
indices = globalrange(block_sizes, block_index)
218+
copy!(getblock(block_array, block_index...), arr[indices...])
219+
end
220+
221+
return block_array
222+
end
223+
end
224+
225+
function Base.fill!(block_array::BlockArray, v)
226+
for block in block_array.blocks
227+
fill!(block, v)
228+
end
229+
end

src/pseudo_blockarray.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ julia> A = PseudoBlockArray(sprand(6, 0.5), [3,2,1])
3636
0.0553841
3737
```
3838
"""
39-
immutable PseudoBlockArray{T, N, R} <: AbstractBlockArray{T, N}
39+
immutable PseudoBlockArray{T, N, R <: AbstractArray} <: AbstractBlockArray{T, N}
4040
blocks::R
4141
block_sizes::BlockSizes{N}
4242
function PseudoBlockArray(blocks::R, block_sizes::BlockSizes{N})
@@ -252,6 +252,14 @@ end
252252
# Misc #
253253
########
254254

255-
function Base.full{T,N,R}(block_array::PseudoBlockArray{T, N, R})
255+
function Base.full(block_array::PseudoBlockArray)
256256
return block_array.blocks
257257
end
258+
259+
function Base.copy!{T, N, R <: AbstractArray}(block_array::PseudoBlockArray{T, N, R}, arr::R)
260+
copy!(block_array.blocks, arr)
261+
end
262+
263+
function Base.fill!(block_array::PseudoBlockArray, v)
264+
fill!(block_array.blocks, v)
265+
end

test/runtests.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ let
5454
getblock!(q2, BA_1, u)
5555
@test q2 == q
5656
end
57+
fill!(BA_1, 1.0)
58+
@test BA_1 == ones(size(BA_1))
59+
ran = rand(size(BA_1))
60+
copy!(BA_1, ran)
61+
@test BA_1 == ran
5762

5863
a_1_sparse = sprand(6, 0.9)
5964
BA_1_sparse = BlockType(a_1_sparse, [1,2,3])
@@ -83,6 +88,11 @@ let
8388
getblock!(q2, BA_2, u, γ)
8489
@test q2 == q
8590
end
91+
fill!(BA_2, 1.0)
92+
@test BA_2 == ones(size(BA_2))
93+
ran = rand(size(BA_2))
94+
copy!(BA_2, ran)
95+
@test BA_2 == ran
8696

8797
a_2_sparse = sprand(3, 7, 0.9)
8898
BA_2_sparse = BlockType(a_2_sparse, [1,2], [3,4])
@@ -111,7 +121,11 @@ let
111121
getblock!(q3, BA_3, u, γ, γ)
112122
@test q3 == q
113123
end
114-
124+
fill!(BA_3, 1.0)
125+
@test BA_3 == ones(size(BA_3))
126+
ran = rand(size(BA_3))
127+
copy!(BA_3, ran)
128+
@test BA_3 == ran
115129
end
116130
end
117131

0 commit comments

Comments
 (0)