3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Vincent van Ravesteijn
8 * Full author contact details are available in file CREDITS.
15 #include "BufferParams.h"
18 #include "insets/InsetText.h"
20 #include "support/lassert.h"
21 #include "support/qstring_helpers.h"
23 #include <boost/next_prior.hpp>
26 using namespace lyx::support;
38 static void step(DocIterator & dit, Direction direction)
40 if (direction == Forward)
41 dit.top().forwardPos();
43 dit.top().backwardPos();
47 static void step(DocIterator & dit, DocIterator const & end, Direction direction)
55 * A pair of two DocIterators that form a range.
59 DocRange(DocIterator from_, DocIterator to_)
60 : from(from_), to(to_)
63 DocRange(Buffer const * buf)
65 from = doc_iterator_begin(buf);
66 to = doc_iterator_end(buf);
71 Text * text() const { return from.text(); }
73 bool empty() const { return to <= from; }
75 size_t length() const;
77 /// The begin of the range
79 /// The end of the range
84 size_t DocRange::length() const
86 ParagraphList const & ps = from.text()->paragraphs();
88 pit_type pit = from.pit();
89 pit_type const endpit = to.pit();
90 for (; pit < endpit; ++pit)
91 length += ps[pit].size() + 1;
92 length += to.pos() - from.pos();
101 DocPair(DocIterator o_, DocIterator n_)
105 bool operator!=(DocPair const & rhs) {
106 // this might not be intuitive but correct for our purpose
107 return o != rhs.o && n != rhs.n;
111 DocPair & operator++()
118 DocPair & operator--()
131 * A pair of two DocRanges.
135 DocRangePair(DocRange o_, DocRange n_)
139 DocRangePair(DocPair from, DocPair to)
140 : o(from.o, to.o), n(from.n, to.n)
143 DocRangePair(Buffer const * o_buf, Buffer const * n_buf)
147 /// Returns the from pair
148 DocPair from() const { return DocPair(o.from, n.from); }
150 /// Returns the to pair
151 DocPair to() const { return DocPair(o.to, n.to); }
158 static DocRangePair stepIntoInset(DocPair const & inset_location)
160 DocRangePair rp(inset_location, inset_location);
161 rp.o.from.forwardPos();
162 rp.n.from.forwardPos();
163 step(rp.o.to, Forward);
164 step(rp.n.to, Forward);
165 rp.o.to.backwardPos();
166 rp.n.to.backwardPos();
172 * This class is designed to hold a vector that has both positive as
173 * negative indices. It is internally represented as two vectors, one
174 * for non-zero indices and one for negative indices. In this way, the
175 * vector can grow in both directions.
176 * If an index is not available in the vector, the default value is
177 * returned. If an object is put in the vector beyond its size, the
178 * empty spots in between are also filled with the default value.
185 void reset(T const & def)
192 /// Gets the value at index. If it is not in the vector
193 /// the default value is inserted and returned.
194 T & operator[](int index) {
195 vector<T> & V = index >= 0 ? Vp_ : Vn_;
196 unsigned int const ii = index >= 0 ? index : -index - 1;
197 while (ii >= V.size())
198 V.push_back(default_);
203 /// The vector for positive indices
205 /// The vector for negative indices
207 /// The default value that is inserted in the vector
208 /// if more space is needed
214 * The implementation of the algorithm that does the comparison
215 * between two documents.
217 class Compare::Impl {
220 Impl(Compare const & compare)
221 : abort_(false), compare_(compare), recursion_level_(0), D_(0)
227 // Algorithm to find the shortest edit string. This algorithm
228 // only needs a linear amount of memory (linear with the sum
229 // of the number of characters in the two paragraph-lists).
230 bool diff(Buffer const * new_buf, Buffer const * old_buf,
231 Buffer const * dest_buf);
233 /// Set to true to cancel the algorithm
239 status += toqstr("recursion level:") + " " + QString::number(recursion_level_)
240 + " " + toqstr("differences:") + " " + QString::number(D_);
245 /// Finds the middle snake and returns the length of the
246 /// shortest edit script.
247 int findMiddleSnake(DocRangePair const & rp, DocPair & middle_snake);
255 /// Retrieve the middle snake when there is overlap between
256 /// the forward and backward path.
257 SnakeResult retrieveMiddleSnake(int k, int D, Direction direction,
258 DocPair & middle_snake);
260 /// Find the the furthest reaching D-path (number of horizontal
261 /// and vertical steps; differences between the old and new
262 /// document) in the k-diagonal (vertical minus horizontal steps).
263 void furthestDpathKdiagonal(int D, int k,
264 DocRangePair const & rp, Direction direction);
266 /// Is there overlap between the forward and backward path
267 bool overlap(int k, int D);
269 /// This function is called recursively by a divide and conquer
270 /// algorithm. Each time, the string is divided into two split
271 /// around the middle snake.
272 void diff_i(DocRangePair const & rp);
274 /// Processes the splitted chunks. It either adds them as deleted,
275 /// as added, or call diff_i for further processing.
276 void diffPart(DocRangePair const & rp);
278 /// Runs the algorithm for the inset located at /c it and /c it_n
279 /// and adds the result to /c pars.
280 void diffInset(Inset * inset, DocPair const & p);
282 /// Adds the snake to the destination buffer. The algorithm will
283 /// recursively be applied to any InsetTexts that are within the snake.
284 void processSnake(DocRangePair const & rp);
286 /// Writes the range to the destination buffer
287 void writeToDestBuffer(DocRange const & range,
288 Change::Type type = Change::UNCHANGED);
290 /// Writes the paragraph list to the destination buffer
291 void writeToDestBuffer(ParagraphList const & copy_pars) const;
293 /// The length of the old chunk currently processed
295 /// The length of the new chunk currently processed
297 /// The offset diagonal of the reverse path of the
298 /// currently processed chunk
299 int offset_reverse_diagonal_;
300 /// Is the offset odd or even ?
303 /// The thread object, used to emit signals to the GUI
304 Compare const & compare_;
306 /// The buffer containing text that will be marked as old
307 Buffer const * old_buf_;
308 /// The buffer containing text that will be marked as new
309 Buffer const * new_buf_;
310 /// The buffer containing text that will be marked as new
311 Buffer const * dest_buf_;
313 /// The paragraph list of the destination buffer
314 ParagraphList * dest_pars_;
316 /// The level of recursion
317 int recursion_level_;
319 /// The number of nested insets at this level
320 int nested_inset_level_;
322 /// The position/snake in the old/new document
323 /// of the forward/reverse search
324 compl_vector<DocIterator> ofp;
325 compl_vector<DocIterator> nfp;
326 compl_vector<DocIterator> ofs;
327 compl_vector<DocIterator> nfs;
328 compl_vector<DocIterator> orp;
329 compl_vector<DocIterator> nrp;
330 compl_vector<DocIterator> ors;
331 compl_vector<DocIterator> nrs;
333 /// The number of differences in the path the algorithm
334 /// is currently processing.
338 /////////////////////////////////////////////////////////////////////
342 /////////////////////////////////////////////////////////////////////
344 Compare::Compare(Buffer const * new_buf, Buffer const * old_buf,
345 Buffer * const dest_buf, CompareOptions const & options)
346 : new_buffer(new_buf), old_buffer(old_buf), dest_buffer(dest_buf),
347 options_(options), pimpl_(new Impl(*this))
349 connect(&status_timer_, SIGNAL(timeout()),
350 this, SLOT(doStatusMessage()));
351 status_timer_.start(1000);
355 void Compare::doStatusMessage()
357 statusMessage(pimpl_->status());
363 if (!dest_buffer || !new_buffer || !old_buffer)
366 // Copy the buffer params to the new buffer
367 dest_buffer->params() = options_.settings_from_new
368 ? new_buffer->params() : old_buffer->params();
376 finished(pimpl_->abort_);
381 int Compare::doCompare()
383 return pimpl_->diff(new_buffer, old_buffer, dest_buffer);
387 void Compare::abort()
389 pimpl_->abort_ = true;
390 condition_.wakeOne();
392 pimpl_->abort_ = false;
396 static void getParagraphList(DocRange const & range,
397 ParagraphList & pars)
399 // Clone the paragraphs within the selection.
400 pit_type startpit = range.from.pit();
401 pit_type endpit = range.to.pit();
402 ParagraphList const & ps_ = range.text()->paragraphs();
403 ParagraphList tmp_pars(boost::next(ps_.begin(), startpit),
404 boost::next(ps_.begin(), endpit + 1));
406 // Remove the end of the last paragraph; afterwards, remove the
407 // beginning of the first paragraph. Keep this order - there may only
409 Paragraph & back = tmp_pars.back();
410 back.eraseChars(range.to.pos(), back.size(), false);
411 Paragraph & front = tmp_pars.front();
412 front.eraseChars(0, range.from.pos(), false);
414 pars.insert(pars.begin(), tmp_pars.begin(), tmp_pars.end());
418 static bool equal(Inset const * i_o, Inset const * i_n)
423 // Different types of insets
424 if (i_o->lyxCode() != i_n->lyxCode())
427 // Editable insets are assumed to be the same as they are of the
428 // same type. If we later on decide that we insert them in the
429 // document as being unchanged, we will run the algorithm on the
430 // contents of the two insets.
431 // FIXME: This fails if the parameters of the insets differ.
432 // FIXME: We do not recurse into InsetTabulars.
433 // FIXME: We need methods inset->equivalent(inset).
434 if (i_o->editable() && !i_o->asInsetMath()
435 && i_o->asInsetText())
442 return o_os.str() == n_os.str();
446 static bool equal(DocIterator & o, DocIterator & n) {
447 // Explicitly check for this, so we won't call
448 // Paragraph::getChar for the last pos.
449 bool const o_lastpos = o.pos() == o.lastpos();
450 bool const n_lastpos = n.pos() == n.lastpos();
451 if (o_lastpos || n_lastpos)
452 return o_lastpos && n_lastpos;
454 Paragraph const & old_par = o.text()->getPar(o.pit());
455 Paragraph const & new_par = n.text()->getPar(n.pit());
457 char_type const c_o = old_par.getChar(o.pos());
458 char_type const c_n = new_par.getChar(n.pos());
462 if (old_par.isInset(o.pos())) {
463 Inset const * i_o = old_par.getInset(o.pos());
464 Inset const * i_n = new_par.getInset(n.pos());
467 return equal(i_o, i_n);
470 Font fo = old_par.getFontSettings(o.buffer()->params(), o.pos());
471 Font fn = new_par.getFontSettings(n.buffer()->params(), n.pos());
476 /// Traverses a snake in a certain direction. p points to a
477 /// position in the old and new file and they are synchronously
478 /// moved along the snake. The function returns true if a snake
480 static bool traverseSnake(DocPair & p, DocRangePair const & range,
484 DocPair const & p_end =
485 direction == Forward ? range.to() : range.from();
488 if (direction == Backward)
490 if (!equal(p.o, p.n)) {
491 if (direction == Backward)
495 if (direction == Forward)
503 /////////////////////////////////////////////////////////////////////
507 /////////////////////////////////////////////////////////////////////
510 void Compare::Impl::furthestDpathKdiagonal(int D, int k,
511 DocRangePair const & rp, Direction direction)
513 compl_vector<DocIterator> & op = direction == Forward ? ofp : orp;
514 compl_vector<DocIterator> & np = direction == Forward ? nfp : nrp;
515 compl_vector<DocIterator> & os = direction == Forward ? ofs : ors;
516 compl_vector<DocIterator> & ns = direction == Forward ? nfs : nrs;
518 // A vertical step means stepping one character in the new document.
519 bool vertical_step = k == -D;
520 if (!vertical_step && k != D) {
521 vertical_step = direction == Forward
522 ? op[k - 1] < op[k + 1] : op[k - 1] > op[k + 1];
525 // Where do we take the step from ?
526 int const kk = vertical_step ? k + 1 : k - 1;
527 DocPair p(op[kk], np[kk]);
528 DocPair const s(os[kk], ns[kk]);
530 // If D==0 we simulate a vertical step from (0,-1) by doing nothing.
533 if (vertical_step && direction == Forward)
534 step(p.n, rp.n.to, direction);
535 else if (vertical_step && direction == Backward)
536 step(p.n, rp.n.from, direction);
537 else if (!vertical_step && direction == Forward)
538 step(p.o, rp.o.to, direction);
539 else if (!vertical_step && direction == Backward)
540 step(p.o, rp.o.from, direction);
544 if (traverseSnake(p, rp, direction)) {
549 // Copy last snake from the previous step
554 //Record new position
560 bool Compare::Impl::overlap(int k, int D)
562 // To generalize for the forward and reverse checks
563 int kk = offset_reverse_diagonal_ - k;
565 // Can we have overlap ?
566 if (kk <= D && kk >= -D) {
567 // Do we have overlap ?
569 return ofp[k] >= orp[kk] && nfp[k] >= nrp[kk];
571 return ofp[kk] >= orp[k] && nfp[kk] >= nrp[k];
577 Compare::Impl::SnakeResult Compare::Impl::retrieveMiddleSnake(
578 int k, int D, Direction direction, DocPair & middle_snake)
580 compl_vector<DocIterator> & os = direction == Forward ? ofs : ors;
581 compl_vector<DocIterator> & ns = direction == Forward ? nfs : nrs;
582 compl_vector<DocIterator> & os_r = direction == Forward ? ors : ofs;
583 compl_vector<DocIterator> & ns_r = direction == Forward ? nrs : nfs;
585 // The diagonal while doing the backward search
586 int kk = -k + offset_reverse_diagonal_;
588 // Did we find a snake ?
589 if (os[k].empty() && os_r[kk].empty()) {
590 // No, there is no snake at all, in which case
591 // the length of the shortest edit script is M+N.
592 LASSERT(2 * D - odd_offset_ == M_ + N_, /**/);
597 // Yes, but there is only 1 snake and we found it in the
599 middle_snake.o = os_r[kk];
600 middle_snake.n = ns_r[kk];
604 middle_snake.o = os[k];
605 middle_snake.n = ns[k];
610 int Compare::Impl::findMiddleSnake(DocRangePair const & rp,
611 DocPair & middle_snake)
613 // The lengths of the old and new chunks.
617 // Forward paths are centered around the 0-diagonal; reverse paths
618 // are centered around the diagonal N - M. (Delta in the article)
619 offset_reverse_diagonal_ = N_ - M_;
621 // If the offset is odd, only check for overlap while extending forward
622 // paths, otherwise only check while extending reverse paths.
623 odd_offset_ = (offset_reverse_diagonal_ % 2 != 0);
625 ofp.reset(rp.o.from);
626 nfp.reset(rp.n.from);
627 ofs.reset(DocIterator());
628 nfs.reset(DocIterator());
631 ors.reset(DocIterator());
632 nrs.reset(DocIterator());
634 // In the formula below, the "+ 1" ensures we round like ceil()
635 int const D_max = (M_ + N_ + 1)/2;
636 // D is the number of horizontal and vertical steps, i.e.
637 // different characters in the old and new chunk.
638 for (int D = 0; D <= D_max; ++D) {
639 // to be used in the status messages
642 // Forward and reverse paths
643 for (int f = 0; f < 2; ++f) {
644 Direction direction = f == 0 ? Forward : Backward;
646 // Diagonals between -D and D can be reached by a D-path
647 for (int k = -D; k <= D; k += 2) {
648 // Find the furthest reaching D-path on this diagonal
649 furthestDpathKdiagonal(D, k, rp, direction);
651 // Only check for overlap for forward paths if the offset is odd
652 // and only for reverse paths if the offset is even.
653 if (odd_offset_ == (direction == Forward)) {
655 // Do the forward and backward paths overlap ?
656 if (overlap(k, D - odd_offset_)) {
657 retrieveMiddleSnake(k, D, direction, middle_snake);
658 return 2 * D - odd_offset_;
666 // This should never be reached
671 bool Compare::Impl::diff(Buffer const * new_buf, Buffer const * old_buf,
672 Buffer const * dest_buf)
674 if (!new_buf || !old_buf || !dest_buf)
679 dest_buf_ = dest_buf;
680 dest_pars_ = &dest_buf->inset().asInsetText()->paragraphs();
683 recursion_level_ = 0;
684 nested_inset_level_ = 0;
686 DocRangePair rp(old_buf_, new_buf_);
688 DocPair from = rp.from();
689 traverseSnake(from, rp, Forward);
690 DocRangePair const snake(rp.from(), from);
693 // Start the recursive algorithm
694 DocRangePair rp_new(from, rp.to());
695 if (!rp_new.o.empty() || !rp_new.n.empty())
698 for (pit_type p = 0; p < (pit_type)dest_pars_->size(); ++p) {
699 (*dest_pars_)[p].setBuffer(const_cast<Buffer &>(*dest_buf));
700 (*dest_pars_)[p].setInsetOwner(&dest_buf_->inset());
707 void Compare::Impl::diff_i(DocRangePair const & rp)
713 DocPair middle_snake;
715 // Divides the problem into two smaller problems, split around
716 // the snake in the middle.
717 int const L_ses = findMiddleSnake(rp, middle_snake);
719 // Set maximum of progress bar
720 if (++recursion_level_ == 1)
721 compare_.progressMax(L_ses);
723 // There are now three possibilities: the strings were the same,
724 // the strings were completely different, or we found a middle
725 // snake and we can split the string into two parts to process.
727 // Two the same strings (this must be a very rare case, because
728 // usually this will be part of a snake adjacent to these strings).
729 writeToDestBuffer(rp.o);
731 else if (middle_snake.o.empty()) {
732 // Two totally different strings
733 writeToDestBuffer(rp.o, Change::DELETED);
734 writeToDestBuffer(rp.n, Change::INSERTED);
737 // Retrieve the complete snake
738 DocPair first_part_end = middle_snake;
739 traverseSnake(first_part_end, rp, Backward);
740 DocRangePair first_part(rp.from(), first_part_end);
742 DocPair second_part_begin = middle_snake;
743 traverseSnake(second_part_begin, rp, Forward);
744 DocRangePair second_part(second_part_begin, rp.to());
746 // Split the string in three parts:
747 // 1. in front of the snake
748 diffPart(first_part);
750 // 2. the snake itself, and
751 DocRangePair const snake(first_part.to(), second_part.from());
754 // 3. behind the snake.
755 diffPart(second_part);
761 void Compare::Impl::diffPart(DocRangePair const & rp)
763 // Is there a finite length string in both buffers, if not there
764 // is an empty string and we write the other one to the buffer.
765 if (!rp.o.empty() && !rp.n.empty())
768 else if (!rp.o.empty())
769 writeToDestBuffer(rp.o, Change::DELETED);
771 else if (!rp.n.empty())
772 writeToDestBuffer(rp.n, Change::INSERTED);
776 void Compare::Impl::diffInset(Inset * inset, DocPair const & p)
778 // Find the dociterators for the beginning and the
779 // end of the inset, for the old and new document.
780 DocRangePair const rp = stepIntoInset(p);
782 // Recurse into the inset. Temporarily replace the dest_pars
783 // paragraph list by the paragraph list of the nested inset.
784 ParagraphList * backup_dest_pars = dest_pars_;
785 dest_pars_ = &inset->asInsetText()->text().paragraphs();
788 ++nested_inset_level_;
790 --nested_inset_level_;
792 dest_pars_ = backup_dest_pars;
796 void Compare::Impl::processSnake(DocRangePair const & rp)
799 getParagraphList(rp.o, pars);
801 // Find insets in this paragaph list
802 DocPair it = rp.from();
803 for (; it.o < rp.o.to; ++it) {
804 Inset * inset = it.o.text()->getPar(it.o.pit()).getInset(it.o.pos());
805 if (inset && inset->editable() && inset->asInsetText()) {
806 // Find the inset in the paragraph list that will be pasted into
807 // the final document. The contents of the inset will be replaced
808 // by the output of the algorithm below.
809 pit_type const pit = it.o.pit() - rp.o.from.pit();
810 pos_type const pos = pit ? it.o.pos() : it.o.pos() - rp.o.from.pos();
811 inset = pars[pit].getInset(pos);
812 LASSERT(inset, /**/);
813 diffInset(inset, it);
816 writeToDestBuffer(pars);
820 void Compare::Impl::writeToDestBuffer(DocRange const & range,
824 getParagraphList(range, pars);
829 ParagraphList::iterator it = pars.begin();
830 for (; it != pars.end(); ++it) {
831 it->setChange(Change(type));
835 writeToDestBuffer(pars);
837 if (nested_inset_level_ == 0)
838 compare_.progress(size);
842 void Compare::Impl::writeToDestBuffer(ParagraphList const & pars) const
844 pit_type const pit = dest_pars_->size() - 1;
845 dest_pars_->insert(dest_pars_->end(), pars.begin(), pars.end());
847 mergeParagraph(dest_buf_->params(), *dest_pars_, pit);
851 #include "moc_Compare.cpp"