diff --git a/base/array.jl b/base/array.jl index 04a9ffc0a2c85..780ec2d2acdba 100644 --- a/base/array.jl +++ b/base/array.jl @@ -1331,9 +1331,9 @@ end function findin(a, b) ind = Array(Int, 0) - bset = Set(b...) + bset = add_each!(Set(), b) for i = 1:length(a) - if has(bset, a[i]) + if contains(bset, a[i]) push!(ind, i) end end @@ -1486,14 +1486,17 @@ ctranspose{T<:Number}(x::StridedMatrix{T}) = [ conj(x[j,i]) for i=1:size(x,2), j # set-like operators for vectors # These are moderately efficient, preserve order, and remove dupes. -# algorithm: do intersect on sets first, then iterate through the first -# vector and produce only those in the set function intersect(vs...) args_type = promote_type([eltype(v) for v in vs]...) ret = Array(args_type,0) - all_elems = intersect([Set(v...) for v in vs]...) for v_elem in vs[1] - if has(all_elems, v_elem) + inall = true + for i = 2:length(vs) + if !contains(vs[i], v_elem) + inall=false; break + end + end + if inall push!(ret, v_elem) end end @@ -1505,7 +1508,7 @@ function union(vs...) seen = Set() for v in vs for v_elem in v - if !has(seen, v_elem) + if !contains(seen, v_elem) push!(ret, v_elem) add!(seen, v_elem) end @@ -1520,7 +1523,7 @@ function setdiff(a, b) ret = Array(args_type,0) seen = Set() for a_elem in a - if !has(seen, a_elem) && !has(bset, a_elem) + if !contains(seen, a_elem) && !contains(bset, a_elem) push!(ret, a_elem) add!(seen, a_elem) end diff --git a/base/darray2.jl b/base/darray2.jl index 546a585aded10..e11bc8cb3a90f 100644 --- a/base/darray2.jl +++ b/base/darray2.jl @@ -60,7 +60,7 @@ function defaultdist(dims, procs) dims = [dims...] chunks = ones(Int, length(dims)) np = length(procs) - f = sort!(keys(factor(np)), Sort.Reverse) + f = sort!(collect(keys(factor(np))), Sort.Reverse) k = 1 while np > 1 # repeatedly allocate largest factor to largest dim diff --git a/base/deprecated.jl b/base/deprecated.jl index c8077ff45ab76..769b61bb21cd7 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -185,6 +185,8 @@ export PipeString @deprecate remote_call remotecall @deprecate remote_call_fetch remotecall_fetch @deprecate remote_call_wait remotecall_wait +@deprecate has(s::Set, x) contains(s, x) +@deprecate has(s::IntSet, x) contains(s, x) @deprecate expr(hd, a...) Expr(hd, a...) @deprecate expr(hd, a::Array{Any,1}) Expr(hd, a...) diff --git a/base/dict.jl b/base/dict.jl index 45f639bff771e..f253c21448950 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -4,8 +4,12 @@ abstract Associative{K,V} const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__ -has(t::Associative, key) = !is(get(t, key, secret_table_token), - secret_table_token) +has(d::Associative, k) = contains(keys(d),k) + +function contains(a::Associative, p::(Any,Any)) + v = get(a,p[1],secret_table_token) + !is(v, secret_table_token) && isequal(v, p[2]) +end function show{K,V}(io::IO, t::Associative{K,V}) if isempty(t) @@ -29,25 +33,36 @@ function show{K,V}(io::IO, t::Associative{K,V}) end end -function keys(T, a::Associative) - i = 0 - keyz = Array(T,length(a)) - for (k,v) in a - keyz[i+=1] = k - end - return keyz +immutable KeyIterator{T<:Associative} + dict::T +end +immutable ValueIterator{T<:Associative} + dict::T end -keys{K,V}(a::Associative{K,V}) = keys(K,a) -function values(T, a::Associative) - i = 0 - vals = Array(T,length(a)) - for (k,v) in a - vals[i+=1] = v - end - return vals +length(v::Union(KeyIterator,ValueIterator)) = length(v.dict) +isempty(v::Union(KeyIterator,ValueIterator)) = isempty(v.dict) +eltype(v::KeyIterator) = eltype(v.dict)[1] +eltype(v::ValueIterator) = eltype(v.dict)[2] + +start(v::Union(KeyIterator,ValueIterator)) = start(v.dict) +done(v::Union(KeyIterator,ValueIterator), state) = done(v.dict, state) + +function next(v::KeyIterator, state) + n = next(v.dict, state) + n[1][1], n[2] +end + +function next(v::ValueIterator, state) + n = next(v.dict, state) + n[1][2], n[2] end -values{K,V}(a::Associative{K,V}) = values(V,a) + +contains(v::KeyIterator, k) = !is(get(v.dict, k, secret_table_token), + secret_table_token) + +keys(a::Associative) = KeyIterator(a) +values(a::Associative) = ValueIterator(a) function copy(a::Associative) b = similar(a) @@ -451,6 +466,7 @@ function get{K,V}(h::Dict{K,V}, key, deflt) end has(h::Dict, key) = (ht_keyindex(h, key) >= 0) +contains{T<:Dict}(v::KeyIterator{T}, key) = (ht_keyindex(v.dict, key) >= 0) function getkey{K,V}(h::Dict{K,V}, key, deflt) index = ht_keyindex(h, key) @@ -492,6 +508,9 @@ next(t::Dict, i) = ((t.keys[i],t.vals[i]), skip_deleted(t,i+1)) isempty(t::Dict) = (t.count == 0) length(t::Dict) = t.count +next{T<:Dict}(v::KeyIterator{T}, i) = (v.dict.keys[i], skip_deleted(v.dict,i+1)) +next{T<:Dict}(v::ValueIterator{T}, i) = (v.dict.vals[i], skip_deleted(v.dict,i+1)) + # weak key dictionaries function add_weak_key(t::Dict, k, v) diff --git a/base/env.jl b/base/env.jl index 5bb9dc6ed3162..81fea34891451 100644 --- a/base/env.jl +++ b/base/env.jl @@ -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) -has(::EnvHash, k::String) = _hasenv(k) +contains(::KeyIterator{EnvHash}, k::String) = _hasenv(k) delete!(::EnvHash, k::String) = (v = ENV[k]; _unsetenv(k); v) delete!(::EnvHash, k::String, def) = has(ENV,k) ? delete!(ENV,k) : def setindex!(::EnvHash, v, k::String) = _setenv(k,string(v)) diff --git a/base/git.jl b/base/git.jl index 4ea318e36ff7f..af3443e931163 100644 --- a/base/git.jl +++ b/base/git.jl @@ -111,7 +111,7 @@ end function write_config(file::String, cfg::Dict) tmp = tempname() - for key in sort!(keys(cfg)) + for key in sort!(collect(keys(cfg))) val = cfg[key] if isa(val,Array) for x in val diff --git a/base/intset.jl b/base/intset.jl index 5f34c8a6d1ae8..f69cf66e116f2 100644 --- a/base/intset.jl +++ b/base/intset.jl @@ -106,7 +106,7 @@ function copy!(to::IntSet, from::IntSet) union!(to, from) end -function has(s::IntSet, n::Integer) +function contains(s::IntSet, n::Integer) if n >= s.limit s.fill1s && n >= 0 else diff --git a/base/meta.jl b/base/meta.jl index 883cbd7502601..f1f640643e7d6 100644 --- a/base/meta.jl +++ b/base/meta.jl @@ -10,7 +10,7 @@ export quot, quot(ex) = Expr(:quote, ex) isexpr(ex::Expr, head) = ex.head === head -isexpr(ex::Expr, heads::Set) = has(heads, ex.head) +isexpr(ex::Expr, heads::Set) = contains(heads, ex.head) isexpr(ex::Expr, heads::Vector) = contains(heads, ex.head) isexpr(ex, head) = false diff --git a/base/pkg.jl b/base/pkg.jl index c42c961f8ded4..2a8056f644493 100644 --- a/base/pkg.jl +++ b/base/pkg.jl @@ -310,7 +310,7 @@ function _resolve() end want = Resolve.resolve(reqs,vers,deps) - pkgs = sort!(keys(merge(want,have))) + pkgs = sort!(collect(keys(merge(want,have)))) for pkg in pkgs if has(have,pkg) managed = cd(pkg) do diff --git a/base/set.jl b/base/set.jl index d7ea4e9a9528b..8aaf3f4a78143 100644 --- a/base/set.jl +++ b/base/set.jl @@ -14,8 +14,7 @@ isempty(s::Set) = isempty(s.dict) length(s::Set) = length(s.dict) eltype{T}(s::Set{T}) = T -has(s::Set, x) = has(s.dict, x) -contains(s::Set, x) = has(s, x) +contains(s::Set, x) = has(s.dict, x) add!(s::Set, x) = (s.dict[x] = nothing; s) delete!(s::Set, x) = (delete!(s.dict, x); x) @@ -62,7 +61,7 @@ function intersect(s::Set, sets::Set...) i = copy(s) for x in s for t in sets - if !has(t,x) & has(i,x) + if !contains(t,x) & contains(i,x) delete!(i,x) end end @@ -73,7 +72,7 @@ end function setdiff(a::Set, b::Set) d = copy(a) for x in b - if has(d, x) + if contains(d, x) delete!(d, x) end end @@ -88,7 +87,7 @@ isequal(l::Set, r::Set) = (length(l) == length(r)) && (l <= r) isless(l::Set, r::Set) = (length(l) < length(r)) && (l <= r) function <=(l::Set, r::Set) for elt in l - if !has(r, elt) + if !contains(r, elt) return false end end diff --git a/base/show.jl b/base/show.jl index 58c14225dae92..09666d3a5ae87 100644 --- a/base/show.jl +++ b/base/show.jl @@ -140,8 +140,8 @@ function show_indented(io::IO, ex::Expr, indent::Int) end const paren_quoted_syms = Set{Symbol}(:(:),:(::),:(:=),:(=),:(==),:(===),:(=>)) function show_indented(io::IO, sym::Symbol, indent::Int) - if has(paren_quoted_syms, sym); print(io, ":($sym)") - else print(io, ":$sym") + if contains(paren_quoted_syms, sym); print(io, ":($sym)") + else print(io, ":$sym") end end function default_show_quoted(io::IO, ex, indent::Int) @@ -227,9 +227,9 @@ function show_unquoted(io::IO, ex::Expr, indent::Int) show_unquoted(io, args[2], indent + indent_width) print(io, ')') end - elseif (has(_expr_infix, head) && nargs==2) || (is(head,:(:)) && nargs==3) + elseif (contains(_expr_infix, head) && nargs==2) || (is(head,:(:)) && nargs==3) show_list(io, args, head, indent) - elseif has(_expr_infix_wide, head) && nargs == 2 + elseif contains(_expr_infix_wide, head) && nargs == 2 show_list(io, args, " $head ", indent) elseif is(head, symbol("::")) && nargs == 1 print(io, "::") diff --git a/base/sparse.jl b/base/sparse.jl index 49de22084146d..422bf08a5d2f2 100644 --- a/base/sparse.jl +++ b/base/sparse.jl @@ -183,9 +183,9 @@ end # Construct a sparse vector -sparsevec{K<:Integer,V}(d::Dict{K,V}, len::Int) = sparsevec(keys(d), values(d), len) +sparsevec{K<:Integer,V}(d::Dict{K,V}, len::Int) = sparsevec(collect(keys(d)), collect(values(d)), len) -sparsevec{K<:Integer,V}(d::Dict{K,V}) = sparsevec(keys(d), values(d)) +sparsevec{K<:Integer,V}(d::Dict{K,V}) = sparsevec(collect(keys(d)), collect(values(d))) sparsevec(I::AbstractVector, V, m::Integer) = sparsevec(I, V, m, +) diff --git a/test/corelib.jl b/test/corelib.jl index ea03da79f14c2..e63cce61cde20 100644 --- a/test/corelib.jl +++ b/test/corelib.jl @@ -118,7 +118,7 @@ let end _d = {"a"=>0} -@test isa([k for k in filter(x->length(x)==1, keys(_d))], Vector{Any}) +@test isa([k for k in filter(x->length(x)==1, collect(keys(_d)))], Vector{Any}) # issue #1821 let @@ -154,7 +154,7 @@ begin if id > 0 x = xs[id] add!(s, x) - @test has(s, x) # check that x can be found + @test contains(s, x) # check that x can be found else delete!(s, xs[-id]) end @@ -258,8 +258,6 @@ for i in 1:2:1000 delete!(s, i) end for i in 1:2:1000 - @test !has(s, i) - @test has(s, i+1) @test !contains(s, i) @test contains(s, i+1) end @@ -351,8 +349,8 @@ c = copy(s) @test isequal(s,c) add!(s,100) add!(c,200) -@test !has(c, 100) -@test !has(s, 200) +@test !contains(c, 100) +@test !contains(s, 200) # start, done, next for data_in in ((7,8,4,5), @@ -368,7 +366,7 @@ for data_in in ((7,8,4,5), t = tuple(s...) @test length(t) == length(s) for e in t - @test has(s,e) + @test contains(s,e) end end @@ -388,8 +386,8 @@ origs = Set(1,2,3,"apple") s = copy(origs) for i in 1:length(origs) el = pop!(s) - @test !has(s, el) - @test has(origs, el) + @test !contains(s, el) + @test contains(origs, el) end @test isempty(s) # isequal