Cockatrice/libcockatrice_models/libcockatrice/models/database/card/card_search_model.h
BruebachL f4b4365464
[Search] Stop allocating per card in card-name completion (#7604)
* [Search] Stop allocating per card in card-name completion

CardSearchModel::updateSearchResults() runs on every textEdited()
signal, walks the whole card database and runs Levenshtein against every
matching name. Three costs dominated that path:

1. Every candidate built a QStringList of its searchable names, then
   lowercased each name into another QString, only to break out of the
   loop after the first hit.
2. The sort comparator called sortableName() for each comparison, which
   re-fetched the name (QMap lookup plus QString construction) O(n log n)
   times.
3. levenshteinDistance() allocated a full len1 x len2 vector of vectors.

The search model now lowercases the one name it tests directly, stores
the tie-breaking name length once per result, and the comparator only
compares integers. levenshteinDistance() uses the standard two-row
formulation: same result, O(len2) memory instead of O(len1*len2), with
early exits for empty and equal strings.

FilterString query closures get the same treatment:
- Set queries iterate the set map directly instead of materializing
  keys()/values() lists for every card.
- Rarity queries iterate printings in place instead of copying them into
  a QList<PrintingInfo> first.
- Power/toughness split the string with indexOf and compare QStringViews
  instead of allocating a QStringList.
- Field queries do one hash lookup instead of contains() followed by
  value().
- Color queries bind the colors string by reference instead of copying.

Verified with filter_string_test (42 tests) and a full client build.

* [Search] Address review comments

- Use std::any_of/std::none_of over the set map's key/value iterators
  instead of hand-rolled loops.
- Extract the name matching into matchingLowercaseName() returning
  std::optional<QString>.
- Reuse the matched name's length for the distance tie-break instead of
  looking the name up again, removing one getLocalizedName() call per card.
- Take the color strings by value; the getters already return by value.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
2026-10-23 11:23:04 +02:00

75 lines
2 KiB
C++

/**
* @file card_search_model.h
* @ingroup CardDatabaseModels
*/
//! \todo Document this file.
#ifndef CARD_SEARCH_MODEL_H
#define CARD_SEARCH_MODEL_H
#include "libcockatrice/card/card_info.h"
#include "libcockatrice/card/card_localization.h"
#include <QAbstractListModel>
#include <QList>
#include <QModelIndex>
#include <QSharedPointer>
#include <QString>
#include <QStringList>
#include <optional>
#include <qnamespace.h>
#include <qtmetamacros.h>
class CardDatabaseDisplayModel;
class QObject;
class CardSearchModel : public QAbstractListModel
{
Q_OBJECT
public:
enum CardSearchRoles
{
CardInfoRole = Qt::UserRole + 1,
};
explicit CardSearchModel(CardDatabaseDisplayModel *sourceModel, QObject *parent = nullptr);
[[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
[[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
void updateSearchResults(const QString &query); // Update results based on input
void setSearchLanguage(const CardSearchLanguage &searchLang)
{
if (searchLanguage == searchLang) {
return;
}
searchLanguage = searchLang;
}
private:
struct SearchResult
{
CardInfoPtr card;
int distance;
int nameLength;
};
/**
* @brief Finds the lowercased card name that matches the query.
*
* The completer suggestions match against the same languages the card
* search uses, so typing a localized name finds the card. In Both mode
* either language can match.
*
* @return The matched lowercased name, or std::nullopt when the card does not match.
*/
[[nodiscard]] std::optional<QString> matchingLowercaseName(const CardInfoPtr &card,
const QString &lowerQuery) const;
CardDatabaseDisplayModel *sourceModel;
QList<SearchResult> searchResults;
CardSearchLanguage searchLanguage;
};
#endif // CARD_SEARCH_MODEL_H