Get Involved

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

  TypedList::TypedList() [code]

Constructs an empty TypedList.

TypedList<String> list;
  TypedList::TypedList(TypedList&& other) [code]

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.

Assignment

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);

Capacity

 bool TypedList::isEmpty() const [code]

Returns true if the list is empty.

Modification

 ArrayView<T> TypedList::beginWriteViewNoConstruct() [code]

Returns an ArrayView of allocated but uninitialized items. The returned ArrayView always contains at least one item. The caller is meant to construct some number of items, then call endWrite.

ArrayView<String> view = list.beginWriteViewNoConstruct();
for (u32 i = 0; i < view.numItems; i++) {
    new (&view[i]) String{String::from(i)};
}
list.endWrite(view.numItems);
 T* TypedList::beginWriteNoConstruct() [code]

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();
 void TypedList::endWrite(u32 numItems = 1) [code]

Must be called after beginWriteViewNoConstruct or beginWriteNoConstruct after constructing some number of items. Advances the write position and makes the newly constructed items available for read.

 T& TypedList::append(const T& item) [code]
 T& TypedList::append(T&& item) [code]
 template <typename Args>
T& TypedList::append(Args&& args) [code]

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 or moveToArray is called.

TypedList<String> list;
list.append("Hello");
 void TypedList::clear() [code]

Destructs all items in the list and frees any associated memory. The TypedList is left in an empty state.

Conversion

 Array<T> TypedList::moveToArray() [code]

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 the Array, avoiding unnecessary memory reallocation or copying. The TypedList is left in an unusable state and cannot be modified after this operation.

TypedList<String> list;
list.append("Hello");
Array<String> arr = list.moveToArray();

Iteration

 TypedListIterator<T> TypedList::begin() const [code]
 TypedListIterator<T> TypedList::end() const [code]

Required functions to support range-for syntax. Allows you to iterate over all the items in the list.

for (const T& item : list) {
    ...
}