Back to index

texmacs  1.0.7.15
Functions
commute.cpp File Reference
#include "patch.hpp"

Go to the source code of this file.

Functions

int insert_length (tree t)
static tree insert_range (tree t, int i, int len)
static modification dup (modification m)
modification invert (modification m, tree t)
static bool swap1 (modification &m1, modification &m2, int i, int d)
static bool swap2 (modification &m1, modification &m2, int i, int d)
bool swap_basic (modification &m1, modification &m2)
bool swap (modification &m1, modification &m2)
bool commute (modification m1, modification m2)
bool join (modification &m1, modification m2, tree t)

Function Documentation

bool commute ( modification  m1,
modification  m2 
)

Definition at line 309 of file commute.cpp.

                                           {
  modification s1= m1;
  modification s2= m2;
  return swap (s1, s2);
}

Here is the call graph for this function:

static modification dup ( modification  m) [static]

Definition at line 31 of file commute.cpp.

                     {
  return modification (m->k, copy (m->p), m->t);
}

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:

static tree insert_range ( tree  t,
int  i,
int  len 
) [static]

Definition at line 25 of file commute.cpp.

                                      {
  if (is_atomic (t)) return t->label (i, i+len);
  else return t (i, i+len);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 40 of file commute.cpp.

                                {
  ASSERT (is_applicable (t, m), "modification not applicable");
  path rp= root (m);
  switch (m->k) {
  case MOD_ASSIGN:
    return mod_assign (rp, copy (subtree (t, rp)));
  case MOD_INSERT:
    return mod_remove (rp, index (m), insert_length (m->t));
  case MOD_REMOVE:
    {
      int i= index (m);
      int n= argument (m);
      return mod_insert (rp, i, copy (insert_range (subtree (t, rp), i, n)));
    }
  case MOD_SPLIT:
    return mod_join (rp, index (m));
  case MOD_JOIN:
    {
      int  i= index (m);
      return mod_split (rp, i, insert_length (subtree (t, rp * i)));
    }
  case MOD_ASSIGN_NODE:
    return mod_assign_node (rp, L (subtree (t, rp)));
  case MOD_INSERT_NODE:
    return mod_remove_node (rp, argument (m));
  case MOD_REMOVE_NODE:
    {
      tree u= subtree (t, rp);
      int  i= index (m);
      return mod_insert_node (rp, i, copy (u (0, i) * u (i+1, N(u))));
    }
  case MOD_SET_CURSOR:
    return m;
  default:
    FAILED ("unexpected situation");
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool join ( modification m1,
modification  m2,
tree  t 
)

Definition at line 320 of file commute.cpp.

                                                 {
  if (m1->k == MOD_INSERT &&
      m2->k == MOD_INSERT &&
      is_atomic (m1->t) &&
      root (m1) == root (m2) &&
      (index (m2) == index (m1) ||
       index (m2) == index (m1) + N (m1->t->label)))
    {
      string s= m1->t->label * m2->t->label;
      if (index (m2) == index (m1))
       s= m2->t->label * m1->t->label;
      m1= mod_insert (root (m1), index (m1), tree (s));
      return true;
    }
  if (m1->k == MOD_REMOVE &&
      m2->k == MOD_REMOVE &&
      is_atomic (subtree (t, root (m1))) &&
      root (m1) == root (m2) &&
      (index (m1) == index (m2) ||
       index (m1) == index (m2) + argument (m2)))
    {
      m1= mod_remove (root (m2), index (m2),
                    argument (m1) + argument (m2));
      return true;
    }
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool swap ( modification m1,
modification m2 
)

Definition at line 151 of file commute.cpp.

                                          {
  path rp1= root (m1);
  path rp2= root (m2);
  if (is_nil (rp1))
    switch (m1->k) {
    case MOD_ASSIGN:
      {
       if (m1 == m2) return true;
       if (!is_nil (rp2) || m2->k != MOD_INSERT_NODE) return false;
       modification aux= m2;
       m2= modification (m1->k, path (m2->p->item, m1->p), m1->t);
       m1= aux;
       return true;
      }
    case MOD_INSERT:
      {
       if (is_nil (m2->p)) return false;
       int b= m1->p->item;
       int e= b + insert_length (m1->t);
       if (m2->p->item >= b && m2->p->item < e)
         if (!is_nil (root (m2)) || m2->p->item != b || m2->k != MOD_INSERT)
           if (!is_nil (root (m2)) || m2->k != MOD_INSERT_NODE)
             return false;
       return swap1 (m1, m2, b, e-b);
      }
    case MOD_REMOVE:
      {
       if (is_nil (m2->p)) return false;
       int i= m1->p->item;
       int d= m1->p->next->item;
       return swap1 (m1, m2, i, -d);
      }
    case MOD_SPLIT:
      {
       if (is_nil (m2->p)) return false;
       int i= m1->p->item;
       if (m2->p->item == i || m2->p->item == i+1)
         if (!is_nil (root (m2)) || m2->p->item != i || m2->k != MOD_INSERT)
           if (!is_nil (root (m2)) || m2->k != MOD_INSERT_NODE)
             return false;
       return swap1 (m1, m2, i, 1);
      }
    case MOD_JOIN:
      {
       if (is_nil (m2->p)) return false;
       int i= m1->p->item;
       if (m2->p->item == i)
         if (!is_nil (root (m2)) || m2->k != MOD_INSERT)
           return false;
       return swap1 (m1, m2, i, -1);
      }
    case MOD_ASSIGN_NODE:
      {
       if (!is_nil (root (m2))) return swap_basic (m1, m2);
       if (m1 == m2) return true;
       if (!is_nil (rp2) || m2->k != MOD_INSERT_NODE) return false;
       modification aux= m2;
       m2= modification (m1->k, path (m2->p->item, m1->p), m1->t);
       m1= aux;
       return true;
      }
    case MOD_INSERT_NODE:
      {
       if (is_nil (root (m2))) return false;
       if (m2->p->item != m1->p->item) return false;
       modification aux= m1;
       m1= modification (m2->k, m2->p->next, m2->t);
       if (m2->k != MOD_INSERT_NODE || !is_nil (root (m1))) m2= aux;
       else m2= modification (aux->k, path (m1->p->item, aux->p), aux->t);
       return true;
      }
    case MOD_REMOVE_NODE:
      {
       modification aux= m1;
       m1= modification (m2->k, path (m1->p->item, m2->p), m2->t);
       m2= aux;
       return true;
      }
    case MOD_SET_CURSOR:
      {
        if (!is_nil (rp2) ||
            m2->k == MOD_JOIN ||
            m2->k == MOD_SPLIT ||
            m2->k == MOD_ASSIGN_NODE)
          return swap_basic (m1, m2);
        return false;
      }
    }
  else if (is_nil (rp2))
    switch (m2->k) {
    case MOD_ASSIGN:
      return false;
    case MOD_INSERT:
      {
       int b= m2->p->item;
       int e= b + insert_length (m2->t);
       return swap2 (m1, m2, b, e-b);
      }
    case MOD_REMOVE:
      {
       int b= m2->p->item;
       int e= b + m2->p->next->item;
       if (m1->p->item >= b && m1->p->item < e) return false;
       return swap2 (m1, m2, b, b-e);
      }
    case MOD_SPLIT:
      {
       int i= m2->p->item;
       if (m1->p->item == i) return false;
       return swap2 (m1, m2, i, 1);
      }
    case MOD_JOIN:
      {
       int i= m2->p->item;
       if (m1->p->item == i || m1->p->item == i+1) return false;
       return swap2 (m1, m2, i, -1);
      }
    case MOD_ASSIGN_NODE:
      return swap_basic (m1, m2);
    case MOD_INSERT_NODE:
      {
       modification aux= m2;
       m2= modification (m1->k, path (m2->p->item, m1->p), m1->t);
       m1= aux;
       return true;
      }
    case MOD_REMOVE_NODE:
      {
       if (m1->p->item != m2->p->item) return false;
       modification aux= m2;
       m2= modification (m1->k, m1->p->next, m1->t);
       m1= aux;
       return true;
      }
    case MOD_SET_CURSOR:
      {
        if (!is_nil (rp1) ||
            m1->k == MOD_JOIN ||
            m1->k == MOD_SPLIT ||
            m1->k == MOD_ASSIGN_NODE)
          return swap_basic (m1, m2);
        return false;
      }
    }
  else if (rp1->item == rp2->item) {
    path h (rp1->item);
    modification s1= m1 / h;
    modification s2= m2 / h;
    bool r= swap (s1, s2);
    m1= h * s1;
    m2= h * s2;
    return r;
  }
  else return swap_basic (m1, m2);
  FAILED ("unexpected situation");
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool swap1 ( modification m1,
modification m2,
int  i,
int  d 
) [static]

Definition at line 83 of file commute.cpp.

                                                         {
  modification r1= dup (m2);
  modification r2= dup (m1);
  if (m2->p->item >= i)
    if (m2->p->item != i || !is_nil (root (m2)) || m2->k != MOD_INSERT)
      r1->p->item -= d;
  if (is_nil (root (m2)))
    switch (m2->k) {
    case MOD_INSERT:
      {
       int b2= m2->p->item;
       int e2= b2 + insert_length (m2->t);
       if (b2 <= i) r2->p->item += (e2-b2);
       break;
      }
    case MOD_REMOVE:
      {
       int b2= m2->p->item;
       int e2= b2 + m2->p->next->item;
       if (b2 <= i && i < e2)
         if (b2 != i || m1->k != MOD_REMOVE)
           return false;
       if (b2 < i) r2->p->item -= (e2-b2);
       break;
      }
    case MOD_SPLIT:
      if (m2->p->item < i) r2->p->item++;
      break;
    case MOD_JOIN:
      if (m2->p->item == i-1) return false;
      if (m2->p->item < i) r2->p->item--;
      break;
    case MOD_INSERT_NODE:
      {
       modification aux= m2;
       m2= modification (m1->k, path (m2->p->item, m1->p), m1->t);
       m1= aux;
       return true;
      }
    case MOD_REMOVE_NODE:
      return false;
    case MOD_SET_CURSOR:
      return false;
    }
  m1= r1;
  m2= r2;
  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool swap2 ( modification m1,
modification m2,
int  i,
int  d 
) [static]

Definition at line 133 of file commute.cpp.

                                                         {
  modification r1= dup (m2);
  modification r2= dup (m1);
  if (m1->p->item >= i) r2->p->item += d;
  m1= r1;
  m2= r2;
  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool swap_basic ( modification m1,
modification m2 
)

Definition at line 143 of file commute.cpp.

                                                {
  modification aux= m1;
  m1= m2;
  m2= aux;
  return true;
}

Here is the caller graph for this function: