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

feat: Unordered set types #436

Merged
merged 3 commits into from
Jun 12, 2024
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
11 changes: 6 additions & 5 deletions .github/workflows/test-linux-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ jobs:
- "1.10"
- "nightly"
os:
- macos-14
- macos-13
- ubuntu-latest
arch:
- x64
- aarch64
exclude:
- os: ubuntu-latest
include:
- os: macos-14
arch: aarch64
- version: 1.6
version: "1.10"
- os: macos-14
arch: aarch64
version: "nightly"
steps:
- uses: actions/checkout@v4
with:
Expand Down
4 changes: 2 additions & 2 deletions src/CxxWrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,8 @@ ConstCxxPtr, ConstCxxRef, CxxRef, CxxPtr,
CppEnum, ConstArray, CxxBool, CxxLong, CxxULong, CxxChar, CxxChar16, CxxChar32, CxxWchar, CxxUChar, CxxSignedChar,
CxxLongLong, CxxULongLong, ptrunion, gcprotect, gcunprotect, isnull

using .StdLib: StdVector, StdString, StdWString, StdValArray, StdThread, StdDeque, StdQueue, StdSet, StdMultiset
using .StdLib: StdVector, StdString, StdWString, StdValArray, StdThread, StdDeque, StdQueue, StdSet, StdMultiset, StdUnorderedSet, StdUnorderedMultiset

export StdLib, StdVector, StdString, StdWString, StdValArray, StdThread, StdDeque, StdQueue, StdSet, StdMultiset
export StdLib, StdVector, StdString, StdWString, StdValArray, StdThread, StdDeque, StdQueue, StdSet, StdMultiset, StdUnorderedSet, StdUnorderedMultiset

end # module
36 changes: 20 additions & 16 deletions src/StdLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,26 @@ Base.push!(v::StdQueue, x) = push_back!(v, x)
Base.first(v::StdQueue) = front(v)
Base.pop!(v::StdQueue) = pop_front!(v)

Base.size(v::StdSet) = (Int(cppsize(v)),)
Base.length(v::StdSet) = Int(cppsize(v))
Base.isempty(v::StdSet) = set_isempty(v)
Base.empty!(v::StdSet) = (set_empty!(v); v)
Base.push!(v::StdSet, x) = (set_insert!(v, x); v)
Base.in(x, v::StdSet) = set_in(v, x)
Base.delete!(v::StdSet, x) = (set_delete!(v, x); v)

Base.size(v::StdMultiset) = (Int(cppsize(v)),)
Base.length(v::StdMultiset) = Int(cppsize(v))
Base.isempty(v::StdMultiset) = multiset_isempty(v)
Base.empty!(v::StdMultiset) = (multiset_empty!(v); v)
Base.push!(v::StdMultiset, x) = (multiset_insert!(v, x); v)
Base.in(x, v::StdMultiset) = multiset_in(v, x)
Base.delete!(v::StdMultiset, x) = (multiset_delete!(v, x); v)
Base.count(x, v::StdMultiset) = multiset_count(v, x)
for StdSetType in (StdSet, StdUnorderedSet)
Base.size(v::StdSetType) = (Int(cppsize(v)),)
Base.length(v::StdSetType) = Int(cppsize(v))
Base.isempty(v::StdSetType) = set_isempty(v)
Base.empty!(v::StdSetType) = (set_empty!(v); v)
Base.push!(v::StdSetType, x) = (set_insert!(v, x); v)
Base.in(x, v::StdSetType) = set_in(v, x)
Base.delete!(v::StdSetType, x) = (set_delete!(v, x); v)
end

for StdMultisetType in (StdMultiset, StdUnorderedMultiset)
Base.size(v::StdMultisetType) = (Int(cppsize(v)),)
Base.length(v::StdMultisetType) = Int(cppsize(v))
Base.isempty(v::StdMultisetType) = multiset_isempty(v)
Base.empty!(v::StdMultisetType) = (multiset_empty!(v); v)
Base.push!(v::StdMultisetType, x) = (multiset_insert!(v, x); v)
Base.in(x, v::StdMultisetType) = multiset_in(v, x)
Base.delete!(v::StdMultisetType, x) = (multiset_delete!(v, x); v)
Base.count(x, v::StdMultisetType) = multiset_count(v, x)
end

function Base.fill!(v::T, x) where T <: Union{StdVector, StdValArray, StdDeque}
StdFill(v, x)
Expand Down
202 changes: 103 additions & 99 deletions test/stdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,110 +327,114 @@ let
@test length(queue) == 1
end

@testset "StdSet" begin
@testset "Set with integers" begin
set = StdSet{Int64}()
@test isempty(set) == true
@test length(set) == 0
set = push!(set, 10)
push!(set, 20)
@test isempty(set) == false
@test length(set) == 2
@test (10 ∈ set) == true
@test (20 ∈ set) == true
set = delete!(set, 20)
@test length(set) == 1
@test (20 ∈ set) == false
@test (30 ∈ set) == false
empty!(set)
@test isempty(set) == true
end
@testset "StdSet and StdUnorderedSet" begin
for StdSetType in (StdSet, StdUnorderedSet)
@testset "Set with integers" begin
set = StdSetType{Int64}()
@test isempty(set) == true
@test length(set) == 0
set = push!(set, 10)
push!(set, 20)
@test isempty(set) == false
@test length(set) == 2
@test (10 ∈ set) == true
@test (20 ∈ set) == true
set = delete!(set, 20)
@test length(set) == 1
@test (20 ∈ set) == false
@test (30 ∈ set) == false
empty!(set)
@test isempty(set) == true
end

@testset "Set with bools" begin
set = StdSet{CxxBool}()
@test isempty(set) == true
@test length(set) == 0
push!(set, true)
push!(set, false)
@test isempty(set) == false
@test length(set) == 2
@test (true ∈ set) == true
@test (false ∈ set) == true
set = empty!(set)
@test isempty(set) == true
end
@testset "Set with bools" begin
set = StdSetType{CxxBool}()
@test isempty(set) == true
@test length(set) == 0
push!(set, true)
push!(set, false)
@test isempty(set) == false
@test length(set) == 2
@test (true ∈ set) == true
@test (false ∈ set) == true
set = empty!(set)
@test isempty(set) == true
end

@testset "Set with floats" begin
set = StdSet{Float64}()
@test isempty(set) == true
@test length(set) == 0
push!(set, 1.4)
push!(set, 2.2)
@test isempty(set) == false
@test length(set) == 2
@test (1.4 ∈ set) == true
@test (10.0 ∈ set) == false
@test (2.2 ∈ set) == true
empty!(set)
@test isempty(set) == true
@testset "Set with floats" begin
set = StdSetType{Float64}()
@test isempty(set) == true
@test length(set) == 0
push!(set, 1.4)
push!(set, 2.2)
@test isempty(set) == false
@test length(set) == 2
@test (1.4 ∈ set) == true
@test (10.0 ∈ set) == false
@test (2.2 ∈ set) == true
empty!(set)
@test isempty(set) == true
end
end
end

@testset "StdMultiset" begin
@testset "Multiset with integers" begin
multiset = StdMultiset{Int64}()
@test isempty(multiset) == true
@test length(multiset) == 0
multiset = push!(multiset, 10)
push!(multiset, 20)
push!(multiset, 20)
count(20, multiset) == 2
@test isempty(multiset) == false
@test length(multiset) == 3
@test (10 ∈ multiset) == true
@test (20 ∈ multiset) == true
multiset = delete!(multiset, 20)
@test length(multiset) == 1
@test (20 ∈ multiset) == false
@test (30 ∈ multiset) == false
empty!(multiset)
@test isempty(multiset) == true
end

@testset "Multiset with bools" begin
multiset = StdMultiset{CxxBool}()
push!(multiset, true)
push!(multiset, true)
push!(multiset, true)
push!(multiset, false)
@test isempty(multiset) == false
@test count(true, multiset) == 3
@test count(false, multiset) == 1
@test length(multiset) == 4
multiset = delete!(multiset, true)
@test length(multiset) == 1
multiset = empty!(multiset)
@test length(multiset) == 0
@test isempty(multiset) == true
end

@testset "Multiset with floats" begin
multiset = StdMultiset{Float64}()
@test isempty(multiset) == true
@test length(multiset) == 0
push!(multiset, 1.4)
push!(multiset, 2.2)
push!(multiset, 2.2)
@test isempty(multiset) == false
@test length(multiset) == 3
@test (1.4 ∈ multiset) == true
@test count(1.4, multiset) == 1
@test (10.0 ∈ multiset) == false
@test count(10.0, multiset) == 0
@test (2.2 ∈ multiset) == true
@test count(2.2, multiset) == 2
empty!(multiset)
@test isempty(multiset) == true
@testset "StdMultiset and StdUnorderedMultiset" begin
for StdMultisetType in (StdMultiset, StdUnorderedMultiset)
@testset "Multiset with integers" begin
multiset = StdMultisetType{Int64}()
@test isempty(multiset) == true
@test length(multiset) == 0
multiset = push!(multiset, 10)
push!(multiset, 20)
push!(multiset, 20)
count(20, multiset) == 2
@test isempty(multiset) == false
@test length(multiset) == 3
@test (10 ∈ multiset) == true
@test (20 ∈ multiset) == true
multiset = delete!(multiset, 20)
@test length(multiset) == 1
@test (20 ∈ multiset) == false
@test (30 ∈ multiset) == false
empty!(multiset)
@test isempty(multiset) == true
end

@testset "Multiset with bools" begin
multiset = StdMultisetType{CxxBool}()
push!(multiset, true)
push!(multiset, true)
push!(multiset, true)
push!(multiset, false)
@test isempty(multiset) == false
@test count(true, multiset) == 3
@test count(false, multiset) == 1
@test length(multiset) == 4
multiset = delete!(multiset, true)
@test length(multiset) == 1
multiset = empty!(multiset)
@test length(multiset) == 0
@test isempty(multiset) == true
end

@testset "Multiset with floats" begin
multiset = StdMultisetType{Float64}()
@test isempty(multiset) == true
@test length(multiset) == 0
push!(multiset, 1.4)
push!(multiset, 2.2)
push!(multiset, 2.2)
@test isempty(multiset) == false
@test length(multiset) == 3
@test (1.4 ∈ multiset) == true
@test count(1.4, multiset) == 1
@test (10.0 ∈ multiset) == false
@test count(10.0, multiset) == 0
@test (2.2 ∈ multiset) == true
@test count(2.2, multiset) == 2
empty!(multiset)
@test isempty(multiset) == true
end
end
end

Expand Down