Advertisement
overloop

mysort.cpp

Feb 5th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1.  
  2. #include <QStringList>
  3. #include <QPair>
  4. #include <QVariant>
  5.  
  6. namespace MySort {
  7.  
  8. typedef QPair<QList<QVariant>,int> Item; //(int|string)* sequence, value index
  9. typedef bool (*lessThan)(const Item&, const Item&);
  10.  
  11. template <class T> class Helper {
  12. public:
  13.     Helper(QStringList& keys, QList<T>& values) :  m_values(values) { initKeys(keys); }
  14.     Helper(QList<Item>& keys, QList<T>& values) : m_keys(keys), m_values(values) {}
  15.     void initKeys(const QStringList& keys) {
  16.         int n = keys.size();
  17.         // foreach keys
  18.         for(int i=0;i<n;i++) {
  19.             QString key = keys.at(i);
  20.             QList<QVariant> item;
  21.             QString intPart;
  22.             QString charPart;
  23.             int l = key.size();
  24.             // char-by-char parse
  25.             for (int j=0;j<l;j++) {
  26.                 QChar c = key.at(j);
  27.                 if (c.isDigit()) {
  28.                     intPart.append(c);
  29.                     if (!charPart.isEmpty()) {
  30.                         item.append(charPart);
  31.                         charPart = QString();
  32.                     }
  33.                 } else {
  34.                     charPart.append(c);
  35.                     if (!intPart.isEmpty()) {
  36.                         item.append(intPart.toInt());
  37.                         intPart = QString();
  38.                     }
  39.                 }
  40.             }
  41.             // flush
  42.             if (!charPart.isEmpty())
  43.                 item.append(charPart);
  44.             if (!intPart.isEmpty())
  45.                 item.append(intPart.toInt());
  46.             m_keys.append(Item(item,i));
  47.         }
  48.     }
  49.     void sort(lessThan theLessThan) {
  50.         qSort(m_keys.begin(),m_keys.end(),theLessThan);
  51.         QList<T> m_sortedValues;
  52.         Item item;
  53.         foreach(item,m_keys) {
  54.             int index = item.second;
  55.             m_sortedValues.append(m_values.at(index));
  56.         }
  57.         m_values = m_sortedValues; // hmmm...
  58.     }
  59.  
  60. private:
  61.     QList<Item> m_keys;
  62.     QList<T>& m_values;
  63. };
  64.  
  65.  
  66. bool firstPartLessThan(const Item& item1, const Item& item2) {
  67.     if (item1.first.size()<1)
  68.         return true;
  69.     if (item2.first.size()<1)
  70.         return false;
  71.     const QVariant& v1 = item1.first.at(0);
  72.     const QVariant& v2 = item2.first.at(0);
  73.     if (v1.type() == QVariant::Int && v2.type() == QVariant::Int) {
  74.         return v1.toInt() < v2.toInt();
  75.     } else if (v1.type() == QVariant::String && v2.type() == QVariant::String) {
  76.         return v1.toString() < v2.toString();
  77.     } else if (v1.type() == QVariant::Int) {
  78.         return true;
  79.     } else if (v2.type() == QVariant::Int) {
  80.         return false;
  81.     }
  82.     return false;
  83. }
  84.  
  85. }
  86.  
  87. #include <QDebug>
  88. #include <time.h>
  89.  
  90. int main(int,char**) {
  91.  
  92.     qsrand(time(0));
  93.  
  94.     QStringList values;
  95.     QStringList keys;
  96.  
  97.     QString chars = "01234567890abcdefgh";
  98.     int chars_size = chars.size();
  99.  
  100.     //generate 20 random strings
  101.     for (int i=0;i<20;i++) {
  102.         QString key;
  103.         // length 5..10
  104.         int l = qrand() % 5 + 5;
  105.         for (int j=0;j<l;j++)
  106.             key.append(chars.at(qrand() % chars_size));
  107.         keys << key;
  108.         values << key;
  109.     }
  110.  
  111.     MySort::Helper<QString> helper(keys,values);
  112.     helper.sort(&MySort::firstPartLessThan);
  113.  
  114.     QString value;
  115.     foreach (value,values) {
  116.         qDebug() << value;
  117.     }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement