AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ASet.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
   14#include <set>
   15#include <ostream>
   16#include <AUI/Traits/containers.h>
   17
   18
   23template <class KeyType, class Comparator = std::less<KeyType>, class Allocator = std::allocator<KeyType>>
   24class ASet: public std::set<KeyType, Comparator, Allocator>
   25{
   26private:
   27    using p = std::set<KeyType, Comparator, Allocator>;
   28    using self = ASet<KeyType, Comparator, Allocator>;
   29
   30public:
   31
   32    using p::p;
   33    using iterator = typename p::iterator;
   34
   35
   41    template<typename OtherContainer>
   42    void insertAll(const OtherContainer& c) noexcept {
   43        p::insert(c.begin(), c.end());
   44    }
   45
   49    template<typename OtherContainer>
   50    bool isSubsetOf(const OtherContainer& c) const noexcept
   51    {
   52        return aui::container::is_subset(*this, c);
   53    }
   54
   60    inline self& operator<<(const KeyType& rhs) noexcept
   61    {
   62        p::insert(rhs);
   63        return *this;
   64    }
   65
   71    inline self& operator<<(KeyType&& rhs) noexcept
   72    {
   73        p::insert(std::move(rhs));
   74        return *this;
   75    }
   76
   81    inline self& operator>>(const KeyType& rhs) noexcept
   82    {
   83        p::erase(rhs);
   84        return *this;
   85    }
   86
   92    template<typename OtherContainer, std::enable_if_t<!std::is_convertible_v<OtherContainer, KeyType>, bool> = true>
   93    inline self& operator<<(const OtherContainer& c) noexcept
   94    {
   95        insertAll(c);
   96        return *this;
   97    }
   98
   99    bool contains(const KeyType& value) const noexcept
  100    {
  101        return p::find(value) != p::end();
  102    }
  103};
  104
  105
  106template<typename T>
  107inline std::ostream& operator<<(std::ostream& o, const ASet<T>& v) {
  108    if (v.empty()) {
  109        o << "[empty]";
  110    } else {
  111        auto it = v.begin();
  112        o << "[ " << *it;
  113        ++it;
  114        for (; it != v.end(); ++it) {
  115            o << ", " << *it;
  116        }
  117        o << " ]";
  118    }
  119
  120    return o;
  121}
  122
A std::set with AUI extensions.
Definition ASet.h:25
self & operator<<(const KeyType &rhs) noexcept
Definition ASet.h:60
bool isSubsetOf(const OtherContainer &c) const noexcept
Definition ASet.h:50
self & operator<<(const OtherContainer &c) noexcept
Definition ASet.h:93
self & operator>>(const KeyType &rhs) noexcept
Definition ASet.h:81
self & operator<<(KeyType &&rhs) noexcept
Definition ASet.h:71
void insertAll(const OtherContainer &c) noexcept
Definition ASet.h:42
bool is_subset(LContainer &l, RContainer &r) noexcept
Definition containers.h:205