Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document and export Base.in! #51636

Merged
merged 5 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Build system changes
New library functions
---------------------

* `in!(x, s::AbstractSet)` will return whether `x` is in `s`, and insert `x` in `s` if not.
* The new `Libc.mkfifo` function wraps the `mkfifo` C function on Unix platforms ([#34587]).
* `hardlink(src, dst)` can be used to create hard links. ([#41639])
* `diskstat(path=pwd())` can be used to return statistics about the disk. ([#42248])
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ export
getkey,
haskey,
in,
in!,
intersect!,
intersect,
isdisjoint,
Expand Down
42 changes: 38 additions & 4 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,46 @@ isempty(s::Set) = isempty(s.dict)
length(s::Set) = length(s.dict)
in(x, s::Set) = haskey(s.dict, x)

# This avoids hashing and probing twice and it works the same as
# in!(x, s::Set) = in(x, s) ? true : (push!(s, x); false)
"""
in!(x, s::AbstractSet) -> Bool

If `x` is in `s`, return `true`. If not, push `x` into `s` and return `false`.
This is equivalent to `in(x, s) ? true : (push!(s, x); false)`, but may have a
more efficient implementation.

See also: [`in`](@ref), [`push!`](@ref), [`Set`](@ref)

jakobnissen marked this conversation as resolved.
Show resolved Hide resolved
!!! compat "Julia 1.11"
This function requires at least 1.11.

# Examples
```jldoctest; filter = r"^ [1234]\$"
julia> s = Set{Any}([1, 2, 3]); in!(4, s)
false

julia> length(s)
4

julia> in!(0x04, s)
true

julia> s
Set{Any} with 4 elements:
4
2
3
1
```
"""
function in!(x, s::AbstractSet)
x ∈ s ? true : (push!(s, x); false)
end

function in!(x, s::Set)
idx, sh = ht_keyindex2_shorthash!(s.dict, x)
xT = convert(eltype(s), x)
idx, sh = ht_keyindex2_shorthash!(s.dict, xT)
idx > 0 && return true
_setindex!(s.dict, nothing, x, -idx, sh)
_setindex!(s.dict, nothing, xT, -idx, sh)
return false
end

Expand Down
1 change: 1 addition & 0 deletions doc/src/base/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ Base.symdiff
Base.symdiff!
Base.intersect!
Base.issubset
Base.in!
Base.:⊈
Base.:⊊
Base.issetequal
Expand Down
25 changes: 25 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ end
@test_throws ArgumentError pop!(s)
@test length(Set(['x',120])) == 2
end

@testset "in!" begin
s = Set()
@test !(in!(0x01, s))
@test !(in!(Int32(2), s))
@test in!(1, s)
@test in!(2.0, s)
(a, b, c...) = sort!(collect(s))
@test a === 0x01
@test b === Int32(2)
@test isempty(c)

# in! will convert to the right type automatically
s = Set{Int32}()
@test !(in!(1, s))
@test only(s) === Int32(1)
@test_throws Exception in!("hello", s)

# Other set types
s = BitSet()
@test !(in!(13, s))
@test in!(UInt16(13), s)
@test only(s) === 13
end

@testset "copy" begin
data_in = (1,2,9,8,4)
s = Set(data_in)
Expand Down