How to implement an unchangeable collection with constant addition and random access time?

advertisements

I'm looking for a solution like this one proposed by Eric Lippert. It is a great implementation, as it is immutable plus the append time is O(1), but it downside is the O(i) random access time.

On the other side there is a great implementation of collection with O(1) on both append and random access. The only problem is that it strongly relies on mutability.

My questions is how to implement a collection which combines the benefits of both solutions? That is:

  1. immutability
  2. O(1) append time
  3. O(1) random access time

Memory complexity is not that big issue for me.


I do not know of a way to implement a list which has all your requirements -- immutability, persistence, O(1) insertion, O(1) removal, O(1) random access.

My suggestion to you is that (1) if you are interested in this topic, read Chris Okasaki's book. (Or, get a copy of his thesis, which was the basis of the book.) And (2) Chris Okasaki suggests the data structure described here for your purposes:

http://www.codeproject.com/Articles/9680/Persistent-Data-Structures#RandomAccessLists

This list is O(1) insert and O(1) removal to the head and O(lg) for random access.