QTableGenerator: give some TLC to the sorting predicate
- Provide op()(uint[], QComposeTableElement) as well, since the standard (does|did) not specify in which order the two are called. - Use std::lexicographical_compare to do the ... lexicographical comparison. - Share code by calling a new op()(uint[], uint[]) overload from all other overloads. - Mark all op() overloads const noexept. - Rename from 'Compare' to 'ByKeys', as in 'sort(vector, ByKeys()))'. - Replace a hand-rolled loop with std::equal. - Replace a #define with a static constexpr variable. Change-Id: I5ed487199916d0ae44ac38741fc95099bd2f8a22 Reviewed-by: Sune Vuorela <sune@vuorela.dk> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
This commit is contained in:
parent
2d3be6a989
commit
3c274d4e3e
@ -645,6 +645,6 @@ void TableGenerator::orderComposeTable()
|
|||||||
// Stable-sorting to ensure that the item that appeared before the other in the
|
// Stable-sorting to ensure that the item that appeared before the other in the
|
||||||
// original container will still appear first after the sort. This property is
|
// original container will still appear first after the sort. This property is
|
||||||
// needed to handle the cases when user re-defines already defined key sequence
|
// needed to handle the cases when user re-defines already defined key sequence
|
||||||
std::stable_sort(m_composeTable.begin(), m_composeTable.end(), Compare());
|
std::stable_sort(m_composeTable.begin(), m_composeTable.end(), ByKeys());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,9 @@
|
|||||||
#include <QtCore/QMap>
|
#include <QtCore/QMap>
|
||||||
#include <QtCore/QString>
|
#include <QtCore/QString>
|
||||||
|
|
||||||
#define QT_KEYSEQUENCE_MAX_LEN 6
|
#include <algorithm>
|
||||||
|
|
||||||
|
static Q_CONSTEXPR int QT_KEYSEQUENCE_MAX_LEN = 6;
|
||||||
|
|
||||||
//#define DEBUG_GENERATOR
|
//#define DEBUG_GENERATOR
|
||||||
|
|
||||||
@ -65,25 +67,30 @@ Q_DECLARE_TYPEINFO(QComposeTableElement, Q_PRIMITIVE_TYPE);
|
|||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class Compare
|
struct ByKeys
|
||||||
{
|
{
|
||||||
public:
|
using uint_array = uint[QT_KEYSEQUENCE_MAX_LEN];
|
||||||
bool operator () (const QComposeTableElement &lhs, const uint rhs[QT_KEYSEQUENCE_MAX_LEN])
|
using result_type = bool;
|
||||||
|
|
||||||
|
bool operator()(const uint_array &lhs, const uint_array &rhs) const Q_DECL_NOTHROW
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < QT_KEYSEQUENCE_MAX_LEN; i++) {
|
return std::lexicographical_compare(lhs, lhs + QT_KEYSEQUENCE_MAX_LEN,
|
||||||
if (lhs.keys[i] != rhs[i])
|
rhs, rhs + QT_KEYSEQUENCE_MAX_LEN);
|
||||||
return (lhs.keys[i] < rhs[i]);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator () (const QComposeTableElement &lhs, const QComposeTableElement &rhs)
|
bool operator()(const uint_array &lhs, const QComposeTableElement &rhs) const Q_DECL_NOTHROW
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < QT_KEYSEQUENCE_MAX_LEN; i++) {
|
return operator()(lhs, rhs.keys);
|
||||||
if (lhs.keys[i] != rhs.keys[i])
|
|
||||||
return (lhs.keys[i] < rhs.keys[i]);
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
bool operator()(const QComposeTableElement &lhs, const uint_array &rhs) const Q_DECL_NOTHROW
|
||||||
|
{
|
||||||
|
return operator()(lhs.keys, rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator()(const QComposeTableElement &lhs, const QComposeTableElement &rhs) const Q_DECL_NOTHROW
|
||||||
|
{
|
||||||
|
return operator()(lhs.keys, rhs.keys);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -155,11 +155,8 @@ void QComposeInputContext::update(Qt::InputMethodQueries q)
|
|||||||
|
|
||||||
static bool isDuplicate(const QComposeTableElement &lhs, const QComposeTableElement &rhs)
|
static bool isDuplicate(const QComposeTableElement &lhs, const QComposeTableElement &rhs)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < QT_KEYSEQUENCE_MAX_LEN; i++) {
|
return std::equal(lhs.keys, lhs.keys + QT_KEYSEQUENCE_MAX_LEN,
|
||||||
if (lhs.keys[i] != rhs.keys[i])
|
QT_MAKE_CHECKED_ARRAY_ITERATOR(rhs.keys, QT_KEYSEQUENCE_MAX_LEN));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QComposeInputContext::checkComposeTable()
|
bool QComposeInputContext::checkComposeTable()
|
||||||
@ -182,7 +179,7 @@ bool QComposeInputContext::checkComposeTable()
|
|||||||
}
|
}
|
||||||
Q_ASSERT(!m_composeTable.isEmpty());
|
Q_ASSERT(!m_composeTable.isEmpty());
|
||||||
QVector<QComposeTableElement>::const_iterator it =
|
QVector<QComposeTableElement>::const_iterator it =
|
||||||
std::lower_bound(m_composeTable.constBegin(), m_composeTable.constEnd(), m_composeBuffer, Compare());
|
std::lower_bound(m_composeTable.constBegin(), m_composeTable.constEnd(), m_composeBuffer, ByKeys());
|
||||||
|
|
||||||
// prevent dereferencing an 'end' iterator, which would result in a crash
|
// prevent dereferencing an 'end' iterator, which would result in a crash
|
||||||
if (it == m_composeTable.constEnd())
|
if (it == m_composeTable.constEnd())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user