Skip to content

Commit

Permalink
Create a CellTable class that stores cells with QHash
Browse files Browse the repository at this point in the history
QMap is too slow to use as storage for cells, with
QHash I was able cut writeBool() & friends time by half.
  • Loading branch information
dantti committed Sep 2, 2024
1 parent 073bec2 commit 2b4e086
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 112 deletions.
21 changes: 19 additions & 2 deletions QXlsx/header/xlsxcellreference.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@

QT_BEGIN_NAMESPACE_XLSX

const int XLSX_ROW_MAX = 1048576;
const int XLSX_COLUMN_MAX = 16384;
const int XLSX_STRING_MAX = 32767;

class QXLSX_EXPORT CellReference
{
public:
CellReference();
CellReference(int row, int column);
/*!
Constructs the Reference from the given \a row, and \a column.
*/
constexpr CellReference(int row, int column)
: _row(row)
, _column(column)
{
}
CellReference(const QString &cell);
CellReference(const char *cell);
CellReference(const CellReference &other);
Expand All @@ -36,9 +47,15 @@ class QXLSX_EXPORT CellReference
return _row != other._row || _column != other._column;
}

inline bool operator>(const CellReference &other) const
{
return _row > other._row || _column != other._column;
}

private:
void init(const QString &cell);
int _row, _column;
int _row{-1};
int _column{-1};
};

QT_END_NAMESPACE_XLSX
Expand Down
55 changes: 49 additions & 6 deletions QXlsx/header/xlsxworksheet_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
#include <QSharedPointer>
#include <QString>
#include <QVector>
#include <QtGlobal>
#include <QHash>

class QXmlStreamWriter;
class QXmlStreamReader;

QT_BEGIN_NAMESPACE_XLSX

const int XLSX_ROW_MAX = 1048576;
const int XLSX_COLUMN_MAX = 16384;
const int XLSX_STRING_MAX = 32767;

class SharedStrings;

struct XlsxHyperlinkData {
Expand Down Expand Up @@ -137,6 +133,53 @@ struct XlsxColumnInfo {
bool collapsed;
};

class CellTable
{
public:
static QList<int> sorteIntList(QList<int> &&keys) {
std::sort(keys.begin(), keys.end());
return keys;
}

inline QList<int> sortedRows() const {
QList<int> keys = cells.keys();
std::sort(keys.begin(), keys.end());
return keys;
}

void setValue(int row, int column, const std::shared_ptr<Cell> &cell) {
cells[row].insert(column, cell);
firstRow = qMin(firstRow, row);
firstColumn = qMin(firstColumn, column);
lastRow = qMin(lastRow, row);
lastColumn = qMin(lastColumn, column);
}

std::shared_ptr<Cell> cellAt(int row, int column) const{
return cells.value(row).value(column);
}

bool contains(int row, int column) const{
auto it = cells.find(row);
if (it != cells.end()) {
return it->contains(column);
}
return false;
}

bool isEmpty() const {
return cells.isEmpty();
}

// It's faster with a single QHash, but in Qt5 it's capacity limits
// how much cells we can hold
QHash<int, QHash<int, std::shared_ptr<Cell>>> cells;
int firstRow = -1;
int firstColumn = -1;
int lastRow = -1;
int lastColumn = -1;
};

class WorksheetPrivate : public AbstractSheetPrivate
{
Q_DECLARE_PUBLIC(Worksheet)
Expand Down Expand Up @@ -182,7 +225,7 @@ class WorksheetPrivate : public AbstractSheetPrivate
SharedStrings *sharedStrings() const;

public:
QMap<int, QMap<int, std::shared_ptr<Cell>>> cellTable;
CellTable cellTable;

QMap<int, QMap<int, QString>> comments;
QMap<int, QMap<int, QSharedPointer<XlsxHyperlinkData>>> urlTable;
Expand Down
16 changes: 2 additions & 14 deletions QXlsx/source/xlsxcellreference.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// xlsxcellreference.cpp

#include "xlsxcellreference.h"
#include "xlsxworksheet_p.h"

#include <QMap>
#include <QRegularExpression>
Expand Down Expand Up @@ -69,20 +70,7 @@ int col_from_name(const QString &col_str)
/*!
Constructs an invalid Cell Reference
*/
CellReference::CellReference()
: _row(-1)
, _column(-1)
{
}

/*!
Constructs the Reference from the given \a row, and \a column.
*/
CellReference::CellReference(int row, int column)
: _row(row)
, _column(column)
{
}
CellReference::CellReference() = default;

/*!
\overload
Expand Down
Loading

0 comments on commit 2b4e086

Please sign in to comment.