diff --git a/Makefile b/Makefile index 26c05229b7a3d0..e16d620e168f9d 100644 --- a/Makefile +++ b/Makefile @@ -188,7 +188,7 @@ CORE_SRCS := $(addprefix $(JULIAHOME)/, \ base/hashing.jl \ base/inference.jl \ base/int.jl \ - base/intset.jl \ + base/bitset.jl \ base/number.jl \ base/operators.jl \ base/options.jl \ diff --git a/NEWS.md b/NEWS.md index 842527d7ac0b0e..7d1277a02dc3a6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -430,8 +430,6 @@ Library improvements definition relies on `ncodeunits` however, so for optimal performance you may need to define a custom method for that function. - * `IntSet` now lives up to its premise and can store any `Int`. - Compiler/Runtime improvements ----------------------------- diff --git a/base/intset.jl b/base/bitset.jl similarity index 73% rename from base/intset.jl rename to base/bitset.jl index 6975e5407a6117..9e089ee8961571 100644 --- a/base/intset.jl +++ b/base/bitset.jl @@ -7,41 +7,41 @@ const NoOffset = -one(Int) << 60 # An offset is in the range -2^57:2^57 # + when the offset is NoOffset, the bits field *must* be empty # + NoOffset could be made to be > 0, but a negative one allows -# a small optimization in the in(x, ::IntSet) +# a small optimization in the in(x, ::BitSet) -mutable struct IntSet <: AbstractSet{Int} +mutable struct BitSet <: AbstractSet{Int} bits::Vector{UInt64} # 1st stored Int equals 64*offset offset::Int - IntSet() = new(sizehint!(zeros(UInt64, 0), 4), NoOffset) + BitSet() = new(sizehint!(zeros(UInt64, 0), 4), NoOffset) 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) -@inline intoffset(s::IntSet) = s.offset << 6 +@inline intoffset(s::BitSet) = s.offset << 6 -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.offset = src.offset dest end -eltype(s::IntSet) = Int +eltype(s::BitSet) = Int -sizehint!(s::IntSet, n::Integer) = sizehint!(s.bits, n) +sizehint!(s::BitSet, n::Integer) = sizehint!(s.bits, n) # given an integer i, return the chunk which stores it chk_indice(i::Int) = i >> 6 @@ -70,7 +70,7 @@ function _bits_findprev(b::Bits, start::Int) end # An internal function for setting the inclusion bit for a given integer -@inline function _setint!(s::IntSet, idx::Int, b::Bool) +@inline function _setint!(s::BitSet, idx::Int, b::Bool) cidx = chk_indice(idx) len = length(s.bits) diff = cidx - s.offset @@ -110,7 +110,7 @@ end @inbounds b[1:nchunks] = Chk0 end -function _matched_map!(f, s1::IntSet, s2::IntSet) +function _matched_map!(f, s1::BitSet, s2::BitSet) left_false_is_false = f(false, false) == f(false, true) == false right_false_is_false = f(false, false) == f(true, false) == false @@ -194,130 +194,130 @@ function _matched_map!(f, a1::Bits, b1::Int, a2::Bits, b2::Int, end -@noinline _throw_intset_bounds_err() = - throw(ArgumentError("elements of IntSet must be between typemin(Int) and typemax(Int)")) +@noinline _throw_bitset_bounds_err() = + throw(ArgumentError("elements of BitSet must be between typemin(Int) and typemax(Int)")) @inline _is_convertible_Int(n) = typemin(Int) <= n <= typemax(Int) -@inline _check_intset_bounds(n) = - _is_convertible_Int(n) ? Int(n) : _throw_intset_bounds_err() +@inline _check_bitset_bounds(n) = + _is_convertible_Int(n) ? Int(n) : _throw_bitset_bounds_err() -@inline _check_intset_bounds(n::Int) = n +@inline _check_bitset_bounds(n::Int) = n @noinline _throw_keyerror(n) = throw(KeyError(n)) -@inline push!(s::IntSet, n::Integer) = _setint!(s, _check_intset_bounds(n), true) +@inline push!(s::BitSet, n::Integer) = _setint!(s, _check_bitset_bounds(n), true) -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 pop!(s::IntSet) = pop!(s, last(s)) +@inline pop!(s::BitSet) = pop!(s, last(s)) -@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::Int) = _setint!(s, n, false) -@inline delete!(s::IntSet, n::Integer) = _is_convertible_Int(n) ? delete!(s, Int(n)) : s +@inline delete!(s::BitSet, n::Int) = _setint!(s, n, false) +@inline delete!(s::BitSet, n::Integer) = _is_convertible_Int(n) ? delete!(s, Int(n)) : s -shift!(s::IntSet) = pop!(s, first(s)) +shift!(s::BitSet) = pop!(s, first(s)) -function empty!(s::IntSet) +function empty!(s::BitSet) empty!(s.bits) s.offset = NoOffset s end -isempty(s::IntSet) = all(equalto(Chk0), s.bits) +isempty(s::BitSet) = all(equalto(Chk0), 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) -union!(s1::IntSet, s2::IntSet) = _matched_map!(|, s1, s2) - -intersect(s1::IntSet) = copy(s1) -intersect(s1::IntSet, ss::IntSet...) = intersect(s1, intersect(ss...)) -function intersect(s1::IntSet, ns) - s = 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) +union!(s1::BitSet, s2::BitSet) = _matched_map!(|, s1, s2) + +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`. """ -intersect!(s1::IntSet, s2::IntSet) = _matched_map!(&, s1, s2) +intersect!(s1::BitSet, s2::BitSet) = _matched_map!(&, s1, s2) -setdiff(s::IntSet, ns) = setdiff!(copy(s), ns) -setdiff!(s::IntSet, ns) = (for n in ns; delete!(s, n); end; s) -setdiff!(s1::IntSet, s2::IntSet) = _matched_map!((p, q) -> p & ~q, s1, s2) +setdiff(s::BitSet, ns) = setdiff!(copy(s), ns) +setdiff!(s::BitSet, ns) = (for n in ns; delete!(s, n); end; s) +setdiff!(s1::BitSet, s2::BitSet) = _matched_map!((p, q) -> p & ~q, s1, s2) -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) - n0 = _check_intset_bounds(n) +function int_symdiff!(s::BitSet, n::Integer) + n0 = _check_bitset_bounds(n) val = !(n0 in s) _setint!(s, n0, val) s end -symdiff!(s1::IntSet, s2::IntSet) = _matched_map!(xor, s1, s2) +symdiff!(s1::BitSet, s2::BitSet) = _matched_map!(xor, s1, s2) -@inline in(n::Int, s::IntSet) = _bits_getindex(s.bits, n, s.offset) -@inline in(n::Integer, s::IntSet) = _is_convertible_Int(n) ? in(Int(n), s) : false +@inline in(n::Int, s::BitSet) = _bits_getindex(s.bits, n, s.offset) +@inline in(n::Integer, s::BitSet) = _is_convertible_Int(n) ? in(Int(n), s) : false # Use the next-set index as the state to prevent looking it up again in done -start(s::IntSet) = _bits_findnext(s.bits, 0) +start(s::BitSet) = _bits_findnext(s.bits, 0) -function next(s::IntSet, i::Int) +function next(s::BitSet, i::Int) nextidx = _bits_findnext(s.bits, i+1) (i+intoffset(s), nextidx) end -done(s::IntSet, i) = i == -1 +done(s::BitSet, i) = i == -1 -@noinline _throw_intset_notempty_error() = +@noinline _throw_bitset_notempty_error() = throw(ArgumentError("collection must be non-empty")) -function first(s::IntSet) +function first(s::BitSet) idx = _bits_findnext(s.bits, 0) - idx == -1 ? _throw_intset_notempty_error() : idx + intoffset(s) + idx == -1 ? _throw_bitset_notempty_error() : idx + intoffset(s) end -function last(s::IntSet) +function last(s::BitSet) idx = _bits_findprev(s.bits, (length(s.bits) << 6) - 1) - idx == -1 ? _throw_intset_notempty_error() : idx + intoffset(s) + idx == -1 ? _throw_bitset_notempty_error() : idx + intoffset(s) end -length(s::IntSet) = bitcount(s.bits) # = mapreduce(count_ones, +, 0, s.bits) +length(s::BitSet) = bitcount(s.bits) # = mapreduce(count_ones, +, 0, 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, ", ") @@ -334,7 +334,7 @@ function _check0(a::Vector{UInt64}, b::Int, e::Int) true end -function ==(s1::IntSet, s2::IntSet) +function ==(s1::BitSet, s2::BitSet) # Swap so s1 has always the smallest offset if s1.offset > s2.offset s1, s2 = s2, s1 @@ -363,12 +363,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 i = 1 @@ -391,9 +391,9 @@ 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 nothing diff --git a/base/codevalidation.jl b/base/codevalidation.jl index 9f85b12318fda6..8c0d87f972b151 100644 --- a/base/codevalidation.jl +++ b/base/codevalidation.jl @@ -76,13 +76,13 @@ 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 - ssavals = IntSet() - lhs_slotnums = IntSet() + ssavals = BitSet() + lhs_slotnums = BitSet() for x in c.code if isa(x, Expr) head = x.head diff --git a/base/coreimg.jl b/base/coreimg.jl index bad5f78c53a023..10b56c1f8b0636 100644 --- a/base/coreimg.jl +++ b/base/coreimg.jl @@ -55,7 +55,7 @@ include("reduce.jl") ## core structures include("bitarray.jl") -include("intset.jl") +include("bitset.jl") include("associative.jl") include("namedtuple.jl") diff --git a/base/deprecated.jl b/base/deprecated.jl index 528da0d21c31b3..68960bb2911048 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -2058,8 +2058,8 @@ end @deprecate Vector(m::Integer) Vector(uninitialized, m) @deprecate Matrix(m::Integer, n::Integer) Matrix(uninitialized, m, n) -# deprecate BitSet to IntSet -@deprecate_binding BitSet IntSet +# deprecate IntSet to BitSet +@deprecate_binding IntSet BitSet # Issue 24219 @deprecate float(x::AbstractString) parse(Float64, x) 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 9986e108035728..ed3f5d2b34b499 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -68,7 +68,7 @@ export IndexLinear, IndexStyle, InsertionSort, - IntSet, + BitSet, IOBuffer, IOStream, LinSpace, diff --git a/base/inference.jl b/base/inference.jl index c7b0c70f97ffc8..0870f053143db3 100644 --- a/base/inference.jl +++ b/base/inference.jl @@ -221,7 +221,7 @@ mutable struct InferenceState # return type bestguess #::Type # current active instruction pointers - ip::IntSet + ip::BitSet pc´´::LineNum nstmts::Int # current exception handler info @@ -229,7 +229,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 @@ -303,7 +303,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 @@ -3088,7 +3088,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) @@ -3096,7 +3096,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 === :(=)) @@ -5671,8 +5671,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/precompile.jl b/base/precompile.jl index 69278588b353b5..2376f45058d7fa 100644 --- a/base/precompile.jl +++ b/base/precompile.jl @@ -863,14 +863,14 @@ precompile(Tuple{typeof(Core.Inference.alloc_elim_pass!), Core.Inference.Inferen 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}}) @@ -1641,14 +1641,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 31eb4a0dce6ef6..4cc18dc92bcaea 100644 --- a/base/random/generation.jl +++ b/base/random/generation.jl @@ -255,14 +255,14 @@ Sampler(rng::AbstractRNG, t::Set, n::Repetition) = SamplerTag{Set}(Sampler(rng, rand(rng::AbstractRNG, sp::SamplerTag{Set,<:Sampler}) = rand(rng, sp.data).first -## random values from IntSet +## random values from BitSet -function Sampler(rng::AbstractRNG, t::IntSet, n::Repetition) +function Sampler(rng::AbstractRNG, t::BitSet, n::Repetition) isempty(t) && throw(ArgumentError("collection must be non-empty")) SamplerSimple(t, Sampler(rng, linearindices(t.bits), Val(Inf))) end -function rand(rng::AbstractRNG, sp::SamplerSimple{IntSet,<:Sampler}) +function rand(rng::AbstractRNG, sp::SamplerSimple{BitSet,<:Sampler}) while true n = rand(rng, sp.data) @inbounds b = sp[].bits[n] diff --git a/base/random/random.jl b/base/random/random.jl index a72ec299010290..e624e9c0497007 100644 --- a/base/random/random.jl +++ b/base/random/random.jl @@ -201,7 +201,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 ffde2541421589..dea43bc4b665f3 100644 --- a/base/set.jl +++ b/base/set.jl @@ -15,7 +15,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/base/sysimg.jl b/base/sysimg.jl index ec41777dc5197e..7204750b00d854 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -183,7 +183,7 @@ include("reduce.jl") ## core structures include("reshapedarray.jl") include("bitarray.jl") -include("intset.jl") +include("bitset.jl") if !isdefined(Core, :Inference) include("docs/core.jl") 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/src/common_symbols2.inc b/src/common_symbols2.inc index 0a1d0eb73cbea6..9fd6d0c8aac4c5 100644 --- a/src/common_symbols2.inc +++ b/src/common_symbols2.inc @@ -210,7 +210,7 @@ jl_symbol("ssavaluetypes"), jl_symbol("rehash!"), jl_symbol("show.jl"), jl_symbol("jl_array_ptr_1d_push"), -jl_symbol("intset.jl"), +jl_symbol("bitset.jl"), jl_symbol("I"), jl_symbol("def"), jl_symbol("_collect"), diff --git a/stdlib/Dates/src/query.jl b/stdlib/Dates/src/query.jl index 06ed17356ea83f..88806673cc1577 100644 --- a/stdlib/Dates/src/query.jl +++ b/stdlib/Dates/src/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/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index 9ffa63636c6d58..916834c42bf3b9 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -1405,7 +1405,7 @@ Base.reverse(s::SubString{GenericString}) = """ The `GenericSet` can be used to test generic set APIs that program to the `AbstractSet` interface, in order to ensure that functions can work -with set types besides the standard `Set` and `IntSet` types. +with set types besides the standard `Set` and `BitSet` types. """ struct GenericSet{T} <: AbstractSet{T} s::AbstractSet{T} diff --git a/test/intset.jl b/test/bitset.jl similarity index 52% rename from test/intset.jl rename to test/bitset.jl index 3bb74a7fa772c3..c4c97e59ef6e9d 100644 --- a/test/intset.jl +++ b/test/bitset.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,26 +51,26 @@ 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])) # test with a different internal structure - s = IntSet([129]) + s = BitSet([129]) pop!(push!(s, 65), 65) - @test hash(IntSet([1])) != hash(s) + @test hash(BitSet([1])) != hash(s) - @test !(-1 in IntSet(1:10)) + @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]) @@ -81,26 +81,26 @@ end empty!(i) @test length(i) === 0 - @test symdiff!(i, -3) == IntSet([-3]) - @test symdiff!(i, -3) == IntSet([]) + @test symdiff!(i, -3) == BitSet([-3]) + @test symdiff!(i, -3) == BitSet([]) - @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]) - @test 0 ∈ symdiff!(IntSet(rand(1:100, 30)), 0) - @test IntSet(0:2:4) ⊆ symdiff!(IntSet(rand(5:100, 30)), [0, 2, 4]) + @test 0 ∈ symdiff!(BitSet(rand(1:100, 30)), 0) + @test BitSet(0:2:4) ⊆ symdiff!(BitSet(rand(5: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) @@ -109,44 +109,44 @@ 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() - @test push!(s1, -1) == IntSet([-1]) + s1 = BitSet() + @test push!(s1, -1) == BitSet([-1]) push!(s1, -10, 1, 10, 100, 1000) @test collect(s1) == [-10, -1, 1, 10, 100, 1000] push!(s1, 606) @test collect(s1) == [-10, -1, 1, 10, 100, 606, 1000] - s2 = IntSet() + s2 = BitSet() @test s2 === union!(s2, s1) - s3 = IntSet([-1, 1, 10, 100]) + s3 = BitSet([-1, 1, 10, 100]) union!(s3, [-10, 1, 606, 1000]) - s4 = union(IntSet([-1, 1, 100, 1000]), IntSet([-10, 10, 100, 606])) + s4 = union(BitSet([-1, 1, 100, 1000]), BitSet([-10, 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) @@ -175,64 +175,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) @@ -240,27 +240,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) @@ -272,7 +272,7 @@ end end @testset "misc" begin - s = IntSet() + s = BitSet() push!(s, 1, 2, 100) @test !(0 in s) @test 1 in s @@ -283,15 +283,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 @@ -303,15 +303,15 @@ end end @testset "unsigned overflow" begin - @test IntSet(UInt8(2^8-1)) == IntSet(2^8-1) - @test [x for x in IntSet(UInt8(2^8-1))] == [UInt8(2^8-1)] - @test IntSet(UInt16(2^16-1)) == IntSet(2^16-1) - @test [x for x in IntSet(UInt16(2^16-1))] == [UInt16(2^16-1)] + @test BitSet(UInt8(2^8-1)) == BitSet(2^8-1) + @test [x for x in BitSet(UInt8(2^8-1))] == [UInt8(2^8-1)] + @test BitSet(UInt16(2^16-1)) == BitSet(2^16-1) + @test [x for x in BitSet(UInt16(2^16-1))] == [UInt16(2^16-1)] 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) @@ -319,6 +319,6 @@ end end @testset "extreme values" begin - @test pop!(IntSet(typemin(Int))) == typemin(Int) - @test pop!(IntSet(typemax(Int))) == typemax(Int) + @test pop!(BitSet(typemin(Int))) == typemin(Int) + @test pop!(BitSet(typemax(Int))) == typemax(Int) end diff --git a/test/choosetests.jl b/test/choosetests.jl index b2510aeea196eb..ae0ed781549ebb 100644 --- a/test/choosetests.jl +++ b/test/choosetests.jl @@ -49,7 +49,7 @@ function choosetests(choices = []) "nullable", "meta", "stacktraces", "libgit2", "docs", "markdown", "serialize", "misc", "threads", "enums", "cmdlineargs", "i18n", "workspace", "libdl", "int", - "checked", "intset", "floatfuncs", "compile", "distributed", "inline", + "checked", "bitset", "floatfuncs", "compile", "distributed", "inline", "boundscheck", "error", "ambiguous", "cartesian", "asmvariant", "osutils", "channels", "iostream", "specificity", "codegen", "codevalidation", "reinterpretarray", "syntax", "missing" diff --git a/test/random.jl b/test/random.jl index ef6227b6bfe17c..831ae034619603 100644 --- a/test/random.jl +++ b/test/random.jl @@ -323,7 +323,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}, @@ -381,7 +381,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 212dfb220404fb..ef46d7fec75643 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -165,7 +165,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])) @@ -177,7 +177,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])) @@ -189,7 +189,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]))