Note that there are some explanatory texts on larger screens.

plurals
  1. POA data structure supporting O(1) random access and worst-case O(1) append?
    primarykey
    data
    text
    <p>I realize a resizable indexed collection that uses an array to store its elements (like <code>List&lt;T&gt;</code> in .NET or <code>ArrayList</code> in Java) has <a href="http://en.wikipedia.org/wiki/Amortized_analysis" rel="noreferrer">amortized O(1) insertion time</a> at the end of the collection. But then there's always that <em>one</em> pesky insertion at critical junctures where the collection has <em>just</em> reached its capacity and the next insertion requires a full copy of all elements in the internal array to a new one (presumably twice as large).</p> <p>A common mistake (in my opinion) is to go with a linked list to "fix" this problem; but I believe the overhead of allocating a node for every element can be quite wasteful, and in fact would dwarf the benefit of a guaranteed O(1) insertion in that rare case that the array insertion is costly—when, in fact, every <em>other</em> array insertion is significantly cheaper (and faster).</p> <p>What I was thinking might make sense is a hybrid approach consisting of a linked list of arrays, where every time the current "head" array reaches its capacity, a new array twice as large is added to the linked list. Then no copy would be necessary since the linked list would still have the original array. Essentially this seems analogous (to me) to the <code>List&lt;T&gt;</code> or <code>ArrayList</code> approach, except that wherever you previously would've incurred the cost of copying all the elements of the internal array, here you only incur the cost of allocating a <em>new</em> array plus a single node insertion.</p> <p>Of course, this would complicate other features if they were desired (e.g., inserting/removing into/from the middle of the collection); but as I've expressed in the title, I'm really just looking for an <em>add-only</em> (and iterate) collection.</p> <p>Are there any data structures out there ideally suited to this purpose? Or, can you think of one yourself?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload