mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-21 00:55:09 -07:00
[DeckList] Extract deck-node sort helpers (#7320)
Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
1a6d9d7749
commit
c45cb8ac32
2 changed files with 36 additions and 18 deletions
|
|
@ -192,31 +192,35 @@ void InnerDecklistNode::writeElement(QXmlStreamWriter *xml)
|
|||
xml->writeEndElement(); // zone
|
||||
}
|
||||
|
||||
QVector<QPair<int, int>> InnerDecklistNode::sort(Qt::SortOrder order)
|
||||
QVector<QPair<int, AbstractDecklistNode *>> InnerDecklistNode::indexedSnapshot() const
|
||||
{
|
||||
QVector<QPair<int, AbstractDecklistNode *>> snapshot(size());
|
||||
for (int i = size() - 1; i >= 0; --i) {
|
||||
snapshot[i].first = i;
|
||||
snapshot[i].second = at(i);
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
QVector<QPair<int, int>> InnerDecklistNode::applySortedOrder(const QVector<QPair<int, AbstractDecklistNode *>> &sorted)
|
||||
{
|
||||
QVector<QPair<int, int>> result(size());
|
||||
|
||||
// Initialize temporary list with contents of current list
|
||||
QVector<QPair<int, AbstractDecklistNode *>> tempList(size());
|
||||
for (int i = size() - 1; i >= 0; --i) {
|
||||
tempList[i].first = i;
|
||||
tempList[i].second = at(i);
|
||||
result[i].first = sorted[i].first;
|
||||
result[i].second = i;
|
||||
replace(i, sorted[i].second);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QVector<QPair<int, int>> InnerDecklistNode::sort(Qt::SortOrder order)
|
||||
{
|
||||
auto snapshot = indexedSnapshot();
|
||||
|
||||
// Sort temporary list
|
||||
auto cmp = [order](const auto &a, const auto &b) {
|
||||
return (order == Qt::AscendingOrder) ? (b.second->compare(a.second)) : (a.second->compare(b.second));
|
||||
};
|
||||
std::sort(snapshot.begin(), snapshot.end(), cmp);
|
||||
|
||||
std::sort(tempList.begin(), tempList.end(), cmp);
|
||||
|
||||
// Map old indexes to new indexes and
|
||||
// copy temporary list to the current one
|
||||
for (int i = size() - 1; i >= 0; --i) {
|
||||
result[i].first = tempList[i].first;
|
||||
result[i].second = i;
|
||||
replace(i, tempList[i].second);
|
||||
}
|
||||
|
||||
return result;
|
||||
return applySortedOrder(snapshot);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -223,6 +223,20 @@ public:
|
|||
*/
|
||||
QVector<QPair<int, int>> sort(Qt::SortOrder order = Qt::AscendingOrder);
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Snapshots the current children as (old index, node) pairs.
|
||||
*/
|
||||
QVector<QPair<int, AbstractDecklistNode *>> indexedSnapshot() const;
|
||||
|
||||
/**
|
||||
* @brief Replaces this node's children with @p sorted and maps old indexes to new ones.
|
||||
*
|
||||
* @return A list of (old index, new index) pairs for each reordered child.
|
||||
*/
|
||||
QVector<QPair<int, int>> applySortedOrder(const QVector<QPair<int, AbstractDecklistNode *>> &sorted);
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Deserialize this node and its children from XML.
|
||||
* @param xml Reader positioned at this element.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue