From 3e434ac1dc1e957ce280894788516bc9e2f9f532 Mon Sep 17 00:00:00 2001 From: Sacha Verweij Date: Sun, 22 Oct 2017 19:49:03 -0700 Subject: [PATCH] Deprecate IntSet to BitSet. --- base/codevalidation.jl | 6 +- base/dates/query.jl | 6 +- base/deprecated.jl | 3 + base/distributed/process_messages.jl | 4 +- base/exports.jl | 2 +- base/inference.jl | 18 +-- base/intset.jl | 122 ++++++++--------- base/precompile.jl | 12 +- base/random/generation.jl | 6 +- base/random/random.jl | 2 +- base/set.jl | 2 +- doc/src/stdlib/collections.md | 16 +-- test/intset.jl | 194 +++++++++++++-------------- test/random.jl | 4 +- test/sets.jl | 6 +- 15 files changed, 203 insertions(+), 200 deletions(-) diff --git a/base/codevalidation.jl b/base/codevalidation.jl index 4914033d064f1f..7c0daf76aca5fb 100644 --- a/base/codevalidation.jl +++ b/base/codevalidation.jl @@ -56,8 +56,8 @@ InvalidCodeError(kind::AbstractString) = InvalidCodeError(kind, nothing) Validate `c`, logging any violation by pushing an `InvalidCodeError` into `errors`. """ function validate_code!(errors::Vector{>:InvalidCodeError}, c::CodeInfo, is_top_level::Bool = false) - ssavals = IntSet() - lhs_slotnums = IntSet() + ssavals = BitSet() + lhs_slotnums = BitSet() walkast(c.code) do x if isa(x, Expr) !is_top_level && x.head == :method && push!(errors, InvalidCodeError(NON_TOP_LEVEL_METHOD)) @@ -86,7 +86,7 @@ function validate_code!(errors::Vector{>:InvalidCodeError}, c::CodeInfo, is_top_ end end elseif isa(x, SSAValue) - id = x.id + 1 # ensures that id > 0 for use with IntSet + id = x.id + 1 # ensures that id > 0 for use with BitSet !in(id, ssavals) && push!(ssavals, id) end end diff --git a/base/dates/query.jl b/base/dates/query.jl index 48392ab9e03122..c1567cb9f4f84c 100644 --- a/base/dates/query.jl +++ b/base/dates/query.jl @@ -196,9 +196,9 @@ end # Total number of a day of week in the month # e.g. are there 4 or 5 Mondays in this month? -const TWENTYNINE = IntSet([1, 8, 15, 22, 29]) -const THIRTY = IntSet([1, 2, 8, 9, 15, 16, 22, 23, 29, 30]) -const THIRTYONE = IntSet([1, 2, 3, 8, 9, 10, 15, 16, 17, 22, 23, 24, 29, 30, 31]) +const TWENTYNINE = BitSet([1, 8, 15, 22, 29]) +const THIRTY = BitSet([1, 2, 8, 9, 15, 16, 22, 23, 29, 30]) +const THIRTYONE = BitSet([1, 2, 3, 8, 9, 10, 15, 16, 17, 22, 23, 24, 29, 30, 31]) """ daysofweekinmonth(dt::TimeType) -> Int diff --git a/base/deprecated.jl b/base/deprecated.jl index fce361c891a243..51a670e31dd9f1 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1909,6 +1909,9 @@ end # issue #24006 @deprecate linearindices(s::AbstractString) eachindex(s) +# deprecate IntSet to BitSet +@deprecate_binding IntSet BitSet + # Issue 24219 @deprecate float(x::AbstractString) parse(Float64, x) @deprecate float(a::AbstractArray{<:AbstractString}) parse.(Float64, a) diff --git a/base/distributed/process_messages.jl b/base/distributed/process_messages.jl index 8b4a1c7ea1df35..1e2458eff35609 100644 --- a/base/distributed/process_messages.jl +++ b/base/distributed/process_messages.jl @@ -4,13 +4,13 @@ def_rv_channel() = Channel(1) mutable struct RemoteValue c::AbstractChannel - clientset::IntSet # Set of workerids that have a reference to this channel. + clientset::BitSet # Set of workerids that have a reference to this channel. # Keeping ids instead of a count aids in cleaning up upon # a worker exit. waitingfor::Int # processor we need to hear from to fill this, or 0 - RemoteValue(c) = new(c, IntSet(), 0) + RemoteValue(c) = new(c, BitSet(), 0) end wait(rv::RemoteValue) = wait(rv.c) diff --git a/base/exports.jl b/base/exports.jl index 09fd49d701fb25..7e8091e7c6a000 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -69,7 +69,7 @@ export IndexLinear, IndexStyle, InsertionSort, - IntSet, + BitSet, IOBuffer, IOStream, LinSpace, diff --git a/base/inference.jl b/base/inference.jl index 4f65ff8a3ba967..53585a3288371e 100644 --- a/base/inference.jl +++ b/base/inference.jl @@ -129,7 +129,7 @@ mutable struct InferenceState # return type bestguess #::Type # current active instruction pointers - ip::IntSet + ip::BitSet pc´´::LineNum nstmts::Int # current exception handler info @@ -137,7 +137,7 @@ mutable struct InferenceState handler_at::Vector{Any} n_handlers::Int # ssavalue sparsity and restart info - ssavalue_uses::Vector{IntSet} + ssavalue_uses::Vector{BitSet} ssavalue_defs::Vector{LineNum} vararg_type_container #::Type @@ -254,7 +254,7 @@ mutable struct InferenceState handler_at = Any[ () for i=1:n ] n_handlers = 0 - W = IntSet() + W = BitSet() push!(W, 1) #initial pc to visit if !toplevel @@ -2926,7 +2926,7 @@ end function find_ssavalue_uses(body::Vector{Any}, nvals::Int) - uses = IntSet[ IntSet() for i = 1:nvals ] + uses = BitSet[ BitSet() for i = 1:nvals ] for line in 1:length(body) e = body[line] isa(e, Expr) && find_ssavalue_uses(e, uses, line) @@ -2934,7 +2934,7 @@ function find_ssavalue_uses(body::Vector{Any}, nvals::Int) return uses end -function find_ssavalue_uses(e::Expr, uses::Vector{IntSet}, line::Int) +function find_ssavalue_uses(e::Expr, uses::Vector{BitSet}, line::Int) head = e.head is_meta_expr_head(head) && return skiparg = (head === :(=)) @@ -5805,7 +5805,7 @@ function getfield_elim_pass!(sv::OptimizationState) end end -function _getfield_elim_pass!(e::Expr, ssa_defs::Vector{LineNum}, ssa_uses::Vector{IntSet}, sv::OptimizationState) +function _getfield_elim_pass!(e::Expr, ssa_defs::Vector{LineNum}, ssa_uses::Vector{BitSet}, sv::OptimizationState) nargs = length(e.args) for i = 1:nargs e.args[i] = _getfield_elim_pass!(e.args[i], ssa_defs, ssa_uses, sv) @@ -5870,7 +5870,7 @@ function _getfield_elim_pass!(e::Expr, ssa_defs::Vector{LineNum}, ssa_uses::Vect return e end -_getfield_elim_pass!(@nospecialize(e), ssa_defs::Vector{LineNum}, ssa_uses::Vector{IntSet}, sv::OptimizationState) = e +_getfield_elim_pass!(@nospecialize(e), ssa_defs::Vector{LineNum}, ssa_uses::Vector{BitSet}, sv::OptimizationState) = e # check if e is a successful allocation of an struct # if it is, returns (n,f) such that it is always valid to call @@ -5928,8 +5928,8 @@ end function basic_dce_pass!(sv::OptimizationState) body = sv.src.code labelmap = get_label_map(body) - reachable = IntSet() - W = IntSet() + reachable = BitSet() + W = BitSet() push!(W, 1) while !isempty(W) pc = pop!(W) diff --git a/base/intset.jl b/base/intset.jl index a83a27175aacc9..0827ba78466768 100644 --- a/base/intset.jl +++ b/base/intset.jl @@ -1,33 +1,33 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -struct IntSet <: AbstractSet{Int} +struct BitSet <: AbstractSet{Int} bits::BitVector - IntSet() = new(sizehint!(falses(0), 256)) + BitSet() = new(sizehint!(falses(0), 256)) end """ - IntSet([itr]) + BitSet([itr]) Construct a sorted set of positive `Int`s generated by the given iterable object, or an empty set. Implemented as a bit string, and therefore designed for dense integer sets. Only `Int`s greater than 0 can be stored. If the set will be sparse (for example holding a few very large integers), use [`Set`](@ref) instead. """ -IntSet(itr) = union!(IntSet(), itr) +BitSet(itr) = union!(BitSet(), itr) -eltype(::Type{IntSet}) = Int -similar(s::IntSet) = IntSet() -copy(s1::IntSet) = copy!(IntSet(), s1) -function copy!(dest::IntSet, src::IntSet) +eltype(::Type{BitSet}) = Int +similar(s::BitSet) = BitSet() +copy(s1::BitSet) = copy!(BitSet(), s1) +function copy!(dest::BitSet, src::BitSet) resize!(dest.bits, length(src.bits)) copy!(dest.bits, src.bits) dest end -eltype(s::IntSet) = Int -sizehint!(s::IntSet, n::Integer) = (n > length(s.bits) && _resize0!(s.bits, n); s) +eltype(s::BitSet) = Int +sizehint!(s::BitSet, n::Integer) = (n > length(s.bits) && _resize0!(s.bits, n); s) # An internal function for setting the inclusion bit for a given integer n >= 0 -@inline function _setint!(s::IntSet, idx::Integer, b::Bool) +@inline function _setint!(s::BitSet, idx::Integer, b::Bool) if idx > length(s.bits) b || return s # setting a bit to zero outside the set's bits is a no-op _resize0!(s.bits, idx) @@ -83,124 +83,124 @@ function _bit_map!(f, b1::BitArray, b2::BitArray) b1 end -@noinline _throw_intset_bounds_err() = throw(ArgumentError("elements of IntSet must be between 1 and typemax(Int)")) +@noinline _throw_intset_bounds_err() = throw(ArgumentError("elements of BitSet must be between 1 and typemax(Int)")) @noinline _throw_keyerror(n) = throw(KeyError(n)) -@inline function push!(s::IntSet, n::Integer) +@inline function push!(s::BitSet, n::Integer) 0 < n <= typemax(Int) || _throw_intset_bounds_err() _setint!(s, n, true) end -push!(s::IntSet, ns::Integer...) = (for n in ns; push!(s, n); end; s) +push!(s::BitSet, ns::Integer...) = (for n in ns; push!(s, n); end; s) -@inline function pop!(s::IntSet) +@inline function pop!(s::BitSet) pop!(s, last(s)) end -@inline function pop!(s::IntSet, n::Integer) +@inline function pop!(s::BitSet, n::Integer) n in s ? (_delete!(s, n); n) : _throw_keyerror(n) end -@inline function pop!(s::IntSet, n::Integer, default) +@inline function pop!(s::BitSet, n::Integer, default) n in s ? (_delete!(s, n); n) : default end -@inline _delete!(s::IntSet, n::Integer) = _setint!(s, n, false) -@inline delete!(s::IntSet, n::Integer) = n > 0 ? _delete!(s, n) : s -shift!(s::IntSet) = pop!(s, first(s)) +@inline _delete!(s::BitSet, n::Integer) = _setint!(s, n, false) +@inline delete!(s::BitSet, n::Integer) = n > 0 ? _delete!(s, n) : s +shift!(s::BitSet) = pop!(s, first(s)) -empty!(s::IntSet) = (fill!(s.bits, false); s) -isempty(s::IntSet) = !any(s.bits) +empty!(s::BitSet) = (fill!(s.bits, false); s) +isempty(s::BitSet) = !any(s.bits) # Mathematical set functions: union!, intersect!, setdiff!, symdiff! -union(s::IntSet) = copy(s) -union(s1::IntSet, s2::IntSet) = union!(copy(s1), s2) -union(s1::IntSet, ss::IntSet...) = union(s1, union(ss...)) -union(s::IntSet, ns) = union!(copy(s), ns) -union!(s::IntSet, ns) = (for n in ns; push!(s, n); end; s) -function union!(s1::IntSet, s2::IntSet) +union(s::BitSet) = copy(s) +union(s1::BitSet, s2::BitSet) = union!(copy(s1), s2) +union(s1::BitSet, ss::BitSet...) = union(s1, union(ss...)) +union(s::BitSet, ns) = union!(copy(s), ns) +union!(s::BitSet, ns) = (for n in ns; push!(s, n); end; s) +function union!(s1::BitSet, s2::BitSet) _matched_map!(|, s1.bits, s2.bits) s1 end -intersect(s1::IntSet) = copy(s1) -intersect(s1::IntSet, ss::IntSet...) = intersect(s1, intersect(ss...)) -function intersect(s1::IntSet, ns) - s = IntSet() +intersect(s1::BitSet) = copy(s1) +intersect(s1::BitSet, ss::BitSet...) = intersect(s1, intersect(ss...)) +function intersect(s1::BitSet, ns) + s = BitSet() for n in ns n in s1 && push!(s, n) end s end -intersect(s1::IntSet, s2::IntSet) = +intersect(s1::BitSet, s2::BitSet) = length(s1.bits) < length(s2.bits) ? intersect!(copy(s1), s2) : intersect!(copy(s2), s1) """ - intersect!(s1::IntSet, s2::IntSet) + intersect!(s1::BitSet, s2::BitSet) Intersects sets `s1` and `s2` and overwrites the set `s1` with the result. If needed, `s1` will be expanded to the size of `s2`. """ -function intersect!(s1::IntSet, s2::IntSet) +function intersect!(s1::BitSet, s2::BitSet) _matched_map!(&, s1.bits, s2.bits) s1 end -setdiff(s::IntSet, ns) = setdiff!(copy(s), ns) -setdiff!(s::IntSet, ns) = (for n in ns; delete!(s, n); end; s) -function setdiff!(s1::IntSet, s2::IntSet) +setdiff(s::BitSet, ns) = setdiff!(copy(s), ns) +setdiff!(s::BitSet, ns) = (for n in ns; delete!(s, n); end; s) +function setdiff!(s1::BitSet, s2::BitSet) _matched_map!((p, q) -> p & ~q, s1.bits, s2.bits) s1 end -symdiff(s::IntSet, ns) = symdiff!(copy(s), ns) +symdiff(s::BitSet, ns) = symdiff!(copy(s), ns) """ symdiff!(s, itr) For each element in `itr`, destructively toggle its inclusion in set `s`. """ -symdiff!(s::IntSet, ns) = (for n in ns; int_symdiff!(s, n); end; s) +symdiff!(s::BitSet, ns) = (for n in ns; int_symdiff!(s, n); end; s) """ symdiff!(s, n) The set `s` is destructively modified to toggle the inclusion of integer `n`. """ -symdiff!(s::IntSet, n::Integer) = int_symdiff!(s, n) +symdiff!(s::BitSet, n::Integer) = int_symdiff!(s, n) -function int_symdiff!(s::IntSet, n::Integer) +function int_symdiff!(s::BitSet, n::Integer) 0 < n < typemax(Int) || _throw_intset_bounds_err() val = !(n in s) _setint!(s, n, val) s end -function symdiff!(s1::IntSet, s2::IntSet) +function symdiff!(s1::BitSet, s2::BitSet) _matched_map!(xor, s1.bits, s2.bits) s1 end -@inline in(n::Integer, s::IntSet) = get(s.bits, n, false) +@inline in(n::Integer, s::BitSet) = get(s.bits, n, false) # Use the next-set index as the state to prevent looking it up again in done -start(s::IntSet) = next(s, 0)[2] -function next(s::IntSet, i) +start(s::BitSet) = next(s, 0)[2] +function next(s::BitSet, i) nextidx = i == typemax(Int) ? 0 : findnext(s.bits, i+1) (i, nextidx) end -done(s::IntSet, i) = i <= 0 +done(s::BitSet, i) = i <= 0 @noinline _throw_intset_notempty_error() = throw(ArgumentError("collection must be non-empty")) -function first(s::IntSet) +function first(s::BitSet) idx = findfirst(s.bits) idx == 0 ? _throw_intset_notempty_error() : idx end -function last(s::IntSet) +function last(s::BitSet) idx = findprev(s.bits, length(s.bits)) idx == 0 ? _throw_intset_notempty_error() : idx end -length(s::IntSet) = sum(s.bits) +length(s::BitSet) = sum(s.bits) -function show(io::IO, s::IntSet) - print(io, "IntSet([") +function show(io::IO, s::BitSet) + print(io, "BitSet([") first = true for n in s !first && print(io, ", ") @@ -210,7 +210,7 @@ function show(io::IO, s::IntSet) print(io, "])") end -function ==(s1::IntSet, s2::IntSet) +function ==(s1::BitSet, s2::BitSet) l1 = length(s1.bits) l2 = length(s2.bits) # If the lengths are the same, simply punt to bitarray comparison @@ -234,12 +234,12 @@ function ==(s1::IntSet, s2::IntSet) return true end -issubset(a::IntSet, b::IntSet) = isequal(a, intersect(a,b)) -<(a::IntSet, b::IntSet) = (a<=b) && !isequal(a,b) -<=(a::IntSet, b::IntSet) = issubset(a, b) +issubset(a::BitSet, b::BitSet) = isequal(a, intersect(a,b)) +<(a::BitSet, b::BitSet) = (a<=b) && !isequal(a,b) +<=(a::BitSet, b::BitSet) = issubset(a, b) const hashis_seed = UInt === UInt64 ? 0x88989f1fc7dea67d : 0xc7dea67d -function hash(s::IntSet, h::UInt) +function hash(s::BitSet, h::UInt) h ⊻= hashis_seed bc = s.bits.chunks i = length(bc) @@ -254,7 +254,7 @@ function hash(s::IntSet, h::UInt) h end -minimum(s::IntSet) = first(s) -maximum(s::IntSet) = last(s) -extrema(s::IntSet) = (first(s), last(s)) -issorted(s::IntSet) = true +minimum(s::BitSet) = first(s) +maximum(s::BitSet) = last(s) +extrema(s::BitSet) = (first(s), last(s)) +issorted(s::BitSet) = true diff --git a/base/precompile.jl b/base/precompile.jl index 86bcc216f32c83..7dbcf8b309b771 100644 --- a/base/precompile.jl +++ b/base/precompile.jl @@ -863,7 +863,7 @@ precompile(Tuple{typeof(Core.Inference.copy_exprargs), Array{Any, 1}}) precompile(Tuple{typeof(Core.Inference.copy), Expr}) precompile(Tuple{typeof(Core.Inference.copy!), Array{Any, 1}, Core.Inference.Generator{Array{Any, 1}, typeof(Core.Inference.copy_exprs)}}) precompile(Tuple{typeof(Core.Inference._widen_all_consts!), Expr, Array{Bool, 1}}) -precompile(Tuple{typeof(Core.Inference._delete!), Core.Inference.IntSet, Int64}) +precompile(Tuple{typeof(Core.Inference._delete!), Core.Inference.BitSet, Int64}) precompile(Tuple{typeof(Core.Inference.promote_type), Type{Float16}, Type{Int64}}) precompile(Tuple{typeof(Core.Inference.mk_tuplecall), Array{Any, 1}, Core.Inference.InferenceState}) precompile(Tuple{typeof(Core.Inference.inlining_pass), Expr, Core.Inference.InferenceState, Array{Any, 1}, Int64}) @@ -876,14 +876,14 @@ precompile(Tuple{typeof(Core.Inference.getfield_elim_pass!), Core.Inference.Infe precompile(Tuple{typeof(Core.Inference.popmeta!), Array{Any, 1}, Symbol}) precompile(Tuple{typeof(Core.Inference.widen_all_consts!), CodeInfo}) precompile(Tuple{typeof(Core.Inference.stupdate!), Array{Any, 1}, Array{Any, 1}}) -precompile(Tuple{typeof(Core.Inference.push!), Core.Inference.IntSet, Int64}) +precompile(Tuple{typeof(Core.Inference.push!), Core.Inference.BitSet, Int64}) precompile(Tuple{typeof(Core.Inference.abstract_eval_call), Expr, Array{Any, 1}, Core.Inference.InferenceState}) precompile(Tuple{typeof(Core.Inference.return_type_tfunc), Array{Any, 1}, Array{Any, 1}, Core.Inference.InferenceState}) precompile(Tuple{typeof(Core.Inference.abstract_call), typeof(===), Tuple{}, Array{Any, 1}, Array{Any, 1}, Core.Inference.InferenceState}) precompile(Tuple{typeof(Core.Inference.abstract_call), typeof(===), Array{Any, 1}, Array{Any, 1}, Array{Any, 1}, Core.Inference.InferenceState}) precompile(Tuple{typeof(Core.Inference.type_too_complex), TypeVar, Int64}) precompile(Tuple{typeof(Core.Inference.abstract_eval), Expr, Array{Any, 1}, Core.Inference.InferenceState}) -precompile(Tuple{typeof(Core.Inference._setint!), Core.Inference.IntSet, Int64, Bool}) +precompile(Tuple{typeof(Core.Inference._setint!), Core.Inference.BitSet, Int64, Bool}) precompile(Tuple{typeof(Core.Inference.stupdate1!), Array{Any, 1}, Core.Inference.StateUpdate}) precompile(Tuple{typeof(Core.Inference.optimize), Core.Inference.InferenceState}) precompile(Tuple{typeof(Core.Inference.deleteat!), Core.Inference.BitArray{1}, Core.Inference.UnitRange{Int64}}) @@ -1656,14 +1656,14 @@ precompile(Tuple{typeof(Base.close), Base.PipeEndpoint}) precompile(Tuple{typeof(Base.uvfinalize), Base.TCPServer}) precompile(Tuple{typeof(Base.uvfinalize), Base.TCPSocket}) precompile(Tuple{typeof(Base._uv_hook_close), Base.TCPSocket}) -precompile(Tuple{typeof(Base.in), Int64, Base.IntSet}) +precompile(Tuple{typeof(Base.in), Int64, Base.BitSet}) precompile(Tuple{typeof(Base._uv_hook_close), Base.Process}) precompile(Tuple{typeof(Base.Distributed.finalize_ref), Base.Distributed.RemoteChannel{Base.Channel{Any}}}) precompile(Tuple{typeof(Base._delete!), Base.Dict{WeakRef, Void}, Int64}) precompile(Tuple{typeof(Base.Distributed.send_del_client), Base.Distributed.RemoteChannel{Base.Channel{Any}}}) precompile(Tuple{typeof(Base.:(==)), Base.Distributed.RemoteChannel{Base.Channel{Any}}, Base.Distributed.RemoteChannel{Base.Channel{Any}}}) -precompile(Tuple{typeof(Base.delete!), Base.IntSet, Int64}) -precompile(Tuple{typeof(Base.isempty), Base.IntSet}) +precompile(Tuple{typeof(Base.delete!), Base.BitSet, Int64}) +precompile(Tuple{typeof(Base.isempty), Base.BitSet}) precompile(Tuple{typeof(Base.any), Base.BitArray{1}}) precompile(Tuple{typeof(Base.uvfinalize), Base.PipeEndpoint}) precompile(Tuple{typeof(Base.uvfinalize), Base.Process}) diff --git a/base/random/generation.jl b/base/random/generation.jl index 413b714478701a..998a3fd6fc4ee4 100644 --- a/base/random/generation.jl +++ b/base/random/generation.jl @@ -323,7 +323,7 @@ rand(rng::AbstractRNG, r::AbstractArray, dims::Integer...) = rand(rng, r, Dims(d rand( r::AbstractArray, dims::Integer...) = rand(GLOBAL_RNG, r, Dims(dims)) -## random values from Dict, Set, IntSet +## random values from Dict, Set, BitSet function rand(r::AbstractRNG, t::Dict) isempty(t) && throw(ArgumentError("collection must be non-empty")) @@ -336,7 +336,7 @@ end rand(r::AbstractRNG, s::Set) = rand(r, s.dict).first -function rand(r::AbstractRNG, s::IntSet) +function rand(r::AbstractRNG, s::BitSet) isempty(s) && throw(ArgumentError("collection must be non-empty")) # s can be empty while s.bits is not, so we cannot rely on the # length check in RangeGenerator below @@ -361,7 +361,7 @@ rand(s::Union{Associative,AbstractSet}) = rand(GLOBAL_RNG, s) ### arrays -function rand!(r::AbstractRNG, A::AbstractArray, s::Union{Dict,Set,IntSet}) +function rand!(r::AbstractRNG, A::AbstractArray, s::Union{Dict,Set,BitSet}) for i in eachindex(A) @inbounds A[i] = rand(r, s) end diff --git a/base/random/random.jl b/base/random/random.jl index 3823211237f22a..aa5a59c2b94980 100644 --- a/base/random/random.jl +++ b/base/random/random.jl @@ -83,7 +83,7 @@ julia> rand(MersenneTwister(0), Dict(1=>2, 3=>4)) The complexity of `rand(rng, s::Union{Associative,AbstractSet})` is linear in the length of `s`, unless an optimized method with constant complexity is available, which is the case for `Dict`, - `Set` and `IntSet`. For more than a few calls, use `rand(rng, + `Set` and `BitSet`. For more than a few calls, use `rand(rng, collect(s))` instead, or either `rand(rng, Dict(s))` or `rand(rng, Set(s))` as appropriate. """ diff --git a/base/set.jl b/base/set.jl index ad9bf4236f9872..d02209d025d684 100644 --- a/base/set.jl +++ b/base/set.jl @@ -12,7 +12,7 @@ Set() = Set{Any}() Set([itr]) Construct a [`Set`](@ref) of the values generated by the given iterable object, or an -empty set. Should be used instead of [`IntSet`](@ref) for sparse integer sets, or +empty set. Should be used instead of [`BitSet`](@ref) for sparse integer sets, or for sets of arbitrary objects. """ Set(itr) = Set{eltype(itr)}(itr) diff --git a/doc/src/stdlib/collections.md b/doc/src/stdlib/collections.md index 4c22007965b0f8..8b5d9ede0d5721 100644 --- a/doc/src/stdlib/collections.md +++ b/doc/src/stdlib/collections.md @@ -40,7 +40,7 @@ Fully implemented by: * `Tuple` * `Number` * [`AbstractArray`](@ref) - * [`IntSet`](@ref) + * [`BitSet`](@ref) * [`ObjectIdDict`](@ref) * [`Dict`](@ref) * [`WeakKeyDict`](@ref) @@ -64,7 +64,7 @@ Fully implemented by: * `Tuple` * `Number` * [`AbstractArray`](@ref) - * [`IntSet`](@ref) + * [`BitSet`](@ref) * [`ObjectIdDict`](@ref) * [`Dict`](@ref) * [`WeakKeyDict`](@ref) @@ -214,7 +214,7 @@ Fully implemented by: Partially implemented by: - * [`IntSet`](@ref) + * [`BitSet`](@ref) * [`Set`](@ref) * [`EnvDict`](@ref Base.EnvDict) * [`Array`](@ref) @@ -224,23 +224,23 @@ Partially implemented by: ```@docs Base.Set -Base.IntSet +Base.BitSet Base.union Base.union! Base.intersect Base.setdiff Base.setdiff! Base.symdiff -Base.symdiff!(::IntSet, ::Integer) -Base.symdiff!(::IntSet, ::Any) -Base.symdiff!(::IntSet, ::IntSet) +Base.symdiff!(::BitSet, ::Integer) +Base.symdiff!(::BitSet, ::Any) +Base.symdiff!(::BitSet, ::BitSet) Base.intersect! Base.issubset ``` Fully implemented by: - * [`IntSet`](@ref) + * [`BitSet`](@ref) * [`Set`](@ref) Partially implemented by: diff --git a/test/intset.jl b/test/intset.jl index fbe507747c38db..212c452e168334 100644 --- a/test/intset.jl +++ b/test/intset.jl @@ -1,29 +1,29 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -# Test functionality of IntSet +# Test functionality of BitSet @testset "Construction, collect" begin data_in = (1,5,100) - s = IntSet(data_in) + s = BitSet(data_in) data_out = collect(s) @test all(map(d->in(d,data_out), data_in)) @test length(data_out) === length(data_in) end @testset "eltype, similar" begin - @test eltype(IntSet()) === Int - @test eltype(IntSet) === Int - @test isequal(similar(IntSet([1,2,3])), IntSet()) + @test eltype(BitSet()) === Int + @test eltype(BitSet) === Int + @test isequal(similar(BitSet([1,2,3])), BitSet()) end @testset "show" begin - @test sprint(show, IntSet()) == "IntSet([])" - @test sprint(show, IntSet([1,2,3])) == "IntSet([1, 2, 3])" - show(IOBuffer(), IntSet()) + @test sprint(show, BitSet()) == "BitSet([])" + @test sprint(show, BitSet([1,2,3])) == "BitSet([1, 2, 3])" + show(IOBuffer(), BitSet()) end @testset "in, hashing" begin - s = IntSet([1,2,10,20,200,300,1000,10000,10002]) + s = BitSet([1,2,10,20,200,300,1000,10000,10002]) @test last(s) === 10002 @test first(s) === 1 @test length(s) === 9 @@ -39,8 +39,8 @@ end @test in(10000,s) @test in(10000.0,s) @test !in(10002.0,s) - @test_throws ArgumentError first(IntSet()) - @test_throws ArgumentError last(IntSet()) + @test_throws ArgumentError first(BitSet()) + @test_throws ArgumentError last(BitSet()) t = copy(s) sizehint!(t, 20000) #check that hash does not depend on size of internal storage @test hash(s) === hash(t) @@ -51,24 +51,24 @@ end @test pop!(t, 20000) === 20000 @test hash(s) === hash(t) # Ensure empty chunks don't affect hash - @test hash(IntSet([1])) != hash(IntSet([17])) - @test hash(IntSet([1])) != hash(IntSet([33])) - @test hash(IntSet([1])) != hash(IntSet([65])) - @test hash(IntSet([1])) != hash(IntSet([129])) + @test hash(BitSet([1])) != hash(BitSet([17])) + @test hash(BitSet([1])) != hash(BitSet([33])) + @test hash(BitSet([1])) != hash(BitSet([65])) + @test hash(BitSet([1])) != hash(BitSet([129])) # issue #7851 - @test_throws ArgumentError IntSet(-1) - @test !(-1 in IntSet(1:10)) + @test_throws ArgumentError BitSet(-1) + @test !(-1 in BitSet(1:10)) end # # issue #8570 # This requires 2^29 bytes of storage, which is too much for a simple test -# s = IntSet(typemax(Int32)) +# s = BitSet(typemax(Int32)) # @test length(s) === 1 # for b in s; b; end @testset "union!, symdiff!" begin - i = IntSet([1, 2, 3]) + i = BitSet([1, 2, 3]) union!(i, [1, 2]) @test length(i) === 3 union!(i, [3, 4, 5]) @@ -80,24 +80,24 @@ end @test length(i) === 0 @test_throws ArgumentError symdiff!(i, -3) - @test symdiff!(i, 3) == IntSet([3]) - @test symdiff!(i, 257) == IntSet([3, 257]) - @test symdiff!(i, [3, 6]) == IntSet([6, 257]) + @test symdiff!(i, 3) == BitSet([3]) + @test symdiff!(i, 257) == BitSet([3, 257]) + @test symdiff!(i, [3, 6]) == BitSet([6, 257]) - i = IntSet(1:6) - @test symdiff!(i, IntSet([6, 513])) == IntSet([1:5; 513]) + i = BitSet(1:6) + @test symdiff!(i, BitSet([6, 513])) == BitSet([1:5; 513]) # issue #23099 : these tests should not segfault - @test_throws ArgumentError symdiff!(IntSet(rand(1:100, 30)), 0) - @test_throws ArgumentError symdiff!(IntSet(rand(1:100, 30)), [0, 2, 4]) + @test_throws ArgumentError symdiff!(BitSet(rand(1:100, 30)), 0) + @test_throws ArgumentError symdiff!(BitSet(rand(1:100, 30)), [0, 2, 4]) # issue #23557 : - @test_throws MethodError symdiff!(IntSet([1]), ['a']) # should no stack-overflow - @test_throws MethodError symdiff!(IntSet([1, 2]), [[1]]) # should not return IntSet([2]) + @test_throws MethodError symdiff!(BitSet([1]), ['a']) # should no stack-overflow + @test_throws MethodError symdiff!(BitSet([1, 2]), [[1]]) # should not return BitSet([2]) end @testset "copy, copy!, similar" begin - s1 = IntSet([1,2,3]) + s1 = BitSet([1,2,3]) s2 = similar(s1) copy!(s2, s1) s3 = copy(s2) @@ -106,43 +106,43 @@ end end @testset "push!, union" begin - i = IntSet([1, 2, 3]) + i = BitSet([1, 2, 3]) j = union(i) @test j == i @test !(j === i) - j = IntSet([4, 5, 6]) - @test union(i, j) == IntSet(1:6) + j = BitSet([4, 5, 6]) + @test union(i, j) == BitSet(1:6) - k = IntSet([7, 8, 9]) - @test union(i, j, k) == IntSet(1:9) - i = IntSet([1, 2, 3]) + k = BitSet([7, 8, 9]) + @test union(i, j, k) == BitSet(1:9) + i = BitSet([1, 2, 3]) j = union(i) @test j == i @test !(j === i) - j = IntSet([4, 5, 6]) - @test union(i, j) == IntSet(1:6) + j = BitSet([4, 5, 6]) + @test union(i, j) == BitSet(1:6) - k = IntSet([7, 8, 9]) - @test union(i, j, k) == IntSet(1:9) + k = BitSet([7, 8, 9]) + @test union(i, j, k) == BitSet(1:9) - s1 = IntSet() + s1 = BitSet() @test_throws ArgumentError push!(s1, -1) push!(s1, 1, 10, 100, 1000) @test collect(s1) == [1, 10, 100, 1000] push!(s1, 606) @test collect(s1) == [1, 10, 100, 606, 1000] - s2 = IntSet() + s2 = BitSet() @test s2 === union!(s2, s1) - s3 = IntSet([1, 10, 100]) + s3 = BitSet([1, 10, 100]) union!(s3, [1, 606, 1000]) - s4 = union(IntSet([1, 100, 1000]), IntSet([10, 100, 606])) + s4 = union(BitSet([1, 100, 1000]), BitSet([10, 100, 606])) @test s1 == s2 == s3 == s4 end @testset "pop!, delete!" begin - s = IntSet(1:2:10) + s = BitSet(1:2:10) # deleting non-positive values should be no-op # (Issue #23179 : delete!(s, 0) should not crash) len = length(s) @@ -171,64 +171,64 @@ end end @testset "intersect" begin - i = IntSet([1, 2, 3]) - j = IntSet([4, 5, 6]) + i = BitSet([1, 2, 3]) + j = BitSet([4, 5, 6]) @test intersect(i) == i @test !(intersect(i) === i) - @test intersect(i, j) == IntSet([]) + @test intersect(i, j) == BitSet([]) push!(j, 257) - @test intersect(i, j) == IntSet([]) + @test intersect(i, j) == BitSet([]) push!(j, 2, 3, 17) - @test intersect(i, j) == IntSet([2, 3]) - k = IntSet([1, 2, 3, 4, 5, 6, 7]) - @test intersect(i, j, k) == IntSet([2, 3]) - - @test isempty(intersect(IntSet())) - @test isempty(intersect(IntSet(1:10), IntSet())) - @test isempty(intersect(IntSet(), IntSet(1:10))) - - @test intersect(IntSet([1,2,3])) == IntSet([1,2,3]) - @test intersect(IntSet(1:7), IntSet(3:10)) == - intersect(IntSet(3:10), IntSet(1:7)) == IntSet(3:7) - @test intersect(IntSet(1:10), IntSet(1:4), 1:5, [2,3,10]) == [2,3] + @test intersect(i, j) == BitSet([2, 3]) + k = BitSet([1, 2, 3, 4, 5, 6, 7]) + @test intersect(i, j, k) == BitSet([2, 3]) + + @test isempty(intersect(BitSet())) + @test isempty(intersect(BitSet(1:10), BitSet())) + @test isempty(intersect(BitSet(), BitSet(1:10))) + + @test intersect(BitSet([1,2,3])) == BitSet([1,2,3]) + @test intersect(BitSet(1:7), BitSet(3:10)) == + intersect(BitSet(3:10), BitSet(1:7)) == BitSet(3:7) + @test intersect(BitSet(1:10), BitSet(1:4), 1:5, [2,3,10]) == [2,3] end @testset "setdiff, symdiff" begin - @test setdiff(IntSet([1, 2, 3, 4]), IntSet([2, 4, 5, 6])) == IntSet([1, 3]) - @test symdiff(IntSet([1, 2, 3, 4]), IntSet([2, 4, 5, 6])) == IntSet([1, 3, 5, 6]) + @test setdiff(BitSet([1, 2, 3, 4]), BitSet([2, 4, 5, 6])) == BitSet([1, 3]) + @test symdiff(BitSet([1, 2, 3, 4]), BitSet([2, 4, 5, 6])) == BitSet([1, 3, 5, 6]) - s2 = IntSet([1, 2, 3, 4]) - setdiff!(s2, IntSet([2, 4, 5, 6])) - @test s2 == IntSet([1, 3]) + s2 = BitSet([1, 2, 3, 4]) + setdiff!(s2, BitSet([2, 4, 5, 6])) + @test s2 == BitSet([1, 3]) - s1 = IntSet(1:100) - setdiff!(s1, IntSet(1:2:100)) - s2 = setdiff(IntSet(1:100), IntSet(1:2:100)) - @test s1 == s2 == IntSet(2:2:100) + s1 = BitSet(1:100) + setdiff!(s1, BitSet(1:2:100)) + s2 = setdiff(BitSet(1:100), BitSet(1:2:100)) + @test s1 == s2 == BitSet(2:2:100) @test collect(s1) == collect(2:2:100) # issue #23191 : these tests should not segfault @test setdiff(s1, 0) == s1 @test setdiff(s1, -9:0) == s1 - @test symdiff(IntSet([1, 2, 3, 4]), IntSet([2, 4, 5, 6])) == - symdiff(IntSet([2, 4, 5, 6]), IntSet([1, 2, 3, 4])) == - symdiff(IntSet([1, 2, 3, 4]), [2, 4, 5, 6]) == - symdiff(IntSet([2, 4, 5, 6]), [1, 2, 3, 4]) == IntSet([1, 3, 5, 6]) + @test symdiff(BitSet([1, 2, 3, 4]), BitSet([2, 4, 5, 6])) == + symdiff(BitSet([2, 4, 5, 6]), BitSet([1, 2, 3, 4])) == + symdiff(BitSet([1, 2, 3, 4]), [2, 4, 5, 6]) == + symdiff(BitSet([2, 4, 5, 6]), [1, 2, 3, 4]) == BitSet([1, 3, 5, 6]) end @testset "subsets, equality" begin - i = IntSet([1, 2, 3]) - k = IntSet([4, 5]) + i = BitSet([1, 2, 3]) + k = BitSet([4, 5]) copy!(k, i) @test k == i @test !(k === i) copy!(k, k) @test k == i - i = IntSet([1, 2, 3]) - j = IntSet([1, 2, 4]) + i = BitSet([1, 2, 3]) + j = BitSet([1, 2, 4]) @test i != j push!(j, 257) @@ -236,27 +236,27 @@ end @test i != j @test j != i - @test issubset(IntSet([1, 2, 4]), IntSet(1:10)) - @test issubset(IntSet([]), IntSet([])) - @test IntSet([1, 2, 4]) < IntSet(1:10) - @test !(IntSet([]) < IntSet([])) - @test IntSet([1, 2, 4]) <= IntSet(1:10) - @test IntSet([1, 2, 4]) <= IntSet([1, 2, 4]) - @test IntSet([]) <= IntSet([]) + @test issubset(BitSet([1, 2, 4]), BitSet(1:10)) + @test issubset(BitSet([]), BitSet([])) + @test BitSet([1, 2, 4]) < BitSet(1:10) + @test !(BitSet([]) < BitSet([])) + @test BitSet([1, 2, 4]) <= BitSet(1:10) + @test BitSet([1, 2, 4]) <= BitSet([1, 2, 4]) + @test BitSet([]) <= BitSet([]) - @test IntSet(2:2:10) < IntSet(1:10) - @test !(IntSet(2:2:10) < IntSet(2:2:10)) - @test IntSet(2:2:10) <= IntSet(2:10) - @test IntSet(2:2:10) <= IntSet(2:2:10) + @test BitSet(2:2:10) < BitSet(1:10) + @test !(BitSet(2:2:10) < BitSet(2:2:10)) + @test BitSet(2:2:10) <= BitSet(2:10) + @test BitSet(2:2:10) <= BitSet(2:2:10) # == with last-bit set (groups.google.com/forum/#!topic/julia-users/vZNjiIEG_sY) - s = IntSet(255) + s = BitSet(255) @test s == s end @testset "setlike" begin - p = IntSet([1,2,5,6]) - q = IntSet([1,3,5,7]) + p = BitSet([1,2,5,6]) + q = BitSet([1,3,5,7]) a = Set(p) b = Set(q) for f in (union, intersect, setdiff, symdiff) @@ -268,7 +268,7 @@ end end @testset "misc" begin - s = IntSet() + s = BitSet() push!(s, 1, 2, 100) @test !(0 in s) @test 1 in s @@ -279,15 +279,15 @@ end @test !(1000 in s) @test first(s) === 1 @test last(s) === 100 - @test s == IntSet([1, 2, 100]) + @test s == BitSet([1, 2, 100]) push!(s, 1000) @test [i for i in s] == [1, 2, 100, 1000] @test pop!(s) === 1000 - @test s == IntSet([1, 2, 100]) - @test hash(s) === hash(IntSet([1, 2, 100])) + @test s == BitSet([1, 2, 100]) + @test hash(s) === hash(BitSet([1, 2, 100])) b = 1:1000 - s = IntSet(b) + s = BitSet(b) @test collect(s) == collect(b) @test length(s) === length(b) @test pop!(s, 100) === 100 @@ -300,7 +300,7 @@ end @testset "order" begin a = rand(1:1000, 100) - s = IntSet(a) + s = BitSet(a) m, M = extrema(s) @test m == first(s) == minimum(s) == minimum(a) @test M == last(s) == maximum(s) == maximum(a) diff --git a/test/random.jl b/test/random.jl index 38d53151966d9a..8472c54a177a96 100644 --- a/test/random.jl +++ b/test/random.jl @@ -325,7 +325,7 @@ for rng in ([], [MersenneTwister(0)], [RandomDevice()]) types = [Bool, Char, BigFloat, Base.BitInteger_types..., ftypes...] randset = Set(rand(Int, 20)) randdict = Dict(zip(rand(Int,10), rand(Int, 10))) - collections = [IntSet(rand(1:100, 20)) => Int, + collections = [BitSet(rand(1:100, 20)) => Int, randset => Int, GenericSet(randset) => Int, randdict => Pair{Int,Int}, @@ -380,7 +380,7 @@ for rng in ([], [MersenneTwister(0)], [RandomDevice()]) end end end - for C in [1:0, Dict(), Set(), IntSet(), Int[], + for C in [1:0, Dict(), Set(), BitSet(), Int[], GenericDict(Dict()), GenericSet(Set()), "", Test.GenericString("")] @test_throws ArgumentError rand(rng..., C) diff --git a/test/sets.jl b/test/sets.jl index 1a28e0a5dd3f34..bf1f800ff5c715 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -149,7 +149,7 @@ end s = Set([1,3,5,7]) union!(s,(2,3,4,5)) @test isequal(s,Set([1,2,3,4,5,7])) - @test ===(typeof(union(Set([1]), IntSet())), Set{Int}) + @test ===(typeof(union(Set([1]), BitSet())), Set{Int}) @test isequal(union(Set([1,2,3]), 2:4), Set([1,2,3,4])) @test isequal(union(Set([1,2,3]), [2,3,4]), Set([1,2,3,4])) @test isequal(union(Set([1,2,3]), [2,3,4], Set([5])), Set([1,2,3,4,5])) @@ -161,7 +161,7 @@ end s = intersect(Set([5,6,7,8]), Set([7,8,9])) @test isequal(s, Set([7,8])) @test isequal(intersect(Set([2,3,1]), Set([4,2,3]), Set([5,4,3,2])), Set([2,3])) - @test ===(typeof(intersect(Set([1]), IntSet())), Set{Int}) + @test ===(typeof(intersect(Set([1]), BitSet())), Set{Int}) @test isequal(intersect(Set([1,2,3]), 2:10), Set([2,3])) @test isequal(intersect(Set([1,2,3]), [2,3,4]), Set([2,3])) @test isequal(intersect(Set([1,2,3]), [2,3,4], 3:4), Set([3])) @@ -173,7 +173,7 @@ end @test isequal(setdiff(Set([1,2,3]), Set([1,2,3])), Set()) @test isequal(setdiff(Set([1,2,3]), Set([4])), Set([1,2,3])) @test isequal(setdiff(Set([1,2,3]), Set([4,1])), Set([2,3])) - @test ===(typeof(setdiff(Set([1]), IntSet())), Set{Int}) + @test ===(typeof(setdiff(Set([1]), BitSet())), Set{Int}) @test isequal(setdiff(Set([1,2,3]), 2:10), Set([1])) @test isequal(setdiff(Set([1,2,3]), [2,3,4]), Set([1])) @test_throws MethodError setdiff(Set([1,2,3]), Set([2,3,4]), Set([1]))