AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ADeque.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13#include <deque>
   14#include "AUI/Core.h"
   15#include <algorithm>
   16#include "ASet.h"
   17#include <AUI/Traits/containers.h>
   18
   19
   24template <class StoredType,
   25    class Allocator = std::allocator<StoredType>>
   26class ADeque: public std::deque<StoredType, Allocator>
   27{
   28private:
   29    using p = std::deque<StoredType, Allocator>;
   30    using self = ADeque<StoredType, Allocator>;
   31public:
   32    using p::p;
   33    using iterator = typename p::iterator;
   34
   35
   42    template<typename OtherContainer>
   43    iterator insertAll(const OtherContainer& c) noexcept {
   44        return insertAll(p::end(), c);
   45    }
   46
   47
   55    template<typename OtherContainer>
   56    iterator insertAll(iterator at, const OtherContainer& c) noexcept {
   57        return p::insert(at, c.begin(), c.end());
   58    }
   59
   60
   65    void removeAll(const StoredType& item) noexcept
   66    {
   67        aui::container::remove_all(*this, item);
   68    }
   69
   74    void removeFirst(const StoredType& item) noexcept
   75    {
   76        aui::container::remove_first(*this, item);
   77    }
   78
   82    template<typename OtherContainer>
   83    bool isSubsetOf(const OtherContainer& c) const noexcept
   84    {
   85        return aui::container::is_subset(*this, c);
   86    }
   87
   91    bool contains(const StoredType& value) const noexcept {
   92        return aui::container::contains(*this, value);
   93    }
   94
   95
  101    inline self& operator<<(const StoredType& rhs) noexcept
  102    {
  103        p::push_back(rhs);
  104        return *this;
  105    }
  106
  112    inline self& operator<<(StoredType&& rhs) noexcept
  113    {
  114        p::push_back(std::move(rhs));
  115        return *this;
  116    }
  117
  123    template<typename OtherContainer>
  124    inline self& operator<<(const OtherContainer& c) noexcept
  125    requires (!std::is_convertible_v<OtherContainer, StoredType>) {
  126        insertAll(c);
  127        return *this;
  128    }
  129
  130
  138    StoredType& first() noexcept
  139    {
  140        AUI_ASSERTX(!p::empty(), "empty container could not have the first element");
  141        return p::front();
  142    }
  143
  151    const StoredType& first() const noexcept
  152    {
  153        AUI_ASSERTX(!p::empty(), "empty container could not have the first element");
  154        return p::front();
  155    }
  156
  164    StoredType& last() noexcept
  165    {
  166        AUI_ASSERTX(!p::empty(), "empty container could not have the last element");
  167        return p::back();
  168    }
  169
  177    const StoredType& last() const noexcept
  178    {
  179        AUI_ASSERTX(!p::empty(), "empty container could not have the last element");
  180        return p::back();
  181    }
  182
  187    [[nodiscard]]
  188    AOptional<size_t> indexOf(const StoredType& value) const noexcept
  189    {
  190        return aui::container::index_of(*this, value);
  191    }
  192
  193
  194    void sort() noexcept {
  195        std::sort(p::begin(), p::end());
  196    }
  197
  198    template<typename Comparator>
  199    void sort(Comparator&& comparator) noexcept {
  200        std::sort(p::begin(), p::end(), std::forward<Comparator>(comparator));
  201    }
  202
  211    void removeAt(size_t index) noexcept
  212    {
  213        aui::container::remove_at(*this, index);
  214    }
  215
  220    template<typename Predicate>
  221    void removeIf(Predicate&& predicate) noexcept
  222    {
  223        p::erase(std::remove_if(p::begin(), p::end(), std::forward<Predicate>(predicate)), p::end());
  224    }
  225
  226
  227    ASet<StoredType> toSet() const noexcept {
  228        return ASet<StoredType>(p::begin(), p::end());
  229    }
  230};
  231
  232template<typename T>
  233inline std::ostream& operator<<(std::ostream& o, const ADeque<T>& v) {
  234    if (v.empty()) {
  235        o << "[empty]";
  236    } else {
  237        o << "[ " << v.first();
  238        for (auto it = v.begin() + 1; it != v.end(); ++it) {
  239            o << ", " << *it;
  240        }
  241        o << " ]";
  242    }
  243
  244    return o;
  245}
  246
A std::deque with AUI extensions.
Definition ADeque.h:27
StoredType & last() noexcept
Definition ADeque.h:164
self & operator<<(const OtherContainer &c) noexcept
Definition ADeque.h:124
void removeAt(size_t index) noexcept
Definition ADeque.h:211
const StoredType & last() const noexcept
Definition ADeque.h:177
bool contains(const StoredType &value) const noexcept
Definition ADeque.h:91
self & operator<<(const StoredType &rhs) noexcept
Definition ADeque.h:101
self & operator<<(StoredType &&rhs) noexcept
Definition ADeque.h:112
iterator insertAll(iterator at, const OtherContainer &c) noexcept
Definition ADeque.h:56
iterator insertAll(const OtherContainer &c) noexcept
Definition ADeque.h:43
bool isSubsetOf(const OtherContainer &c) const noexcept
Definition ADeque.h:83
void removeAll(const StoredType &item) noexcept
Definition ADeque.h:65
void removeIf(Predicate &&predicate) noexcept
Definition ADeque.h:221
const StoredType & first() const noexcept
Definition ADeque.h:151
StoredType & first() noexcept
Definition ADeque.h:138
void removeFirst(const StoredType &item) noexcept
Definition ADeque.h:74
AOptional< size_t > indexOf(const StoredType &value) const noexcept
Definition ADeque.h:188
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition AOptional.h:33
A std::set with AUI extensions.
Definition ASet.h:25
bool is_subset(LContainer &l, RContainer &r) noexcept
Definition containers.h:205
AOptional< size_t > index_of(const Container &c, const typename Container::const_reference value) noexcept
Finds the index of the first occurrence of the value.
Definition containers.h:112
bool contains(const Container &c, const typename Container::const_reference value) noexcept
Definition containers.h:124
AOptional< std::size_t > remove_first(Container &container, typename Container::const_reference value) noexcept
Removes first occurrence of value.
Definition containers.h:172
void remove_at(Container &c, size_t index) noexcept
Removes element at the specified index.
Definition containers.h:98
void remove_all(Container &container, typename Container::const_reference value) noexcept
Removes all occurrences of value.
Definition containers.h:143
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74