diff -r 9ca53009bc5c -r cf9a854b56a9 src/functional.h --- a/src/functional.h Tue Jun 28 19:36:03 2022 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,190 +0,0 @@ -/* - * LDForge: LDraw parts authoring CAD - * Copyright (C) 2020 Teemu Piippo - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once -#include -#include -#include -#include -#include - -namespace fn -{ - template - class InsertIterator; -} - -/** - * Like std::back_inserter but calls x.insert(v) instead - */ -template -class fn::InsertIterator -{ -public: - InsertIterator(C& container) : - container{container} {} - auto operator*() - { - return *this; - } - auto operator++() - { - return *this; - } - auto operator++(int) - { - return *this; - } - template - auto operator=(T&& value) - { - this->container.insert(std::forward(value)); - return *this; - } -private: - C& container; -}; - -namespace fn -{ - // Constructs a fn::InsertIterator - template - InsertIterator makeInsertIterator(C& container) - { - return InsertIterator{container}; - } - - // Constructs a back_inserter for std::vector - template - auto makeDefaultInserter(std::vector& vec) - { - return std::back_inserter(vec); - } - -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - // In Qt6 QVector is now a QList instead, so we need to disable this - // Constructs a back_inserter for QVector - template - auto makeDefaultInserter(QVector& vec) - { - return std::back_inserter(vec); - } -#endif - - template - auto makeDefaultInserter(QList& vec) - { - return std::back_inserter(vec); - } - - // Constructs an fn::InsertIterator for QSet - template - auto makeDefaultInserter(QSet& vec) - { - return InsertIterator(vec); - } - - // Changes the value type of C - template - struct ChangeContainerValueType - { - }; - - // Changes the value type of std::vector - template - struct ChangeContainerValueType, TT> - { - using type = std::vector; - }; - -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - // Changes the value type of QVector - template - struct ChangeContainerValueType, TT> - { - using type = QVector; - }; -#endif - - // Changes the value type of QSet - template - struct ChangeContainerValueType, TT> - { - using type = QSet; - }; - - // Changes the value type of QList - template - struct ChangeContainerValueType, TT> - { - using type = QList; - }; - - // Changes the value type of C - template - using ChangeContainerValueType_t = typename ChangeContainerValueType::type; - - /** - * \brief Applies \c function to all elements of \c container - * \param container Container to iterate - * \param function Function to apply - * \param Rt result type. If not provided, a suitable result type is deduced from inputs - * \returns mapped result container - */ - template - auto map(C&& container, Fn&& function) - { - using value_t = decltype(*std::declval().begin()); - using newvalue_t = std::result_of_t; - using result_t = std::conditional_t< - std::is_same_v, - ChangeContainerValueType_t, newvalue_t>, - Rt>; - result_t result; - result.reserve(std::end(container) - std::begin(container)); - std::transform(std::begin(container), std::end(container), makeDefaultInserter(result), function); - return result; - } -} - -template -bool any(T&& container, Fn&& f) -{ - for (auto&& x : container) - { - if (f(x)) - { - return true; - } - } - return false; -} - - -template -bool all(T&& container, Fn&& f) -{ - for (auto&& x : container) - { - if (not f(x)) - { - return false; - } - } - return true; -}