Why Removing the First Array Element Costs Work
A contiguous array that preserves order normally moves later elements left after removing an item near its front.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a short deletion
Removing A from A, B, C, D requires later cells to hold B, C, D in order. Removing D at the end has no later cells to shift. See removal by value or position for the cost of locating an element as well.
Count repeated removals
Removing the first item of a length-four array until empty shifts 3, 2, 1, and 0 positions. A FIFO queue can implement front removals with a representation that avoids those repeated shifts.[1]
Add the exact number of movements when draining
For a compact array of five elements, removing the first leaves four survivors that move one position left. The next removal moves three, then two, one and zero. This is a reason to use a representation with an advancing head for repeated front removals. See ring buffer indices.
Remaining before removal: 5 4 3 2 1
Number of moved entries: 4 3 2 1 0
Total moves: 10
Determine whether physical compaction is actually required
A contiguous array can grow a head offset and compact occasionally instead of shifting every time. Keeping discarded references can retain memory unexpectedly. Test allocation and order after many fills and drains, including the point when compaction runs.