| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 1999, 2000, 2001 Carsten Pfeiffer <pfeiffer@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "kcompletionmatches.h" |
| 9 | |
| 10 | #include <kcompletion.h> |
| 11 | #include <kcompletion_p.h> // for KCompletionMatchesWrapper |
| 12 | |
| 13 | class KCompletionMatchesPrivate |
| 14 | { |
| 15 | public: |
| 16 | KCompletionMatchesPrivate(bool sort, KCompletionMatches *parent) |
| 17 | : sorting(sort) |
| 18 | , q_ptr(parent) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | bool sorting; |
| 23 | KCompletionMatches *const q_ptr; |
| 24 | |
| 25 | Q_DECLARE_PUBLIC(KCompletionMatches) |
| 26 | }; |
| 27 | |
| 28 | KCompletionMatches::KCompletionMatches(const KCompletionMatches &o) |
| 29 | : KSortableList<QString, int>() |
| 30 | , d_ptr(new KCompletionMatchesPrivate(o.sorting(), this)) |
| 31 | { |
| 32 | *this = KCompletionMatches::operator=(o); |
| 33 | } |
| 34 | |
| 35 | KCompletionMatches &KCompletionMatches::operator=(const KCompletionMatches &o) |
| 36 | { |
| 37 | Q_D(KCompletionMatches); |
| 38 | if (*this == o) { |
| 39 | return *this; |
| 40 | } |
| 41 | KCompletionMatchesList::operator=(o); |
| 42 | d->sorting = o.sorting(); |
| 43 | |
| 44 | return *this; |
| 45 | } |
| 46 | |
| 47 | KCompletionMatches::KCompletionMatches(bool sort_P) |
| 48 | : d_ptr(new KCompletionMatchesPrivate(sort_P, this)) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | KCompletionMatches::KCompletionMatches(const KCompletionMatchesWrapper &matches) |
| 53 | : d_ptr(new KCompletionMatchesPrivate(matches.sorting(), this)) |
| 54 | { |
| 55 | if (matches.m_sortedListPtr) { |
| 56 | KCompletionMatchesList::operator=(*matches.m_sortedListPtr); |
| 57 | } else { |
| 58 | const QStringList list = matches.list(); |
| 59 | reserve(asize: list.size()); |
| 60 | std::transform(first: list.crbegin(), last: list.crend(), result: std::back_inserter(x&: *this), unary_op: [](const QString &str) { |
| 61 | return KSortableItem<QString, int>(1, str); |
| 62 | }); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | KCompletionMatches::~KCompletionMatches() |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | QStringList KCompletionMatches::list(bool sort_P) const |
| 71 | { |
| 72 | Q_D(const KCompletionMatches); |
| 73 | if (d->sorting && sort_P) { |
| 74 | const_cast<KCompletionMatches *>(this)->sort(); |
| 75 | } |
| 76 | QStringList stringList; |
| 77 | stringList.reserve(asize: size()); |
| 78 | // high weight == sorted last -> reverse the sorting here |
| 79 | std::transform(first: crbegin(), last: crend(), result: std::back_inserter(x&: stringList), unary_op: [](const KSortableItem<QString> &item) { |
| 80 | return item.value(); |
| 81 | }); |
| 82 | return stringList; |
| 83 | } |
| 84 | |
| 85 | bool KCompletionMatches::sorting() const |
| 86 | { |
| 87 | Q_D(const KCompletionMatches); |
| 88 | return d->sorting; |
| 89 | } |
| 90 | |
| 91 | void KCompletionMatches::removeDuplicates() |
| 92 | { |
| 93 | for (auto it1 = begin(); it1 != end(); ++it1) { |
| 94 | auto it2 = it1; |
| 95 | ++it2; |
| 96 | while (it2 != end()) { |
| 97 | if ((*it1).value() == (*it2).value()) { |
| 98 | // Use the max weight |
| 99 | (*it1).first = std::max(a: (*it1).key(), b: (*it2).key()); |
| 100 | it2 = erase(pos: it2); |
| 101 | continue; |
| 102 | } |
| 103 | ++it2; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |