Resizing
Julia does not currently have a well developed interface for changing the size of collections. Resizing
provides common methods for growing and shrinking collections. Although the relative position where a resizing method is executed may vary by method and collection type, a common pattern makes them straightforward to use and overload for new collections. For example, the following methods are responsible for growing a collection from the last index:
Resizing.unsafe_grow_end!(collection, n)
: assumes that all necessary conditions for growingcollection
byn
elements from the last index are met without checking.Resizing.grow_end!(collection, n)
: CallsResizing.unsafe_grow_end!(collection, n)
if it can determine thatcollection
can safely grow byn
elements from the last index, returningtrue
if successful.Resizing.assert_grow_end!(collection, n)
: CallsResizing.grow_end!(collection, n)
. Iffalse
is returned it throws an error, otherwise returnsnothing
.
Note that grow_end!
and unsafe_grow_end!
must be defined for each new collection type, but assert_grow_end!
can rely completely on the former two methods.
This same pattern exists for shrinking or growing at the beginning, end, or a specified index.