Skip to content

ASmallVector#

Vector-like container consisting of few elements on stack and switches to dynamic allocation vector if needed.

Header:#include <AUI/Common/ASmallVector.h>
CMake:aui_link(my_target PUBLIC aui::core)

Detailed Description#

Vector-like container optimized for the case when it contains up to StaticVectorSize in place, avoiding dynamic allocation. In case when element count exceeds StaticVectorSize, ASmallVector seamlessly switches to traditional dynamic allocation vector.

Examples#

examples/app/game_of_life/src/main.cpp

Game of Life - Game of Life implementation that uses advanced large dynamic data rendering techniques such as ITexture, AImage to be GPU friendly. The computation is performed in AThreadPool.

        if (mTexture) {
            ctx.render.rectangle(ATexturedBrush { mTexture }, { 0, 0 }, float(SCALE) * glm::vec2(mCells->size()));
        }
        auto drawGrid = [&] {
            ASmallVector<std::pair<glm::vec2, glm::vec2>, 128 * 2> points;
            for (int i = 1; i < mCells->size().x; ++i) {
                points << std::make_pair(glm::vec2(i * SCALE, 0.f), glm::vec2(i * SCALE, getSize().y));
            }
            for (int i = 1; i < mCells->size().y; ++i) {
                points << std::make_pair(glm::vec2(0.f, i * SCALE), glm::vec2(getSize().x, i * SCALE));

Public Methods#

contains#


bool ASmallVector::contains(const StoredType& value)
Returns
true if container contains an element, false otherwise.

findIf#


template<aui::predicate<StoredType> Predicate >
StoredType* ASmallVector::findIf(Predicate&& predicate)

Finds element by predicate

Arguments
predicate
predicate
Returns
Pointer to the value on which the predicate returned true, nullptr otherwise

template<typename T, aui::mapper<const StoredType &, const T &> Projection >
StoredType* ASmallVector::findIf(const T& value, Projection&& projection)

Finds element by value

Arguments
value
value
projection
callable that transforms const StoredType& to const T&. Can be any operator() cappable object, including lambda and pointer-to-member.
Returns
Pointer to the value on which the predicate returned true, nullptr otherwise

first#


StoredType& ASmallVector::first()
Returns
the first element.
Sneaky assertions
Container is not empty.

Examples#

examples/ui/contacts/src/view/ContactDetailsView.cpp

AUI Contacts - Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.

namespace {
_<AView> profilePhoto(const _<Contact>& contact) {
    return Centered {
        Label {} & contact->displayName.readProjected([](const AString& s) {
            return s.empty() ? "?" : AString(1, s.first()).uppercase();
        }) AUI_WITH_STYLE { Opacity(0.5f), FontSize { 32_dp } },
    } AUI_WITH_STYLE {
        FixedSize { 64_dp },
        BorderRadius { 32_dp },
        BackgroundGradient { AColor::GRAY.lighter(0.5f), AColor::GRAY, 163_deg },
examples/7guis/cells/src/AST.cpp

7GUIs Cells - Spreadsheet processor (Excel).

            // should assign it to some operator
            for (const auto& o : binaryOperators | ranges::views::reverse) {
                if (o.op->right == nullptr) {
                    o.op->right = std::move(temporaryValue);
                    AUI_ASSERT(binaryOperators.first().owning != nullptr);
                    return std::move(binaryOperators.first().owning);
                }
            }
            throw AException {};
        }
examples/7guis/cells/src/main.cpp

7GUIs Cells - Spreadsheet processor (Excel).

                      [](const AString& v) -> _<AView> { return Label { "{}"_format(v) }; },
                      [](const formula::Range& v) -> _<AView> { return Label { "#RANGE?" }; },
                    },
                    v));
            connect(getViews().first()->clicked, me::inflateEditor);
        });
    }

    void inflateEditor() {
        mState->currentExpression = mCell.expression;

const StoredType& ASmallVector::first()
Returns
the first element.
Sneaky assertions
Container is not empty.

Examples#

examples/ui/contacts/src/view/ContactDetailsView.cpp

AUI Contacts - Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.

namespace {
_<AView> profilePhoto(const _<Contact>& contact) {
    return Centered {
        Label {} & contact->displayName.readProjected([](const AString& s) {
            return s.empty() ? "?" : AString(1, s.first()).uppercase();
        }) AUI_WITH_STYLE { Opacity(0.5f), FontSize { 32_dp } },
    } AUI_WITH_STYLE {
        FixedSize { 64_dp },
        BorderRadius { 32_dp },
        BackgroundGradient { AColor::GRAY.lighter(0.5f), AColor::GRAY, 163_deg },
examples/7guis/cells/src/AST.cpp

7GUIs Cells - Spreadsheet processor (Excel).

            // should assign it to some operator
            for (const auto& o : binaryOperators | ranges::views::reverse) {
                if (o.op->right == nullptr) {
                    o.op->right = std::move(temporaryValue);
                    AUI_ASSERT(binaryOperators.first().owning != nullptr);
                    return std::move(binaryOperators.first().owning);
                }
            }
            throw AException {};
        }
examples/7guis/cells/src/main.cpp

7GUIs Cells - Spreadsheet processor (Excel).

                      [](const AString& v) -> _<AView> { return Label { "{}"_format(v) }; },
                      [](const formula::Range& v) -> _<AView> { return Label { "#RANGE?" }; },
                    },
                    v));
            connect(getViews().first()->clicked, me::inflateEditor);
        });
    }

    void inflateEditor() {
        mState->currentExpression = mCell.expression;

fromRange#


template<aui::incrementable Iterator, aui::invocable<decltype(* std::declval<Iterator>())> UnaryOperation >
static auto ASmallVector::fromRange(aui::range<Iterator> range, UnaryOperation&& transformer)

Constructs a new vector of transformed items of the range.

Arguments
range
items to transform from.
transformer
transformer function.
Returns
A new vector.

indexOf#


AOptional<size_t> ASmallVector::indexOf(const StoredType& value)
Arguments
value
element to find.
Returns
index of the specified element. If element is not found, std::nullopt is returned.

insertAll#


template<typename OtherContainer >
iterator ASmallVector::insertAll(const OtherContainer& c)
Arguments
c
other container
Returns
iterator pointing to the first element inserted.

Inserts all values of the specified container to the end.


template<typename OtherContainer >
iterator ASmallVector::insertAll(OtherContainer&& c)
Arguments
c
other container
Returns
iterator pointing to the first element inserted.

Inserts all values of the specified container to the end.


template<typename OtherContainer >
iterator ASmallVector::insertAll(iterator at, const OtherContainer& c)
Arguments
at
position to insert at.
c
other container
Returns
iterator pointing to the first element inserted.

Inserts all values of the specified container.


template<typename OtherContainer >
iterator ASmallVector::insertAll(iterator at, OtherContainer&& c)
Arguments
at
position to insert at.
c
other container
Returns
iterator pointing to the first element inserted.

Inserts all values of the specified container.

isSubsetOf#


template<typename OtherContainer >
bool ASmallVector::isSubsetOf(const OtherContainer& c)
Returns
true if c container is a subset of this container, false otherwise.

last#


StoredType& ASmallVector::last()
Returns
the last element.
Sneaky assertions
Container is not empty.

Examples#

examples/7guis/cells/src/AST.cpp

7GUIs Cells - Spreadsheet processor (Excel).

            if (temporaryValue) {
                throw AException("SYNTAX");
            }
            if (!binaryOperators.empty()) {
                if (binaryOperators.last().op->right) {
                    throw AException {};
                }
                binaryOperators.last().op->right = std::move(node);
                return;
            }

const StoredType& ASmallVector::last()
Returns
the last element.
Sneaky assertions
Container is not empty.

Examples#

examples/7guis/cells/src/AST.cpp

7GUIs Cells - Spreadsheet processor (Excel).

            if (temporaryValue) {
                throw AException("SYNTAX");
            }
            if (!binaryOperators.empty()) {
                if (binaryOperators.last().op->right) {
                    throw AException {};
                }
                binaryOperators.last().op->right = std::move(node);
                return;
            }

removeAll#


void ASmallVector::removeAll(const StoredType& item)
Arguments
item
element to remove.

Removes all occurrences of item.


template<typename T, aui::mapper<const StoredType &, const T &> Projection >
void ASmallVector::removeAll(const T& item, Projection projection)
Arguments
item
element to remove.
projection
callable that transforms const StoredType& to const T&. Can be any operator() cappable object, including lambda and pointer-to-member.

Removes all occurrences of item with specified projection.

removeAt#


void ASmallVector::removeAt(size_t index)
Arguments
index
index of the element.

Removes element at the specified index.

Sneaky assertions
index points to the existing element.

removeFirst#


void ASmallVector::removeFirst(const StoredType& item)
Arguments
item
element to remove.

Removes first occurrence of item.

removeIf#


template<typename Predicate >
void ASmallVector::removeIf(Predicate&& predicate)
Arguments
predicate
predicate

Removes element if predicate(container[i]) == true.

size#


std::size_t ASmallVector::size()
Returns
Size of the container.

Examples#

examples/app/fractal/src/FractalView.cpp

Fractal Example - Fractal viewer application demonstrating usage of custom shaders.

    mTexture->bind();
    context.render.rectangle(ACustomShaderBrush {}, { 0, 0 }, getSize());
}

void FractalView::setSize(glm::ivec2 size) {
    AView::setSize(size);
    mShader.use();
    mShader.set(UNIFORM_RATIO, mAspectRatio = float(size.x) / float(size.y));
}
examples/app/game_of_life/src/main.cpp

Game of Life - Game of Life implementation that uses advanced large dynamic data rendering techniques such as ITexture, AImage to be GPU friendly. The computation is performed in AThreadPool.

using CellsImage = AFormattedImage<APixelFormat::RGBA_BYTE>;

class Cells : public AObject {
public:
    Cells(glm::ivec2 size) {
        mSize = size;
        for (auto s : { &mStorage, &mNextPopulation }) {
            s->resize(size.x * size.y);
        }
examples/7guis/cells/src/Spreadsheet.h

7GUIs Cells - Spreadsheet processor (Excel).

#include "AUI/Common/AException.h"

class Spreadsheet {
public:
    explicit Spreadsheet(glm::uvec2 size) : mSize(size) {
        mCells.resize(size.x * size.y);
        for (auto& v : mCells) {
            v = std::make_unique<Cell>();
            v->spreadsheet = this;
        }
examples/7guis/cells/src/main.cpp

7GUIs Cells - Spreadsheet processor (Excel).

            AGridSplitter::Builder()
                    .noDefaultSpacers()
                    .withItems([&] {
                        AVector<AVector<_<AView>>> views;
                        views.resize(mState->spreadsheet.size().y + 1);
                        for (auto& c : views) {
                            c.resize(mState->spreadsheet.size().x + 1);
                        }

                        views[0][0] = _new<AView>();   // blank
examples/7guis/cells/src/Functions.cpp

7GUIs Cells - Spreadsheet processor (Excel).

              return double(accumulator);
            } },
        { "IF",
            [](Ctx ctx) {
              if (ctx.args.size() != 3) {
                  throw AException("ARG");
              }
              auto condition = std::get_if<double>(&ctx.args[0]);
              if (condition == nullptr) {
                  throw AException("ARG0");