From bd68f78d4786f3cc5c5917d53e6910d6c7c0285c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sun, 26 Jan 2014 14:33:18 +0100 Subject: [PATCH] Paragraph: Cache length --- src/apps/haiku-depot/textview/Paragraph.cpp | 30 +++++++++++++++++++-- src/apps/haiku-depot/textview/Paragraph.h | 4 +++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/apps/haiku-depot/textview/Paragraph.cpp b/src/apps/haiku-depot/textview/Paragraph.cpp index dd71858520..0409e55ab3 100644 --- a/src/apps/haiku-depot/textview/Paragraph.cpp +++ b/src/apps/haiku-depot/textview/Paragraph.cpp @@ -19,7 +19,8 @@ Paragraph::Paragraph() Paragraph::Paragraph(const ParagraphStyle& style) : fStyle(style), - fTextSpans() + fTextSpans(), + fCachedLength(-1) { } @@ -27,7 +28,8 @@ Paragraph::Paragraph(const ParagraphStyle& style) Paragraph::Paragraph(const Paragraph& other) : fStyle(other.fStyle), - fTextSpans(other.fTextSpans) + fTextSpans(other.fTextSpans), + fCachedLength(other.fCachedLength) { } @@ -37,6 +39,7 @@ Paragraph::operator=(const Paragraph& other) { fStyle = other.fStyle; fTextSpans = other.fTextSpans; + fCachedLength = other.fCachedLength; return *this; } @@ -70,6 +73,8 @@ Paragraph::SetStyle(const ParagraphStyle& style) bool Paragraph::Prepend(const TextSpan& span) { + _InvalidateCachedLength(); + // Try to merge with first span if the TextStyles are equal if (fTextSpans.CountItems() > 0) { const TextSpan& firstSpan = fTextSpans.ItemAtFast(0); @@ -86,6 +91,8 @@ Paragraph::Prepend(const TextSpan& span) bool Paragraph::Append(const TextSpan& span) { + _InvalidateCachedLength(); + // Try to merge with last span if the TextStyles are equal if (fTextSpans.CountItems() > 0) { const TextSpan& lastSpan = fTextSpans.LastItem(); @@ -103,6 +110,8 @@ Paragraph::Append(const TextSpan& span) bool Paragraph::Insert(int32 offset, const TextSpan& newSpan) { + _InvalidateCachedLength(); + int32 index = 0; while (index < fTextSpans.CountItems()) { const TextSpan& span = fTextSpans.ItemAtFast(index); @@ -151,6 +160,8 @@ Paragraph::Remove(int32 offset, int32 length) if (length == 0) return true; + _InvalidateCachedLength(); + int32 index = 0; while (index < fTextSpans.CountItems()) { const TextSpan& span = fTextSpans.ItemAtFast(index); @@ -227,11 +238,16 @@ Paragraph::Clear() int32 Paragraph::Length() const { + if (fCachedLength >= 0) + return fCachedLength; + int32 length = 0; for (int32 i = fTextSpans.CountItems() - 1; i >= 0; i--) { const TextSpan& span = fTextSpans.ItemAtFast(i); length += span.CountChars(); } + + fCachedLength = length; return length; } @@ -308,3 +324,13 @@ Paragraph::PrintToStream() const } printf("

\n"); } + + +// #pragma mark - + + +void +Paragraph::_InvalidateCachedLength() +{ + fCachedLength = -1; +} diff --git a/src/apps/haiku-depot/textview/Paragraph.h b/src/apps/haiku-depot/textview/Paragraph.h index a9f16b3916..a6ad090d09 100644 --- a/src/apps/haiku-depot/textview/Paragraph.h +++ b/src/apps/haiku-depot/textview/Paragraph.h @@ -43,9 +43,13 @@ public: void PrintToStream() const; +private: + void _InvalidateCachedLength(); + private: ParagraphStyle fStyle; TextSpanList fTextSpans; + mutable int32 fCachedLength; };