This patch changes dociterator to not inherit from vector, the vector is now a private member instead.
IMHO this actually cleans up things a tiny bit: - can ovoid operator[](i) syntax in several places - same function always used to quesry the cursor depth: depth() - is LCursor a cursor a dociterator or a vector... confusion reduced. I find it good. Opinions might differ. Note that this is not the full patch all the mechanical changes because fo this is not included. But they doesn't bring anything interesting either...
Index: src/cursor.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/cursor.C,v retrieving revision 1.120 diff -u -p -B -b -w -r1.120 cursor.C --- src/cursor.C 31 Jan 2005 16:29:38 -0000 1.120 +++ src/cursor.C 6 Feb 2005 23:55:05 -0000 @@ -69,11 +69,11 @@ namespace { positionable(DocIterator const & cursor, DocIterator const & anchor) { // avoid deeper nested insets when selecting - if (cursor.size() > anchor.size()) + if (cursor.depth() > anchor.depth()) return false; // anchor might be deeper, should have same path then - for (size_t i = 0; i < cursor.size(); ++i) + for (size_t i = 0; i < cursor.depth(); ++i) if (&cursor[i].inset() != &anchor[i].inset()) return false; @@ -91,11 +91,12 @@ namespace { DocIterator result; DocIterator it = c; - it.back().pos() = 0; + it.top().pos() = 0; DocIterator et = c; - et.back().pos() = et.back().asMathInset()->cell(et.back().idx()).size(); + et.top().pos() = et.top().asMathInset()->cell(et.top().idx()).size(); for (int i = 0; ; ++i) { - int xo, yo; + int xo; + int yo; LCursor cur = c; cur.setCursor(it); cur.inset().getCursorPos(cur.top(), xo, yo); @@ -192,15 +193,16 @@ void LCursor::setCursor(DocIterator cons void LCursor::dispatch(FuncRequest const & cmd0) { - lyxerr[Debug::DEBUG] << "LCursor::dispatch: cmd: " - << cmd0 << endl << *this << endl; + lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION + << " cmd: " << cmd0 << '\n' + << *this << endl; if (empty()) return; FuncRequest cmd = cmd0; LCursor safe = *this; - for (; size(); pop()) { + for (; depth(); pop()) { lyxerr[Debug::DEBUG] << "LCursor::dispatch: cmd: " << cmd0 << endl << *this << endl; BOOST_ASSERT(pos() <= lastpos()); @@ -249,7 +251,7 @@ Buffer & LCursor::buffer() const void LCursor::pop() { - BOOST_ASSERT(size() >= 1); + BOOST_ASSERT(depth() >= 1); pop_back(); } @@ -297,7 +299,7 @@ bool LCursor::popRight() int LCursor::currentMode() { BOOST_ASSERT(!empty()); - for (int i = size() - 1; i >= 0; --i) { + for (int i = depth() - 1; i >= 0; --i) { int res = operator[](i).inset().currentMode(); if (res != InsetBase::UNDECIDED_MODE) return res; @@ -347,9 +349,9 @@ bool LCursor::posRight() CursorSlice LCursor::anchor() const { - BOOST_ASSERT(anchor_.size() >= size()); - CursorSlice normal = anchor_[size() - 1]; - if (size() < anchor_.size() && back() <= normal) { + BOOST_ASSERT(anchor_.depth() >= depth()); + CursorSlice normal = anchor_[depth() - 1]; + if (depth() < anchor_.depth() && top() <= normal) { // anchor is behind cursor -> move anchor behind the inset ++normal.pos(); } @@ -360,16 +362,16 @@ CursorSlice LCursor::anchor() const CursorSlice LCursor::selBegin() const { if (!selection()) - return back(); - return anchor() < back() ? anchor() : back(); + return top(); + return anchor() < top() ? anchor() : top(); } CursorSlice LCursor::selEnd() const { if (!selection()) - return back(); - return anchor() > back() ? anchor() : back(); + return top(); + return anchor() > top() ? anchor() : top(); } @@ -377,7 +379,7 @@ DocIterator LCursor::selectionBegin() co { if (!selection()) return *this; - return anchor() < back() ? anchor_ : *this; + return anchor() < top() ? anchor_ : *this; } @@ -385,7 +387,7 @@ DocIterator LCursor::selectionEnd() cons { if (!selection()) return *this; - return anchor() > back() ? anchor_ : *this; + return anchor() > top() ? anchor_ : *this; } @@ -465,15 +467,15 @@ void LCursor::selHandle(bool sel) std::ostream & operator<<(std::ostream & os, LCursor const & cur) { os << "\n cursor: | anchor:\n"; - for (size_t i = 0, n = cur.size(); i != n; ++i) { - os << " " << cur.operator[](i) << " | "; - if (i < cur.anchor_.size()) + for (size_t i = 0, n = cur.depth(); i != n; ++i) { + os << " " << cur[i] << " | "; + if (i < cur.anchor_.depth()) os << cur.anchor_[i]; else os << "-------------------------------"; os << "\n"; } - for (size_t i = cur.size(), n = cur.anchor_.size(); i < n; ++i) { + for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) { os << "------------------------------- | " << cur.anchor_[i] << "\n"; } os << " selection: " << cur.selection_ @@ -525,7 +527,7 @@ bool LCursor::openable(MathAtom const & return true; // we can't move into anything new during selection - if (depth() >= anchor_.size()) + if (depth() >= anchor_.depth()) return false; if (!ptr_cmp(t.nucleus(), &anchor_[depth()].inset())) return false; @@ -620,7 +622,7 @@ void LCursor::niceInsert(string const & void LCursor::niceInsert(MathAtom const & t) { macroModeClose(); - string safe = lyx::cap::grabAndEraseSelection(*this); + string const safe = lyx::cap::grabAndEraseSelection(*this); plainInsert(t); // enter the new inset and move the contents of the selection if possible if (t->isActive()) { @@ -752,7 +754,7 @@ void LCursor::macroModeClose() return; MathUnknownInset * p = activeMacro(); p->finalize(); - string s = p->name(); + string const s = p->name(); --pos(); cell().erase(pos()); @@ -803,10 +805,11 @@ int LCursor::targetX() const void LCursor::setTargetX() { - //for now this is good enough. A better solution would be to + // For now this is good enough. A better solution would be to //avoid this rebreak by setting cursorX only after drawing bottom().text()->redoParagraph(bottom().pit()); - int x, y; + int x; + int y; getPos(x, y); x_target_ = x; } @@ -934,7 +937,7 @@ bool LCursor::goUpDown(bool up) //} // try to find an inset that knows better then we - while (1) { + while (true) { //lyxerr << "updown: We are in " << &inset() << " idx: " << idx() << endl; // ask inset first if (inset().idxUpDown(*this, up)) { @@ -953,7 +956,8 @@ bool LCursor::goUpDown(bool up) } // any improvement so far? - int xnew, ynew; + int xnew; + int ynew; getPos(xnew, ynew); if (up ? ynew < yo : ynew > yo) return true; @@ -1082,7 +1086,7 @@ Encoding const * LCursor::getEncoding() int s = 0; // go up until first non-0 text is hit // (innermost text is 0 in mathed) - for (s = size() - 1; s >= 0; --s) + for (s = depth() - 1; s >= 0; --s) if (operator[](s).text()) break; CursorSlice const & sl = operator[](s); @@ -1123,7 +1127,7 @@ LyXFont LCursor::getFont() const int s = 0; // go up until first non-0 text is hit // (innermost text is 0 in mathed) - for (s = size() - 1; s >= 0; --s) + for (s = depth() - 1; s >= 0; --s) if (operator[](s).text()) break; CursorSlice const & sl = operator[](s); @@ -1132,7 +1136,9 @@ LyXFont LCursor::getFont() const bv().buffer()->params(), sl.pos(), outerFont(sl.pit(), text.paragraphs())); - for (; s < size(); ++s) - ; + + // And the point of this empty construct is what? (Lgb) + //for (; s < depth(); ++s) + // ; return font; } Index: src/dociterator.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/dociterator.C,v retrieving revision 1.22 diff -u -p -B -b -w -r1.22 dociterator.C --- src/dociterator.C 31 Jan 2005 16:29:38 -0000 1.22 +++ src/dociterator.C 6 Feb 2005 23:55:05 -0000 @@ -246,9 +246,9 @@ LyXText * DocIterator::innerText() BOOST_ASSERT(!empty()); // go up until first non-0 text is hit // (innermost text is 0 in mathed) - for (int i = size() - 1; i >= 0; --i) - if (operator[](i).text()) - return operator[](i).text(); + for (int i = depth() - 1; i >= 0; --i) + if (slices_[i].text()) + return slices_[i].text(); return 0; } @@ -257,18 +257,18 @@ LyXText const * DocIterator::innerText() BOOST_ASSERT(!empty()); // go up until first non-0 text is hit // (innermost text is 0 in mathed) - for (int i = size() - 1; i >= 0; --i) - if (operator[](i).text()) - return operator[](i).text(); + for (int i = depth() - 1; i >= 0; --i) + if (slices_[i].text()) + return slices_[i].text(); return 0; } InsetBase * DocIterator::innerInsetOfType(int code) const { - for (int i = size() - 1; i >= 0; --i) - if (operator[](i).inset_->lyxCode() == code) - return operator[](i).inset_; + for (int i = depth() - 1; i >= 0; --i) + if (slices_[i].inset_->lyxCode() == code) + return slices_[i].inset_; return 0; } @@ -281,7 +281,7 @@ void DocIterator::forwardPos() return; } - CursorSlice & top = back(); + CursorSlice & tip = top(); //lyxerr << "XXX\n" << *this << endl; // this is used twice and shows up in the profiler! @@ -290,13 +290,13 @@ void DocIterator::forwardPos() // move into an inset to the right if possible InsetBase * n = 0; - if (top.pos() != lastp) { + if (tip.pos() != lastp) { // this is impossible for pos() == size() if (inMathed()) { - n = (top.cell().begin() + top.pos())->nucleus(); + n = (tip.cell().begin() + tip.pos())->nucleus(); } else { - if (paragraph().isInset(top.pos())) - n = paragraph().getInset(top.pos()); + if (paragraph().isInset(tip.pos())) + n = paragraph().getInset(tip.pos()); } } @@ -307,28 +307,28 @@ void DocIterator::forwardPos() } // otherwise move on one position if possible - if (top.pos() < lastp) { + if (tip.pos() < lastp) { //lyxerr << "... next pos" << endl; - ++top.pos(); + ++tip.pos(); return; } //lyxerr << "... no next pos" << endl; // otherwise move on one paragraph if possible - if (top.pit() < lastpit()) { + if (tip.pit() < lastpit()) { //lyxerr << "... next par" << endl; - ++top.pit(); - top.pos() = 0; + ++tip.pit(); + tip.pos() = 0; return; } //lyxerr << "... no next pit" << endl; // otherwise try to move on one cell if possible - if (top.idx() < lastidx()) { + if (tip.idx() < lastidx()) { //lyxerr << "... next idx" << endl; - ++top.idx(); - top.pit() = 0; - top.pos() = 0; + ++tip.idx(); + tip.pit() = 0; + tip.pos() = 0; return; } //lyxerr << "... no next idx" << endl; @@ -336,8 +336,8 @@ void DocIterator::forwardPos() // otherwise leave inset and jump over inset as a whole pop_back(); // 'top' is invalid now... - if (size()) - ++back().pos(); + if (!empty()) + ++top().pos(); } @@ -352,7 +352,7 @@ void DocIterator::forwardPar() void DocIterator::forwardChar() { forwardPos(); - while (size() != 0 && pos() == lastpos()) + while (!empty() && pos() == lastpos()) forwardPos(); } @@ -360,7 +360,7 @@ void DocIterator::forwardChar() void DocIterator::forwardInset() { forwardPos(); - while (size() != 0 && (pos() == lastpos() || nextInset() == 0)) + while (!empty() && (pos() == lastpos() || nextInset() == 0)) forwardPos(); } @@ -368,7 +368,7 @@ void DocIterator::forwardInset() void DocIterator::backwardChar() { backwardPos(); - while (size() != 0 && pos() == lastpos()) + while (!empty() && pos() == lastpos()) backwardPos(); } @@ -378,24 +378,24 @@ void DocIterator::backwardPos() //this dog bites his tail if (empty()) { push_back(CursorSlice(*inset_)); - back().idx() = lastidx(); - back().pit() = lastpit(); - back().pos() = lastpos(); + top().idx() = lastidx(); + top().pit() = lastpit(); + top().pos() = lastpos(); return; } - CursorSlice & top = back(); + CursorSlice & tip = top(); - if (top.pos() != 0) { - --top.pos(); - } else if (top.pit() != 0) { - --top.pit(); - top.pos() = lastpos(); + if (tip.pos() != 0) { + --tip.pos(); + } else if (tip.pit() != 0) { + --tip.pit(); + tip.pos() = lastpos(); return; - } else if (top.idx() != 0) { - --top.idx(); - top.pit() = lastpit(); - top.pos() = lastpos(); + } else if (tip.idx() != 0) { + --tip.idx(); + tip.pit() = lastpit(); + tip.pos() = lastpos(); return; } else { pop_back(); @@ -406,17 +406,17 @@ void DocIterator::backwardPos() InsetBase * n = 0; if (inMathed()) { - n = (top.cell().begin() + top.pos())->nucleus(); + n = (tip.cell().begin() + tip.pos())->nucleus(); } else { - if (paragraph().isInset(top.pos())) - n = paragraph().getInset(top.pos()); + if (paragraph().isInset(tip.pos())) + n = paragraph().getInset(tip.pos()); } if (n && n->isActive()) { push_back(CursorSlice(*n)); - back().idx() = lastidx(); - back().pit() = lastpit(); - back().pos() = lastpos(); + top().idx() = lastidx(); + top().pit() = lastpit(); + top().pos() = lastpos(); } } @@ -424,18 +424,18 @@ void DocIterator::backwardPos() bool DocIterator::hasPart(DocIterator const & it) const { // it can't be a part if it is larger - if (it.size() > size()) + if (it.depth() > depth()) return false; // as inset adresses are the 'last' level - return &it.back().inset() == &operator[](it.size() - 1).inset(); + return &it.top().inset() == &slices_[it.depth() - 1].inset(); } std::ostream & operator<<(std::ostream & os, DocIterator const & dit) { - for (size_t i = 0, n = dit.size(); i != n; ++i) - os << " " << dit.operator[](i) << "\n"; + for (size_t i = 0, n = dit.depth(); i != n; ++i) + os << " " << dit[i] << "\n"; return os; } @@ -443,9 +443,9 @@ std::ostream & operator<<(std::ostream & /////////////////////////////////////////////////////// -StableDocIterator::StableDocIterator(const DocIterator & dit) +StableDocIterator::StableDocIterator(DocIterator const & dit) { - data_ = dit; + data_ = dit.internalData(); for (size_t i = 0, n = data_.size(); i != n; ++i) data_[i].inset_ = 0; } @@ -459,14 +459,15 @@ DocIterator StableDocIterator::asDocIter for (size_t i = 0, n = data_.size(); i != n; ++i) { if (inset == 0) { // FIXME - lyxerr << "Should not happen, but does e.g. after C-n C-l C-z S-C-z" - << endl << "dit: " << dit << endl + lyxerr << BOOST_CURRENT_FUNCTION + << " Should not happen, but does e.g. after C-n C-l C-z S-C-z" + << '\n' << "dit: " << dit << '\n' << " lastpos: " << dit.lastpos() << endl; //break; BOOST_ASSERT(false); } dit.push_back(data_[i]); - dit.back().inset_ = inset; + dit.top().inset_ = inset; if (i + 1 != n) inset = dit.nextInset(); } Index: src/dociterator.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/dociterator.h,v retrieving revision 1.15 diff -u -p -B -b -w -r1.15 dociterator.h --- src/dociterator.h 31 Jan 2005 16:29:38 -0000 1.15 +++ src/dociterator.h 6 Feb 2005 23:55:05 -0000 @@ -34,8 +34,21 @@ bool ptr_cmp(A const * a, B const * b) // The public inheritance should go in favour of a suitable data member // (or maybe private inheritance) at some point of time. -class DocIterator : public std::vector<CursorSlice> +class DocIterator // : public std::vector<CursorSlice> { +protected: + void clear() { slices_.clear(); } + void push_back(CursorSlice const & sl) { + slices_.push_back(sl); + } + void pop_back() { + slices_.pop_back(); + } +private: + std::vector<CursorSlice> const & internalData() const { + return slices_; + } + std::vector<CursorSlice> slices_; public: /// type for cell number in inset typedef CursorSlice::idx_type idx_type; @@ -54,42 +67,55 @@ public: /// explicit DocIterator(InsetBase & inset); + CursorSlice const & operator[](size_t i) const { + return slices_[i]; + } + + CursorSlice & operator[](size_t i) { + return slices_[i]; + } + + // What is the point of this function? + void resize(size_t i) { slices_.resize(i); } + /// is the iterator valid? operator const void*() const { return empty() ? 0 : this; } /// is this iterator invalid? bool operator!() const { return empty(); } + bool empty() const { return slices_.empty(); } + // // access to slice at tip // /// access to tip - CursorSlice & top() { return back(); } + CursorSlice & top() { return slices_.back(); } /// access to tip - CursorSlice const & top() const { return back(); } + CursorSlice const & top() const { return slices_.back(); } /// access to outermost slice - CursorSlice & bottom() { return front(); } + CursorSlice & bottom() { return slices_.front(); } /// access to outermost slicetip - CursorSlice const & bottom() const { return front(); } + CursorSlice const & bottom() const { return slices_.front(); } /// how many nested insets do we have? - size_t depth() const { return size(); } + size_t depth() const { return slices_.size(); } /// the containing inset - InsetBase & inset() const { return back().inset(); } + InsetBase & inset() const { return top().inset(); } /// return the cell of the inset this cursor is in - idx_type idx() const { return back().idx(); } + idx_type idx() const { return top().idx(); } /// return the cell of the inset this cursor is in - idx_type & idx() { return back().idx(); } + idx_type & idx() { return top().idx(); } /// return the last possible cell in this inset idx_type lastidx() const; /// return the paragraph this cursor is in - pit_type pit() const { return back().pit(); } + pit_type pit() const { return top().pit(); } /// return the paragraph this cursor is in - pit_type & pit() { return back().pit(); } + pit_type & pit() { return top().pit(); } /// return the last possible paragraph in this inset pit_type lastpit() const; /// return the position within the paragraph - pos_type pos() const { return back().pos(); } + pos_type pos() const { return top().pos(); } /// return the position within the paragraph - pos_type & pos() { return back().pos(); } + pos_type & pos() { return top().pos(); } /// return the last position within the paragraph pos_type lastpos() const; @@ -191,6 +217,8 @@ public: /// output friend std::ostream & operator<<(std::ostream & os, DocIterator const & cur); + friend bool operator==(DocIterator const &, DocIterator const &); + friend class StableDocIterator; private: InsetBase * inset_; }; @@ -198,6 +226,20 @@ private: DocIterator doc_iterator_begin(InsetBase & inset); DocIterator doc_iterator_end(InsetBase & inset); + + +inline +bool operator==(DocIterator const & di1, DocIterator const & di2) +{ + return di1.slices_ == di2.slices_; +} + + +inline +bool operator!=(DocIterator const & di1, DocIterator const & di2) +{ + return !(di1 == di2); +} // The difference to a ('non stable') DocIterator is the removed
-- Lgb