* TexRow.{h,cpp} switch from std::list to std::vector

(operator+=): remove unused method 
	(getRowFromIdPos) new method to get the texrow from id,pos



git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@19502 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Alfredo Braunstein 2007-08-13 14:04:35 +00:00
parent 484dd46b35
commit 6b87801d09
2 changed files with 38 additions and 48 deletions

View File

@ -20,30 +20,10 @@
namespace lyx { 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() void TexRow::reset()
{ {
rowlist.clear(); rowlist.clear();
count = 0;
lastid = -1; lastid = -1;
lastpos = -1; lastpos = -1;
} }
@ -59,32 +39,41 @@ void TexRow::start(int id, int pos)
void TexRow::newline() void TexRow::newline()
{ {
int const id = lastid; int const id = lastid;
RowList::value_type tmp(id, lastpos, ++count); RowList::value_type tmp(id, lastpos);
rowlist.push_back(tmp); rowlist.push_back(tmp);
} }
bool TexRow::getIdFromRow(int row, int & id, int & pos) const bool TexRow::getIdFromRow(int row, int & id, int & pos) const
{ {
RowList::const_iterator cit = if (row <= 0 || row > rowlist.size()) {
find_if(rowlist.begin(), rowlist.end(),
same_rownumber(row));
if (cit != rowlist.end()) {
id = cit->id();
pos = cit->pos();
return true;
}
id = -1; id = -1;
pos = 0; pos = 0;
return false; 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()); int bestrow = 0;
return *this; 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;
}
return bestrow;
} }

View File

@ -14,7 +14,7 @@
#ifndef TEXROW_H #ifndef TEXROW_H
#define TEXROW_H #define TEXROW_H
#include <list> #include <vector>
namespace lyx { namespace lyx {
@ -26,9 +26,7 @@ namespace lyx {
class TexRow { class TexRow {
public: public:
/// ///
TexRow() : count(0), lastid(-1), lastpos(-1) {} TexRow() : lastid(-1), lastpos(-1) {}
TexRow & operator+= (TexRow const &);
/// Clears structure /// Clears structure
void reset(); void reset();
@ -51,14 +49,22 @@ public:
*/ */
bool getIdFromRow(int row, int & id, int & pos) const; bool getIdFromRow(int row, int & id, int & pos) const;
/**
* getRowFromIdPos - find row containing a given id and pos
* @param id of the paragraph
* @param pos a given position in that paragraph
* @return the row number within the rowlist
*/
int getRowFromIdPos(int id, int pos) const;
/// Returns the number of rows contained /// Returns the number of rows contained
int rows() const { return count; } int rows() const { return rowlist.size(); }
/// an individual id/pos <=> row mapping /// an individual id/pos <=> row mapping
class RowItem { class RowItem {
public: public:
RowItem(int id, int pos, int row) RowItem(int id, int pos)
: id_(id), pos_(pos), rownumber_(row) : id_(id), pos_(pos)
{} {}
/// paragraph id /// paragraph id
@ -67,19 +73,14 @@ public:
void pos(int p) { pos_ = p; } void pos(int p) { pos_ = p; }
/// paragraph position /// paragraph position
int pos() const { return pos_; } int pos() const { return pos_; }
/// row number
int rownumber() const { return rownumber_; }
private: private:
RowItem(); RowItem();
int id_; int id_;
int pos_; int pos_;
int rownumber_;
}; };
/// ///
typedef std::list<RowItem> RowList; typedef std::vector<RowItem> RowList;
private: private:
/// number of lines
unsigned int count;
/// container of id/pos <=> row mapping /// container of id/pos <=> row mapping
RowList rowlist; RowList rowlist;
/// Last paragraph /// Last paragraph