comparison UniqueString.hpp @ 24:d0502678429c

Multiplex between different stringtypes.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Wed, 05 Sep 2012 21:56:45 +0200
parents 06166d6c083b
children bf3dce7fedcb
comparison
equal deleted inserted replaced
23:5d14d8c2c299 24:d0502678429c
17 map.insert(str, str); 17 map.insert(str, str);
18 } 18 }
19 QString::operator=(map.value(str)); 19 QString::operator=(map.value(str));
20 } 20 }
21 }; 21 };
22 #else 22 #elseif 0
23 #include "RBTree.hpp" 23 #include "RBTree.hpp"
24 #include <QtCore/QString> 24 #include <QtCore/QString>
25 #include <QtCore/QStringList>
25 #include <QtCore/QDebug> 26 #include <QtCore/QDebug>
26 27
27 class UniqueString : public QString 28 class UniqueString
28 { 29 {
29 private: 30 private:
30 static RBTree<QString> map; 31 static RBTree<QString> map;
32 QString myString;
33 static QMap<QString, int> lup;
34 static uint numInserts;
31 35
32 public: 36 public:
37 QStringList chunk(const QString& str)
38 {
39 QStringList list = str.split("");
40 list << QString();
41 return list;
42 }
43
33 UniqueString(const QString& str) 44 UniqueString(const QString& str)
34 { 45 {
46 //QString str = assemble(in_str);
35 boost::optional<QString> present = map.find(str); 47 boost::optional<QString> present = map.find(str);
36 if (!present) { 48 if (!present) {
37 map.insert(str); 49 QStringList chunks = chunk(str);
38 present = str; 50 foreach (const QString& ch, chunks) {
51 ++lup[ch];
52 }
53 if (++numInserts % 100 == 0) {
54 foreach(const QString& key, lup.keys()) {
55 qDebug() << key << ":" << lup[key];
56 }
57 }
58 QString tmpStr = str;
59 tmpStr.squeeze();
60 map.insert(tmpStr);
61 present = tmpStr;
39 //qDebug() << map.optimal_depth() << " : " << map.avg_depth() << " : " << map.depth(); 62 //qDebug() << map.optimal_depth() << " : " << map.avg_depth() << " : " << map.depth();
40 } 63 }
41 QString::operator=(*present); 64 myString = *present;
65 }
42 66
67 operator const QString() const
68 {
69 return myString;
43 } 70 }
44 }; 71
72 bool operator<(const UniqueString& rhs) const
73 {
74 return myString < rhs.myString;
75 }
76
77 bool operator==(const UniqueString& rhs) const
78 {
79 return myString == rhs.myString;
80 }
81 };
82 #else
83 #include "HuffmanString.hpp"
84 typedef HuffmanString UniqueString;
45 #endif 85 #endif
46 86
47 #endif //UNIQUESTRING_HPP 87 #endif //UNIQUESTRING_HPP