I have to implement a kind of an array or sequence or list, which supports the cheapest way of circulated forwarding and back winding of elements. See this example:
Original sequence: 1 2 3 4 5
Forwarded once: 5 1 2 3 4
Forwarded twice: 4 5 1 2 3
Same but opposite is for the back winding. What would be the cheapest and most Scala-style way of implementing this? In Java I could use LinkedList and it would do great… However, I could not find any definite answer for Scala.
Also, it also has to be easy to replace any given element by index, as in LinkedList.
UPDATE:
For the fastest, but not-so-idiomatic variant of algorithm (you know when you need it), refer to the answer of Petr Pudlák!!!
Immutable implementation
A ring buffer is a pair of an
IndexedSeqand anIntpointer into this sequence. I provide code for a immutable version. Note that not all methods that might be useful are implemented; like the mutators that change the content of theIndexedSeq.With this implementation, shifting is just creating one new object. So it’s pretty efficient.
Example code
Output
Performance comparison to mutable implementations
The OP mentions that you should look at the mutable implementations (e.g. this answer), if you need performance. This is not true in general. As always: It depends.
Immutable
O(log n), which is basically the update complexity of the underlyingIndexedSeq;O(1), also involves creating a new object which may cost some cyclesMutable
O(1), array update, as fast as it getsO(n), you have to touch every element once; fast implementations on primitive arrays might still win against the immutable version for small arrays, because of constant factor