Skip to content

Commit

Permalink
Merge pull request #379 from gridap/filtered_array
Browse files Browse the repository at this point in the history
Filtered array
  • Loading branch information
santiagobadia authored Aug 25, 2020
2 parents 17b81a6 + 46e2f72 commit cd0c943
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Arrays/Arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export CompressedArray
export LocalToGlobalArray
export LocalToGlobalPosNegArray
export FilteredCellArray
export FilterKernel

export kernel_cache
export kernel_caches
Expand Down
31 changes: 31 additions & 0 deletions src/Arrays/FilteredArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,34 @@ function getindex(a::FilteredCellArray,i::Integer...)
cache = array_cache(a)
getindex!(cache,a,i...)
end

struct FilterKernel <: Kernel end

function kernel_return_type(f::FilterKernel,x...)
typeof(kernel_testitem(f,x...))
end

function kernel_cache(k::FilterKernel,f,a)
# vals = testitem(a)
vals = a
T = eltype(eltype(a))
r = zeros(T,length(vals))
c = CachedArray(r)
end

function apply_kernel!(cache,k::FilterKernel,f,a)
c = cache
vals = a
filters = f
@assert size(vals) == size(filters) "Local arrays mismatch"
setsize!(c,(sum(filters),))
r = c.array
i = 0
for (val,filter) in zip(vals,filters)
if filter
i += 1
r[i] = val
end
end
r
end
14 changes: 14 additions & 0 deletions test/ArraysTests/FilteredArraysTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ fa = FilteredCellArray(a,filters)

test_array(fa,[a[i][2:3] for i in 1:6])

k = FilterKernel()

cache = kernel_cache(k,filters[1],a[1])

kernel_return_type(k,filters[1],a[1])

apply_kernel!(cache,k,filters[1],a[1])

test_kernel(k,(filters[1],a[1]),a[1][2:3])

@test apply(k,filters,a) == fa

#

v1 = [ [2 4 3 3]; [2 4 3 3]]
Expand Down Expand Up @@ -48,4 +60,6 @@ res = [r1,r2,r3]
r = CompressedArray(res,ptrs)
test_array(fa,r)

@test apply(k,filters,a) == fa

end #module

0 comments on commit cd0c943

Please sign in to comment.