MicroCollections.jl

MicroCollections.MicroCollectionsModule

MicroCollections

Dev GitHub Actions

MicroCollections.jl provides immutable empty and singleton collections.

julia> using MicroCollections

julia> vec0()  # or EmptyVector()
0-element EmptyVector{Union{}}

julia> vec0(Int)  # or EmptyVector{Int}()
0-element EmptyVector{Int64}

julia> vec1(1)  # or SingletonVector((1,))
1-element SingletonVector{Int64}:
 1

julia> EmptyDict()
EmptyDict{Union{},Union{}}()

julia> EmptyDict{Symbol,Char}()
EmptyDict{Symbol,Char}()

julia> SingletonDict(:a => 0)
SingletonDict{Symbol,Int64} with 1 entry:
  :a => 0

julia> EmptySet()
EmptySet{Union{}}()

julia> EmptySet{Int64}()
EmptySet{Int64}()

julia> SingletonSet((1,))
SingletonSet{Int64} with 1 element:
  1

With BangBang.jl, MicroCollections.jl is useful for constructing singleton solutions that can be combined with a reduce:

julia> using BangBang.Experimental: mergewith!!

julia> @assert mapreduce(
           x -> SingletonDict(abs(x) % 10 => 1), mergewith!!(+), 1:1000,
       ) == Dict(
           0 => 100,
           1 => 100,
           2 => 100,
           3 => 100,
           4 => 100,
           5 => 100,
           6 => 100,
           7 => 100,
           8 => 100,
           9 => 100,
       )
source
MicroCollections.EmptyDictType
EmptyDict() :: AbstractDict{Union{},Union{}}
EmptyDict(itr) :: AbstractDict
EmptyDict{K,V}() :: AbstractDict{K,V}
EmptyDict{K,V}(itr) :: AbstractDict{K,V}
source
MicroCollections.EmptyVectorType
EmptyVector() :: AbstractVector{Union{}}
EmptyVector(itr) :: AbstractVector
EmptyVector{T}() :: AbstractVector{T}
EmptyVector{T}(itr) :: AbstractVector{T}

Create an empty vector. Iterator itr must be empty.

The unary constructors EmptyVector(itr) and EmptyVector{T}(itr) asserts that the iterator itr is empty. The constructor EmptyVector(itr) uses eltype(itr) if IteratorEltype(itr) is HasEltype.

source
MicroCollections.OneHotArrayType
OneHotArray(index => value, shape) -> array::AbstractArray

Create an array such that isequal(array[index], value). The values at indices other than index are undefined.

Currently, this array type is likely only useful as the second argument to BangBang.Extras.broadcast_inplace!! and as the input argument to FLoops.jl's @reduce

Examples

julia> using MicroCollections, BangBang.Extras

julia> broadcast_inplace!!(+, ones(Int64, 4), OneHotVector(2 => 3, 4))
4-element Vector{Int64}:
 1
 4
 1
 1

julia> using InitialValues

julia> broadcast_inplace!!(+, InitialValue(+), OneHotVector(2 => 3, 4))
4-element Vector{Int64}:
 0
 3
 0
 0
source
MicroCollections.SingletonDictType
SingletonDict(k => v) :: AbstractDict
SingletonDict(itr) :: AbstractDict
SingletonDict{K,V}(k => v) :: AbstractDict{K,V}
SingletonDict{K,V}(itr) :: AbstractDict{K,V}
source
MicroCollections.SingletonVectorType
SingletonVector(itr) :: AbstractVector
SingletonVector{T}(itr) :: AbstractVector{T}

Create a singleton vector. Iterator itr must have one and only one element.

The constructor SingletonVector(itr) uses eltype(itr) if IteratorEltype(itr) is HasEltype.

source
MicroCollections.UndefArrayType
UndefArray(size [, factory])
UndefArray{T}(size [, factory])
UndefArray{T,N}(size [, factory])

Examples

julia> using MicroCollections

julia> UndefArray((2,))
2-element UndefVector{Union{}}(2):
 #undef
 #undef

julia> UndefArray{Int}((2, 3))
2×3 UndefArray{2,Int64}((2, 3)):
 #undef  #undef  #undef
 #undef  #undef  #undef

The size of an UndefArray can be "changed" by using Setfield.@set

julia> using Setfield

julia> x = UndefArray((2,))
2-element UndefVector{Union{}}(2):
 #undef
 #undef

julia> @set size(x) = (1, 3)
1×3 UndefArray{2,Union{}}((1, 3)):
 #undef  #undef  #undef
source
MicroCollections.UndefVectorType
UndefVector(length)

Examples

julia> using MicroCollections

julia> UndefVector(3)
3-element UndefVector{Union{}}(3):
 #undef
 #undef
 #undef
source
MicroCollections.emptyshimFunction
emptyshim(ContainerType)
emptyshim(ContainerType, ElementType)

Create an empty "shim" container that is widen to ContainerType when appending elements to it. Default ElementType is Union{}.

Examples

julia> using MicroCollections, BangBang

julia> @assert append!!(emptyshim(Vector), [0]) == [0]

julia> @assert merge!!(emptyshim(Dict), Dict(:a => 1)) == Dict(:a => 1)

julia> @assert union!!(emptyshim(Set), Set([0])) == Set([0])
source
MicroCollections.singletonshimFunction
singletonshim(ContainerType, x)

Create a "shim" container with one element x that is widen to ContainerType when appending elements to it.

Examples

julia> using MicroCollections, BangBang

julia> @assert push!!(singletonshim(BitVector, false), true)::BitArray == [false, true]
source