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.
16 #include "BufferParams.h"
18 #include "CutAndPaste.h"
19 #include "ErrorList.h"
22 #include "insets/InsetText.h"
24 #include "support/docstream.h"
25 #include "support/lassert.h"
26 #include "support/lyxalgo.h"
27 #include "support/qstring_helpers.h"
30 using namespace lyx::support;
42 static void step(DocIterator & dit, Direction direction)
44 if (direction == Forward)
45 dit.top().forwardPos();
47 dit.top().backwardPos();
51 static void step(DocIterator & dit, DocIterator const & end, Direction direction)
59 * A pair of two DocIterators that form a range.
63 DocRange(DocIterator const & from_, DocIterator const & to_)
64 : from(from_), to(to_)
67 DocRange(Buffer const * buf) :
68 from(doc_iterator_begin(buf)),
69 to(doc_iterator_end(buf))
75 Text * text() const { return from.text(); }
77 bool empty() const { return to <= from; }
79 size_t length() const;
81 /// The begin of the range
83 /// The end of the range
88 size_t DocRange::length() const
90 ParagraphList const & ps = from.text()->paragraphs();
92 pit_type pit = from.pit();
93 pit_type const endpit = to.pit();
94 for (; pit < endpit; ++pit)
95 length += ps[pit].size() + 1;
96 length += to.pos() - from.pos();
106 DocPair(DocIterator o_, DocIterator n_)
110 bool operator!=(DocPair const & rhs)
112 // this might not be intuitive but correct for our purpose
113 return o != rhs.o && n != rhs.n;
117 DocPair & operator++()
124 DocPair & operator--()
137 * A pair of two DocRanges.
141 DocRangePair(DocRange const & o_, DocRange const & n_)
145 DocRangePair(DocPair const & from, DocPair const & to)
146 : o(from.o, to.o), n(from.n, to.n)
149 DocRangePair(Buffer const * o_buf, Buffer const * n_buf)
153 /// Returns the from pair
156 return DocPair(o.from, n.from);
159 /// Returns the to pair
162 return DocPair(o.to, n.to);
170 static DocRangePair stepIntoInset(DocPair const & inset_location)
172 DocRangePair rp(inset_location, inset_location);
173 rp.o.from.forwardPos();
174 rp.n.from.forwardPos();
175 step(rp.o.to, Forward);
176 step(rp.n.to, Forward);
177 rp.o.to.backwardPos();
178 rp.n.to.backwardPos();
184 * This class is designed to hold a vector that has both positive as
185 * negative indices. It is internally represented as two vectors, one
186 * for non-zero indices and one for negative indices. In this way, the
187 * vector can grow in both directions.
188 * If an index is not available in the vector, the default value is
189 * returned. If an object is put in the vector beyond its size, the
190 * empty spots in between are also filled with the default value.
198 void reset(T const & def)
205 /// Gets the value at index. If it is not in the vector
206 /// the default value is inserted and returned.
207 T & operator[](int index) {
208 vector<T> & V = index >= 0 ? Vp_ : Vn_;
209 unsigned int const ii = index >= 0 ? index : -index - 1;
210 while (ii >= V.size())
211 V.push_back(default_);
216 /// The vector for positive indices
218 /// The vector for negative indices
220 /// The default value that is inserted in the vector
221 /// if more space is needed
227 * The implementation of the algorithm that does the comparison
228 * between two documents.
230 class Compare::Impl {
233 Impl(Compare const & compare)
234 : abort_(false), n_(0), m_(0), offset_reverse_diagonal_(0),
235 odd_offset_(0), compare_(compare),
236 old_buf_(0), new_buf_(0), dest_buf_(0), dest_pars_(0),
237 recursion_level_(0), nested_inset_level_(0), D_(0)
244 // Algorithm to find the shortest edit string. This algorithm
245 // only needs a linear amount of memory (linear with the sum
246 // of the number of characters in the two paragraph-lists).
247 bool diff(Buffer const * new_buf, Buffer const * old_buf,
248 Buffer const * dest_buf);
250 /// Set to true to cancel the algorithm
257 status += toqstr("recursion level:") + " " + QString::number(recursion_level_)
258 + " " + toqstr("differences:") + " " + QString::number(D_);
263 /// Finds the middle snake and returns the length of the
264 /// shortest edit script.
265 int findMiddleSnake(DocRangePair const & rp, DocPair & middle_snake);
273 /// Retrieve the middle snake when there is overlap between
274 /// the forward and backward path.
275 SnakeResult retrieveMiddleSnake(int k, int D, Direction direction,
276 DocPair & middle_snake);
278 /// Find the furthest reaching D-path (number of horizontal
279 /// and vertical steps; differences between the old and new
280 /// document) in the k-diagonal (vertical minus horizontal steps).
281 void furthestDpathKdiagonal(int D, int k,
282 DocRangePair const & rp, Direction direction);
284 /// Is there overlap between the forward and backward path
285 bool overlap(int k, int D);
287 /// This function is called recursively by a divide and conquer
288 /// algorithm. Each time, the string is divided into two split
289 /// around the middle snake.
290 void diff_i(DocRangePair const & rp);
292 /// Processes the split chunks. It either adds them as deleted,
293 /// as added, or call diff_i for further processing.
294 void diffPart(DocRangePair const & rp);
296 /// Runs the algorithm for the inset located at /c it and /c it_n
297 /// and adds the result to /c pars.
298 void diffInset(Inset * inset, DocPair const & p);
300 /// Adds the snake to the destination buffer. The algorithm will
301 /// recursively be applied to any InsetTexts that are within the snake.
302 void processSnake(DocRangePair const & rp);
304 /// Writes the range to the destination buffer
305 void writeToDestBuffer(DocRange const & range,
306 Change::Type type = Change::UNCHANGED);
308 /// Writes the paragraph list to the destination buffer
309 void writeToDestBuffer(ParagraphList const & copy_pars) const;
311 /// The length of the old chunk currently processed
313 /// The length of the new chunk currently processed
315 /// The offset diagonal of the reverse path of the
316 /// currently processed chunk
317 int offset_reverse_diagonal_;
318 /// Is the offset odd or even ?
321 /// The thread object, used to emit signals to the GUI
322 Compare const & compare_;
324 /// The buffer containing text that will be marked as old
325 Buffer const * old_buf_;
326 /// The buffer containing text that will be marked as new
327 Buffer const * new_buf_;
328 /// The buffer containing text that will be marked as new
329 Buffer const * dest_buf_;
331 /// The paragraph list of the destination buffer
332 ParagraphList * dest_pars_;
334 /// The level of recursion
335 int recursion_level_;
337 /// The number of nested insets at this level
338 int nested_inset_level_;
340 /// The position/snake in the old/new document
341 /// of the forward/reverse search
342 compl_vector<DocIterator> ofp;
343 compl_vector<DocIterator> nfp;
344 compl_vector<DocIterator> ofs;
345 compl_vector<DocIterator> nfs;
346 compl_vector<DocIterator> orp;
347 compl_vector<DocIterator> nrp;
348 compl_vector<DocIterator> ors;
349 compl_vector<DocIterator> nrs;
351 /// The number of differences in the path the algorithm
352 /// is currently processing.
356 /////////////////////////////////////////////////////////////////////
360 /////////////////////////////////////////////////////////////////////
362 Compare::Compare(Buffer const * new_buf, Buffer const * old_buf,
363 Buffer * const dest_buf, CompareOptions const & options)
364 : new_buffer(new_buf), old_buffer(old_buf), dest_buffer(dest_buf),
365 options_(options), pimpl_(new Impl(*this))
367 connect(&status_timer_, SIGNAL(timeout()),
368 this, SLOT(doStatusMessage()));
369 status_timer_.start(1000);
373 void Compare::doStatusMessage()
375 statusMessage(pimpl_->status());
381 if (!dest_buffer || !new_buffer || !old_buffer)
384 // Copy the buffer params to the destination buffer
385 dest_buffer->params() = options_.settings_from_new
386 ? new_buffer->params() : old_buffer->params();
388 // We do not want to share the DocumentClass with the other Buffer
390 DocumentClassConstPtr olddc = dest_buffer->params().documentClassPtr();
391 dest_buffer->params().makeDocumentClass();
393 cap::switchBetweenClasses(
394 olddc, dest_buffer->params().documentClassPtr(),
395 static_cast<InsetText &>(dest_buffer->inset()), el);
397 // Copy extra authors to the destination buffer
398 AuthorList const & extra_authors = options_.settings_from_new ?
399 old_buffer->params().authors() : new_buffer->params().authors();
400 AuthorList::Authors::const_iterator it = extra_authors.begin();
401 for (; it != extra_authors.end(); ++it)
402 dest_buffer->params().authors().record(*it);
410 finished(pimpl_->abort_);
415 int Compare::doCompare()
417 return pimpl_->diff(new_buffer, old_buffer, dest_buffer);
421 void Compare::abort()
423 pimpl_->abort_ = true;
424 condition_.wakeOne();
426 pimpl_->abort_ = false;
430 static void getParagraphList(DocRange const & range,
431 ParagraphList & pars)
433 // Clone the paragraphs within the selection.
434 pit_type startpit = range.from.pit();
435 pit_type endpit = range.to.pit();
436 ParagraphList const & ps_ = range.text()->paragraphs();
437 ParagraphList tmp_pars(lyx::next(ps_.begin(), startpit),
438 lyx::next(ps_.begin(), endpit + 1));
440 // Remove the end of the last paragraph; afterwards, remove the
441 // beginning of the first paragraph. Keep this order - there may only
443 Paragraph & back = tmp_pars.back();
444 back.eraseChars(range.to.pos(), back.size(), false);
445 Paragraph & front = tmp_pars.front();
446 front.eraseChars(0, range.from.pos(), false);
448 pars.insert(pars.begin(), tmp_pars.begin(), tmp_pars.end());
452 static bool equal(Inset const * i_o, Inset const * i_n)
457 // Different types of insets
458 if (i_o->lyxCode() != i_n->lyxCode())
461 // Editable insets are assumed to be the same as they are of the
462 // same type. If we later on decide that we insert them in the
463 // document as being unchanged, we will run the algorithm on the
464 // contents of the two insets.
465 // FIXME: This fails if the parameters of the insets differ.
466 // FIXME: We do not recurse into InsetTabulars.
467 // FIXME: We need methods inset->equivalent(inset).
468 if (i_o->editable() && !i_o->asInsetMath()
469 && i_o->asInsetText())
476 return o_os.str() == n_os.str();
480 static bool equal(DocIterator & o, DocIterator & n)
482 // Explicitly check for this, so we won't call
483 // Paragraph::getChar for the last pos.
484 bool const o_lastpos = o.pos() == o.lastpos();
485 bool const n_lastpos = n.pos() == n.lastpos();
486 if (o_lastpos || n_lastpos)
487 return o_lastpos && n_lastpos;
489 Paragraph const & old_par = o.text()->getPar(o.pit());
490 Paragraph const & new_par = n.text()->getPar(n.pit());
492 char_type const c_o = old_par.getChar(o.pos());
493 char_type const c_n = new_par.getChar(n.pos());
497 if (old_par.isInset(o.pos())) {
498 Inset const * i_o = old_par.getInset(o.pos());
499 Inset const * i_n = new_par.getInset(n.pos());
502 return equal(i_o, i_n);
505 Font fo = old_par.getFontSettings(o.buffer()->params(), o.pos());
506 Font fn = new_par.getFontSettings(n.buffer()->params(), n.pos());
511 /// Traverses a snake in a certain direction. p points to a
512 /// position in the old and new file and they are synchronously
513 /// moved along the snake. The function returns true if a snake
515 static bool traverseSnake(DocPair & p, DocRangePair const & range,
519 DocPair const & p_end =
520 direction == Forward ? range.to() : range.from();
523 if (direction == Backward)
525 if (!equal(p.o, p.n)) {
526 if (direction == Backward)
530 if (direction == Forward)
538 /////////////////////////////////////////////////////////////////////
542 /////////////////////////////////////////////////////////////////////
545 void Compare::Impl::furthestDpathKdiagonal(int D, int k,
546 DocRangePair const & rp, Direction direction)
548 compl_vector<DocIterator> & op = direction == Forward ? ofp : orp;
549 compl_vector<DocIterator> & np = direction == Forward ? nfp : nrp;
550 compl_vector<DocIterator> & os = direction == Forward ? ofs : ors;
551 compl_vector<DocIterator> & ns = direction == Forward ? nfs : nrs;
553 // A vertical step means stepping one character in the new document.
554 bool vertical_step = k == -D;
555 if (!vertical_step && k != D) {
556 vertical_step = direction == Forward
557 ? op[k - 1] < op[k + 1] : op[k - 1] > op[k + 1];
560 // Where do we take the step from ?
561 int const kk = vertical_step ? k + 1 : k - 1;
562 DocPair p(op[kk], np[kk]);
563 DocPair const s(os[kk], ns[kk]);
565 // If D==0 we simulate a vertical step from (0,-1) by doing nothing.
568 if (vertical_step && direction == Forward)
569 step(p.n, rp.n.to, direction);
570 else if (vertical_step && direction == Backward)
571 step(p.n, rp.n.from, direction);
572 else if (!vertical_step && direction == Forward)
573 step(p.o, rp.o.to, direction);
574 else if (!vertical_step && direction == Backward)
575 step(p.o, rp.o.from, direction);
579 if (traverseSnake(p, rp, direction)) {
584 // Copy last snake from the previous step
589 //Record new position
595 bool Compare::Impl::overlap(int k, int D)
597 // To generalize for the forward and reverse checks
598 int kk = offset_reverse_diagonal_ - k;
600 // Can we have overlap ?
601 if (kk <= D && kk >= -D) {
602 // Do we have overlap ?
604 return ofp[k] >= orp[kk] && nfp[k] >= nrp[kk];
606 return ofp[kk] >= orp[k] && nfp[kk] >= nrp[k];
612 Compare::Impl::SnakeResult Compare::Impl::retrieveMiddleSnake(
613 int k, int D, Direction direction, DocPair & middle_snake)
615 compl_vector<DocIterator> & os = direction == Forward ? ofs : ors;
616 compl_vector<DocIterator> & ns = direction == Forward ? nfs : nrs;
617 compl_vector<DocIterator> & os_r = direction == Forward ? ors : ofs;
618 compl_vector<DocIterator> & ns_r = direction == Forward ? nrs : nfs;
620 // The diagonal while doing the backward search
621 int kk = -k + offset_reverse_diagonal_;
623 // Did we find a snake ?
624 if (os[k].empty() && os_r[kk].empty()) {
625 // No, there is no snake at all, in which case
626 // the length of the shortest edit script is M+N.
627 LATTEST(2 * D - odd_offset_ == m_ + n_);
632 // Yes, but there is only 1 snake and we found it in the
634 middle_snake.o = os_r[kk];
635 middle_snake.n = ns_r[kk];
639 middle_snake.o = os[k];
640 middle_snake.n = ns[k];
645 int Compare::Impl::findMiddleSnake(DocRangePair const & rp,
646 DocPair & middle_snake)
648 // The lengths of the old and new chunks.
652 // Forward paths are centered around the 0-diagonal; reverse paths
653 // are centered around the diagonal N - M. (Delta in the article)
654 offset_reverse_diagonal_ = n_ - m_;
656 // If the offset is odd, only check for overlap while extending forward
657 // paths, otherwise only check while extending reverse paths.
658 odd_offset_ = (offset_reverse_diagonal_ % 2 != 0);
660 ofp.reset(rp.o.from);
661 nfp.reset(rp.n.from);
662 ofs.reset(DocIterator());
663 nfs.reset(DocIterator());
666 ors.reset(DocIterator());
667 nrs.reset(DocIterator());
669 // In the formula below, the "+ 1" ensures we round like ceil()
670 int const D_max = (m_ + n_ + 1)/2;
671 // D is the number of horizontal and vertical steps, i.e.
672 // different characters in the old and new chunk.
673 for (int D = 0; D <= D_max; ++D) {
674 // to be used in the status messages
677 // Forward and reverse paths
678 for (int f = 0; f < 2; ++f) {
679 Direction direction = f == 0 ? Forward : Backward;
681 // Diagonals between -D and D can be reached by a D-path
682 for (int k = -D; k <= D; k += 2) {
683 // Find the furthest reaching D-path on this diagonal
684 furthestDpathKdiagonal(D, k, rp, direction);
686 // Only check for overlap for forward paths if the offset is odd
687 // and only for reverse paths if the offset is even.
688 if (odd_offset_ == (direction == Forward)) {
690 // Do the forward and backward paths overlap ?
691 if (overlap(k, D - odd_offset_)) {
692 retrieveMiddleSnake(k, D, direction, middle_snake);
693 return 2 * D - odd_offset_;
701 // This should never be reached
706 bool Compare::Impl::diff(Buffer const * new_buf, Buffer const * old_buf,
707 Buffer const * dest_buf)
709 if (!new_buf || !old_buf || !dest_buf)
714 dest_buf_ = dest_buf;
715 dest_pars_ = &dest_buf->inset().asInsetText()->paragraphs();
718 recursion_level_ = 0;
719 nested_inset_level_ = 0;
721 DocRangePair rp(old_buf_, new_buf_);
723 DocPair from = rp.from();
724 traverseSnake(from, rp, Forward);
725 DocRangePair const snake(rp.from(), from);
728 // Start the recursive algorithm
729 DocRangePair rp_new(from, rp.to());
730 if (!rp_new.o.empty() || !rp_new.n.empty())
733 for (pit_type p = 0; p < (pit_type)dest_pars_->size(); ++p) {
734 (*dest_pars_)[p].setBuffer(const_cast<Buffer &>(*dest_buf));
735 (*dest_pars_)[p].setInsetOwner(&dest_buf_->inset());
742 void Compare::Impl::diff_i(DocRangePair const & rp)
748 DocPair middle_snake;
750 // Divides the problem into two smaller problems, split around
751 // the snake in the middle.
752 int const L_ses = findMiddleSnake(rp, middle_snake);
754 // Set maximum of progress bar
755 if (++recursion_level_ == 1)
756 compare_.progressMax(L_ses);
758 // There are now three possibilities: the strings were the same,
759 // the strings were completely different, or we found a middle
760 // snake and we can split the string into two parts to process.
762 // Two the same strings (this must be a very rare case, because
763 // usually this will be part of a snake adjacent to these strings).
764 writeToDestBuffer(rp.o);
766 else if (middle_snake.o.empty()) {
767 // Two totally different strings
768 writeToDestBuffer(rp.o, Change::DELETED);
769 writeToDestBuffer(rp.n, Change::INSERTED);
772 // Retrieve the complete snake
773 DocPair first_part_end = middle_snake;
774 traverseSnake(first_part_end, rp, Backward);
775 DocRangePair first_part(rp.from(), first_part_end);
777 DocPair second_part_begin = middle_snake;
778 traverseSnake(second_part_begin, rp, Forward);
779 DocRangePair second_part(second_part_begin, rp.to());
781 // Split the string in three parts:
782 // 1. in front of the snake
783 diffPart(first_part);
785 // 2. the snake itself, and
786 DocRangePair const snake(first_part.to(), second_part.from());
789 // 3. behind the snake.
790 diffPart(second_part);
796 void Compare::Impl::diffPart(DocRangePair const & rp)
798 // Is there a finite length string in both buffers, if not there
799 // is an empty string and we write the other one to the buffer.
800 if (!rp.o.empty() && !rp.n.empty())
803 else if (!rp.o.empty())
804 writeToDestBuffer(rp.o, Change::DELETED);
806 else if (!rp.n.empty())
807 writeToDestBuffer(rp.n, Change::INSERTED);
811 void Compare::Impl::diffInset(Inset * inset, DocPair const & p)
813 // Find the dociterators for the beginning and the
814 // end of the inset, for the old and new document.
815 DocRangePair const rp = stepIntoInset(p);
817 // Recurse into the inset. Temporarily replace the dest_pars
818 // paragraph list by the paragraph list of the nested inset.
819 ParagraphList * backup_dest_pars = dest_pars_;
820 dest_pars_ = &inset->asInsetText()->text().paragraphs();
823 ++nested_inset_level_;
825 --nested_inset_level_;
827 dest_pars_ = backup_dest_pars;
831 void Compare::Impl::processSnake(DocRangePair const & rp)
834 getParagraphList(rp.o, pars);
836 // Find insets in this paragaph list
837 DocPair it = rp.from();
838 for (; it.o < rp.o.to; ++it) {
839 Inset * inset = it.o.text()->getPar(it.o.pit()).getInset(it.o.pos());
840 if (inset && inset->editable() && inset->asInsetText()) {
841 // Find the inset in the paragraph list that will be pasted into
842 // the final document. The contents of the inset will be replaced
843 // by the output of the algorithm below.
844 pit_type const pit = it.o.pit() - rp.o.from.pit();
845 pos_type const pos = pit ? it.o.pos() : it.o.pos() - rp.o.from.pos();
846 inset = pars[pit].getInset(pos);
847 LASSERT(inset, continue);
848 diffInset(inset, it);
851 writeToDestBuffer(pars);
855 void Compare::Impl::writeToDestBuffer(DocRange const & range,
859 getParagraphList(range, pars);
864 ParagraphList::iterator it = pars.begin();
865 for (; it != pars.end(); ++it) {
866 it->setChange(Change(type));
870 writeToDestBuffer(pars);
872 if (nested_inset_level_ == 0)
873 compare_.progress(size);
877 void Compare::Impl::writeToDestBuffer(ParagraphList const & pars) const
879 pit_type const pit = dest_pars_->size() - 1;
880 dest_pars_->insert(dest_pars_->end(), pars.begin(), pars.end());
882 mergeParagraph(dest_buf_->params(), *dest_pars_, pit);
886 #include "moc_Compare.cpp"