AVector#
An std::vector with AUI extensions.
| Header: | #include <AUI/Common/AVector.h> |
| CMake: | aui_link(my_target PUBLIC aui::core) |
Detailed Description#
AVector inherits std::vector, so it can be used wherever std::vector is expected, and adds shortcuts for
the operations that otherwise require the erase-remove idiom or an explicit loop.
Adding elements#
operator<< appends a value or all elements of another container and can be chained:
AVector<int> v = { 1, 2 };
v << 3 << 4; // { 1, 2, 3, 4 }
v << AVector<int>{ 5, 6 }; // { 1, 2, 3, 4, 5, 6 }
v.insertAll(otherContainer); // same, with an optional position
Searching and removing#
AVector<AString> names = { "Alice", "Bob", "Carol", "Bob" };
names.contains("Bob"); // true
AOptional<size_t> index = names.indexOf("Bob"); // 1
names.removeFirst("Bob"); // removes only the first one
names.removeAll("Bob"); // removes the rest
names.removeIf([](const AString& s) { return s.startsWith("C"); });
names.removeAt(0);
findIf returns a pointer to the first element matching a predicate or nullptr.
Transforming#
map produces a new vector of transformed elements, filter keeps the elements matching a predicate, toMap
builds an AMap and toSet an ASet:
AVector<int> numbers = { 1, 2, 3, 4 };
auto squares = numbers.map([](int i) { return i * i; }); // { 1, 4, 9, 16 }
auto even = numbers.filter([](int i) { return i % 2 == 0; }); // { 2, 4 }
auto strings = numbers.map([](int i) { return AString::number(i); }); // AVector<AString>
Accessing#
first() and last() return references to the boundary elements (asserting that the vector is not empty), and
at() throws an AUI exception instead of invoking undefined behaviour when the index is out of range.
Examples#
examples/7guis/cells/src/Tokens.h
7GUIs Cells — Spreadsheet processor (Excel).
};
using Any = std::variant<Identifier, Double, Semicolon, LPar, RPar, Colon, Plus, Minus, Asterisk, Slash, LAngle, RAngle, StringLiteral>;
AVector<token::Any> parse(aui::no_escape<ATokenizer> t);
} // namespace token
examples/7guis/crud/src/main.cpp
7GUIs CRUD — Create/Read/Update/Delete example.
} AUI_OVERRIDE_STYLE { LayoutSpacing { 4_dp } });
}
private:
AProperty<AVector<_<User>>> mUsers;
User mEditedUser;
AProperty<_<User>> mSelectedUser;
AProperty<AString> mFilterPrefix;
APropertyPrecomputed<bool> mCreateEnabled = [this] { return !(mEditedUser.surname->empty() || mEditedUser.name->empty()); };
APropertyPrecomputed<bool> mDeleteEnabled = [this] { return mSelectedUser != nullptr; };
examples/app/minesweeper/src/MinesweeperWindow.h
Minesweeper Game — Minesweeper game implementation driven by ass.
int mBombs;
int mOpenedCells;
_<AViewContainer> mGrid;
AVector<FieldCell> mField;
void openCell(int x, int y, bool doGameLoseIfBomb);
int countBombsAround(int x, int y);
bool isValidCell(int x, int y) { return x >= 0 && x < mFieldColumns && y >= 0 && y < mFieldRows; }
FieldCell& fieldAt(int x, int y) { return mField[mFieldColumns * y + x]; }
examples/app/notes/src/main.cpp
Notes App — Note taking app that demonstrates usage of AListModel, AProperty, user data saving and loading.
examples/ui/contacts/src/main.cpp
AUI Contacts — Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.
});
}
private:
AProperty<AVector<_<Contact>>> mContacts =
predefined::PERSONS | ranges::views::transform([](Contact& p) { return _new<Contact>(std::move(p)); }) |
ranges::to_vector | CONTACTS_SORT;
APropertyPrecomputed<std::size_t> mContactCount = [this] { return mContacts->size(); };
AProperty<_<Contact>> mSelectedContact = nullptr;
AProperty<AString> mSearchQuery;
examples/ui/views/src/ExampleWindow.cpp
Views Example — All-in-one views building example.
GroupBox {
Label { "AForEachUI" },
[this] { // lambda style inlining
struct State {
AProperty<AVector<AColor>> colors;
};
auto state = _new<State>();
return Vertical {
Horizontal {
examples/ui/infinite_lazy_list/src/main.cpp
Infinite Lazy List — Usage of AUI_DECLARATIVE_FOR to make an infinite lazy list.
examples/7guis/cells/src/Functions.h
7GUIs Cells — Spreadsheet processor (Excel).
examples/7guis/cells/src/Tokens.cpp
7GUIs Cells — Spreadsheet processor (Excel).
*/
#include "Tokens.h"
AVector<token::Any> token::parse(aui::no_escape<ATokenizer> t) {
AVector<token::Any> out;
t->readChar(); // =
try {
while (!t->isEof()) {
switch (char c = t->readChar()) {
examples/7guis/cells/src/AST.cpp
7GUIs Cells — Spreadsheet processor (Excel).
int priority = -1;
_unique<BinaryOperatorNode> owning;
};
AVector<BinaryOperatorAndItsPriority> binaryOperators;
auto putValue = [&](_unique<INode> node) {
if (temporaryValue) {
throw AException("SYNTAX");
}
examples/7guis/cells/src/Spreadsheet.h
7GUIs Cells — Spreadsheet processor (Excel).
examples/7guis/cells/src/main.cpp
7GUIs Cells — Spreadsheet processor (Excel).
this,
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);
}
Public Methods#
contains#
- Returns
- true if container contains an element, false otherwise.
Examples:
findIf#
Finds element by predicate
- Arguments
predicatepredicate- Returns
- Pointer to the value on which the predicate returned true, nullptr otherwise
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 {
AUI_REACT(contact->displayName->empty() ? "?" : AString(1, contact->displayName->first()).uppercase())
} AUI_OVERRIDE_STYLE { Opacity(0.5f), FontSize { 32_dp } },
} AUI_OVERRIDE_STYLE {
FixedSize { 64_dp },
BorderRadius { 32_dp },
BackgroundGradient { AColor::GRAY.lighter(0.5f), AColor::GRAY, 163_deg },
- Returns
- the first element.
- Sneaky assertions
- Container is not empty.
Examples:
fromRange#
static auto AVector::fromRange(aui::range<Iterator> range, UnaryOperation&& transformer)
Constructs a new vector of transformed items of the range.
- Arguments
rangeitems to transform from.transformertransformer function.- Returns
- A new vector.
indexOf#
AOptional<size_t> AVector::indexOf(const StoredType& value)
- Arguments
valueelement to find.- Returns
- index of the specified element. If element is not found, std::nullopt is returned.
insertAll#
- Arguments
cother container- Returns
- iterator pointing to the first element inserted.
Inserts all values of the specified container to the end.
Examples:
- Arguments
cother container- Returns
- iterator pointing to the first element inserted.
Inserts all values of the specified container to the end.
Examples:
template<typename OtherContainer >
iterator AVector::insertAll(iterator at, const OtherContainer& c)
- Arguments
atposition to insert at.cother container- Returns
- iterator pointing to the first element inserted.
Inserts all values of the specified container.
Examples:
- Arguments
atposition to insert at.cother container- Returns
- iterator pointing to the first element inserted.
Inserts all values of the specified container.
Examples:
isSubsetOf#
- Returns
- true if
ccontainer is a subset of this container, false otherwise.
last#
- Returns
- the last element.
- Sneaky assertions
- Container is not empty.
Examples:
- Returns
- the last element.
- Sneaky assertions
- Container is not empty.
Examples:
removeAll#
- Arguments
itemelement to remove.
Removes all occurrences of item.
template<typename T, aui::mapper<const StoredType &, const T &> Projection >
void AVector::removeAll(const T& item, Projection projection)
- Arguments
itemelement to remove.projectioncallable that transformsconst StoredType&toconst T&. Can be any operator() cappable object, including lambda and pointer-to-member.
Removes all occurrences of item with specified projection.
removeAt#
void AVector::removeAt(size_t index)
- Arguments
Removes element at the specified index.
- Sneaky assertions
indexpoints to the existing element.
removeFirst#
AOptional<std::size_t> AVector::removeFirst(const StoredType& item)
- Arguments
itemelement to remove.- Returns
- If the item is removed, it's index returned.
Removes first occurrence of item.
Examples:
removeIf#
template<aui::predicate<StoredType> Predicate >
void AVector::removeIf(Predicate&& predicate)
- Arguments
predicatepredicate
Removes element if predicate(container[i]) == true.
removeIfFirst#
template<aui::predicate<StoredType> Predicate >
void AVector::removeIfFirst(Predicate&& predicate)
- Arguments
predicatepredicate
Removes only the first element if predicate(container[i]) == true.