Tuesday 4 July 2017

Unrolled List In Java

Hello!
Didn't write to much recently, being busy, but I got something for you: Unrolled Linked List in Java.
This is an Wikipedia article; in my implementation, there is no remove and insert function - it would complicate API too much, imo.
It has O(1) performance with: push, add, pop (removes the first) and popLast. Performance of:
- replace (updates element with the given index) and
- get (returns - not remove an element with the given index)
are O(n / [#of internal arrays]) - which is better (with max elements 1000 and n = million  would be 500) than pure Linked List and obviously worst than Augumented Array.
There is a bigger memory overhead per node: an array of elements plus some helper fields; for million integers, there would be around 1600 bytes overhead plus whatever comes from arrays management.
Where to use it? Any queues, stacks, et cetera, especially when we do lots of insertions and lookups at the front - in that case ArrayList is much slower. Code as usually on github. Till the next time!