ElasticArrays/ArraysofArrays for Vector of Arrays

Hi @mrVeng ,

happy to assist.

As for as

ela2 = ElasticVector([zeros(T, Nrows) for _ in 1:Ncols])

is concerned: ElasticVector is only part of ElasticArrays for completeness. Standard Julia Vectors are already resizable, of course, so it doesn’t add any important functionality. ElasticArray{T,N} only becomes interesting for N > 2, really.

Is there a way that I can convert ela2 to an ArraysOfArrays […] still push!() and resize!() […] stored as a pure Matrix

Unfortunately, no: As long as the underlying matrix (backed by a vector) is stored in memory in a contiguous fashion (as ElasticArray does), resizing it in any but the last dimension is not an efficient operation, since the matrix (resp. the vector backing it) would have to be completely rearranged in memory.

There’s an old issue by @ChrisRackauckas regarding resizing ElasticArray in other dimensions. @stevengj suggested to use padding and reallocating only when running out of padding space, this would make it efficient. Two problems though:

  • ElasticArray is currently an immutable struct, and it would have to become mutable for this. In the past, it didn’t matter much whether it was mutable or immutable, but nowadays Julia is able to stack-allocate immutable objects that point to arrays. So nowadays having it immutable is a plus. Of course a mutable elastic array type could be added to ElasticArrays.jl, sharing much of the code with ElasticArray.

  • You want to push to individual elements of the vector or vectors. As soon as you do that, they would become dissimilar in length, so a VectorOfSimilarVectors would literally not describe your use case anymore.

I can, however, offer a solution, though it’s not implemented yet:

ArraysOfArray.jl provides a type VectorOfArrays, which can handle element arrays that differ in size, backed by a single flat vector. Currently, you can’t push to the element vectors/arrays, since that would require rearranging the backing vector. I have firm plans to support vectors of arrays that, while backed by a single vector, do not have to be stored in-order. This one will allow resizing element arrays, which will result in appending the new element and just changing indexing info. This will be efficient, as long as pushes/appends aren’t in random order in respect to the element index. It will also come with a “compactify” operation, to re-compact and sort things after all appending/pushing, etc. is done. I would like to add this soon, just have to find a bit of time.

2 Likes