]> git.lyx.org Git - lyx.git/blobdiff - src/Compare.cpp
Part of r32942. We want to avoid reload flickering when
[lyx.git] / src / Compare.cpp
index cfbfdce45928b4a6d45c195300530338a44fe9b0..95eaf5abaef0f9cc1bf3eacfc89297b7348d8062 100644 (file)
@@ -21,6 +21,8 @@
 
 #include <boost/next_prior.hpp>
 
+#include <cmath>
+
 using namespace std;
 using namespace lyx::support;
 
@@ -82,30 +84,14 @@ public:
 
 size_t DocRange::length() const
 {
-       pit_type startpit = from.pit();
-       pit_type endpit = to.pit();
-       ParagraphList const & ps_ = from.text()->paragraphs();
-
-       ParagraphList pars(boost::next(ps_.begin(), startpit),
-                               boost::next(ps_.begin(), endpit + 1));
-
-       // Remove the end of the last paragraph; afterwards, remove the
-       // beginning of the first paragraph.
-       Paragraph & back = pars.back();
-       back.eraseChars(to.pos(), back.size(), false);
-       Paragraph & front = pars.front();
-       front.eraseChars(0, from.pos(), false);
-
-       ParagraphList::const_iterator pit = pars.begin();
-       ParagraphList::const_iterator end_it = pars.end();
-
+       ParagraphList const & ps = from.text()->paragraphs();
        size_t length = 0;
-       for (; pit != end_it; ++pit)
-               length += pit->size() + 1;
-
-       // The last paragraph has no paragraph-end
-       --length;
-       return length;  
+       pit_type pit = from.pit();
+       pit_type const endpit = to.pit();
+       for (; pit < endpit; ++pit)
+               length += ps[pit].size() + 1;
+       length += to.pos() - from.pos();
+       return length;
 }
 
 
@@ -183,6 +169,48 @@ static DocRangePair stepIntoInset(DocPair const & inset_location)
 }
 
 
+/**
+ *  This class is designed to hold a vector that has both positive as
+ *  negative indices. It is internally represented as two vectors, one
+ *  for non-zero indices and one for negative indices. In this way, the
+ *  vector can grow in both directions.
+ *    If an index is not available in the vector, the default value is
+ *  returned. If an object is put in the vector beyond its size, the
+ *  empty spots in between are also filled with the default value.
+ */
+template<class T>
+class compl_vector {
+public:
+       compl_vector() {}
+
+       void reset(T const & def)
+       {
+               default_ = def;
+               Vp_.clear();
+               Vn_.clear();
+       }
+
+       /// Gets the value at index. If it is not in the vector
+       /// the default value is inserted and returned.
+       T & operator[](int index) {
+               vector<T> & V = index >= 0 ? Vp_ : Vn_;
+               unsigned int const ii = index >= 0 ? index : -index - 1;
+               while (ii >= V.size())
+                       V.push_back(default_);
+               return V[ii];
+       }
+
+private:
+       /// The vector for positive indices
+       vector<T> Vp_;
+       /// The vector for negative indices
+       vector<T> Vn_;
+       /// The default value that is inserted in the vector
+       /// if more space is needed
+       T default_;
+};
+
+
 /**
  * The implementation of the algorithm that does the comparison
  * between two documents.
@@ -211,6 +239,26 @@ private:
        /// shortest edit script.
        int find_middle_snake(DocRangePair const & rp, DocPair & middle_snake);
 
+       enum SnakeResult {
+               NoSnake,
+               SingleSnake,
+               NormalSnake
+       };
+
+       /// Retrieve the middle snake when there is overlap between
+       /// the forward and backward path.
+       SnakeResult retrieve_middle_snake(int k, int D, Direction direction,
+               DocPair & middle_snake);
+       
+       /// Find the the furthest reaching D-path (number of horizontal
+       /// and vertical steps; differences between the old and new
+       /// document) in the k-diagonal (vertical minus horizontal steps).
+       void furthest_Dpath_kdiagonal(int D, int k,
+               DocRangePair const & rp, Direction direction);
+
+       /// Is there overlap between the forward and backward path
+       bool overlap(int k, int D);
+       
        /// This function is called recursively by a divide and conquer
        /// algorithm. Each time, the string is divided into two split
        /// around the middle snake.
@@ -239,6 +287,11 @@ private:
        int N_;
        /// The length of the new chunk currently processed
        int M_;
+       /// The offset diagonal of the reverse path of the
+       /// currently processed chunk
+       int offset_reverse_diagonal_;
+       /// Is the offset odd or even ?
+       bool odd_offset_;
 
        /// The thread object, used to emit signals to the GUI
        Compare const & compare_;
@@ -258,6 +311,17 @@ private:
 
        /// The number of nested insets at this level
        int nested_inset_level_;
+
+       /// The position/snake in the old/new document
+       /// of the forward/reverse search
+       compl_vector<DocIterator> ofp;
+       compl_vector<DocIterator> nfp;
+       compl_vector<DocIterator> ofs;
+       compl_vector<DocIterator> nfs;
+       compl_vector<DocIterator> orp;
+       compl_vector<DocIterator> nrp;
+       compl_vector<DocIterator> ors;
+       compl_vector<DocIterator> nrs;
 };
 
 /////////////////////////////////////////////////////////////////////
@@ -413,12 +477,159 @@ static bool traverse_snake(DocPair & p, DocRangePair const & range,
 //
 /////////////////////////////////////////////////////////////////////
 
+
+void Compare::Impl::furthest_Dpath_kdiagonal(int D, int k,
+        DocRangePair const & rp, Direction direction)
+{
+       compl_vector<DocIterator> & op = direction == Forward ? ofp : orp;
+       compl_vector<DocIterator> & np = direction == Forward ? nfp : nrp;
+       compl_vector<DocIterator> & os = direction == Forward ? ofs : ors;
+       compl_vector<DocIterator> & ns = direction == Forward ? nfs : nrs;
+
+       // A vertical step means stepping one character in the new document.
+       bool vertical_step = k == -D;
+       if (!vertical_step && k != D) {
+               vertical_step = direction == Forward
+                       ? op[k - 1] < op[k + 1] : op[k - 1] > op[k + 1];
+       }
+
+       // Where do we take the step from ?
+       int const kk = vertical_step ? k + 1 : k - 1;
+       DocPair p(op[kk], np[kk]);
+
+       // If D==0 we simulate a vertical step from (0,-1) by doing nothing.
+       if (D != 0) {
+               // Take a step
+               if (vertical_step && direction == Forward)
+                       step(p.n, rp.n.to, direction);
+               else if (vertical_step && direction == Backward)
+                       step(p.n, rp.n.from, direction);
+               else if (!vertical_step && direction == Forward)
+                       step(p.o, rp.o.to, direction);
+               else if (!vertical_step && direction == Backward)
+                       step(p.o, rp.o.from, direction);
+       }       
+       
+       // Traverse snake
+       if (traverse_snake(p, rp, direction)) {
+               // Record last snake
+               os[k] = p.o;
+               ns[k] = p.n;
+       } else {
+               // Copy last snake from the previous step
+               os[k] = os[kk];
+               ns[k] = ns[kk];
+       }
+
+       //Record new position
+       op[k] = p.o;
+       np[k] = p.n;
+}
+
+
+bool Compare::Impl::overlap(int k, int D)
+{
+       // To generalize for the forward and reverse checks
+       int kk = offset_reverse_diagonal_ - k;
+
+       // Can we have overlap ?
+       if (kk <= D && kk >= -D) {
+               // Do we have overlap ?
+               if (odd_offset_)
+                       return ofp[k] >= orp[kk] && nfp[k] >= nrp[kk];
+               else
+                       return ofp[kk] >= orp[k] && nfp[kk] >= nrp[k];
+       }
+       return false;
+}
+
+
+Compare::Impl::SnakeResult Compare::Impl::retrieve_middle_snake(
+       int k, int D, Direction direction, DocPair & middle_snake)
+{
+       compl_vector<DocIterator> & os = direction == Forward ? ofs : ors;
+       compl_vector<DocIterator> & ns = direction == Forward ? nfs : nrs;
+       compl_vector<DocIterator> & os_r = direction == Forward ? ors : ofs;
+       compl_vector<DocIterator> & ns_r = direction == Forward ? nrs : nfs;
+
+       // The diagonal while doing the backward search
+       int kk = -k + offset_reverse_diagonal_;
+
+       // Did we find a snake ?
+       if (os[k].empty() && os_r[kk].empty()) {
+               // No, there is no snake at all, in which case
+               // the length of the shortest edit script is M+N.
+               LASSERT(2 * D - odd_offset_ == M_ + N_, /**/);
+               return NoSnake;
+       } 
+       
+       if (os[k].empty()) {
+               // Yes, but there is only 1 snake and we found it in the
+               // reverse path.
+               middle_snake.o = os_r[kk];
+               middle_snake.n = ns_r[kk];
+               return SingleSnake;
+       }
+
+       middle_snake.o = os[k];
+       middle_snake.n = ns[k];
+       return NormalSnake;
+}
+
+
 int Compare::Impl::find_middle_snake(DocRangePair const & rp,
-       DocPair &)
+       DocPair & middle_snake)
 {
+       // The lengths of the old and new chunks.
        N_ = rp.o.length();
        M_ = rp.n.length();
-       return M_ + N_;
+
+       // Forward paths are centered around the 0-diagonal; reverse paths
+       // are centered around the diagonal N - M. (Delta in the article)
+       offset_reverse_diagonal_ = N_ - M_;
+
+       // If the offset is odd, only check for overlap while extending forward
+    // paths, otherwise only check while extending reverse paths.
+       odd_offset_ = (offset_reverse_diagonal_ % 2 != 0);
+
+       ofp.reset(rp.o.from);
+       nfp.reset(rp.n.from);
+       ofs.reset(DocIterator());
+       nfs.reset(DocIterator());
+       orp.reset(rp.o.to);
+       nrp.reset(rp.n.to);
+       ors.reset(DocIterator());
+       nrs.reset(DocIterator());
+
+       // D is the number of horizontal and vertical steps, i.e.
+       // different characters in the old and new chunk.
+       int const D_max = ceil(((double)M_ + N_)/2);
+       for (int D = 0; D <= D_max; ++D) {
+
+               // Forward and reverse paths
+               for (int f = 0; f < 2; ++f) {
+                       Direction direction = f == 0 ? Forward : Backward;
+
+                       // Diagonals between -D and D can be reached by a D-path
+                       for (int k = -D; k <= D; k += 2) {                      
+                               // Find the furthest reaching D-path on this diagonal
+                               furthest_Dpath_kdiagonal(D, k, rp, direction);
+
+                               // Only check for overlap for forward paths if the offset is odd
+                               // and only for reverse paths if the offset is even.
+                               if (odd_offset_ == (direction == Forward)) {
+
+                                       // Do the forward and backward paths overlap ?
+                                       if (overlap(k, D - odd_offset_)) {
+                                               retrieve_middle_snake(k, D, direction, middle_snake);
+                                               return 2 * D - odd_offset_;
+                                       }
+                               }
+                       }
+               }
+       }
+       // This should never be reached
+       return -2;
 }