Permalink
Browse files

replace contains(x,y) with in(y,x). closes #4153

also add contains(String,String) to test for substrings
  • Loading branch information...
1 parent 3f73066 commit 7ee71738c1d6a3261585f4eaf335b25089470291 @JeffBezanson JeffBezanson committed Aug 29, 2013
@@ -157,7 +157,7 @@ vec(a::AbstractVector) = a
function squeeze(A::AbstractArray, dims)
d = ()
for i in 1:ndims(A)
- if contains(dims,i)
+ if in(i,dims)
if size(A,i) != 1
error("squeezed dims must all be size 1")
end
@@ -1397,12 +1397,12 @@ function nnz{T}(a::AbstractArray{T})
end
# for reductions that expand 0 dims to 1
-reduced_dims(A, region) = ntuple(ndims(A), i->(contains(region, i) ? 1 :
+reduced_dims(A, region) = ntuple(ndims(A), i->(in(i, region) ? 1 :
size(A,i)))
# keep 0 dims in place
reduced_dims0(A, region) = ntuple(ndims(A), i->(size(A,i)==0 ? 0 :
- contains(region, i) ? 1 :
+ in(i, region) ? 1 :
size(A,i)))
reducedim(f::Function, A, region, v0) =
View
@@ -1378,7 +1378,7 @@ function findin(a, b)
ind = Array(Int, 0)
bset = union!(Set(), b)
for i = 1:length(a)
- if contains(bset, a[i])
+ if in(a[i], bset)
push!(ind, i)
end
end
@@ -1424,7 +1424,7 @@ function gen_reducedim_func(n, f)
rvars = { symbol(string("r",i)) for i=1:n }
setlims = { quote
# each dim of reduction is either 1:sizeA or ivar:ivar
- if contains(region,$i)
+ if in($i,region)
$(lo[i]) = 1
$(hi[i]) = size(A,$i)
else
@@ -1573,7 +1573,7 @@ function intersect(vs...)
for v_elem in vs[1]
inall = true
for i = 2:length(vs)
- if !contains(vs[i], v_elem)
+ if !in(v_elem, vs[i])
inall=false; break
end
end
@@ -1589,7 +1589,7 @@ function union(vs...)
seen = Set()
for v in vs
for v_elem in v
- if !contains(seen, v_elem)
+ if !in(v_elem, seen)
push!(ret, v_elem)
push!(seen, v_elem)
end
@@ -1604,7 +1604,7 @@ function setdiff(a, b)
ret = Array(args_type,0)
seen = Set()
for a_elem in a
- if !contains(seen, a_elem) && !contains(bset, a_elem)
+ if !in(a_elem, seen) && !in(a_elem, bset)
push!(ret, a_elem)
push!(seen, a_elem)
end
View
@@ -256,10 +256,10 @@ function process_options(args::Array{Any,1})
global have_color = true
elseif args[i][8] == '='
val = args[i][9:]
- if contains(("no","0","false"), val)
+ if in(val, ("no","0","false"))
color_set = true
global have_color = false
- elseif contains(("yes","1","true"), val)
+ elseif in(val, ("yes","1","true"))
color_set = true
global have_color = true
end
View
@@ -86,7 +86,7 @@ const valid_opts = [:has_header, :ignore_invalid_chars, :use_mmap]
function val_opts(opts)
d = Dict{Symbol,Bool}()
for opt in opts
- !contains(valid_opts, opt[1]) && error("unknown option $(opt[1])")
+ !in(opt[1], valid_opts) && error("unknown option $(opt[1])")
!isa(opt[2], Bool) && error("$(opt[1]) can only be boolean")
d[opt[1]] = opt[2]
end
@@ -148,7 +148,7 @@ function dlm_offsets(sbuff::UTF8String, dlm, eol, offsets::Array{Int,2})
idx = 1
while(idx <= length(sbuff.data))
val,idx = next(sbuff, idx)
- (val != eol) && ((dlm == invalid_dlm) ? !contains(_default_delims, val) : (val != dlm)) && continue
+ (val != eol) && ((dlm == invalid_dlm) ? !in(val, _default_delims) : (val != dlm)) && continue
col += 1
offsets[row,col] = idx-2
(row >= maxrow) && (col == maxcol) && break
@@ -164,7 +164,7 @@ function dlm_offsets(dbuff::Vector{Uint8}, dlm::Uint8, eol::Uint8, offsets::Arra
offsets[maxrow,maxcol] = length(dbuff)
for idx in 1:length(dbuff)
val = dbuff[idx]
- (val != eol) && ((dlm == invalid_dlm) ? !contains(_default_delims, val) : (val != dlm)) && continue
+ (val != eol) && ((dlm == invalid_dlm) ? !in(val, _default_delims) : (val != dlm)) && continue
col += 1
offsets[row,col] = idx-1
(row >= maxrow) && (col == maxcol) && break
@@ -178,7 +178,7 @@ function dlm_dims{T,D}(dbuff::T, eol::D, dlm::D)
ncols = nrows = col = 0
try
for val in dbuff
- (val != eol) && ((dlm == invalid_dlm) ? !contains(_default_delims, val) : (val != dlm)) && continue
+ (val != eol) && ((dlm == invalid_dlm) ? !in(val, _default_delims) : (val != dlm)) && continue
col += 1
(val == eol) && (nrows += 1; ncols = max(ncols, col); col = 0)
end
View
@@ -4,9 +4,9 @@ abstract Associative{K,V}
const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
-haskey(d::Associative, k) = contains(keys(d),k)
+haskey(d::Associative, k) = in(k,keys(d))
-function contains(a::Associative, p::(Any,Any))
+function in(p::(Any,Any), a::Associative)
v = get(a,p[1],secret_table_token)
!is(v, secret_table_token) && isequal(v, p[2])
end
@@ -58,8 +58,8 @@ function next(v::ValueIterator, state)
n[1][2], n[2]
end
-contains(v::KeyIterator, k) = !is(get(v.dict, k, secret_table_token),
- secret_table_token)
+in(k, v::KeyIterator) = !is(get(v.dict, k, secret_table_token),
+ secret_table_token)
keys(a::Associative) = KeyIterator(a)
values(a::Associative) = ValueIterator(a)
@@ -495,7 +495,7 @@ function get{K,V}(h::Dict{K,V}, key, deflt)
end
haskey(h::Dict, key) = (ht_keyindex(h, key) >= 0)
-contains{T<:Dict}(v::KeyIterator{T}, key) = (ht_keyindex(v.dict, key) >= 0)
+in{T<:Dict}(key, v::KeyIterator{T}) = (ht_keyindex(v.dict, key) >= 0)
function getkey{K,V}(h::Dict{K,V}, key, deflt)
index = ht_keyindex(h, key)
View
@@ -72,7 +72,7 @@ const ENV = EnvHash()
getindex(::EnvHash, k::String) = @accessEnv k throw(KeyError(k))
get(::EnvHash, k::String, def) = @accessEnv k (return def)
-contains(::KeyIterator{EnvHash}, k::String) = _hasenv(k)
+in(k::String, ::KeyIterator{EnvHash}) = _hasenv(k)
pop!(::EnvHash, k::String) = (v = ENV[k]; _unsetenv(k); v)
pop!(::EnvHash, k::String, def) = haskey(ENV,k) ? pop!(ENV,k) : def
function delete!(::EnvHash, k::String)
View
@@ -693,6 +693,7 @@ export
getindex,
getkey,
haskey,
+ in,
intersect!,
intersect,
isempty,
View
@@ -67,7 +67,7 @@ function init_help()
if !haskey(MODULE_DICT, func)
MODULE_DICT[func] = {}
end
- if !contains(MODULE_DICT[func], mod)
+ if !in(mod, MODULE_DICT[func])
push!(MODULE_DICT[func], mod)
end
end
View
@@ -117,7 +117,7 @@ function copy!(to::IntSet, from::IntSet)
union!(to, from)
end
-function contains(s::IntSet, n::Integer)
+function in(n::Integer, s::IntSet)
if n >= s.limit
# max IntSet length is typemax(Int), so highest possible element is
# typemax(Int)-1
View
@@ -10,8 +10,8 @@ export quot,
quot(ex) = Expr(:quote, ex)
isexpr(ex::Expr, head) = ex.head === head
-isexpr(ex::Expr, heads::Set) = contains(heads, ex.head)
-isexpr(ex::Expr, heads::Vector) = contains(heads, ex.head)
+isexpr(ex::Expr, heads::Set) = in(ex.head, heads)
+isexpr(ex::Expr, heads::Vector) = in(ex.head, heads)
isexpr(ex, head) = false
isexpr(ex, head, n::Int) = isexpr(ex, head) && length(ex.args) == n
View
@@ -276,7 +276,7 @@ type ProcessExitedException <: Exception end
worker_from_id(i) = worker_from_id(PGRP, i)
function worker_from_id(pg::ProcessGroup, i)
# Processes with pids > ours, have to connect to us. May not have happened. Wait for some time.
- if contains(map_del_wrkr, i)
+ if in(i, map_del_wrkr)
throw(ProcessExitedException())
end
if myid()==1 && !haskey(map_pid_wrkr,i)
@@ -330,7 +330,7 @@ function deregister_worker(pg, pid)
ids = {}
tonotify = {}
for (id,rv) in pg.refs
- if contains(rv.clientset,pid)
+ if in(pid,rv.clientset)
push!(ids, id)
end
if rv.waitingfor == pid
@@ -465,7 +465,7 @@ function send_del_client(rr::RemoteRef)
if rr.where == myid()
del_client(rr2id(rr), myid())
else
- if contains(map_del_wrkr, rr.where)
+ if in(rr.where, map_del_wrkr)
# for a removed worker, don't bother
return
end
@@ -883,7 +883,7 @@ function create_message_handler_loop(sock::AsyncStream) #returns immediately
if (myid() == 1)
global rmprocset
- if contains(rmprocset, iderr)
+ if in(iderr, rmprocset)
delete!(rmprocset, iderr)
else
println("Worker $iderr terminated.")
View
@@ -34,7 +34,7 @@ start(x::Number) = false
next(x::Number, state) = (x, true)
done(x::Number, state) = state
isempty(x::Number) = false
-contains(x::Number, y::Number) = x == y
+in(x::Number, y::Number) = x == y
reinterpret{T<:Real}(::Type{T}, x::Real) = box(T,x)
View
@@ -303,12 +303,12 @@ function __fixup(
exclude = []
)
sort!(instlist, lt=function(a,b)
- c = contains(Read.alldependencies(a,avail,free,fixed),b)
- nonordered = (!c && !contains(Read.alldependencies(b,avail,free,fixed),a))
+ c = in(b,Read.alldependencies(a,avail,free,fixed))
+ nonordered = (!c && !in(a,Read.alldependencies(b,avail,free,fixed)))
nonordered ? a < b : c
end)
for p in instlist
- contains(exclude,p) && continue
+ in(p,exclude) && continue
build(p,["fixup"]) || return
end
end
@@ -325,9 +325,9 @@ function _fixup{T<:String}(
oldlength = length(tofixup)
while true
for (p,_) in inst
- contains(tofixup,p) && continue
+ in(p,tofixup) && continue
for pf in tofixup
- if contains(Read.alldependencies(p,avail,free,fixed),pf)
+ if in(pf,Read.alldependencies(p,avail,free,fixed))
push!(tofixup,p)
break
end
View
@@ -61,8 +61,8 @@ function diff(have::Dict, want::Dict, avail::Dict, fixed::Dict)
# Sort packages topologically
sort!(changes, lt=function(a,b)
((a,vera),(b,verb)) = (a,b)
- c = contains(Pkg.Read.alldependencies(a,avail,want,fixed),b)
- unnordered = (!c && !contains(Pkg.Read.alldependencies(b,avail,want,fixed),a))
+ c = in(b,Pkg.Read.alldependencies(a,avail,want,fixed))
+ unnordered = (!c && !in(a,Pkg.Read.alldependencies(b,avail,want,fixed)))
unnordered ? a < b : c
end)
@@ -93,7 +93,7 @@ function prune_versions(reqs::Requires, deps::Dict{ByteString,Dict{VersionNumber
allowed[p] = (VersionNumber=>Bool)[]
allowedp = allowed[p]
for (vn,_) in deps[p]
- allowedp[vn] = contains(vs, vn)
+ allowedp[vn] = in(vn, vs)
end
@assert !isempty(allowedp)
any([a for (_,a) in allowedp]) ||
@@ -162,7 +162,7 @@ function prune_versions(reqs::Requires, deps::Dict{ByteString,Dict{VersionNumber
@assert haskey(vmask, p)
vmaskp = vmask[p]
for (vn,vm) in vmaskp
- push!(vm, contains(vs, vn))
+ push!(vm, in(vn, vs))
end
end
@@ -227,7 +227,7 @@ function prune_versions(reqs::Requires, deps::Dict{ByteString,Dict{VersionNumber
new_deps[p] = (VersionNumber=>Available)[]
pruned_versp = pruned_vers[p]
for (vn,a) in depsp
- contains(pruned_versp, vn) || continue
+ in(vn, pruned_versp) || continue
new_deps[p][vn] = a
end
end
@@ -269,7 +269,7 @@ function dependencies_subset(deps::Dict{ByteString,Dict{VersionNumber,Available}
while !isempty(staged)
staged_next = Set{ByteString}()
for p in staged, (_,a) in deps[p], (rp,_) in a.requires
- if !contains(allpkgs, rp)
+ if !in(rp, allpkgs)
push!(staged_next, rp)
end
end
View
@@ -146,7 +146,7 @@ function alldependencies(pkg::String,avail::Dict=available(),free::Dict=free(ins
deps = [ k for (k,v) in dependencies(pkg,avail,free,fix) ]
alldeps = copy(deps)
for dep in deps
- dep != "julia" && !contains(alldeps,dep) && append!(alldeps,[ k for (k,v) in alldependencies(dep,avail,free,fix) ])
+ dep != "julia" && !in(dep,alldeps) && append!(alldeps,[ k for (k,v) in alldependencies(dep,avail,free,fix) ])
end
alldeps
end
@@ -132,7 +132,7 @@ function verify_solution(sol::Vector{Int}, interface::Interface)
p0 = pdict[p]
@assert sol[p0] != spp[p0]
vn = pvers[p0][sol[p0]]
- @assert contains(vs, vn)
+ @assert in(vn, vs)
end
# verify dependencies
@@ -146,7 +146,7 @@ function verify_solution(sol::Vector{Int}, interface::Interface)
p1 = pdict[rp]
@assert sol[p1] != spp[p1]
vn = pvers[p1][sol[p1]]
- @assert contains(rvs, vn)
+ @assert in(vn, rvs)
end
end
end
@@ -210,7 +210,7 @@ function enforce_optimality!(sol::Vector{Int}, interface::Interface)
break
end
vn = pvers[p1][sol[p1]]
- if !contains(vs, vn)
+ if !in(vn, vs)
# the dependency is violated because
# the other package version is invalid
viol = true
@@ -229,7 +229,7 @@ function enforce_optimality!(sol::Vector{Int}, interface::Interface)
continue
end
vn = pvers[p0][s0+1]
- if !contains(vs, vn)
+ if !in(vn, vs)
# bumping the version would violate
# the dependency
viol = true
@@ -137,7 +137,7 @@ type Graph
end
for v1 = 1:length(pvers[p1])
- if !contains(rvs, pvers[p1][v1])
+ if !in(pvers[p1][v1], rvs)
bm[v1, v0] = false
bmt[v0, v1] = false
end
@@ -200,7 +200,7 @@ type Messages
fld0 = fld[p0]
for v0 = 1:spp[p0]-1
vn = pvers0[v0]
- if !contains(rvs, vn)
+ if !in(vn, rvs)
# the state is forbidden by requirements
fld0[v0] = FieldValue(-1)
else
Oops, something went wrong.

0 comments on commit 7ee7173

Please sign in to comment.