mathed57.diff

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@1764 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Lars Gullik Bjønnes 2001-03-15 10:10:01 +00:00
parent 1a5a092741
commit 24c63359f4
8 changed files with 111 additions and 172 deletions

View File

@ -1,6 +1,12 @@
2001-03-15 André Pönitz <poenitz@htwm.de>
* math_rowst.h: Finally remove MathedRowSt
* math_parser.C:
math_xiter.C: changed accordingly
2001-03-12 André Pönitz <poenitz@htwm.de>
* math_rowst.h: replace MathedRowSt with MathedRowSt,
* math_rowst.h: replace MathedRowSt with MathedRowStruct,
more robust MathedRowSt::[gs]etTab (to get rid of the constructor arg)
2001-03-10 André Pönitz <poenitz@htwm.de>

View File

@ -82,7 +82,9 @@ struct MathStackXIter {
}
MathedXIter * Item(int idx) {
return (idx + 1 <= i) ? &item[i - idx - 1] : 0;
if (idx + 1 > i)
cerr << "Wrong index: " << idx << " i: " << i << endl;
return &item[i - idx - 1];
}
void Reset() {

View File

@ -60,6 +60,15 @@ void MathedIter::fcode(short c) const
fcode_ = c;
}
byte MathedIter::at(int p) const
{
return (*array)[p];
}
byte & MathedIter::at(int p)
{
return (*array)[p];
}
int MathedIter::Empty() const
{
@ -75,8 +84,8 @@ int MathedIter::OK() const
void MathedIter::Reset()
{
if (array->last() > 0 && MathIsFont((*array)[0])) {
fcode((*array)[0]);
if (array->last() > 0 && MathIsFont(at(0))) {
fcode(at(0));
pos = 1;
} else {
fcode(-1);
@ -90,23 +99,23 @@ void MathedIter::Reset()
byte MathedIter::GetChar() const
{
if (IsFont()) {
fcode((*array)[pos]);
fcode(at(pos));
++pos;
}
return (*array)[pos];
return at(pos);
}
string const MathedIter::GetString() const
{
if (IsFont()) {
fcode((*array)[++pos]);
fcode(at(++pos));
++pos;
}
string s;
for (; (*array)[pos] >= ' ' && pos < array->last(); ++pos)
s += (*array)[pos];
for (; at(pos) >= ' ' && pos < array->last(); ++pos)
s += at(pos);
return s;
}
@ -120,7 +129,7 @@ MathedInset * MathedIter::GetInset() const
return p;
} else {
lyxerr << "Math Error: This is not an inset["
<< (*array)[pos] << "]" << endl;
<< at(pos) << "]" << endl;
return 0;
}
}
@ -143,7 +152,7 @@ bool MathedIter::Next()
if (!OK())
return false;
if ((*array)[pos] < ' ') {
if (at(pos) < ' ') {
fcode(-1);
if (IsTab())
++col;
@ -159,7 +168,7 @@ bool MathedIter::Next()
++pos;
if (IsFont())
fcode((*array)[pos++]);
fcode(at(pos++));
return true;
}
@ -168,7 +177,7 @@ bool MathedIter::Next()
bool MathedIter::goNextCode(MathedTextCodes code)
{
while (Next()) {
if ((*array)[pos] == code)
if (at(pos) == code)
return true;
}
@ -198,23 +207,23 @@ void MathedIter::insert(byte c, MathedTextCodes t)
// Address 0x47b857 is 1 byte before start of malloc'd block at
// 0x47b858 of 16 bytes.
if (c == ' ' && ((*array)[pos] == ' ' || (*array)[pos - 1] == ' '))
if (c == ' ' && (at(pos) == ' ' || at(pos - 1) == ' '))
return;
if (IsFont() && (*array)[pos] == t) {
if (IsFont() && at(pos) == t) {
fcode(t);
++pos;
} else {
if (t != fcode() && pos > 0 && MathIsFont((*array)[pos - 1])) {
if (t != fcode() && pos > 0 && MathIsFont(at(pos - 1))) {
--pos;
int k = pos - 1;
for (; k >= 0 && (*array)[k] >= ' '; --k)
for (; k >= 0 && at(k) >= ' '; --k)
;
fcode( (k >= 0 && MathIsFont((*array)[k])) ? (*array)[k] : -1 );
fcode( (k >= 0 && MathIsFont(at(k))) ? at(k) : -1 );
}
}
short const f = ((*array)[pos] < ' ') ? 0 : fcode();
short const f = (at(pos) < ' ') ? 0 : fcode();
int shift = (t == fcode()) ? 1 : ((f) ? 3 : 2);
if (t == LM_TC_TAB || t == LM_TC_CR) {
@ -232,21 +241,21 @@ void MathedIter::insert(byte c, MathedTextCodes t)
else {
array->need_size(array->last() + shift);
array->last(array->last() + shift);
(*array)[array->last()] = '\0';
at(array->last()) = '\0';
}
if (t != fcode()) {
if (f)
(*array)[pos + shift - 1] = fcode();
at(pos + shift - 1) = fcode();
if (c >= ' ') {
(*array)[pos++] = t;
at(pos++) = t;
fcode(t);
} else
fcode(0);
}
(*array)[pos++] = c;
at(pos++) = c;
}
@ -256,8 +265,8 @@ void MathedIter::split(int shift)
if (pos < array->last()) {
bool fg = false;
if ((*array)[pos] >= ' ') {
if (pos> 0 && MathIsFont((*array)[pos - 1]))
if (at(pos) >= ' ') {
if (pos> 0 && MathIsFont(at(pos - 1)))
--pos;
else {
fg = true;
@ -268,7 +277,7 @@ void MathedIter::split(int shift)
array->move(pos, shift);
if (fg)
(*array)[pos + shift - 1] = fcode();
at(pos + shift - 1) = fcode();
} else {
@ -276,7 +285,7 @@ void MathedIter::split(int shift)
array->last(array->last() + shift);
}
(*array)[array->last()] = '\0';
at(array->last()) = '\0';
}
@ -287,20 +296,20 @@ void MathedIter::join(int pos2)
return;
short f = fcode();
if (pos > 0 && (*array)[pos] >= ' ' && MathIsFont((*array)[pos - 1]))
if (pos > 0 && at(pos) >= ' ' && MathIsFont(at(pos - 1)))
--pos;
if (MathIsFont((*array)[pos2 - 1]))
if (MathIsFont(at(pos2 - 1)))
--pos2;
if ((*array)[pos2] >= ' ') {
if (at(pos2) >= ' ') {
for (int p = pos2; p > 0; --p) {
if (MathIsFont((*array)[p])) {
f = (*array)[p];
if (MathIsFont(at(p))) {
f = at(p);
break;
}
}
(*array)[pos++] = f;
at(pos++) = f;
}
array->move(pos2, pos - pos2);
@ -324,11 +333,11 @@ void MathedIter::insertInset(MathedInset * p, int type)
split(shift);
(*array)[pos] = type;
at(pos) = type;
array->raw_pointer_insert(p, pos + 1);
pos += SizeInset;
(*array)[pos - 1] = type;
(*array)[array->last()] = '\0';
at(pos - 1) = type;
at(array->last()) = '\0';
fcode(-1);
#endif
}
@ -343,20 +352,20 @@ bool MathedIter::Delete()
byte const c = GetChar();
if (c >= ' ') {
if (MathIsFont((*array)[pos - 1]) && (*array)[pos + 1] < ' ') {
if (MathIsFont(at(pos - 1)) && at(pos + 1) < ' ') {
shift = 2;
pos--;
int i = pos - 1;
for (; i > 0 && !MathIsFont((*array)[i]); --i)
for (; i > 0 && !MathIsFont(at(i)); --i)
;
if (i > 0 && MathIsFont((*array)[i]))
fcode((*array)[i]);
if (i > 0 && MathIsFont(at(i)))
fcode(at(i));
} else
shift = 1;
} else {
if (MathIsInset((*array)[pos]))
if (MathIsInset(at(pos)))
shift = sizeof(char*) + 2;
else if (c == LM_TC_TAB || c == LM_TC_CR) {
++shift;
@ -412,37 +421,37 @@ void MathedIter::adjustTabs()
bool MathedIter::IsInset() const
{
return MathIsInset((*array)[pos]);
return MathIsInset(at(pos));
}
bool MathedIter::IsActive() const
{
return MathIsActive((*array)[pos]);
return MathIsActive(at(pos));
}
bool MathedIter::IsFont() const
{
return MathIsFont((*array)[pos]);
return MathIsFont(at(pos));
}
bool MathedIter::IsScript() const
{
return MathIsScript((*array)[pos]);
return MathIsScript(at(pos));
}
bool MathedIter::IsTab() const
{
return ((*array)[pos] == LM_TC_TAB);
return (at(pos) == LM_TC_TAB);
}
bool MathedIter::IsCR() const
{
return ((*array)[pos] == LM_TC_CR);
return (at(pos) == LM_TC_CR);
}
@ -452,7 +461,7 @@ MathedIter::MathedIter(MathedArray * d)
pos = 0;
row = 0;
col = 0;
fcode( (array && IsFont()) ? (*array)[0] : 0 );
fcode( (array && IsFont()) ? at(0) : 0 );
}

View File

@ -110,6 +110,10 @@ public:
void setNumCols(int n) { ncols = n; }
///
MathedArray * GetData() const;
///
byte & at(int pos);
///
byte at(int pos) const;
protected:
///
void split(int);

View File

@ -19,7 +19,6 @@ MathMatrixInset::MathMatrixInset(int m, int n, short st)
{
flag = 15;
if (n > 0) {
row_.data_ = new MathedRowSt(nc_ + 1);
MathedXIter it(this);
for (int j = 1; j < n; ++j)
it.addRow();
@ -29,7 +28,8 @@ MathMatrixInset::MathMatrixInset(int m, int n, short st)
it.insert('T', LM_TC_TAB);
}
} else if (n < 0) {
row_.data_ = new MathedRowSt(nc_ + 1);
MathedXIter it(this);
it.addRow();
nr_ = 1;
}
}
@ -93,27 +93,19 @@ void MathMatrixInset::draw(Painter & pain, int x, int baseline)
void MathMatrixInset::Metrics()
{
//if (row_.empty()) {
#warning This leaks row_.data but goes away soon
// lyxerr << " MIDA ";
MathedXIter it(this);
it.GoBegin();
if (!it.crow_) {
it.crow_.st_ = new MathedRowSt(it.ncols + 1); // this leaks
}
MathedRowSt * mrow = it.crow_.st_;
while (it.OK()) {
if (it.IsCR()) {
if (it.col >= it.ncols)
it.ncols = it.col + 1;
MathedRowSt * r = new MathedRowSt(it.ncols + 1); // this leaks
it.crow_.st_->next_ = r;
it.crow_.st_ = r;
}
it.Next();
}
row_.data_ = mrow;
//}
// Adjust row structure
MathedXIter it(this);
it.GoBegin();
int nrows = 1;
while (it.OK()) {
if (it.IsCR()) {
++nrows;
if (it.col >= it.ncols)
it.ncols = it.col + 1;
}
it.Next();
}
row_.data_.resize(nrows);
// Clean the arrays
for (MathedRowContainer::iterator it = row_.begin(); it; ++it)

View File

@ -604,7 +604,7 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
case LM_TK_NEWLINE:
if (mt && (flags & FLAG_END)) {
if (mt->Permit(LMPF_ALLOW_CR)) {
mt->getRowSt().insert_after(crow, MathedRowSt(mt->GetColumns() + 1));
mt->getRowSt().insert(crow);
++crow;
data.insert('K', LM_TC_CR);
} else

View File

@ -17,10 +17,8 @@ public:
typedef std::vector<int> Widths;
///
explicit
MathedRowStruct(int n)
: asc_(0), desc_(0), y_(0), widths_(n + 1, 0),
numbered_(true)
MathedRowStruct()
: asc_(0), desc_(0), y_(0), numbered_(true)
{}
///
string const & getLabel() const;
@ -64,25 +62,6 @@ protected:
};
class MathedRowContainer;
class MathedRowSt : public MathedRowStruct {
public:
///
explicit MathedRowSt(int n)
: MathedRowStruct(n), next_(0)
{}
explicit MathedRowSt(MathedRowStruct const & t)
: MathedRowStruct(t), next_(0)
{}
//private:
///
MathedRowSt * next_;
///
friend class MathedRowContainer;
};
// The idea is to change this MathedRowContainer to mimic the behaviour
// of std::list<MathedRowStruct> in several small steps. In the end it
// could be replaced by such a list and MathedRowSt can go as well.
@ -91,104 +70,51 @@ struct MathedRowContainer {
///
struct iterator {
///
iterator() : st_(0) {}
iterator() : st_(0), pos_(0) {}
///
explicit iterator(MathedRowSt * st) : st_(st) {}
explicit iterator(MathedRowContainer * m) : st_(m), pos_(0) {}
/// "better" conversion to bool, static_cast doens't help?
operator void *() const
{ return (void *)(st_ && pos_ < st_->data_.size()); }
///
explicit iterator(MathedRowContainer * m) : st_(m->data_) {}
/// "better" conversion to bool
operator void *() const { return st_; }
MathedRowStruct & operator*() { Assert(st_); return st_->data_[pos_]; }
///
MathedRowStruct & operator*() { Assert(st_); return *st_; }
MathedRowStruct * operator->() { return &st_->data_[pos_]; }
///
MathedRowStruct * operator->() { return st_; }
MathedRowStruct const * operator->() const { return &st_->data_[pos_]; }
///
MathedRowStruct const * operator->() const { return st_; }
void operator++() { Assert(st_); ++pos_; }
///
void operator++() { Assert(st_); st_ = st_->next_; }
bool is_last() const { Assert(st_); return pos_ == st_->data_.size() - 1; }
///
bool is_last() const { Assert(st_); return st_->next_ == 0; }
///
bool operator==(const iterator & it) const { return st_ == it.st_; }
bool operator==(const iterator & it) const
{ return st_ == it.st_ && pos_ == it.pos_; }
//private:
///
MathedRowSt * st_;
MathedRowContainer * st_;
///
unsigned int pos_;
};
///
MathedRowContainer() : data_(0) {}
///
MathedRowContainer(MathedRowContainer const & c) : data_(0) {
if (!c.empty()) {
MathedRowSt * ro = 0;
MathedRowSt * mrow = c.data_;
while (mrow) {
MathedRowSt * r = new MathedRowSt(*mrow);
if (!ro)
data_ = r;
else
ro->next_ = r;
mrow = mrow->next_;
ro = r;
}
}
}
~MathedRowContainer() {
MathedRowSt * r = data_;
while (r) {
MathedRowSt * q = r->next_;
delete r;
r = q;
}
}
///
iterator begin() { return iterator(this); }
///
bool empty() const { return data_ == 0; }
bool empty() const { return data_.size() == 0; }
/// insert 'item' before 'iterator'
void insert(iterator const & it, MathedRowStruct const & item) {
MathedRowSt * r = new MathedRowSt(item);
if (data_ == it.st_)
data_ = r;
else {
MathedRowSt * pos = data_;
if (pos->next_ == it.st_)
pos->next_ = r;
}
r->next_ = it.st_;
void insert(iterator const & it) {
Assert(it.st_ == this);
data_.insert(data_.begin() + it.pos_, MathedRowStruct());
}
/// insert 'item' after 'iterator'
void insert_after(iterator & it, MathedRowStruct const & item) {
MathedRowSt * r = new MathedRowSt(item);
if (it) {
r->next_ = it.st_->next_;
it.st_->next_ = r;
} else {
it.st_ = r;
r->next_ = 0;
}
}
void erase(iterator & it) {
Assert(it.st_);
MathedRowSt * r = it.st_->next_;
if (r) {
it.st_->next_ = r->next_;
delete r;
}
Assert(it.st_ == this);
data_.erase(data_.begin() + it.pos_);
}
///
MathedRowSt * data_;
std::vector<MathedRowStruct> data_;
private:
// currently unimplemented just to make sure it's not used

View File

@ -156,7 +156,7 @@ void MathedXIter::Merge(MathedArray const & a)
while (pos < pos2 && OK()) {
if (IsCR()) {
if (p_ && p_->Permit(LMPF_ALLOW_CR)) {
container().insert_after(crow_, MathedRowSt(ncols + 1));
container().insert(crow_);
++crow_;
} else {
Delete();
@ -369,7 +369,7 @@ void MathedXIter::addRow()
}
// Create new item for the structure
container().insert_after(crow_, MathedRowSt(ncols + 1));
container().insert(crow_);
// Fill missed tabs in current row
while (col < ncols - 1)
insert('T', LM_TC_TAB);