TypedList
A TypedList
object contains a list of typed items. Internally, a TypedList
maintains a linked
list of chunks, with the items stored contiguously in memory within each chunk. TypedList
offers
an alternative to Array
that can grow to an arbitrary number of items without ever reallocating
memory. Once an item is added to a TypedList
, the item's address never changes. The items are
destructed and freed when the TypedList
is destroyed.
Header File
#include <ply-runtime/container/TypedList.h>
Constructors
-
Constructs an empty
TypedList
.TypedList<String> list;
-
Move constructor.
other
is left in an unusable state and cannot be modified after this operation.TypedList<String> arr = std::move(other);
TypedList::~TypedList()
[code]-
Destructor. Destructs all items and frees the memory associated with the
TypedList
. void TypedList::operator=(TypedList&& other)
[code]-
Move assignment operator.
other
is left in an unusable state and cannot be modified after this operation.arr = std::move(other);
-
Returns
true
if the list is empty. -
Returns an
ArrayView
of allocated but uninitialized items. The returnedArrayView
always contains at least one item. The caller is meant to construct some number of items, then callendWrite
.ArrayView<String> view = list.beginWriteViewNoConstruct(); for (u32 i = 0; i < view.numItems; i++) { new (&view[i]) String{String::from(i)}; } list.endWrite(view.numItems);
-
Returns a pointer to a single uninitialized item. The caller is meant to construct this item, then call
endWrite
.String* item = list.beginWriteNoConstruct(); new (item) String; list.endWrite();
-
Must be called after
beginWriteViewNoConstruct
orbeginWriteNoConstruct
after constructing some number of items. Advances the write position and makes the newly constructed items available for read. -
Appends a single item to the list and returns a reference to it. The arguments are forwarded directly to the item's constructor. The returned reference remains valid until the
TypedList
is destroyed ormoveToArray
is called.TypedList<String> list; list.append("Hello");
-
Destructs all items in the list and frees any associated memory. The
TypedList
is left in an empty state. -
Converts the list of items to an
Array
. When the number of items fits in a single chunk, the chunk is truncated and directly adopted by theArray
, avoiding unnecessary memory reallocation or copying. TheTypedList
is left in an unusable state and cannot be modified after this operation.TypedList<String> list; list.append("Hello"); Array<String> arr = list.moveToArray();
-
Required functions to support range-for syntax. Allows you to iterate over all the items in the list.
for (const T& item : list) { ... }