aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/common/strhash.h
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@ubuntu.com>2014-12-06 17:33:25 -0500
committerLibravatarUnit 193 <unit193@ubuntu.com>2014-12-06 17:33:25 -0500
commit7167ce41b61d2ba2cdb526777a4233eb84a3b66a (patch)
treea35c14143716e1f2c98f808c81f89426045a946f /SQLiteStudio3/coreSQLiteStudio/common/strhash.h
Imported Upstream version 2.99.6upstream/2.99.6
Diffstat (limited to 'SQLiteStudio3/coreSQLiteStudio/common/strhash.h')
-rw-r--r--SQLiteStudio3/coreSQLiteStudio/common/strhash.h182
1 files changed, 182 insertions, 0 deletions
diff --git a/SQLiteStudio3/coreSQLiteStudio/common/strhash.h b/SQLiteStudio3/coreSQLiteStudio/common/strhash.h
new file mode 100644
index 0000000..4fb7bb3
--- /dev/null
+++ b/SQLiteStudio3/coreSQLiteStudio/common/strhash.h
@@ -0,0 +1,182 @@
+#ifndef STRHASH_H
+#define STRHASH_H
+
+#include <QHash>
+#include <QString>
+#include <QStringList>
+#include <QDebug>
+
+template <class T>
+class StrHash
+{
+ public:
+ StrHash() {}
+ StrHash(std::initializer_list<std::pair<QString,T>> list) : hash(QHash<QString,T>(list))
+ {
+ initLower();
+ }
+
+ StrHash(const QHash<QString,T>& other) : hash(QHash<QString,T>(other))
+ {
+ initLower();
+ }
+
+ void insert(const QString& key, const T& value)
+ {
+ if (lowerCaseHash.contains(key.toLower()))
+ remove(key, Qt::CaseInsensitive);
+
+ hash.insert(key, value);
+ lowerCaseHash.insert(key.toLower(), key);
+ }
+
+ bool contains(const QString& key, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
+ {
+ if (cs == Qt::CaseSensitive)
+ return hash.contains(key);
+
+ return lowerCaseHash.contains(key.toLower());
+ }
+
+ int remove(const QString& key, Qt::CaseSensitivity cs = Qt::CaseSensitive)
+ {
+ if (cs == Qt::CaseSensitive)
+ {
+ int res = hash.remove(key);
+ if (res > 0)
+ lowerCaseHash.remove(key.toLower());
+
+ return res;
+ }
+
+ // Case insensitive
+ QString lowerKey = key.toLower();
+ if (lowerCaseHash.contains(lowerKey))
+ {
+ int res = hash.remove(lowerCaseHash.value(lowerKey));
+ lowerCaseHash.remove(lowerKey);
+ return res;
+ }
+
+ return 0;
+ }
+
+ T take(const QString& key, Qt::CaseSensitivity cs = Qt::CaseSensitive)
+ {
+ if (cs == Qt::CaseSensitive)
+ {
+ lowerCaseHash.remove(key.toLower());
+ return hash.take(key);
+ }
+
+ // Case insensitive
+ QString lowerKey = key.toLower();
+ if (lowerCaseHash.contains(lowerKey))
+ {
+ QString theKey = lowerCaseHash.value(lowerKey);
+ lowerCaseHash.remove(lowerKey);
+ return hash.take(theKey);
+ }
+
+ return QString();
+ }
+
+ StrHash<T>& unite(const StrHash<T>& other)
+ {
+ unite(other.hash);
+ return *this;
+ }
+
+ StrHash<T>& unite(const QHash<QString,T>& other)
+ {
+ QHashIterator<QString,T> it(other);
+ while (it.hasNext())
+ {
+ it.next();
+ insert(it.key(), it.value());
+ }
+
+ return *this;
+ }
+
+ T value(const QString& key, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
+ {
+ if (cs == Qt::CaseSensitive)
+ return hash.value(key);
+
+ return hash.value(lowerCaseHash.value(key.toLower()));
+ }
+
+ QList<T> values() const
+ {
+ return hash.values();
+ }
+
+ QStringList keys() const
+ {
+ return hash.keys();
+ }
+
+ QHashIterator<QString,T> iterator() const
+ {
+ return QHashIterator<QString,T>(hash);
+ }
+
+ void clear()
+ {
+ hash.clear();
+ lowerCaseHash.clear();
+ }
+
+ int count() const
+ {
+ return hash.count();
+ }
+
+ int count(const QString& key, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
+ {
+ if (cs == Qt::CaseSensitive)
+ return hash.count(key);
+
+ return lowerCaseHash.count(key.toLower());
+ }
+
+ bool isEmpty() const
+ {
+ return hash.isEmpty();
+ }
+
+ T& operator[](const QString& key)
+ {
+ if (lowerCaseHash.contains(key.toLower()) && !hash.contains(key))
+ {
+ T value = hash[lowerCaseHash[key.toLower()]];
+ remove(key, Qt::CaseInsensitive);
+ hash[key] = value;
+ }
+
+ lowerCaseHash[key.toLower()] = key;
+ return hash[key];
+ }
+
+ const T operator[](const QString& key) const
+ {
+ return hash[lowerCaseHash[key.toLower()]];
+ }
+
+ private:
+ void initLower()
+ {
+ QHashIterator<QString,T> it(hash);
+ while (it.hasNext())
+ {
+ it.next();
+ lowerCaseHash[it.key().toLower()] = it.key();
+ }
+ }
+
+ QHash<QString,QString> lowerCaseHash;
+ QHash<QString,T> hash;
+};
+
+#endif // STRHASH_H