Paragraph: Cache length
This commit is contained in:
@@ -19,7 +19,8 @@ Paragraph::Paragraph()
|
|||||||
Paragraph::Paragraph(const ParagraphStyle& style)
|
Paragraph::Paragraph(const ParagraphStyle& style)
|
||||||
:
|
:
|
||||||
fStyle(style),
|
fStyle(style),
|
||||||
fTextSpans()
|
fTextSpans(),
|
||||||
|
fCachedLength(-1)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,7 +28,8 @@ Paragraph::Paragraph(const ParagraphStyle& style)
|
|||||||
Paragraph::Paragraph(const Paragraph& other)
|
Paragraph::Paragraph(const Paragraph& other)
|
||||||
:
|
:
|
||||||
fStyle(other.fStyle),
|
fStyle(other.fStyle),
|
||||||
fTextSpans(other.fTextSpans)
|
fTextSpans(other.fTextSpans),
|
||||||
|
fCachedLength(other.fCachedLength)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,6 +39,7 @@ Paragraph::operator=(const Paragraph& other)
|
|||||||
{
|
{
|
||||||
fStyle = other.fStyle;
|
fStyle = other.fStyle;
|
||||||
fTextSpans = other.fTextSpans;
|
fTextSpans = other.fTextSpans;
|
||||||
|
fCachedLength = other.fCachedLength;
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -70,6 +73,8 @@ Paragraph::SetStyle(const ParagraphStyle& style)
|
|||||||
bool
|
bool
|
||||||
Paragraph::Prepend(const TextSpan& span)
|
Paragraph::Prepend(const TextSpan& span)
|
||||||
{
|
{
|
||||||
|
_InvalidateCachedLength();
|
||||||
|
|
||||||
// Try to merge with first span if the TextStyles are equal
|
// Try to merge with first span if the TextStyles are equal
|
||||||
if (fTextSpans.CountItems() > 0) {
|
if (fTextSpans.CountItems() > 0) {
|
||||||
const TextSpan& firstSpan = fTextSpans.ItemAtFast(0);
|
const TextSpan& firstSpan = fTextSpans.ItemAtFast(0);
|
||||||
@@ -86,6 +91,8 @@ Paragraph::Prepend(const TextSpan& span)
|
|||||||
bool
|
bool
|
||||||
Paragraph::Append(const TextSpan& span)
|
Paragraph::Append(const TextSpan& span)
|
||||||
{
|
{
|
||||||
|
_InvalidateCachedLength();
|
||||||
|
|
||||||
// Try to merge with last span if the TextStyles are equal
|
// Try to merge with last span if the TextStyles are equal
|
||||||
if (fTextSpans.CountItems() > 0) {
|
if (fTextSpans.CountItems() > 0) {
|
||||||
const TextSpan& lastSpan = fTextSpans.LastItem();
|
const TextSpan& lastSpan = fTextSpans.LastItem();
|
||||||
@@ -103,6 +110,8 @@ Paragraph::Append(const TextSpan& span)
|
|||||||
bool
|
bool
|
||||||
Paragraph::Insert(int32 offset, const TextSpan& newSpan)
|
Paragraph::Insert(int32 offset, const TextSpan& newSpan)
|
||||||
{
|
{
|
||||||
|
_InvalidateCachedLength();
|
||||||
|
|
||||||
int32 index = 0;
|
int32 index = 0;
|
||||||
while (index < fTextSpans.CountItems()) {
|
while (index < fTextSpans.CountItems()) {
|
||||||
const TextSpan& span = fTextSpans.ItemAtFast(index);
|
const TextSpan& span = fTextSpans.ItemAtFast(index);
|
||||||
@@ -151,6 +160,8 @@ Paragraph::Remove(int32 offset, int32 length)
|
|||||||
if (length == 0)
|
if (length == 0)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
_InvalidateCachedLength();
|
||||||
|
|
||||||
int32 index = 0;
|
int32 index = 0;
|
||||||
while (index < fTextSpans.CountItems()) {
|
while (index < fTextSpans.CountItems()) {
|
||||||
const TextSpan& span = fTextSpans.ItemAtFast(index);
|
const TextSpan& span = fTextSpans.ItemAtFast(index);
|
||||||
@@ -227,11 +238,16 @@ Paragraph::Clear()
|
|||||||
int32
|
int32
|
||||||
Paragraph::Length() const
|
Paragraph::Length() const
|
||||||
{
|
{
|
||||||
|
if (fCachedLength >= 0)
|
||||||
|
return fCachedLength;
|
||||||
|
|
||||||
int32 length = 0;
|
int32 length = 0;
|
||||||
for (int32 i = fTextSpans.CountItems() - 1; i >= 0; i--) {
|
for (int32 i = fTextSpans.CountItems() - 1; i >= 0; i--) {
|
||||||
const TextSpan& span = fTextSpans.ItemAtFast(i);
|
const TextSpan& span = fTextSpans.ItemAtFast(i);
|
||||||
length += span.CountChars();
|
length += span.CountChars();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fCachedLength = length;
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,3 +324,13 @@ Paragraph::PrintToStream() const
|
|||||||
}
|
}
|
||||||
printf(" </p>\n");
|
printf(" </p>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// #pragma mark -
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
Paragraph::_InvalidateCachedLength()
|
||||||
|
{
|
||||||
|
fCachedLength = -1;
|
||||||
|
}
|
||||||
|
|||||||
@@ -43,9 +43,13 @@ public:
|
|||||||
|
|
||||||
void PrintToStream() const;
|
void PrintToStream() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void _InvalidateCachedLength();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ParagraphStyle fStyle;
|
ParagraphStyle fStyle;
|
||||||
TextSpanList fTextSpans;
|
TextSpanList fTextSpans;
|
||||||
|
mutable int32 fCachedLength;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user