capacity
Currently allocated capacity.
Values may be added to this deque until size == capacity before the deque will reallocate a larger backing buffer.
Example
// Create a deque with an initial capacity value of `9`
val deque = Deque(9) // deque == {0, 0, 0, 0, 0, 0, 0, 0, 0}
// Even though we have not yet inserted any elements, the capacity is 9
assert(deque.capacity == 9)
// Add some elements to the deque
deque += [1, 2, 3, 4, 5, 6] // deque == {1, 2, 3, 4, 5, 6, 0, 0, 0}
// Deque capacity will still be 9 as we have not yet inserted enough
// elements to require a capacity increase
assert(deque.capacity == 9)
// Increase the capacity to 12
deque.ensureCapacity(12) // deque == {1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0}
Content copied to clipboard
The backing buffer is always increased to at minimum hold the number of values being inserted, but the rate of increase may scale the size of the capacity faster than that.
This is to avoid many repeated re-allocations of the backing container. To put it simply, it would be very expensive to use a deque (or ArrayList for that matter) if every new element required a resize.