Get Involved

ArrayView

An ArrayView references a range of typed items in memory. It consists of a pointer items and an integer numItems. An ArrayView does not own the memory it points to, and no heap memory is freed when the ArrayView is destroyed.

When an ArrayView is const, the items its references are immutable. When an ArrayView is not const, the items its references are mutable only if the item type T is not const. ArrayView<T> is implicitly convertible to ArrayView<const T> and can be passed as an argument to any function that expects ArrayView<const T>.

Header File

#include <ply-runtime/container/ArrayView.h>

Also included from <ply-runtime/Base.h>.

Data Members

 T* items [code]

A pointer to the first item in the ArrayView.

 u32 numItems [code]

The number of items in the ArrayView.

Member Functions

  ArrayView::ArrayView() [code]

Constructs an empty ArrayView.

  ArrayView::ArrayView(T* items, u32 numItems) [code]

Constructs an ArrayView from the given pointer and number of items.

  ArrayView::ArrayView(std::initializer_list<T> init) [code]

Constructs an ArrayView from a braced initializer list. Lets you pass an initializer list to any function expecting an ArrayView, as in the following:

void foo(ArrayView<u32>);
void test() {
    foo({1, 2, 3, 4, 5});
}

Use this carefully; the underlying array only exists for the lifetime of the statement calling this constructor.

ArrayView::operator ArrayView<const T>() const [code]

Conversion operator. Makes ArrayView<T> implicitly convertible to ArrayView<const T>.

 static ArrayView<const T> ArrayView::from(StringView view) [code]
 static ArrayView<T> ArrayView::from(MutableStringView view) [code]

Explicitly create an ArrayView from a StringView or MutableStringView.

 StringView ArrayView::stringView() const [code]
 MutableStringView ArrayView::mutableStringView() [code]

Explicitly convert the ArrayView to a StringView or MutableStringView.

T& ArrayView::operator[](u32 index) [code]
const T& ArrayView::operator[](u32 index) const [code]

Subscript operator with runtime bounds checking.

 T& ArrayView::back(s32 offset = -1) [code]
 const T& ArrayView::back(s32 offset = -1) const [code]

Reverse subscript operator with runtime bound checking. Expects a negative index. -1 returns the last item in the view; -2 returns the second-last item, etc.

 void ArrayView::offsetHead(u32 ofs) [code]

Increases items and decreases numItems by ofs. Equivalent to:

*this = this->subView(ofs);
 void ArrayView::offsetBack(s32 ofs) [code]

Adds ofs, which must be less than or equal to zero, to numItems. Equivalent to:

*this = this->subView(0, this->numItems + ofs);
explicit ArrayView::operator bool() const [code]

Explicit conversion to bool. Returns true if numItems is greater than 0. Allows you to use an ArrayView object inside an if condition.

if (view) {
    ...
}
 bool ArrayView::isEmpty() const [code]

Returns true if numItems is 0.

 u32 ArrayView::sizeBytes() const [code]

Returns the total size, in bytes, of the items in the view. Equivalent to this->numItems * sizeof(T).

 ArrayView ArrayView::subView(u32 start) const [code]
 ArrayView ArrayView::subView(u32 start, u32 numItems) const [code]

Returns a subview that starts at the offset given by start. The optional numItems argument determines the number of items in the subview. If numItems is not specified, the subview continues to the end of the view.

 ArrayView ArrayView::shortenedBy(u32 numItems) const [code]

Returns a subview with the last numItems items of the view omitted.

 T* ArrayView::begin() const [code]
 T* ArrayView::end() const [code]

Required functions to support range-for syntax. Allows you to iterate over all the items in the view as follows:

for (const T& item : view) {
    ...
}
template <typename T0, typename T1>
bool operator==(ArrayView<T0> a, ArrayView<T1> b) [code]
template <typename Arr0, typename Arr1, typename T0 = details::ArrayViewType<Arr0>, typename T1 = details::ArrayViewType<Arr1>>
bool operator==(Arr0&& a, Arr1&& b) [code]

Compares two array-like objects. a and b can have different item types. Returns true if and only if a and b have the same number of items and a[i] == b[i] evaluates to true at every index i.