Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Mostly works on the following index types: (Int, Range1{Int}, Range{Int})
Does not yet work for index of Array{Int}
  • Loading branch information
punkrockpolly committed Nov 10, 2013
1 parent 6330867 commit bcdd56f
Showing 1 changed file with 23 additions and 44 deletions.
67 changes: 23 additions & 44 deletions negatedindex.jl
Original file line number Diff line number Diff line change
@@ -1,69 +1,48 @@
typealias RangeIndices Union(Int, Range1{Int})

typealias RangeIndices Union(Int, Range1{Int}, Range{Int})#, Array{Int})

# Define new type to handle negated index of !idx
type NegatedIndex{T<:RangeIndices}
idx::T
# step::Int
# len::Int

# function NegatedIndex(idx)
# len = length(idx::T)
# new(idx, 1, len)
# end
step::Int
end

function getindex(A::Array, idx::NegatedIndex)
n = length(A)
if !(1 <= min(idx) && max(idx) <= n)
function getindex(A::Array, r::NegatedIndex)
n = Base.length(A)
if !(1 <= minimum(r.idx) && maximum(r.idx) <= n)
throw(BoundsError())
end
b = deepcopy(A)
c = 0
for k=1:n
if k in idx #for i in fullindex(idx, size(A,n))
if k in r
splice!(b, k-c)
c += 1
end
end
return b
end

step(r::NegatedIndex) = r.step

function in(x, r::NegatedIndex)
n = step(r) == 0 ? 1 : iround((x-first(r))/step(r))+1
n >= 1 && n <= r.len && r[n] == x
function getindex(r::NegatedIndex, x)
r.idx[x]
end

function minimum(idx::NegatedIndex)
len = length(idx::T)
m = idx[1]
for x=1:len
if idx[x] < m
m = idx[x]
return m
end
import Base.first
function first(r::NegatedIndex)
first(r.idx)
end

function maximum(idx::NegatedIndex)
len = length(idx::T)
m = idx[1]
for x=1:len
if idx[x] < m
m = idx[x]
return m
end
import Base.length
function length(r::NegatedIndex)
return length(r.idx)
end

## Bounds checking ##
function checkbounds(idx::NegatedIndex, A::Array)
I = to_index(idx)
if I < 1 || I > sz
throw(BoundsError())
end
function in(x, r::NegatedIndex)
n = r.step == 0 ? 1 : iround((x-first(r))/r.step)+1
n >= 1 && n <= length(r) && r[n] == x
end

(!)(x::Int) = NegatedIndex(x)
# (!)(x::Range{Int}) = NegatedIndex(x)
(!)(x::Range1{Int}) = NegatedIndex(x)
# (!)(x::Array{Int}) = NegatedIndex(x)
# Define operator ! to return a NegatedIndex type
(!)(r::Int) = NegatedIndex(r,1)
(!)(r::Range{Int}) = NegatedIndex(r,r.step)
(!)(r::Range1{Int}) = NegatedIndex(r,1)
# (!)(r::Array{Int}) = NegatedIndex(r,1)

0 comments on commit bcdd56f

Please sign in to comment.