Skip to content

Commit 0868573

Browse files
committed
Create a CellTable class that stores cells with QHash
QMap is too slow to use as storage for cells, with QHash I was able cut writeBool() & friends time by half.
1 parent ee6b265 commit 0868573

File tree

4 files changed

+144
-112
lines changed

4 files changed

+144
-112
lines changed

QXlsx/header/xlsxcellreference.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@
99

1010
QT_BEGIN_NAMESPACE_XLSX
1111

12+
const int XLSX_ROW_MAX = 1048576;
13+
const int XLSX_COLUMN_MAX = 16384;
14+
const int XLSX_STRING_MAX = 32767;
15+
1216
class QXLSX_EXPORT CellReference
1317
{
1418
public:
1519
CellReference();
16-
CellReference(int row, int column);
20+
/*!
21+
Constructs the Reference from the given \a row, and \a column.
22+
*/
23+
constexpr CellReference(int row, int column)
24+
: _row(row)
25+
, _column(column)
26+
{
27+
}
1728
CellReference(const QString &cell);
1829
CellReference(const char *cell);
1930
CellReference(const CellReference &other);
@@ -36,9 +47,15 @@ class QXLSX_EXPORT CellReference
3647
return _row != other._row || _column != other._column;
3748
}
3849

50+
inline bool operator>(const CellReference &other) const
51+
{
52+
return _row > other._row || _column != other._column;
53+
}
54+
3955
private:
4056
void init(const QString &cell);
41-
int _row, _column;
57+
int _row{-1};
58+
int _column{-1};
4259
};
4360

4461
QT_END_NAMESPACE_XLSX

QXlsx/header/xlsxworksheet_p.h

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616
#include <QSharedPointer>
1717
#include <QString>
1818
#include <QVector>
19-
#include <QtGlobal>
19+
#include <QHash>
2020

2121
class QXmlStreamWriter;
2222
class QXmlStreamReader;
2323

2424
QT_BEGIN_NAMESPACE_XLSX
2525

26-
const int XLSX_ROW_MAX = 1048576;
27-
const int XLSX_COLUMN_MAX = 16384;
28-
const int XLSX_STRING_MAX = 32767;
29-
3026
class SharedStrings;
3127

3228
struct XlsxHyperlinkData {
@@ -137,6 +133,53 @@ struct XlsxColumnInfo {
137133
bool collapsed;
138134
};
139135

136+
class CellTable
137+
{
138+
public:
139+
static QList<int> sorteIntList(QList<int> &&keys) {
140+
std::sort(keys.begin(), keys.end());
141+
return keys;
142+
}
143+
144+
inline QList<int> sortedRows() const {
145+
QList<int> keys = cells.keys();
146+
std::sort(keys.begin(), keys.end());
147+
return keys;
148+
}
149+
150+
void setValue(int row, int column, const std::shared_ptr<Cell> &cell) {
151+
cells[row].insert(column, cell);
152+
firstRow = qMin(firstRow, row);
153+
firstColumn = qMin(firstColumn, column);
154+
lastRow = qMin(lastRow, row);
155+
lastColumn = qMin(lastColumn, column);
156+
}
157+
158+
std::shared_ptr<Cell> cellAt(int row, int column) const{
159+
return cells.value(row).value(column);
160+
}
161+
162+
bool contains(int row, int column) const{
163+
auto it = cells.find(row);
164+
if (it != cells.end()) {
165+
return it->contains(column);
166+
}
167+
return false;
168+
}
169+
170+
bool isEmpty() const {
171+
return cells.isEmpty();
172+
}
173+
174+
// It's faster with a single QHash, but in Qt5 it's capacity limits
175+
// how much cells we can hold
176+
QHash<int, QHash<int, std::shared_ptr<Cell>>> cells;
177+
int firstRow = -1;
178+
int firstColumn = -1;
179+
int lastRow = -1;
180+
int lastColumn = -1;
181+
};
182+
140183
class WorksheetPrivate : public AbstractSheetPrivate
141184
{
142185
Q_DECLARE_PUBLIC(Worksheet)
@@ -182,7 +225,7 @@ class WorksheetPrivate : public AbstractSheetPrivate
182225
SharedStrings *sharedStrings() const;
183226

184227
public:
185-
QMap<int, QMap<int, std::shared_ptr<Cell>>> cellTable;
228+
CellTable cellTable;
186229

187230
QMap<int, QMap<int, QString>> comments;
188231
QMap<int, QMap<int, QSharedPointer<XlsxHyperlinkData>>> urlTable;

QXlsx/source/xlsxcellreference.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// xlsxcellreference.cpp
22

33
#include "xlsxcellreference.h"
4+
#include "xlsxworksheet_p.h"
45

56
#include <QMap>
67
#include <QRegularExpression>
@@ -62,20 +63,7 @@ int col_from_name(const QString &col_str)
6263
/*!
6364
Constructs an invalid Cell Reference
6465
*/
65-
CellReference::CellReference()
66-
: _row(-1)
67-
, _column(-1)
68-
{
69-
}
70-
71-
/*!
72-
Constructs the Reference from the given \a row, and \a column.
73-
*/
74-
CellReference::CellReference(int row, int column)
75-
: _row(row)
76-
, _column(column)
77-
{
78-
}
66+
CellReference::CellReference() = default;
7967

8068
/*!
8169
\overload

0 commit comments

Comments
 (0)