aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/common/bistrhash.cpp
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@ubuntu.com>2016-06-13 18:42:57 -0400
committerLibravatarUnit 193 <unit193@ubuntu.com>2016-06-13 18:42:57 -0400
commit65d5f68cc6dc81799c5a5d90400a2c1f0dd02547 (patch)
tree6f245ba901b87ef42fed69965aea22f5eea6b590 /SQLiteStudio3/coreSQLiteStudio/common/bistrhash.cpp
parentaeb6bad01630d325a4e768e798a7a6d44e18fdaf (diff)
parent5d9314f134ddd3dc4c853e398ac90ba247fb2e4f (diff)
Merge tag 'upstream/3.1.0'
Upstream version 3.1.0 # gpg: Signature made Mon 13 Jun 2016 06:42:54 PM EDT using RSA key ID EBE9BD91 # gpg: Good signature from "Unit 193 <unit193@gmail.com>" # gpg: aka "Unit 193 <unit193@ninthfloor.org>" # gpg: aka "Unit 193 <unit193@ubuntu.com>" # gpg: aka "Unit 193 <unit193@ninthfloor.com>"
Diffstat (limited to 'SQLiteStudio3/coreSQLiteStudio/common/bistrhash.cpp')
-rw-r--r--SQLiteStudio3/coreSQLiteStudio/common/bistrhash.cpp213
1 files changed, 213 insertions, 0 deletions
diff --git a/SQLiteStudio3/coreSQLiteStudio/common/bistrhash.cpp b/SQLiteStudio3/coreSQLiteStudio/common/bistrhash.cpp
new file mode 100644
index 0000000..091121c
--- /dev/null
+++ b/SQLiteStudio3/coreSQLiteStudio/common/bistrhash.cpp
@@ -0,0 +1,213 @@
+#include "bistrhash.h"
+
+BiStrHash::BiStrHash(std::initializer_list<std::pair<QString, QString> > list)
+{
+ hash = QHash<QString,QString>(list);
+ initInvertedAndLower();
+}
+
+BiStrHash::BiStrHash(const QHash<QString, QString>& other)
+{
+ unite(other);
+}
+
+BiStrHash::BiStrHash(const BiStrHash& other) : hash(other.hash), inverted(other.inverted),
+ lowerHash(other.lowerHash), lowerInverted(other.lowerInverted)
+{
+}
+
+void BiStrHash::insert(const QString& left, const QString& right)
+{
+ if (lowerHash.contains(left.toLower()))
+ removeLeft(left, Qt::CaseInsensitive);
+
+ if (lowerInverted.contains(right.toLower()))
+ removeRight(right, Qt::CaseInsensitive);
+
+ inverted.insert(right, left);
+ hash.insert(left, right);
+ lowerHash.insert(left.toLower(), left);
+ lowerInverted.insert(right.toLower(), right);
+}
+
+bool BiStrHash::containsLeft(const QString& left, Qt::CaseSensitivity cs)
+{
+ if (cs == Qt::CaseSensitive)
+ return hash.contains(left);
+ else
+ return lowerHash.contains(left.toLower());
+}
+
+bool BiStrHash::containsRight(const QString& right, Qt::CaseSensitivity cs)
+{
+ if (cs == Qt::CaseSensitive)
+ return inverted.contains(right);
+ else
+ return lowerInverted.contains(right.toLower());
+}
+
+int BiStrHash::removeLeft(const QString& left, Qt::CaseSensitivity cs)
+{
+ if (cs == Qt::CaseSensitive)
+ {
+ if (!hash.contains(left))
+ return 0;
+
+ inverted.remove(hash.value(left));
+ hash.remove(left);
+
+ return 1;
+ }
+ else
+ {
+ QString lowerLeft = left.toLower();
+ if (!lowerHash.contains(lowerLeft))
+ return 0;
+
+ QString right = hash.value(lowerHash.value(lowerLeft));
+
+ hash.remove(inverted.value(right));
+ inverted.remove(right);
+ lowerHash.remove(lowerLeft);
+ lowerInverted.remove(right.toLower());
+
+ return 1;
+ }
+}
+
+int BiStrHash::removeRight(const QString& right, Qt::CaseSensitivity cs)
+{
+ if (cs == Qt::CaseSensitive)
+ {
+ if (!inverted.contains(right))
+ return 0;
+
+ hash.remove(inverted.value(right));
+ inverted.remove(right);
+
+ return 1;
+ }
+ else
+ {
+ QString lowerRight = right.toLower();
+ if (!lowerInverted.contains(lowerRight))
+ return 0;
+
+ QString left = inverted.value(lowerInverted.value(lowerRight));
+
+ inverted.remove(hash.value(left));
+ hash.remove(left);
+ lowerHash.remove(left.toLower());
+ lowerInverted.remove(lowerRight);
+
+ return 1;
+ }
+}
+
+QString BiStrHash::takeLeft(const QString& left, Qt::CaseSensitivity cs)
+{
+ if (cs == Qt::CaseSensitive)
+ {
+ QString right = hash.take(left);
+ inverted.remove(right);
+ return right;
+ }
+ else
+ {
+ QString right = hash.take(lowerHash.take(left.toLower()));
+ inverted.remove(lowerInverted.take(right.toLower()));
+ return right;
+ }
+}
+
+QString BiStrHash::takeRight(const QString& right, Qt::CaseSensitivity cs)
+{
+ if (cs == Qt::CaseSensitive)
+ {
+ QString left = inverted.take(right);
+ hash.remove(left);
+ return left;
+ }
+ else
+ {
+ QString left = inverted.take(lowerInverted.take(right.toLower()));
+ hash.remove(lowerHash.take(left.toLower()));
+ return left;
+ }
+}
+
+BiStrHash& BiStrHash::unite(const BiStrHash& other)
+{
+ unite(other.hash);
+ return *this;
+}
+
+BiStrHash& BiStrHash::unite(const QHash<QString, QString>& other)
+{
+ QHashIterator<QString, QString> it(other);
+ while (it.hasNext())
+ insert(it.next().key(), it.value());
+
+ return *this;
+}
+
+QString BiStrHash::valueByLeft(const QString& left, Qt::CaseSensitivity cs) const
+{
+ if (cs == Qt::CaseSensitive)
+ return hash.value(left);
+ else
+ return hash.value(lowerHash.value(left.toLower()));
+}
+
+QString BiStrHash::valueByRight(const QString& right, Qt::CaseSensitivity cs) const
+{
+ if (cs == Qt::CaseSensitive)
+ return inverted.value(right);
+ else
+ return inverted.value(lowerInverted.value(right.toLower()));
+}
+
+QStringList BiStrHash::leftValues() const
+{
+ return hash.keys();
+}
+
+QStringList BiStrHash::rightValues() const
+{
+ return inverted.keys();
+}
+
+QHashIterator<QString, QString> BiStrHash::iterator() const
+{
+ return QHashIterator<QString,QString>(hash);
+}
+
+void BiStrHash::clear()
+{
+ hash.clear();
+ inverted.clear();
+ lowerHash.clear();
+ lowerInverted.clear();
+}
+
+int BiStrHash::count() const
+{
+ return hash.count();
+}
+
+bool BiStrHash::isEmpty() const
+{
+ return hash.isEmpty();
+}
+
+void BiStrHash::initInvertedAndLower()
+{
+ QHashIterator<QString,QString> it(hash);
+ while (it.hasNext())
+ {
+ it.next();
+ inverted[it.value()] = it.key();
+ lowerHash[it.key().toLower()] = it.key();
+ lowerInverted[it.value().toLower()] = it.value();
+ }
+}