Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit e8a3368

Browse files
committed
Merge branch 'master' of github.com:JuliaLang/julia
2 parents 5bff0a7 + 867fe1a commit e8a3368

16 files changed

+89
-63
lines changed

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ Library improvements
183183
* The `setenv` function for external processes now accepts a `dir` keyword
184184
argument for specifying the directory to start the child process in ([#4888]).
185185

186+
* Constructors for collections (`Set`, `Dict`, etc.) now generally accept a
187+
single iterable argument giving the elements of the collection ([#4996], [#4871])
188+
186189
Deprecated or removed
187190
---------------------
188191

@@ -270,6 +273,8 @@ Deprecated or removed
270273
[#5748]: https://github.com/JuliaLang/julia/issues/5748
271274
[#5511]: https://github.com/JuliaLang/julia/issues/5511
272275
[#5819]: https://github.com/JuliaLang/julia/issues/5819
276+
[#4871]: https://github.com/JuliaLang/julia/issues/4871
277+
[#4996]: https://github.com/JuliaLang/julia/issues/4996
273278

274279
Julia v0.2.0 Release Notes
275280
==========================

base/array.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ end
13281328
# setdiff only accepts two args
13291329
function setdiff(a, b)
13301330
args_type = promote_type(eltype(a), eltype(b))
1331-
bset = Set(b...)
1331+
bset = Set(b)
13321332
ret = Array(args_type,0)
13331333
seen = Set()
13341334
for a_elem in a

base/collections.jl

+11-9
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,17 @@ type PriorityQueue{K,V} <: Associative{K,V}
124124

125125
function PriorityQueue(ks::AbstractArray{K}, vs::AbstractArray{V},
126126
o::Ordering)
127+
# TODO: maybe deprecate
127128
if length(ks) != length(vs)
128129
error("key and value arrays must have equal lengths")
129130
end
131+
PriorityQueue{K,V}(zip(ks, vs))
132+
end
130133

131-
xs = Array((K, V), length(ks))
134+
function PriorityQueue(itr, o::Ordering)
135+
xs = Array((K, V), length(itr))
132136
index = Dict{K, Int}()
133-
for (i, (k, v)) in enumerate(zip(ks, vs))
137+
for (i, (k, v)) in enumerate(itr)
134138
xs[i] = (k, v)
135139
if haskey(index, k)
136140
error("PriorityQueue keys must be unique")
@@ -150,15 +154,13 @@ end
150154

151155
PriorityQueue(o::Ordering=Forward) = PriorityQueue{Any,Any}(o)
152156

153-
function PriorityQueue{K,V}(ks::AbstractArray{K}, vs::AbstractArray{V},
154-
o::Ordering=Forward)
155-
PriorityQueue{K,V}(ks, vs, o)
156-
end
157+
# TODO: maybe deprecate
158+
PriorityQueue{K,V}(ks::AbstractArray{K}, vs::AbstractArray{V},
159+
o::Ordering=Forward) = PriorityQueue{K,V}(ks, vs, o)
157160

158-
function PriorityQueue{K,V}(kvs::Dict{K,V}, o::Ordering=Forward)
159-
PriorityQueue{K,V}([k for k in keys(kvs)], [v for v in values(kvs)], o)
160-
end
161+
PriorityQueue{K,V}(kvs::Associative{K,V}, o::Ordering=Forward) = PriorityQueue{K,V}(kvs, o)
161162

163+
PriorityQueue{K,V}(a::AbstractArray{(K,V)}, o::Ordering=Forward) = PriorityQueue{K,V}(a, o)
162164

163165
length(pq::PriorityQueue) = length(pq.xs)
164166
isempty(pq::PriorityQueue) = isempty(pq.xs)

base/deprecated.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,16 @@ eval(Sys, :(@deprecate shlib_list dllist))
395395
@deprecate put put!
396396
@deprecate take take!
397397

398+
@deprecate Set(a, b...) Set({a, b...})
399+
# for a bit of backwards compatibility
400+
IntSet(xs::Integer...) = (s=IntSet(); for a in xs; push!(s,a); end; s)
401+
Set{T<:Number}(xs::T...) = Set{T}(xs)
402+
403+
398404
# 0.3 discontinued functions
399405

400406
function nnz(X)
401407
depwarn("nnz has been renamed to countnz and is no longer computed in constant time for sparse matrices. Instead, use nfilled() for the number of elements in a sparse matrix.", :nnz)
402408
countnz(X)
403409
end
404410
export nnz
405-
406-

base/dict.jl

+9
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ push!(t::Associative, key, v) = setindex!(t, v, key)
152152
type ObjectIdDict <: Associative{Any,Any}
153153
ht::Array{Any,1}
154154
ObjectIdDict() = new(cell(32))
155+
156+
function ObjectIdDict(itr)
157+
d = ObjectIdDict()
158+
for (k,v) in itr
159+
d[k] = v
160+
end
161+
d
162+
end
155163
end
156164

157165
similar(d::ObjectIdDict) = ObjectIdDict()
@@ -318,6 +326,7 @@ Dict{K }(ks::(K...), vs::Tuple ) = Dict{K ,Any}(ks, vs)
318326
Dict{V }(ks::Tuple , vs::(V...)) = Dict{Any,V }(ks, vs)
319327

320328
Dict{K,V}(kv::AbstractArray{(K,V)}) = Dict{K,V}(kv)
329+
Dict{K,V}(kv::Associative{K,V}) = Dict{K,V}(kv)
321330

322331
similar{K,V}(d::Dict{K,V}) = (K=>V)[]
323332

base/intset.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type IntSet
66
IntSet() = new(zeros(Uint32,256>>>5), 256, false)
77
end
88

9-
IntSet(args...) = (s=IntSet(); for a in args; push!(s,a); end; s)
9+
IntSet(itr) = (s=IntSet(); for a in itr; push!(s,a); end; s)
1010

1111
similar(s::IntSet) = IntSet()
1212

@@ -167,7 +167,7 @@ length(s::IntSet) = int(ccall(:bitvector_count, Uint64, (Ptr{Uint32}, Uint64, Ui
167167
(s.fill1s ? typemax(Int) - s.limit : 0)
168168

169169
function show(io::IO, s::IntSet)
170-
print(io, "IntSet(")
170+
print(io, "IntSet([")
171171
first = true
172172
for n in s
173173
if n > s.limit
@@ -182,7 +182,7 @@ function show(io::IO, s::IntSet)
182182
if s.fill1s
183183
print(io, ", ..., ", typemax(Int)-1, ")")
184184
else
185-
print(io, ")")
185+
print(io, "])")
186186
end
187187
end
188188

base/pkg/query.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ function dependencies_subset(deps::Dict{ByteString,Dict{VersionNumber,Available}
326326
end
327327

328328
function prune_dependencies(reqs::Requires, deps::Dict{ByteString,Dict{VersionNumber,Available}})
329-
deps = dependencies_subset(deps, Set{ByteString}(keys(reqs)...))
329+
deps = dependencies_subset(deps, Set{ByteString}(keys(reqs)))
330330
deps, _ = prune_versions(reqs, deps)
331331

332332
return deps

base/pkg/resolve/interface.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Interface
4343
function Interface(reqs::Requires, deps::Dict{ByteString,Dict{VersionNumber,Available}})
4444

4545
# generate pkgs
46-
pkgs = sort!(ByteString[Set{ByteString}(keys(deps)...)...])
46+
pkgs = sort!(ByteString[Set{ByteString}(keys(deps))...])
4747

4848
np = length(pkgs)
4949

base/repl.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ showerror(io::IO, e::KeyError) = (print(io, "key not found: "); show(io, e.key))
9999
showerror(io::IO, e::InterruptException) = print(io, "interrupt")
100100

101101
function showerror(io::IO, e::MethodError)
102-
name = e.f.env.name
102+
name = isgeneric(e.f) ? e.f.env.name : :anonymous
103103
if isa(e.f, DataType)
104104
print(io, "no method $(e.f)(")
105105
else

base/set.jl

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ type Set{T}
22
dict::Dict{T,Nothing}
33

44
Set() = new(Dict{T,Nothing}())
5-
Set(x...) = union!(new(Dict{T,Nothing}()), x)
5+
Set(itr) = union!(new(Dict{T,Nothing}()), itr)
6+
7+
# for backwards compat
8+
Set(xs::T...) = Set{T}(xs)
69
end
710
Set() = Set{Any}()
8-
Set(x...) = Set{Any}(x...)
9-
Set{T}(x::T...) = Set{T}(x...)
11+
Set(itr) = Set{eltype(itr)}(itr)
1012

11-
show(io::IO, s::Set) = (show(io, typeof(s)); show_comma_array(io, s,'(',')'))
13+
show(io::IO, s::Set) = (show(io, typeof(s)); show_comma_array(io, s,"({","})"))
1214

1315
isempty(s::Set) = isempty(s.dict)
1416
length(s::Set) = length(s.dict)

base/show.jl

+8-8
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ print(io::IO, n::Unsigned) = print(io, dec(n))
212212
# original expression.
213213
#
214214
# This is consistent with many other show methods, i.e.:
215-
# show(Set(1,2,3)) # ==> "Set{Int64}(2,3,1)"
216-
# eval(parse("Set{Int64}(2,3,1)”) # ==> An actual set
215+
# show(Set([1,2,3])) # ==> "Set{Int64}([2,3,1])"
216+
# eval(parse("Set{Int64}([2,3,1])”) # ==> An actual set
217217
# While this isn’t true of ALL show methods, it is of all ASTs.
218218

219219
typealias ExprNode Union(Expr, QuoteNode, SymbolNode, LineNumberNode,
@@ -227,8 +227,8 @@ show_unquoted(io::IO, ex, ::Int,::Int) = show(io, ex)
227227
## AST printing constants ##
228228

229229
const indent_width = 4
230-
const quoted_syms = Set{Symbol}(:(:),:(::),:(:=),:(=),:(==),:(===),:(=>))
231-
const uni_ops = Set{Symbol}(:(+), :(-), :(!), :(~), :(<:), :(>:))
230+
const quoted_syms = Set{Symbol}([:(:),:(::),:(:=),:(=),:(==),:(===),:(=>)])
231+
const uni_ops = Set{Symbol}([:(+), :(-), :(!), :(~), :(<:), :(>:)])
232232
const bin_ops_by_prec = [
233233
"= := += -= *= /= //= .//= .*= ./= \\= .\\= ^= .^= %= .%= |= &= \$= => <<= >>= >>>= ~ .+= .-=",
234234
"?",
@@ -247,10 +247,10 @@ const bin_ops_by_prec = [
247247
"."
248248
]
249249
const bin_op_precs = Dict{Symbol,Int}(merge([{symbol(op)=>i for op=split(bin_ops_by_prec[i])} for i=1:length(bin_ops_by_prec)]...))
250-
const bin_ops = Set{Symbol}(keys(bin_op_precs)...)
251-
const expr_infix_wide = Set(:(=), :(+=), :(-=), :(*=), :(/=), :(\=), :(&=),
252-
:(|=), :($=), :(>>>=), :(>>=), :(<<=), :(&&), :(||))
253-
const expr_infix = Set(:(:), :(<:), :(->), :(=>), symbol("::"))
250+
const bin_ops = Set{Symbol}(keys(bin_op_precs))
251+
const expr_infix_wide = Set([:(=), :(+=), :(-=), :(*=), :(/=), :(\=), :(&=),
252+
:(|=), :($=), :(>>>=), :(>>=), :(<<=), :(&&), :(||)])
253+
const expr_infix = Set([:(:), :(<:), :(->), :(=>), symbol("::")])
254254
const expr_calls = [:call =>('(',')'), :ref =>('[',']'), :curly =>('{','}')]
255255
const expr_parens = [:tuple=>('(',')'), :vcat=>('[',']'), :cell1d=>('{','}'),
256256
:hcat =>('[',']'), :row =>('[',']')]

base/util.jl

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ end
8989
# searching definitions
9090

9191
function which(f::Callable, args...)
92+
if !isgeneric(f)
93+
throw(ErrorException("not a generic function, no methods available"))
94+
end
9295
ms = methods(f, map(a->(isa(a,Type) ? Type{a} : typeof(a)), args))
9396
isempty(ms) && throw(MethodError(f, args))
9497
ms[1]

doc/stdlib/base.rst

+5-4
Original file line numberDiff line numberDiff line change
@@ -798,13 +798,14 @@ Partially implemented by: ``IntSet``, ``Set``, ``EnvHash``, ``Array``, ``BitArra
798798
Set-Like Collections
799799
--------------------
800800

801-
.. function:: Set(x...)
801+
.. function:: Set([itr])
802802

803-
Construct a ``Set`` with the given elements. Should be used instead of ``IntSet`` for sparse integer sets, or for sets of arbitrary objects.
803+
Construct a ``Set`` of the values generated by the given iterable object, or an empty set.
804+
Should be used instead of ``IntSet`` for sparse integer sets, or for sets of arbitrary objects.
804805

805-
.. function:: IntSet(i...)
806+
.. function:: IntSet([itr])
806807

807-
Construct a sorted set of the given integers. Implemented as a bit string, and therefore designed for dense integer sets. If the set will be sparse (for example holding a single very large integer), use ``Set`` instead.
808+
Construct a sorted set of the integers generated by the given iterable object, or an empty set. Implemented as a bit string, and therefore designed for dense integer sets. If the set will be sparse (for example holding a single very large integer), use ``Set`` instead.
808809

809810
.. function:: union(s1,s2...)
810811

test/collections.jl

+26-26
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,24 @@ end
198198

199199
# isempty
200200
@test isempty(Set())
201-
@test !isempty(Set(1))
202-
@test !isempty(Set("banana", "apple"))
203-
@test !isempty(Set(1, 1:10, "pear"))
201+
@test !isempty(Set([1]))
202+
@test !isempty(Set(["banana", "apple"]))
203+
@test !isempty(Set({1, 1:10, "pear"}))
204204

205205
# ordering
206-
@test Set() < Set(1)
207-
@test Set(1) < Set(1,2)
208-
@test !(Set(3) < Set(1,2))
209-
@test !(Set(3) > Set(1,2))
210-
@test Set(1,2,3) > Set(1,2)
211-
@test !(Set(3) <= Set(1,2))
212-
@test !(Set(3) >= Set(1,2))
213-
@test Set(1) <= Set(1,2)
214-
@test Set(1,2) <= Set(1,2)
215-
@test Set(1,2) >= Set(1,2)
216-
@test Set(1,2,3) >= Set(1,2)
217-
@test !(Set(1,2,3) >= Set(1,2,4))
218-
@test !(Set(1,2,3) <= Set(1,2,4))
206+
@test Set() < Set([1])
207+
@test Set([1]) < Set([1,2])
208+
@test !(Set([3]) < Set([1,2]))
209+
@test !(Set([3]) > Set([1,2]))
210+
@test Set([1,2,3]) > Set([1,2])
211+
@test !(Set([3]) <= Set([1,2]))
212+
@test !(Set([3]) >= Set([1,2]))
213+
@test Set([1]) <= Set([1,2])
214+
@test Set([1,2]) <= Set([1,2])
215+
@test Set([1,2]) >= Set([1,2])
216+
@test Set([1,2,3]) >= Set([1,2])
217+
@test !(Set([1,2,3]) >= Set([1,2,4]))
218+
@test !(Set([1,2,3]) <= Set([1,2,4]))
219219

220220
# add, length
221221
s = Set()
@@ -236,7 +236,7 @@ end
236236

237237
# elements
238238
data_in = (1,"banana", ())
239-
s = Set(data_in...)
239+
s = Set(data_in)
240240
data_out = collect(s)
241241
@test is(typeof(data_out), Array{Any,1})
242242
@test all(map(d->in(d,data_out), data_in))
@@ -246,15 +246,15 @@ data_out = collect(s)
246246
@test length(data_out) == length(data_in)
247247

248248
# homogeneous sets
249-
@test is(typeof(Set(1,2,3)), Set{Int})
250-
@test is(typeof(Set{Int}(3)), Set{Int})
249+
@test is(typeof(Set([1,2,3])), Set{Int})
250+
@test is(typeof(Set{Int}([3])), Set{Int})
251251

252252
# eltype
253-
@test is(eltype(Set(1,"hello")), Any)
253+
@test is(eltype(Set({1,"hello"})), Any)
254254
@test is(eltype(Set{String}()), String)
255255

256256
# no duplicates
257-
s = Set(1,2,3)
257+
s = Set([1,2,3])
258258
@test length(s) == 3
259259
push!(s,2)
260260
@test length(s) == 3
@@ -332,16 +332,16 @@ setdiff!(s,(3,5))
332332
@test isequal(s,Set(1,7))
333333

334334
# similar
335-
s = similar(Set(1,"Banana"))
335+
s = similar(Set([1,"Banana"]))
336336
@test length(s) == 0
337337
@test typeof(s) == Set{Any}
338-
s = similar(Set{Float32}(2.0f0,3.0f0,4.0f0))
338+
s = similar(Set{Float32}([2.0f0,3.0f0,4.0f0]))
339339
@test length(s) == 0
340340
@test typeof(s) == Set{Float32}
341341

342342
# copy
343343
data_in = (1,2,9,8,4)
344-
s = Set(data_in...)
344+
s = Set(data_in)
345345
c = copy(s)
346346
@test isequal(s,c)
347347
push!(s,100)
@@ -352,7 +352,7 @@ push!(c,200)
352352
# start, done, next
353353
for data_in in ((7,8,4,5),
354354
("hello", 23, 2.7, (), [], (1,8)))
355-
s = Set(data_in...)
355+
s = Set(data_in)
356356

357357
s_new = Set()
358358
for el in s
@@ -379,7 +379,7 @@ end
379379
end
380380

381381
# pop!
382-
origs = Set(1,2,3,"apple")
382+
origs = Set([1,2,3,"apple"])
383383
s = copy(origs)
384384
for i in 1:length(origs)
385385
el = pop!(s)

test/perf/kernel/actor_centrality.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616

1717
function centrality_mean(G::Graph, start_node)
1818
dists = Dict{Node,Uint64}()
19-
next = Set(G[start_node])
19+
next = Set([G[start_node]])
2020

2121
cdist = 0
2222
while !isempty(next)

test/perf/spell/perf.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function edits1(word::String)
3838
transposes = ["$a$(b[2])$(b[1])$(b[3:end])" for (a,b) in splits[1:end-2]]
3939
replaces = ["$a$c$(b[2:end])" for (a,b) in splits[1:end-1], c in alphabet]
4040
inserts = ["$a$c$b" for (a,b) in splits, c in alphabet]
41-
return Set(deletes..., transposes..., replaces..., inserts...)
41+
return Set([deletes; transposes; replaces[:]; inserts[:]])
4242
end
4343

4444
function known_edits2(word::String)
@@ -63,7 +63,7 @@ function correct(word::String)
6363
candidates = known([word])
6464
length(candidates) == 0 && (candidates = known(edits1(word)))
6565
length(candidates) == 0 && (candidates = known_edits2(word) )
66-
length(candidates) == 0 && (candidates = Set(word) )
66+
length(candidates) == 0 && (candidates = Set([word]) )
6767

6868
maximum(x->(get(NWORDS, x, 0),x), candidates)[2]
6969
end

0 commit comments

Comments
 (0)