Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtered array #379

Merged
merged 3 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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