-
Notifications
You must be signed in to change notification settings - Fork 0
/
negatedindex.jl
83 lines (69 loc) · 1.64 KB
/
negatedindex.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module NI
# Define new type for negated index of !idx
immutable NegatedIndex{T}
idx::T
end
## Bounds checking ##
import Base.checkbounds
function checkbounds{T<:Integer}(sz::Integer, r::NegatedIndex{T})
if r.idx < 1 || r.idx > sz
throw(BoundsError())
end
end
function checkbounds(sz::Integer, r::NegatedIndex)
if !isempty(r.idx) && (minimum(r.idx) < 1 || maximum(r.idx) > sz)
throw(BoundsError())
end
end
# Copies A{T,1}, only for non-negated indices
function getindex{T}(A::Array{T,1}, r::NegatedIndex)
n = length(A)
m = length(r.idx)
checkbounds(n,r)
b = similar(A,n-m)
c = 1
for k = 1:n
if k in r
b[c] = A[k]
c += 1
end
end
return b
end
# Copies A{T,N}, only for non-negated indices
# NEED TO FIX THIS TO OUTPUT MULTI-DIM RESULTS!!
function getindex{T}(A::Array{T,N}, r::NegatedIndex)
row,col = arraysize(A)
n = length(A)
m = length(r.idx)
checkbounds(n,r)
b = similar(A,n-m)
c = 1
for k = 1:n
if k in r
b[c] = A[k]
c += 1
end
end
return b
end
# Alternative to above, utilizing splice! to remove the negated indices
# function getindex(A::Array, r::NegatedIndex)
# n = length(A)
# checkbounds(n, r)
# b = copy(A)
# c = 0
# for k=1:n
# if k in r
# splice!(b, k-c)
# c += 1
# end
# end
# return b
# end
Base.in(x,r::NegatedIndex) = !in(x,r.idx)
# Define operator ! to return a NegatedIndex type
(!)(r) = NegatedIndex(r)
(!)() = error("zero-argument ! is ambiguous")
end # module
NegatedIndex = NI.NegatedIndex