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

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2001 Carsten Pfeiffer <pfeiffer@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KSORTABLELIST_H
0009 #define KSORTABLELIST_H
0010 
0011 #include <kcompletion_export.h>
0012 
0013 #include <QList>
0014 #include <QPair>
0015 #include <algorithm>
0016 
0017 /**
0018  * \class KSortableItem ksortablelist.h <KSortableItem>
0019  *
0020  * KSortableItem is a QPair that provides several operators
0021  * for sorting.
0022  * @see KSortableList
0023  */
0024 template<typename T, typename Key = int>
0025 class KSortableItem : public QPair<Key, T>
0026 {
0027 public:
0028     /**
0029      * Creates a new KSortableItem with the given values.
0030      * @param i the first value (the key)
0031      * @param t the second value (the item)
0032      */
0033     KSortableItem(Key i, const T &t)
0034         : QPair<Key, T>(i, t)
0035     {
0036     }
0037     /**
0038      * Creates a new KSortableItem that copies another one.
0039      * @param rhs the other item to copy
0040      */
0041     KSortableItem(const KSortableItem<T, Key> &rhs)
0042         : QPair<Key, T>(rhs.first, rhs.second)
0043     {
0044     }
0045 
0046     /**
0047      * Creates a new KSortableItem with uninitialized values.
0048      */
0049     KSortableItem()
0050     {
0051     }
0052 
0053     /**
0054      * Assignment operator, just copies the item.
0055      */
0056     KSortableItem<T, Key> &operator=(const KSortableItem<T, Key> &i)
0057     {
0058         this->first = i.first;
0059         this->second = i.second;
0060         return *this;
0061     }
0062 
0063     // operators for sorting
0064     /**
0065      * Compares the two items. This implementation only compares
0066      * the first value.
0067      */
0068     bool operator>(const KSortableItem<T, Key> &i2) const
0069     {
0070         return (i2.first < this->first);
0071     }
0072     /**
0073      * Compares the two items. This implementation only compares
0074      * the first value.
0075      */
0076     bool operator<(const KSortableItem<T, Key> &i2) const
0077     {
0078         return (this->first < i2.first);
0079     }
0080     /**
0081      * Compares the two items. This implementation only compares
0082      * the first value.
0083      */
0084     bool operator>=(const KSortableItem<T, Key> &i2) const
0085     {
0086         return (this->first >= i2.first);
0087     }
0088     /**
0089      * Compares the two items. This implementation only compares
0090      * the first value.
0091      */
0092     bool operator<=(const KSortableItem<T, Key> &i2) const
0093     {
0094         return !(i2.first < this->first);
0095     }
0096     /**
0097      * Compares the two items. This implementation only compares
0098      * the first value.
0099      */
0100     bool operator==(const KSortableItem<T, Key> &i2) const
0101     {
0102         return (this->first == i2.first);
0103     }
0104     /**
0105      * Compares the two items. This implementation only compares
0106      * the first value.
0107      */
0108     bool operator!=(const KSortableItem<T, Key> &i2) const
0109     {
0110         return (this->first != i2.first);
0111     }
0112 
0113     /**
0114      * @return the second value (the item)
0115      */
0116     T &value()
0117     {
0118         return this->second;
0119     }
0120 
0121     /**
0122      * @return the second value (the item)
0123      */
0124     const T &value() const
0125     {
0126         return this->second;
0127     }
0128 
0129     /**
0130      * @return the first value.
0131      */
0132     Key key() const
0133     {
0134         return this->first;
0135     }
0136 };
0137 
0138 /**
0139  * \class KSortableList ksortablelist.h <KSortableList>
0140  *
0141  * KSortableList is a QList which associates a key with each item in the list.
0142  * This key is used for sorting when calling sort().
0143  *
0144  * This allows to temporarily calculate a key and use it for sorting, without having
0145  * to store that key in the items, or calculate that key many times for the same item
0146  * during sorting if that calculation is expensive.
0147  */
0148 template<typename T, typename Key = int>
0149 class KSortableList : public QList<KSortableItem<T, Key>>
0150 {
0151 public:
0152     /**
0153      * Insert a KSortableItem with the given values.
0154      * @param i the first value
0155      * @param t the second value
0156      */
0157     void insert(Key i, const T &t)
0158     {
0159         QList<KSortableItem<T, Key>>::append(KSortableItem<T, Key>(i, t));
0160     }
0161     // add more as you please...
0162 
0163     /**
0164      * Returns the first value of the KSortableItem at the given position.
0165      * @return the first value of the KSortableItem
0166      */
0167     T &operator[](Key i)
0168     {
0169         return QList<KSortableItem<T, Key>>::operator[](i).value();
0170     }
0171 
0172     /**
0173      * Returns the first value of the KSortableItem at the given position.
0174      * @return the first value of the KSortableItem
0175      */
0176     const T &operator[](Key i) const
0177     {
0178         return QList<KSortableItem<T, Key>>::operator[](i).value();
0179     }
0180 
0181     /**
0182      * Sorts the KSortableItems.
0183      */
0184     void sort()
0185     {
0186         std::sort(this->begin(), this->end());
0187     }
0188 };
0189 
0190 #endif // KSORTABLELIST_H