Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jw3126/enumsets.jl
fast sets containing enum values
https://github.com/jw3126/enumsets.jl
data-structures juila
Last synced: 5 days ago
JSON representation
fast sets containing enum values
- Host: GitHub
- URL: https://github.com/jw3126/enumsets.jl
- Owner: jw3126
- License: mit
- Created: 2024-12-05T14:55:38.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2025-01-09T05:32:34.000Z (about 1 month ago)
- Last Synced: 2025-01-20T17:55:18.448Z (22 days ago)
- Topics: data-structures, juila
- Language: Julia
- Homepage:
- Size: 33.2 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EnumSets
[![Build Status](https://github.com/jw3126/EnumSets.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/jw3126/EnumSets.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Coverage](https://codecov.io/gh/jw3126/EnumSets.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/jw3126/EnumSets.jl)This packages allows to create a very fast immutable type that represents a set of enum values.
```julia
julia> using EnumSetsjulia> @enum Lang Python Julia C
julia> const LangSet = enumsettype(Lang)
EnumSets.EnumSet{Lang, UInt8, EnumSets.OffsetBasedPacking{0}}julia> s = LangSet((Python, Julia))
EnumSets.EnumSet{Lang, UInt8, EnumSets.OffsetBasedPacking{0}} with 2 elements:
Python
Juliajulia> push(s, C) # s is immutable, but we can create modified copies
EnumSets.EnumSet{Lang, UInt8, EnumSets.OffsetBasedPacking{0}} with 3 elements:
Python
Julia
Cjulia> s
EnumSets.EnumSet{Lang, UInt8, EnumSets.OffsetBasedPacking{0}} with 2 elements:
Python
Juliajulia> s2 = LangSet((C, Python))
EnumSets.EnumSet{Lang, UInt8, EnumSets.OffsetBasedPacking{0}} with 2 elements:
Python
Cjulia> s ∪ s2
EnumSets.EnumSet{Lang, UInt8, EnumSets.OffsetBasedPacking{0}} with 3 elements:
Python
Julia
Cjulia> s ∩ s2
EnumSets.EnumSet{Lang, UInt8, EnumSets.OffsetBasedPacking{0}} with 1 element:
Python
...
```## Performance
```julia
using EnumSets@enum Alphabet A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
function workout(sets)
s = first(sets)
b = false
ESet = eltype(sets)
E = eltype(ESet)
for s1 in sets
for s2 in sets
for e in instances(E)
s = (s ∩ s2) ∪ s1
s = s ∪ s1
s = symdiff(s, ESet((e,)))
b = b ⊻ (s1 ⊆ s2)
b = b ⊻ (s1 ⊊ s2)
b = b ⊻ (e in s)
end
end
end
s, b
endconst AlphabetSet = enumsettype(Alphabet)
sets = [AlphabetSet(rand(instances(Alphabet)) for _ in 0:length(instances(Alphabet))) for _ in 1:100]
basesets = map(Set, sets)# warmup
workout(sets, )
workout(basesets, )
# benchmark
println(eltype(sets))
res1 = @time workout(sets, )
println(eltype(basesets))
res2 = @time workout(basesets, )@assert res1 == res2 # both yield the same result
# AlphabetSet
# 0.000279 seconds (1 allocation: 16 bytes)
# Set{Alphabet}
# 0.503022 seconds (8.15 M allocations: 756.469 MiB, 13.51% gc time)
```