Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/juliaaplavin/unioncollections.jl
https://github.com/juliaaplavin/unioncollections.jl
arrays data-structures dictionaries
Last synced: 2 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/juliaaplavin/unioncollections.jl
- Owner: JuliaAPlavin
- License: mit
- Created: 2024-02-18T18:00:26.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-10-17T12:05:24.000Z (3 months ago)
- Last Synced: 2024-11-19T05:59:52.420Z (2 months ago)
- Topics: arrays, data-structures, dictionaries
- Language: Julia
- Homepage:
- Size: 15.6 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UnionCollections.jl
Efficient collections (arrays and dictionaries) with `Union` element types. Compared to `Vector{Union{...}}`, more operations are type-stable: most notably, `map()`.
Under the hood, elements of different types are stored in separate sub-collections. This allows for operations on the collection as a whole to be type-stable, while `X[i]` fundamentally remain type-unstable.
# Usage
```julia
# create a regular vector and a union vector:
julia> V = Union{Int, String}[1, 2, "x", 3, "yy"]
5-element Vector{Union{Int64, String}} <...>julia> A = unioncollection(V)
5-element UnionVector{Union{Int64, String}} <...># these two work basically the same, but the union vector is more efficient
## Vector:julia> map(x -> x^3, V)
5-element Vector{Any}:
1
8
"xxx"
27
"yyyyyy"julia> @code_warntype map(x -> x^3, V)
Body::Union{Vector{Any}, Vector{Int64}, Vector{String}}julia> @btime map(x -> x^2, map(x -> x^3, $V))
3.445 μs (19 allocations: 792 bytes)## UnionVector:
julia> map(x -> x^3, A)
5-element UnionVector{Union{Int64, String}, Tuple{Vector{Int64}, Vector{String}}}:
1
8
"xxx"
27
"yyyyyy"julia> @code_warntype map(x -> x^3, A)
Body::UnionVector{Union{Int64, String}}julia> @btime map(x -> x^2, map(x -> x^3, $A))
197.597 ns (8 allocations: 392 bytes)
```