File indexing completed on 2025-02-02 05:23:21

0001 /*
0002     SPDX-FileCopyrightText: 2016 Ivan Čukić <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef KACTIVITIES_STATS_QFLATSET_H
0008 #define KACTIVITIES_STATS_QFLATSET_H
0009 
0010 #include <QList>
0011 #include <QPair>
0012 
0013 namespace KActivities
0014 {
0015 template<typename T, typename LessThan>
0016 class QFlatSet : public QList<T>
0017 {
0018 public:
0019     QFlatSet()
0020     {
0021     }
0022 
0023     inline
0024         // QPair<typename QList<T>::iterator, bool> insert(const T &value)
0025         std::tuple<typename QList<T>::iterator, int, bool>
0026         insert(const T &value)
0027     {
0028         auto lessThan = LessThan();
0029         auto begin = this->begin();
0030         auto end = this->end();
0031 
0032         if (begin == end) {
0033             QList<T>::insert(0, value);
0034 
0035             return std::make_tuple(QList<T>::begin(), 0, true);
0036 
0037         } else {
0038             auto iterator = std::lower_bound(begin, end, value, lessThan);
0039 
0040             if (iterator != end) {
0041                 if (!lessThan(value, *iterator)) {
0042                     // Already present
0043                     return std::make_tuple(iterator, iterator - begin, false);
0044                 }
0045             }
0046 
0047             QList<T>::insert(iterator, value);
0048 
0049             return std::make_tuple(iterator, iterator - begin, true);
0050         }
0051     }
0052 
0053 private:
0054     QFlatSet(const QFlatSet &original); // = delete
0055 };
0056 
0057 } // namespace KActivities
0058 
0059 #endif // KACTIVITIES_STATS_QFLATSET_H