diff --git a/src/buffer/katetextblock.h b/src/buffer/katetextblock.h --- a/src/buffer/katetextblock.h +++ b/src/buffer/katetextblock.h @@ -21,6 +21,8 @@ #ifndef KATE_TEXTBLOCK_H #define KATE_TEXTBLOCK_H +#include + #include #include @@ -206,7 +208,7 @@ */ void removeCursor(Kate::TextCursor *cursor) { - m_cursors.remove(cursor); + m_cursors.erase(cursor); } /** @@ -246,18 +248,20 @@ /** * Lines contained in this buffer. These are shared pointers. + * We need no sharing, use STL. */ - QVector m_lines; + std::vector m_lines; /** * Startline of this block */ int m_startLine; /** * Set of cursors for this block. + * We need no sharing, use STL. */ - QSet m_cursors; + std::unordered_set m_cursors; /** * Contains for each line-offset the ranges that were cached into it. diff --git a/src/buffer/katetextblock.cpp b/src/buffer/katetextblock.cpp --- a/src/buffer/katetextblock.cpp +++ b/src/buffer/katetextblock.cpp @@ -57,19 +57,13 @@ // right input Q_ASSERT(line >= startLine()); - // calc internal line - line = line - startLine(); - - // in range - Q_ASSERT(line < m_lines.size()); - - // get text line - return m_lines.at(line); + // get text line, at will bail out on out-of-range + return m_lines.at(line - startLine()); } void TextBlock::appendLine(const QString &textOfLine) { - m_lines.append(TextLine::create(textOfLine)); + m_lines.push_back(TextLine::create(textOfLine)); } void TextBlock::clearLines() @@ -80,7 +74,7 @@ void TextBlock::text(QString &text) const { // combine all lines - for (int i = 0; i < m_lines.size(); ++i) { + for (size_t i = 0; i < m_lines.size(); ++i) { // not first line, insert \n if (i > 0 || startLine() > 0) { text.append(QLatin1Char('\n')); @@ -212,7 +206,7 @@ // move last line of previous block to this one, might result in empty block TextLine oldFirst = m_lines.at(0); int lastLineOfPreviousBlock = previousBlock->lines() - 1; - TextLine newFirst = previousBlock->m_lines.last(); + TextLine newFirst = previousBlock->m_lines.back(); m_lines[0] = newFirst; previousBlock->m_lines.erase(previousBlock->m_lines.begin() + (previousBlock->lines() - 1)); @@ -268,7 +262,7 @@ } // move cursors of the moved line from previous block to this block now - QSet newPreviousCursors; + std::unordered_set newPreviousCursors; for (TextCursor *cursor : qAsConst(previousBlock->m_cursors)) { if (cursor->lineInBlock() == lastLineOfPreviousBlock) { cursor->m_line = 0; @@ -522,8 +516,8 @@ void TextBlock::debugPrint(int blockIndex) const { // print all blocks - for (int i = 0; i < m_lines.size(); ++i) - printf("%4d - %4d : %4d : '%s'\n", blockIndex, startLine() + i + for (size_t i = 0; i < m_lines.size(); ++i) + printf("%4d - %4lld : %4d : '%s'\n", blockIndex, (unsigned long long)startLine() + i , m_lines.at(i)->text().size(), qPrintable(m_lines.at(i)->text())); } @@ -537,13 +531,13 @@ // move lines newBlock->m_lines.reserve(linesOfNewBlock); - for (int i = fromLine; i < m_lines.size(); ++i) { - newBlock->m_lines.append(m_lines.at(i)); + for (size_t i = fromLine; i < m_lines.size(); ++i) { + newBlock->m_lines.push_back(m_lines.at(i)); } m_lines.resize(fromLine); // move cursors - QSet oldBlockSet; + std::unordered_set oldBlockSet; for (TextCursor *cursor : qAsConst(m_cursors)) { if (cursor->lineInBlock() >= fromLine) { cursor->m_line = cursor->lineInBlock() - fromLine; @@ -579,8 +573,8 @@ // move lines targetBlock->m_lines.reserve(targetBlock->lines() + lines()); - for (int i = 0; i < m_lines.size(); ++i) { - targetBlock->m_lines.append(m_lines.at(i)); + for (size_t i = 0; i < m_lines.size(); ++i) { + targetBlock->m_lines.push_back(m_lines.at(i)); } m_lines.clear(); @@ -596,7 +590,7 @@ void TextBlock::deleteBlockContent() { // kill cursors, if not belonging to a range - QSet copy = m_cursors; + std::unordered_set copy = m_cursors; for (TextCursor *cursor : qAsConst(copy)) { if (!cursor->kateRange()) { delete cursor; @@ -610,14 +604,20 @@ void TextBlock::clearBlockContent(TextBlock *targetBlock) { // move cursors, if not belonging to a range - QSet copy = m_cursors; - for (TextCursor *cursor : qAsConst(copy)) { + // we can do in-place editing of the current set of cursors + for (auto it = m_cursors.begin(); it != m_cursors.end();) { + auto cursor = *it; if (!cursor->kateRange()) { cursor->m_column = 0; cursor->m_line = 0; cursor->m_block = targetBlock; targetBlock->m_cursors.insert(cursor); - m_cursors.remove(cursor); + + // remove it and advance to next element + it = m_cursors.erase(it); + } else { + // keep this cursor + ++it; } } @@ -628,8 +628,7 @@ void TextBlock::markModifiedLinesAsSaved() { // mark all modified lines as saved - for (int i = 0; i < m_lines.size(); ++i) { - TextLine textLine = m_lines[i]; + for (auto &textLine : m_lines) { if (textLine->markedAsModified()) { textLine->markAsSavedOnDisk(true); }