/* This file is part of the KDE libraries SPDX-FileCopyrightText: 2001 Carsten Pfeiffer SPDX-License-Identifier: LGPL-2.0-or-later */ #ifndef KSORTABLELIST_H #define KSORTABLELIST_H #include #include #include #include /*! * \class KSortableItem * \inheaderfile KSortableList * \inmodule KCompletion * * \brief KSortableItem is a QPair that provides several operators * for sorting. * \sa KSortableList */ template class KSortableItem : public QPair { public: /*! * Creates a new KSortableItem with the given values. * * \a i the first value (the key) * * \a t the second value (the item) */ KSortableItem(Key i, const T &t) : QPair(i, t) { } /*! * Creates a new KSortableItem that copies another one. * * \a rhs the other item to copy */ KSortableItem(const KSortableItem &rhs) : QPair(rhs.first, rhs.second) { } /*! * Creates a new KSortableItem with uninitialized values. */ KSortableItem() { } /*! * Assignment operator, just copies the item. */ KSortableItem &operator=(const KSortableItem &i) { this->first = i.first; this->second = i.second; return *this; } // operators for sorting /*! * Compares the two items. This implementation only compares * the first value. */ bool operator>(const KSortableItem &i2) const { return (i2.first < this->first); } /*! * Compares the two items. This implementation only compares * the first value. */ bool operator<(const KSortableItem &i2) const { return (this->first < i2.first); } /*! * Compares the two items. This implementation only compares * the first value. */ bool operator>=(const KSortableItem &i2) const { return (this->first >= i2.first); } /*! * Compares the two items. This implementation only compares * the first value. */ bool operator<=(const KSortableItem &i2) const { return !(i2.first < this->first); } /*! * Compares the two items. This implementation only compares * the first value. */ bool operator==(const KSortableItem &i2) const { return (this->first == i2.first); } /*! * Compares the two items. This implementation only compares * the first value. */ bool operator!=(const KSortableItem &i2) const { return (this->first != i2.first); } /*! * Returns the second value (the item) */ T &value() { return this->second; } /*! * Returns the second value (the item) */ const T &value() const { return this->second; } /*! * Returns the first value. */ Key key() const { return this->first; } }; /*! * \class KSortableList * \inmodule KCompletion * * \brief KSortableList is a QList which associates a key with each item in the list. * This key is used for sorting when calling sort(). * * This allows to temporarily calculate a key and use it for sorting, without having * to store that key in the items, or calculate that key many times for the same item * during sorting if that calculation is expensive. */ template class KSortableList : public QList> { public: /*! * Insert a KSortableItem with the given values. * * \a i the first value * * \a t the second value */ void insert(Key i, const T &t) { QList>::append(KSortableItem(i, t)); } // add more as you please... /*! * Returns the first value of the KSortableItem at the given position. */ T &operator[](Key i) { return QList>::operator[](i).value(); } /*! * Returns the first value of the KSortableItem at the given position. */ const T &operator[](Key i) const { return QList>::operator[](i).value(); } /*! * Sorts the KSortableItems. */ void sort() { std::sort(this->begin(), this->end()); } }; #endif // KSORTABLELIST_H