File indexing completed on 2024-04-28 03:53:08

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999, 2000, 2001 Carsten Pfeiffer <pfeiffer@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kcompletionmatches.h"
0009 
0010 #include <kcompletion.h>
0011 #include <kcompletion_p.h> // for KCompletionMatchesWrapper
0012 
0013 class KCompletionMatchesPrivate
0014 {
0015 public:
0016     KCompletionMatchesPrivate(bool sort, KCompletionMatches *parent)
0017         : sorting(sort)
0018         , q_ptr(parent)
0019     {
0020     }
0021 
0022     bool sorting;
0023     KCompletionMatches *const q_ptr;
0024 
0025     Q_DECLARE_PUBLIC(KCompletionMatches)
0026 };
0027 
0028 KCompletionMatches::KCompletionMatches(const KCompletionMatches &o)
0029     : KSortableList<QString, int>()
0030     , d_ptr(new KCompletionMatchesPrivate(o.sorting(), this))
0031 {
0032     *this = KCompletionMatches::operator=(o);
0033 }
0034 
0035 KCompletionMatches &KCompletionMatches::operator=(const KCompletionMatches &o)
0036 {
0037     Q_D(KCompletionMatches);
0038     if (*this == o) {
0039         return *this;
0040     }
0041     KCompletionMatchesList::operator=(o);
0042     d->sorting = o.sorting();
0043 
0044     return *this;
0045 }
0046 
0047 KCompletionMatches::KCompletionMatches(bool sort_P)
0048     : d_ptr(new KCompletionMatchesPrivate(sort_P, this))
0049 {
0050 }
0051 
0052 KCompletionMatches::KCompletionMatches(const KCompletionMatchesWrapper &matches)
0053     : d_ptr(new KCompletionMatchesPrivate(matches.sorting(), this))
0054 {
0055     if (matches.m_sortedListPtr) {
0056         KCompletionMatchesList::operator=(*matches.m_sortedListPtr);
0057     } else {
0058         const QStringList list = matches.list();
0059         reserve(list.size());
0060         std::transform(list.crbegin(), list.crend(), std::back_inserter(*this), [](const QString &str) {
0061             return KSortableItem<QString, int>(1, str);
0062         });
0063     }
0064 }
0065 
0066 KCompletionMatches::~KCompletionMatches()
0067 {
0068 }
0069 
0070 QStringList KCompletionMatches::list(bool sort_P) const
0071 {
0072     Q_D(const KCompletionMatches);
0073     if (d->sorting && sort_P) {
0074         const_cast<KCompletionMatches *>(this)->sort();
0075     }
0076     QStringList stringList;
0077     stringList.reserve(size());
0078     // high weight == sorted last -> reverse the sorting here
0079     std::transform(crbegin(), crend(), std::back_inserter(stringList), [](const KSortableItem<QString> &item) {
0080         return item.value();
0081     });
0082     return stringList;
0083 }
0084 
0085 bool KCompletionMatches::sorting() const
0086 {
0087     Q_D(const KCompletionMatches);
0088     return d->sorting;
0089 }
0090 
0091 void KCompletionMatches::removeDuplicates()
0092 {
0093     for (auto it1 = begin(); it1 != end(); ++it1) {
0094         auto it2 = it1;
0095         ++it2;
0096         while (it2 != end()) {
0097             if ((*it1).value() == (*it2).value()) {
0098                 // Use the max weight
0099                 (*it1).first = std::max((*it1).key(), (*it2).key());
0100                 it2 = erase(it2);
0101                 continue;
0102             }
0103             ++it2;
0104         }
0105     }
0106 }