Concatenating vectors into a single vector without allocating memory (opposite of @view)

Suppose we want to construct one large vector x and three smaller vectors x1, x2, x3 such that a relationship x = [x1; x2; x3] is always maintained. Provided that each of the smaller vectors has 100 entries, this can be achieved by

julia> x = rand(300);

julia> x1 = @view(x[1:100]); x2 = @view(x[101:200]); x3 = @view(x[201:300]);

Here, real memory is allocated to x, and x1, x2, x3 are just the views the portions of this memory.

What I would like achieve is something opposite: I would like to create three independent vectors x1, x2, x3 first and construct x such that its each third views one of the memory allocated to x1, x2, x3. In other words, I would like to do something like

julia> x1 = rand(100); x2 = rand(100); x3 = rand(100);

julia> x = [@view(x1); @view(x2); @view(x3)];

though this code does not work.

Does this capability already exist in Julia, or are there any packages doing this?

CatViews.jl

Might need an update for v0.6.

3 Likes