diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2014-09-13 21:18:17 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2015-11-04 14:14:49 +0000 |
commit | 9a07ab9234ccb4ed0d6aa3a64b4b2c888635dae5 (patch) | |
tree | dd29f849487b72ab5519ef574da9adadf4f331e9 /src/widgets/widgets/qcalendarwidget.cpp | |
parent | 932ad390802319af1996f678d9226c2a17a913d0 (diff) |
QCalendarWidget: replace a QMap<Qt::DayOfWeek,.> with a static assoc array
Since Qt::DayOfWeek has such a limited range, it doesn't really make sense
to use it as a key in a QMap, which is optimized for much larger key spaces.
Replace by a C array of the value type, and, since the value type in question
(QTextCharFormat) doesn't have an invalid state, use a bool array to track
whether any of the seven possible keys has been inserted.
Wrap that in a small helper class which provides only the subset of QMap API
that QCalendarModel needs.
Saves 1352 bytes of text size on optimized GCC AMD64 builds.
Change-Id: Ie51390825d5933739659c4ea8e8da0af68577a9d
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'src/widgets/widgets/qcalendarwidget.cpp')
-rw-r--r-- | src/widgets/widgets/qcalendarwidget.cpp | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/widgets/widgets/qcalendarwidget.cpp b/src/widgets/widgets/qcalendarwidget.cpp index 2a60f61bd9..9857cce92a 100644 --- a/src/widgets/widgets/qcalendarwidget.cpp +++ b/src/widgets/widgets/qcalendarwidget.cpp @@ -817,6 +817,41 @@ void QCalendarTextNavigator::setDateEditAcceptDelay(int delay) class QCalendarView; +// a small helper class that replaces a QMap<Qt::DayOfWeek, T>, +// but requires T to have a member-swap and a default constructor +// which should be cheap (no memory allocations) + +QT_WARNING_PUSH +QT_WARNING_DISABLE_MSVC(4351) // "new behavior: elements of array ... will be default initialized" + +template <typename T> +class StaticDayOfWeekAssociativeArray { + bool contained[7]; + T data[7]; + + static Q_DECL_CONSTEXPR int day2idx(Qt::DayOfWeek day) Q_DECL_NOTHROW { return int(day) - 1; } // alt: day % 7 +public: + Q_DECL_CONSTEXPR StaticDayOfWeekAssociativeArray() Q_DECL_NOEXCEPT_EXPR(noexcept(T())) + : contained(), data() {} + + Q_DECL_CONSTEXPR bool contains(Qt::DayOfWeek day) const Q_DECL_NOTHROW { return contained[day2idx(day)]; } + Q_DECL_CONSTEXPR const T &value(Qt::DayOfWeek day) const Q_DECL_NOTHROW { return data[day2idx(day)]; } + + Q_DECL_RELAXED_CONSTEXPR T &operator[](Qt::DayOfWeek day) Q_DECL_NOTHROW + { + const int idx = day2idx(day); + contained[idx] = true; + return data[idx]; + } + + Q_DECL_RELAXED_CONSTEXPR void insert(Qt::DayOfWeek day, T v) Q_DECL_NOTHROW + { + operator[](day).swap(v); + } +}; + +QT_WARNING_POP + class QCalendarModel : public QAbstractTableModel { Q_OBJECT @@ -895,7 +930,7 @@ public: Qt::DayOfWeek m_firstDay; QCalendarWidget::HorizontalHeaderFormat m_horizontalHeaderFormat; bool m_weekNumbersShown; - QMap<Qt::DayOfWeek, QTextCharFormat> m_dayFormats; + StaticDayOfWeekAssociativeArray<QTextCharFormat> m_dayFormats; QMap<QDate, QTextCharFormat> m_dateFormats; QTextCharFormat m_headerFormat; QCalendarView *m_view; |