What is the ideal list-like data structure in Julia?
I want an indexible, growable, collection with a constant-time append operation.
The standard data structure seems to be Array
with the push!
operation. Is this constant time?
As Harlan said, push!
is amortized constant time. See the description of C++'s similar data structure for arguments why: Amortized analysis of std::vector insertion
If you want a legitimately constant time data structure, you probably want to implement a linked list. I've seen lots of sample implementations, but nothing that's ready for production.
Repeatedly calling push!
is not constant time, but it's pretty fast. It does an occasional exponential reallocation of the buffer. See the C source for appending to an array: https://github.com/JuliaLang/julia/blob/master/src/array.c#L564
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With