I recently wrote and registered TypeSortedCollections.jl, a small package that allows you to perform operations on a type-heterogeneous collection in type-stable and statically dispatched fashion. I originally implemented this for use in RigidBodyDynamics.jl, but decided to pull it out into its own package as I hope it will be useful for others as well.
TypeSortedCollections provides the TypeSortedCollection
type, which sorts a type-heterogeneous input collection by type upon construction, and stores the elements in a Tuple
of concretely typed Vectors
, one for each type. TypeSortedCollections provides type stable methods for map!
, foreach
, and mapreduce
that take at least one TypeSortedCollection.
An example (from the readme):
julia> using TypeSortedCollections
julia> f(x::Int64, y::Float64) = x * y
f (generic function with 2 methods)
julia> f(x::Float64, y::Float64) = round(Int64, -x * y)
f (generic function with 2 methods)
julia> xs = Number[1.; 2; 3]; # note the abstract element type; contains both Float64 and Int64
julia> sortedxs = TypeSortedCollection(xs);
julia> ys = [1.; 2.; 3.];
julia> results = Vector{Int64}(length(xs));
julia> map!(f, results, sortedxs, ys)
3-element Array{Int64,1}:
-1
4
9
julia> @allocated map!(f, results, sortedxs, ys)
0
Please check out the readme for additional features and notes (especially on when TypeSortedCollection
s are appropriate to use), and let me know if you have any questions!