iterators for MathArray; cosmetics;

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2459 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2001-08-09 08:53:16 +00:00
parent ad6a378c33
commit 20effa2eb1
13 changed files with 95 additions and 95 deletions

View File

@ -25,24 +25,21 @@ MathArray::~MathArray()
MathArray::MathArray(MathArray const & array)
: bf_(array.bf_)
{
deep_copy(0, size());
deep_copy(begin(), end());
}
MathArray::MathArray(MathArray const & array, int from, int to)
: bf_(array.bf_.begin() + from, array.bf_.begin() + to)
: bf_(array.begin() + from, array.begin() + to)
{
deep_copy(0, size());
deep_copy(begin(), end());
}
void MathArray::deep_copy(int pos1, int pos2)
void MathArray::deep_copy(iterator from, iterator to)
{
for (int pos = pos1; pos < pos2; ++pos) {
MathInset * p = bf_[pos]->clone();
//lyxerr << "cloning: '" << bf_[pos] << " to " << p << "'\n";
bf_[pos] = p;
}
for (iterator it = from; it != to; ++it)
*it = (*it)->clone();
}
@ -55,8 +52,8 @@ int MathArray::last() const
void MathArray::substitute(MathMacro const & m)
{
MathArray tmp;
for (int pos = 0; pos < size(); ++pos)
bf_[pos]->substitute(tmp, m);
for (iterator it = begin(); it != end(); ++it)
(*it)->substitute(tmp, m);
swap(tmp);
}
@ -81,40 +78,22 @@ MathInset const * MathArray::nextInset(int pos) const
}
unsigned char MathArray::getChar(int pos) const
{
return (pos == size()) ? 0 : (bf_[pos]->getChar());
}
MathTextCodes MathArray::getCode(int pos) const
{
return pos < size() ? (bf_[pos]->code()) : LM_TC_MIN;
}
void MathArray::setCode(int pos, MathTextCodes t)
{
bf_[pos]->code(t);
}
void MathArray::insert(int pos, MathInset * p)
{
bf_.insert(bf_.begin() + pos, p);
bf_.insert(begin() + pos, p);
}
void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
{
bf_.insert(bf_.begin() + pos, new MathCharInset(b, t));
bf_.insert(begin() + pos, new MathCharInset(b, t));
}
void MathArray::insert(int pos, MathArray const & array)
{
bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
deep_copy(pos, pos + array.size());
bf_.insert(begin() + pos, array.begin(), array.end());
deep_copy(begin() + pos, begin() + pos + array.size());
}
@ -176,9 +155,9 @@ void MathArray::erase(int pos)
void MathArray::erase(int pos1, int pos2)
{
for (int pos = pos1; pos < pos2; ++pos)
delete bf_[pos];
bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
for (iterator it = begin() + pos1; it != begin() + pos2; ++it)
delete *it;
bf_.erase(begin() + pos1, begin() + pos2);
}
@ -190,15 +169,15 @@ MathInset * MathArray::back() const
void MathArray::dump2(ostream & os) const
{
for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
for (const_iterator it = begin(); it != end(); ++it)
os << *it << ' ';
}
void MathArray::dump(ostream & os) const
{
for (int pos = 0; pos < size(); ++pos)
os << "<" << nextInset(pos) << ">";
for (const_iterator it = begin(); it != end(); ++it)
os << "<" << *it << ">";
}
@ -211,8 +190,8 @@ std::ostream & operator<<(std::ostream & os, MathArray const & ar)
void MathArray::write(ostream & os, bool fragile) const
{
for (int pos = 0; pos < size(); ++pos)
nextInset(pos)->write(os, fragile);
for (const_iterator it = begin(); it != end(); ++it)
(*it)->write(os, fragile);
}
@ -229,8 +208,8 @@ void MathArray::writeNormal(ostream & os) const
void MathArray::validate(LaTeXFeatures & features) const
{
for (int pos = 0; pos < size(); ++pos)
nextInset(pos)->validate(features);
for (const_iterator it = begin(); it != end(); ++it)
(*it)->validate(features);
}
@ -244,3 +223,26 @@ void MathArray::pop_back()
bf_.pop_back();
}
MathArray::const_iterator MathArray::begin() const
{
return bf_.begin();
}
MathArray::const_iterator MathArray::end() const
{
return bf_.end();
}
MathArray::iterator MathArray::begin()
{
return bf_.begin();
}
MathArray::iterator MathArray::end()
{
return bf_.end();
}

View File

@ -40,6 +40,14 @@ class LaTeXFeatures;
\version February 2001
*/
class MathArray {
public:
///
typedef std::vector<MathInset *> buffer_type;
///
typedef buffer_type::const_iterator const_iterator;
///
typedef buffer_type::iterator iterator;
public:
///
MathArray();
@ -102,22 +110,22 @@ public:
///
MathInset const * nextInset(int pos) const;
///
unsigned char getChar(int pos) const;
///
MathTextCodes getCode(int pos) const;
///
void setCode(int pos, MathTextCodes t);
///
void write(std::ostream &, bool) const;
///
void writeNormal(std::ostream &) const;
///
void validate(LaTeXFeatures &) const;
///
const_iterator begin() const;
///
const_iterator end() const;
///
iterator begin();
///
iterator end();
private:
///
typedef std::vector<MathInset *> buffer_type;
///
void deep_copy(int pos1, int pos2);
void deep_copy(iterator from, iterator to);
/// Buffer
buffer_type bf_;
};

View File

@ -789,11 +789,9 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
mathcursor->left();
}
mathcursor->clearLastCode();
// varcode = LM_TC_MIN;
} else if (c == '_' && varcode == LM_TC_TEX) {
mathcursor->insert(c, LM_TC_SPECIAL);
mathcursor->clearLastCode();
// varcode = LM_TC_MIN;
} else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
mathcursor->macroModeOpen();
mathcursor->clearLastCode();

View File

@ -76,3 +76,9 @@ void MathCharInset::writeNormal(std::ostream & os) const
{
os << char_;
}
bool MathCharInset::isRelOp() const
{
return char_ == '=' || char_ == '<' || char_ == '>';
}

View File

@ -36,6 +36,8 @@ public:
bool isCharInset() const { return true; }
///
char getChar() const { return char_; }
///
bool isRelOp() const;
private:
/// the character

View File

@ -861,6 +861,12 @@ void MathCursor::drawSelection(Painter & pain) const
}
MathTextCodes MathCursor::nextCode() const
{
return (pos() == size()) ? LM_TC_MIN : nextInset()->code();
}
void MathCursor::handleFont(MathTextCodes t)
{
if (selection_) {
@ -869,11 +875,13 @@ void MathCursor::handleFont(MathTextCodes t)
getSelection(i1, i2);
if (i1.idx_ == i2.idx_) {
MathArray & ar = i1.cell();
for (int pos = i1.pos_; pos != i2.pos_; ++pos)
if (isalnum(ar.getChar(pos))) {
MathTextCodes c = ar.getCode(pos) == t ? LM_TC_VAR : t;
ar.setCode(pos, c);
for (int pos = i1.pos_; pos != i2.pos_; ++pos) {
MathInset * p = ar.nextInset(pos);
if (isalnum(p->getChar())) {
MathTextCodes c = (p->code() == t) ? LM_TC_VAR : t;
p->code(c);
}
}
}
} else
lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
@ -915,18 +923,6 @@ void MathCursor::getPos(int & x, int & y)
}
MathTextCodes MathCursor::nextCode() const
{
return array().getCode(pos());
}
MathTextCodes MathCursor::prevCode() const
{
return array().getCode(pos() - 1);
}
MathInset * MathCursor::par() const
{
return cursor().par_;

View File

@ -187,8 +187,6 @@ public:
///
MathTextCodes nextCode() const;
///
MathTextCodes prevCode() const;
///
char valign() const;
///
char halign() const;

View File

@ -179,6 +179,8 @@ public:
///
virtual bool isActive() const { return nargs() > 0; }
///
virtual bool isRelOp() const { return false; }
///
virtual char getChar() const { return 0; }
///

View File

@ -68,9 +68,9 @@ int getCols(short int type)
// used for "intelligent splitting"
int firstRelOp(MathArray const & array)
{
for (int pos = 0; pos < array.size(); ++pos)
if (MathIsRelOp(array.getChar(pos), array.getCode(pos)))
return pos;
for (MathArray::const_iterator it = array.begin(); it != array.end(); ++it)
if ((*it)->isRelOp())
return it - array.begin();
return array.size();
}

View File

@ -52,3 +52,9 @@ void MathSymbolInset::draw(Painter & pain, int x, int y) const
drawStr(pain, code_, size_, x, y, ssym_);
}
bool MathSymbolInset::isRelOp() const
{
return sym_->id == LM_leq || sym_->id == LM_geq;
}

View File

@ -23,6 +23,8 @@ public:
void metrics(MathStyles st) const;
///
void draw(Painter &, int x, int y) const;
///
bool isRelOp() const;
private:
///

View File

@ -16,12 +16,6 @@ using std::endl;
using std::max;
bool MathIsInset(MathTextCodes x)
{
return LM_TC_INSET == x;
}
bool MathIsAlphaFont(MathTextCodes x)
{
return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
@ -741,18 +735,6 @@ MathStyles smallerStyleFrac(MathStyles st)
return st;
}
bool MathIsRelOp(unsigned char c, MathTextCodes f)
{
if (f == LM_TC_BOP && (c == '=' || c == '<' || c == '>'))
return true;
#ifndef WITH_WARNINGS
#warning implement me properly
#endif
if (f == LM_TC_SYMB && (c == LM_leq || c == LM_geq))
return true;
return false;
}
void math_font_max_dim(MathTextCodes code, MathStyles siz, int & asc, int & des)
{

View File

@ -32,10 +32,8 @@ int mathed_string_width(MathTextCodes type, MathStyles size, string const & s);
int mathed_string_ascent(MathTextCodes type, MathStyles size, string const & s);
int mathed_string_descent(MathTextCodes type, MathStyles size, string const & s);
bool MathIsInset(MathTextCodes x);
bool MathIsAlphaFont(MathTextCodes x);
bool MathIsSymbol(MathTextCodes x);
bool MathIsRelOp(unsigned char c, MathTextCodes f);
void drawStr(Painter & pain, MathTextCodes type, MathStyles siz,
int x, int y, string const & s);