X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FTexRow.cpp;h=af0268ade280d7be74d08f95c938bff84eaad625;hb=6b651f2ad9f698c01993dcc6e340682c279f1c55;hp=e7165e95fad32650f4c0f4644f25e8510623c4ae;hpb=f630be890494c849981e4fb52ea4740506e92bed;p=lyx.git diff --git a/src/TexRow.cpp b/src/TexRow.cpp index e7165e95fa..af0268ade2 100644 --- a/src/TexRow.cpp +++ b/src/TexRow.cpp @@ -13,37 +13,17 @@ #include #include "TexRow.h" -#include "debug.h" +#include "support/debug.h" #include namespace lyx { -using std::find_if; - - -namespace { - -/// function object returning true when row number is found -class same_rownumber { -public: - same_rownumber(int row) : row_(row) {} - bool operator()(TexRow::RowList::value_type const & vt) const { - return vt.rownumber() == row_; - } - -private: - int row_; -}; - -} // namespace anon - void TexRow::reset() { rowlist.clear(); - count = 0; lastid = -1; lastpos = -1; } @@ -59,32 +39,43 @@ void TexRow::start(int id, int pos) void TexRow::newline() { int const id = lastid; - RowList::value_type tmp(id, lastpos, ++count); + RowList::value_type tmp(id, lastpos); rowlist.push_back(tmp); } bool TexRow::getIdFromRow(int row, int & id, int & pos) const { - RowList::const_iterator cit = - find_if(rowlist.begin(), rowlist.end(), - same_rownumber(row)); - - if (cit != rowlist.end()) { - id = cit->id(); - pos = cit->pos(); - return true; + if (row <= 0 || row > int(rowlist.size())) { + id = -1; + pos = 0; + return false; } - id = -1; - pos = 0; - return false; + + id = rowlist[row - 1].id(); + pos = rowlist[row - 1].pos(); + return true; } -TexRow & TexRow::operator+=(TexRow const & tr) +int TexRow::getRowFromIdPos(int id, int pos) const { - rowlist.insert(rowlist.end(), tr.rowlist.begin(), tr.rowlist.end()); - return *this; + int bestrow = 0; + bool foundid = false; + + // this loop finds the last *nonempty* row whith the same id + // and position <= pos + for (unsigned r = 0, n = rowlist.size(); r != n; ++r) { + if (rowlist[r].id() == id && rowlist[r].pos() <= pos) { + foundid = true; + if (rowlist[bestrow].id() != id || rowlist[r].pos() > rowlist[bestrow].pos()) + bestrow = r; + } else if (foundid) + break; + } + if (!foundid) + return rowlist.size(); + return bestrow; }