Back to index

texmacs  1.0.7.15
Classes | Functions | Variables
patch.cpp File Reference
#include "patch.hpp"

Go to the source code of this file.

Classes

class  modification_patch_rep
class  compound_patch_rep
class  branch_patch_rep
class  birth_patch_rep
class  author_patch_rep

Functions

int insert_length (tree t)
array< patchsingleton (patch p)
static array< patchget_array (patch p)
int nr_children (patch p)
patch child (patch p, int i)
array< patchchildren (patch p)
array< patchchildren (patch p, int i, int j)
int nr_branches (patch p)
patch branch (patch p, int i)
array< patchbranches (patch p)
array< patchbranches (patch p, int i, int j)
double new_author ()
double new_marker ()
void set_author (double a)
double get_author ()
tm_ostreamoperator<< (tm_ostream &out, patch p)
patch copy (patch p)
bool is_applicable (patch p, tree t)
tree clean_apply (patch p, tree t)
void apply (patch p, tree &t)
patch invert (patch p, tree t)
bool possible_inverse (modification m1, modification m2)
static bool swap_basic (patch &p1, patch &p2)
bool swap (patch &p1, patch &p2, double a1, double a2)
bool swap (patch &p1, patch &p2)
bool commute (patch p1, patch p2)
bool join (patch &p1, patch p2, tree t)
void insert (array< patch > &a, patch p)
patch compactify (patch p)
path cursor_hint (modification m, tree t)
path cursor_hint (patch p, tree t)
patch remove_set_cursor (patch p)
bool does_modify (patch p)

Variables

static double current_author = 0.0

Function Documentation

void apply ( patch  p,
tree t 
)

Definition at line 303 of file patch.cpp.

                         {
  switch (get_type (p)) {
  case PATCH_MODIFICATION:
    apply (t, get_modification (p));
    break;
  case PATCH_BRANCH:
    ASSERT (N(p) <= 1, "ambiguous application");
  case PATCH_COMPOUND:
  case PATCH_AUTHOR:
    for (int i=0; i<N(p); i++)
      apply (p[i], t);
    break;
  case PATCH_BIRTH:
    break;
  default:
    FAILED ("unsupported patch type");
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

patch branch ( patch  p,
int  i 
)

Definition at line 139 of file patch.cpp.

                        {
  ASSERT (0 <= i && i < nr_branches (p), "index out of range");
  if (get_type (p) != PATCH_BRANCH) return p;
  else return p[i];
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 146 of file patch.cpp.

                   {
  if (get_type (p) != PATCH_BRANCH) return singleton (p);
  else return get_array (p);
}

Here is the call graph for this function:

Here is the caller graph for this function:

array<patch> branches ( patch  p,
int  i,
int  j 
)

Definition at line 152 of file patch.cpp.

                                 {
  ASSERT (0 <= i && i <= nr_branches (p), "index out of range");
  ASSERT (i <= j && j <= nr_branches (p), "index out of range");
  return range (branches (p), i, j);
}

Here is the call graph for this function:

patch child ( patch  p,
int  i 
)

Definition at line 113 of file patch.cpp.

                       {
  ASSERT (0 <= i && i < nr_children (p), "index out of range");
  if (get_type (p) != PATCH_COMPOUND) return p;
  else return p[i];
}

Here is the call graph for this function:

Definition at line 120 of file patch.cpp.

                   {
  if (get_type (p) != PATCH_COMPOUND) return singleton (p);
  else return get_array (p);
}

Here is the call graph for this function:

Here is the caller graph for this function:

array<patch> children ( patch  p,
int  i,
int  j 
)

Definition at line 126 of file patch.cpp.

                                 {
  ASSERT (0 <= i && i <= nr_children (p), "index out of range");
  ASSERT (i <= j && j <= nr_children (p), "index out of range");
  return range (children (p), i, j);
}

Here is the call graph for this function:

tree clean_apply ( patch  p,
tree  t 
)

Definition at line 283 of file patch.cpp.

                              {
  switch (get_type (p)) {
  case PATCH_MODIFICATION:
    return clean_apply (t, get_modification (p));
  case PATCH_BRANCH:
    ASSERT (N(p) <= 1, "ambiguous application");
  case PATCH_COMPOUND:
  case PATCH_AUTHOR:
    for (int i=0; i<N(p); i++)
      t= clean_apply (p[i], t);
    return t;
  case PATCH_BIRTH:
    return t;
  default:
    FAILED ("unsupported patch type");
    return t;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool commute ( patch  p1,
patch  p2 
)

Definition at line 475 of file patch.cpp.

                             {
  patch s1= p1;
  patch s2= p2;
  return swap (s1, s2);
}

Here is the call graph for this function:

Definition at line 562 of file patch.cpp.

                     {
  switch (get_type (p)) {
  case PATCH_COMPOUND:
    {
      double a= 0;
      for (int i=0; i<N(p); i++)
       if (get_type (p[i]) != PATCH_AUTHOR) a= -1;
       else if (a == 0) a= get_author (p[i]);
       else if (a != get_author (p[i])) a= -1;
      if (a <= 0) {
       array<patch> r;
       insert (r, p);
       if (N(r) == 1) return r[0];
       return patch (r);
      }
      else {
       array<patch> r;
       for (int i=0; i<N(p); i++) insert (r, p[i][0]);
       if (N(r) == 1) return patch (a, r[0]);
       return patch (a, patch (r));
      }
    }
  case PATCH_BRANCH:
    if (N(p) == 1) return p[0];
    else {
      int i, n= N(p);
      array<patch> r (n);
      for (i=0; i<n; i++) r[i]= compactify (p[i]);
      return patch (true, r);
    }
  case PATCH_AUTHOR:
    return patch (get_author (p), compactify (p[0]));
  }
  return p;
}

Here is the call graph for this function:

Here is the caller graph for this function:

patch copy ( patch  p)

Definition at line 231 of file patch.cpp.

               {
  switch (get_type (p)) {
  case PATCH_MODIFICATION:
    return patch (copy (get_modification (p)), copy (get_inverse (p)));
  case PATCH_COMPOUND:
  case PATCH_BRANCH:
    {
      int i, n= N(p);
      array<patch> r (n);
      for (i=0; i<N(p); i++) r[i]= copy (p[i]);
      return patch (get_type (p) == PATCH_BRANCH, r);
    }
  case PATCH_BIRTH:
    return p;
  case PATCH_AUTHOR:
    return patch (get_author (p), copy (p[0]));
  default:
    FAILED ("unsupported patch type");
  }
  return p;
}

Here is the call graph for this function:

Definition at line 599 of file patch.cpp.

                                     {
  ASSERT (is_applicable (t, m), "modification not applicable");
  path rp= root (m);
  tree st= subtree (t, rp);
  switch (m->k) {
  case MOD_ASSIGN:
    return end (t, rp);
  case MOD_INSERT:
    if (is_atomic (st)) return rp * index (m);
    else if (index (m) == N (st)) return end (t, rp);
    else return start (t, rp * index (m));
  case MOD_REMOVE:
    if (is_atomic (st)) return rp * (index (m) + argument (m));
    else if (index (m) == N (st)) return end (t, rp);
    else if (argument (m) == 0) return start (t, rp * index (m));
    else return end (t, rp * (index (m) + argument (m) - 1));
  case MOD_SPLIT:
    if (is_atomic (st [index (m)])) return m->p;
    else if (argument (m) == N (st [index (m)])) return end (t, rp * index(m));
    else return start (t, m->p);
  case MOD_JOIN:
    return end (t, m->p);
  case MOD_ASSIGN_NODE:
    return end (t, rp);
  case MOD_INSERT_NODE:
    return end (t, rp);
  case MOD_REMOVE_NODE:
    return end (t, rp * index (m));
  case MOD_SET_CURSOR:
    return path ();
  default:
    FAILED ("unexpected situation");
    return path ();
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

path cursor_hint ( patch  p,
tree  t 
)

Definition at line 636 of file patch.cpp.

                              {
  switch (get_type (p)) {
  case PATCH_MODIFICATION:
    return cursor_hint (get_modification (p), t);
  case PATCH_COMPOUND:
    for (int i=0; i<N(p); i++) {
      path r= cursor_hint (p[i], t);
      if (!is_nil (r)) return r;
    }
    return path ();
  case PATCH_BRANCH:
    if (N(p) == 0) return path ();
  case PATCH_AUTHOR:
    return cursor_hint (p[0], t);
  case PATCH_BIRTH:
    return path ();
  default:
    FAILED ("unsupported patch type");
  }
  return path ();
}

Here is the call graph for this function:

bool does_modify ( patch  p)

Definition at line 690 of file patch.cpp.

                      {
  switch (get_type (p)) {
  case PATCH_MODIFICATION:
    return get_modification (p)->k != MOD_SET_CURSOR;
  case PATCH_COMPOUND:
  case PATCH_BRANCH:
    for (int i=0; i<N(p); i++)
      if (does_modify (p[i]))
        return true;
    return false;
  case PATCH_BIRTH:
  case PATCH_AUTHOR:
    return false;
  default:
    return true;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static array<patch> get_array ( patch  p) [static]

Definition at line 99 of file patch.cpp.

                    {
  int i, n= N(p);
  array<patch> a (n);
  for (i=0; i<N(p); i++) a[i]= p[i];
  return a;
}

Here is the call graph for this function:

Here is the caller graph for this function:

double get_author ( )

Definition at line 184 of file patch.cpp.

              {
  return current_author;
}

Here is the caller graph for this function:

void insert ( array< patch > &  a,
patch  p 
)

Definition at line 543 of file patch.cpp.

                                  {
  if (get_type (p) == PATCH_COMPOUND) {
    int i, n= N(p);
    for (i=0; i<n; i++)
      insert (a, p[i]);
  }
  else if (get_type (p) == PATCH_MODIFICATION &&
          N(a) > 0 &&
          get_type (a[N(a)-1]) == PATCH_MODIFICATION &&
          (get_inverse (a[N(a)-1]) == get_modification (p) &&
           get_modification (a[N(a)-1]) == get_inverse (p)))
    {
      // cout << "Cancel " << a[N(a)-1] << " against " << p << "\n";
      a->resize (N(a) - 1);
    }
  else a << p;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int insert_length ( tree  t)

Definition at line 19 of file commute.cpp.

                       {
  if (is_atomic (t)) return N(t->label);
  else return N(t);
}

Here is the call graph for this function:

Here is the caller graph for this function:

patch invert ( patch  p,
tree  t 
)

Definition at line 327 of file patch.cpp.

                         {
  switch (get_type (p)) {
  case PATCH_MODIFICATION:
    return patch (get_inverse (p), get_modification (p));
  case PATCH_BRANCH:
    ASSERT (N(p) <= 1, "ambiguous application");
  case PATCH_COMPOUND:
    {
      int i, n=N(p);
      array<patch> r(n);
      for (i=0; i<n; i++) {
       r[n-1-i]= invert (p[i], t);
       t= clean_apply (p[i], t);
      }
      return patch (get_type (p) == PATCH_BRANCH, r);
    }
  case PATCH_BIRTH:
    return patch (get_author (p), !get_birth (p));
  case PATCH_AUTHOR:
    return patch (get_author (p), invert (p[0], t));
  default:
    FAILED ("unsupported patch type");
    return patch ();
  }
}

Here is the call graph for this function:

bool is_applicable ( patch  p,
tree  t 
)

Definition at line 258 of file patch.cpp.

                                {
  switch (get_type (p)) {
  case PATCH_MODIFICATION:
    return is_applicable (t, get_modification (p));
  case PATCH_COMPOUND:
    for (int i=0; i<N(p); i++) {
      if (!is_applicable (p[i], t)) return false;
      t= clean_apply (p[i], t);
    }
    return true;
  case PATCH_BRANCH:
  case PATCH_AUTHOR:
    for (int i=0; i<N(p); i++)
      if (!is_applicable (p[i], t))
       return false;
    return true;
  case PATCH_BIRTH:
    return true;
  default:
    FAILED ("unsupported patch type");
    return false;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool join ( patch p1,
patch  p2,
tree  t 
)

Definition at line 486 of file patch.cpp.

                                   {
  //cout << "Join " << p1 << LF << "with " << p2 << LF;
  if (get_type (p1) == PATCH_AUTHOR &&
      get_type (p2) == PATCH_AUTHOR &&
      get_author (p1) == get_author (p2))
    {
      double author= get_author (p1);
      patch q1= p1[0];
      patch q2= p2[0];
      bool r= join (q1, q2, t);
      if (r) p1= patch (author, q1);
      return r;
    }
  if (get_type (p1) == PATCH_MODIFICATION &&
      get_type (p2) == PATCH_MODIFICATION)
    {
      modification m1= get_modification (p1);
      modification m2= get_modification (p2);
      modification i2= get_inverse (p2);
      modification i1= get_inverse (p1);
      bool r= join (m1, m2, t);
      bool v= join (i2, i1, clean_apply (p2, clean_apply (p1, t)));
      if (r && v) p1= patch (m1, i2);
      return r && v;
    }
  if (get_type (p1) == PATCH_COMPOUND &&
      nr_children (p1) > 0 &&
      nr_children (remove_set_cursor (p1)) == 1 &&
      nr_children (p1[0]) == 1)
    {
      patch q= p1[0];
      bool rf= join (q, p2, t);
      if (rf) p1= q;
      return rf;
    }
  if (get_type (p2) == PATCH_COMPOUND &&
      nr_children (p2) > 0 &&
      nr_children (remove_set_cursor (p2)) == 1 &&
      nr_children (p2[0]) == 1)
    {
      patch q= p2[0];
      bool rf= join (p1, q, t);
      if (rf) {
        array<patch> a= children (p1);
        array<patch> b= children (p2);
        p1= patch (append (a, range (b, 1, N(b))));
      }
      return rf;
    }
  return false;
}

Here is the call graph for this function:

double new_author ( )

Definition at line 165 of file patch.cpp.

              {
  static double next_author= 0.0;
  next_author += 1.0;
  return next_author;
}

Here is the caller graph for this function:

double new_marker ( )

Definition at line 172 of file patch.cpp.

              {
  static double next_marker= 0.5;
  next_marker += 1.0;
  return next_marker;
}

Here is the caller graph for this function:

int nr_branches ( patch  p)

Definition at line 133 of file patch.cpp.

                      {
  if (get_type (p) != PATCH_BRANCH) return 1;
  else return N(p);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int nr_children ( patch  p)

Definition at line 107 of file patch.cpp.

                      {
  if (get_type (p) != PATCH_COMPOUND) return 1;
  else return N(p);
}

Here is the call graph for this function:

Here is the caller graph for this function:

tm_ostream& operator<< ( tm_ostream out,
patch  p 
)

Definition at line 193 of file patch.cpp.

                                       {
  switch (get_type (p)) {
  case PATCH_MODIFICATION:
    out << get_modification (p) << " -- " << get_inverse (p);
    break;
  case PATCH_COMPOUND:
    if (N(p) == 0) out << "No children";
    else {
      out << "Composite" << INDENT;
      for (int i=0; i<N(p); i++)
       out << LF << p[i];
      out << UNINDENT;
    }
    break;
  case PATCH_BRANCH:
    if (N(p) == 0) out << "No branches";
    else for (int i=0; i<N(p); i++) {
      if (i != 0) out << LF;
      out << "Branch " << i << INDENT << LF << p[i] << UNINDENT;
    }
    break;
  case PATCH_BIRTH:
    if (get_birth (p)) out << "Birth ";
    else out << "Death ";
    out << get_author (p);
    break;
  case PATCH_AUTHOR:
    out << "Author " << get_author (p) << INDENT << LF;
    out << p[0];
    out << UNINDENT;
    break;
  default:
    FAILED ("unsupported patch type");
  }
  return out;
}

Here is the call graph for this function:

Definition at line 354 of file patch.cpp.

                                                    {
  if (root (m1) != root (m2)) return false;
  switch (m1->k) {
  case MOD_ASSIGN:
    return m2->k == MOD_ASSIGN;
  case MOD_INSERT:
    return m2->k == MOD_REMOVE && 
           argument (m2) == insert_length (m1->t);
  case MOD_REMOVE:
    return m2->k == MOD_INSERT && 
           insert_length (m2->t) == argument (m1);
  case MOD_SPLIT:
    return m2->k == MOD_JOIN && 
           index (m2) == index (m1);
  case MOD_JOIN:
    return m2->k == MOD_SPLIT && 
           index (m2) == index (m1);
  case MOD_ASSIGN_NODE:
    return m2->k == MOD_ASSIGN_NODE;
  case MOD_INSERT_NODE:
    return m2->k == MOD_REMOVE_NODE && 
           index (m2) == argument (m1);
  case MOD_REMOVE_NODE:
    return m2->k == MOD_INSERT_NODE && 
           argument (m2) == index (m1);
  case MOD_SET_CURSOR:
    return m1 == m2;
  default:
    FAILED ("invalid situation");
    return false;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 659 of file patch.cpp.

                            {
  switch (get_type (p)) {
  case PATCH_MODIFICATION:
    if (get_modification (p)->k != MOD_SET_CURSOR) return copy (p);
    return patch (array<patch> ());
  case PATCH_COMPOUND:
    {
      array<patch> r;
      for (int i=0; i<N(p); i++) {
        patch q= remove_set_cursor (p[i]);
        r << children (q);
      }
      if (N(r) == 1) return r[0];
      return patch (r);
    }
  case PATCH_BRANCH:
  case PATCH_BIRTH:
    return copy (p);
  case PATCH_AUTHOR:
    {
      patch q= remove_set_cursor (p[0]);
      if (nr_children (q) == 0) return q;
      else return patch (get_author (p), q);
    }
  default:
    FAILED ("unsupported patch type");
  }
  return p;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void set_author ( double  a)

Definition at line 179 of file patch.cpp.

                      {
  current_author= a;
}

Here is the caller graph for this function:

Definition at line 92 of file patch.cpp.

                    {
  array<patch> a (1);
  a[0]= p;
  return a;
}

Here is the caller graph for this function:

bool swap ( patch p1,
patch p2,
double  a1,
double  a2 
)

Definition at line 400 of file patch.cpp.

                                                  {
  if (is_nil (p1) || is_nil (p2)) return false;
  if (get_type (p1) == PATCH_BRANCH)
    return false;
  if (get_type (p2) == PATCH_BRANCH)
    return false;
  if (get_type (p1) == PATCH_COMPOUND) {
    int n= N(p1);
    array<patch> a (n);
    for (int i=0; i<n; i++) a[i]= p1[i];
    for (int i=n-1; i>=0; i--) {
      if (!swap (a[i], p2, a1, a2)) return false;
      swap_basic (a[i], p2);
    }
    p1= p2;
    p2= patch (a);
    return true;
  }
  if (get_type (p2) == PATCH_COMPOUND) {
    int n= N(p2);
    array<patch> a (n);
    for (int i=0; i<n; i++) a[i]= p2[i];
    for (int i=0; i<n; i++) {
      if (!swap (p1, a[i], a1, a2)) return false;
      swap_basic (p1, a[i]);
    }
    p2= p1;
    p1= patch (a);
    return true;
  }
  if (get_type (p1) == PATCH_AUTHOR) {
    patch s= p1[0];
    bool r= swap (s, p2, get_author (p1), a2);
    p2= patch (get_author (p1), p2);
    p1= s;
    return r;
  }
  if (get_type (p2) == PATCH_AUTHOR) {
    patch s= p2[0];
    bool r= swap (p1, s, a1, get_author (p2));
    p1= patch (get_author (p2), p1);
    p2= s;
    return r;
  }
  if (get_type (p1) == PATCH_BIRTH) {
    if (get_author (p1) == a2) return false;
    return swap_basic (p1, p2);
  }
  if (get_type (p2) == PATCH_BIRTH) {
    if (get_author (p2) == a1) return false;
    return swap_basic (p1, p2);
  }
  if (get_type (p1) == PATCH_MODIFICATION &&
      get_type (p2) == PATCH_MODIFICATION)
    {
      modification m1= get_modification (p1);
      modification m2= get_modification (p2);
      modification i1= get_inverse (p1);
      modification i2= get_inverse (p2);
      bool r= swap (m1, m2);
      bool v= swap (i2, i1);
      p1= patch (m1, i1);
      p2= patch (m2, i2);
      return r && v && possible_inverse (m1, i1) && possible_inverse (m2, i2);
    }
  FAILED ("invalid situation");
  return false;
}

Here is the call graph for this function:

bool swap ( patch p1,
patch p2 
)

Definition at line 470 of file patch.cpp.

                            {
  return swap (p1, p2, 0, 0);
}

Here is the call graph for this function:

static bool swap_basic ( patch p1,
patch p2 
) [static]

Definition at line 392 of file patch.cpp.

                                  {
  patch aux= p1;
  p1= p2;
  p2= aux;
  return true;
}

Variable Documentation

double current_author = 0.0 [static]

Definition at line 162 of file patch.cpp.