Skip to content

Commit

Permalink
Document and export Base.in! (JuliaLang#51636)
Browse files Browse the repository at this point in the history
I think `in!` is a useful general function for users, and would be good
to have as official API. Its semantics is clear and unambiguous, while
providing a clear performance advantage over the naive implementation.

For more evidence that this functionality is useful, consider:
* Rust's `HashSet::insert` works just like this implementation of `in!`
* This function was already used in the implementation of `Base.unique`,
precisely for the performance over the naive approach

Comes from JuliaLang#45156 with some initial discussion.
  • Loading branch information
jakobnissen authored and mkitti committed Dec 9, 2023
1 parent 139e443 commit 2580d56
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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)
!!! 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 @@ -133,6 +133,31 @@ end
@test pop!(s, 4) === 4.0
@test_throws KeyError pop!(s, 5)
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

0 comments on commit 2580d56

Please sign in to comment.