File indexing completed on 2024-04-07 03:51:19

0001 /*
0002     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003     SPDX-FileContributor: Stephen Kelly <stephen@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kbihash_p.h"
0009 
0010 #include <QBuffer>
0011 #include <QCoreApplication>
0012 #include <QDataStream>
0013 #include <QDebug>
0014 #include <QString>
0015 
0016 int main(int argc, char **argv)
0017 {
0018     QCoreApplication app(argc, argv);
0019     KBiHash<int, QString> biHash;
0020     biHash.insert(5, "5.5");
0021     biHash.insert(6, "6.6");
0022     biHash.insert(7, "7.7");
0023     biHash.insert(8, "8.8");
0024 
0025     qDebug() << "left to right";
0026     KBiHash<int, QString>::left_iterator it1 = biHash.leftBegin();
0027     while (it1 != biHash.leftEnd()) {
0028         qDebug() << it1.key() << it1.value();
0029         if (it1.key() == 7) {
0030             qDebug() << "erase" << it1.key() << ":" << it1.value();
0031             it1 = biHash.eraseLeft(it1);
0032         } else {
0033             ++it1;
0034         }
0035     }
0036     qDebug() << "right to left";
0037     KBiHash<int, QString>::right_const_iterator it2 = biHash.rightConstBegin();
0038     const KBiHash<int, QString>::right_const_iterator end2 = biHash.rightConstEnd();
0039     while (it2 != end2) {
0040         qDebug() << it2.key() << it2.value();
0041         ++it2;
0042     }
0043 
0044     KBiHash<int, QString>::right_iterator it3 = biHash.rightBegin();
0045     while (it3 != biHash.rightEnd()) {
0046         if (it3.value() == 5) {
0047             const int newValue = 55;
0048             qDebug() << "update" << it3.key() << ":" << it3.value() << "to" << newValue;
0049             biHash.updateLeft(it3, newValue);
0050         }
0051         ++it3;
0052     }
0053 
0054     KBiHash<int, QString>::left_iterator it4 = biHash.leftBegin();
0055     while (it4 != biHash.leftEnd()) {
0056         if (it4.value() == "6.6") {
0057             const QLatin1String newValue("66.66");
0058             qDebug() << "update" << it4.key() << ":" << it4.value() << "to" << newValue;
0059             biHash.updateRight(it4, newValue);
0060         }
0061         ++it4;
0062     }
0063 
0064     KBiHash<int, QString>::right_const_iterator it5 = biHash.constFindRight("5.5");
0065     qDebug() << "found" << it5.key() << it5.value();
0066 
0067     KBiHash<int, QString>::left_const_iterator it6 = biHash.constFindLeft(6);
0068     qDebug() << "found" << it6.key() << it6.value();
0069 
0070     qDebug() << "subscript operator" << biHash[8];
0071 
0072     qDebug() << "left to right";
0073     KBiHash<int, QString>::left_iterator it7 = biHash.leftBegin();
0074     while (it7 != biHash.leftEnd()) {
0075         qDebug() << it7.key() << it7.value();
0076         if (it7.key() == 7) {
0077             it7 = biHash.eraseLeft(it7);
0078         } else {
0079             ++it7;
0080         }
0081     }
0082 
0083     KBiHash<int, QString> biHash2;
0084     biHash2.insert(8, "8.8");
0085     biHash2.insert(9, "9.9");
0086     biHash2.insert(10, "10.10");
0087     biHash2.insert(11, "11.11");
0088 
0089     qDebug() << biHash2;
0090     qDebug() << biHash;
0091 
0092     KBiHash<int, QString> biHash3 = biHash.unite(biHash2);
0093 
0094     qDebug() << biHash;
0095     qDebug() << biHash3;
0096 
0097     QByteArray ba;
0098     QBuffer outBuffer(&ba);
0099     outBuffer.open(QIODevice::WriteOnly);
0100     QDataStream out(&outBuffer);
0101     out << biHash;
0102     outBuffer.close();
0103 
0104     KBiHash<int, QString> biHash4;
0105 
0106     QBuffer inBuffer(&ba);
0107     inBuffer.open(QIODevice::ReadOnly);
0108     QDataStream in(&inBuffer);
0109 
0110     in >> biHash4;
0111 
0112     qDebug() << biHash4;
0113 
0114     qDebug() << (biHash == biHash4) << (biHash != biHash4);
0115 
0116     QHash<int, QString> hash;
0117     hash.insert(1, "1");
0118     hash.insert(2, "2");
0119     hash.insert(3, "3");
0120 
0121     KBiHash<int, QString> biHash5 = KBiHash<int, QString>::fromHash(hash);
0122 
0123     qDebug() << biHash5;
0124 
0125     KBiHash<int, QString>::left_iterator it8 = biHash5.findLeft(1);
0126     qDebug() << (it8 == biHash.leftEnd());
0127     qDebug() << it8.key();
0128     //   qDebug() << it8.key() << it8.value();
0129 
0130     return 0;
0131 }