Skip to content

Commit

Permalink
Complement type, possible approach to #1032.
Browse files Browse the repository at this point in the history
Paired on this with @punkrockpolly, see:

https://github.com/punkrockpolly/Playing-with-Julia/blob/master/negatedindex.jl

The premise of the Complement type is that it abstracts the
complement of a collection – primarily  that

	`x in c` <=> `!(x in c.collection)`

This commit punts on indexing for more than two dimensions because
that turns out to be an incredibly invasive change to huge amounts
the multidimensional array indexing code.
  • Loading branch information
StefanKarpinski committed Nov 22, 2013
1 parent ed0374b commit 9063634
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions base/collections.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

module Collections

import Base: setindex!, done, get, haskey, isempty, length, next, getindex, start
import Base: setindex!, done, get, haskey, isempty, length, next, getindex, start, in
import ..Order: Forward, Ordering, lt

export
Expand Down Expand Up @@ -274,5 +274,25 @@ function next(pq::PriorityQueue, i)
end


end # module Collections
# the complement of an arbitrary collection

immutable Complement{T}
collection::T
end

in(x,c::Complement) = !in(x,c.collection)

!(a::AbstractArray) = Complement(a)
!(x::Number) = Complement(x)

getindex(v::AbstractArray, c::Complement) = v[filter(i->in(i,c),1:length(v))]

getindex(m::AbstractMatrix, I::Complement, J::Complement) =
m[filter(i->in(i,I),1:size(m,1)),filter(j->in(j,J),1:size(m,2))]
getindex(m::AbstractMatrix, I::Complement, J...) =
m[filter(i->in(i,I),1:size(m,1)),J]
getindex(m::AbstractMatrix, I, J::Complement) =
m[I,filter(j->in(j,J),1:size(m,2))]


end # module Collections

0 comments on commit 9063634

Please sign in to comment.