From 7167ce41b61d2ba2cdb526777a4233eb84a3b66a Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Sat, 6 Dec 2014 17:33:25 -0500 Subject: Imported Upstream version 2.99.6 --- SQLiteStudio3/coreSQLiteStudio/common/strhash.h | 182 ++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 SQLiteStudio3/coreSQLiteStudio/common/strhash.h (limited to 'SQLiteStudio3/coreSQLiteStudio/common/strhash.h') 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 +#include +#include +#include + +template +class StrHash +{ + public: + StrHash() {} + StrHash(std::initializer_list> list) : hash(QHash(list)) + { + initLower(); + } + + StrHash(const QHash& other) : hash(QHash(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& unite(const StrHash& other) + { + unite(other.hash); + return *this; + } + + StrHash& unite(const QHash& other) + { + QHashIterator 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 values() const + { + return hash.values(); + } + + QStringList keys() const + { + return hash.keys(); + } + + QHashIterator iterator() const + { + return QHashIterator(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 it(hash); + while (it.hasNext()) + { + it.next(); + lowerCaseHash[it.key().toLower()] = it.key(); + } + } + + QHash lowerCaseHash; + QHash hash; +}; + +#endif // STRHASH_H -- cgit v1.2.3