up

A priority search queue and LRU cache.
git clone git://git.jtobin.io/up.git
Log | Files | Refs

up_fuse.c (4132B)


      1 #include "all.h"
      2 
      3 u3_noun
      4 u3qdu_fuse(u3_atom m, u3_noun l, u3_noun r)
      5 {
      6   if ( u3_nul == l ) {
      7     return u3k(r);
      8   }
      9   else {
     10     u3_noun hol = u3h(l);
     11 
     12     if ( c3n == u3ud(hol) )  {
     13       return u3m_bail(c3__exit);
     14     }
     15     else switch ( hol ) {
     16       default:
     17         return u3m_bail(c3__exit);
     18 
     19       case c3__tip: {
     20         if ( u3_nul == r ) {
     21           return u3k(l);
     22         }
     23         else {
     24           u3_noun hor = u3h(r);
     25 
     26           if ( c3n == u3ud(hor) )  {
     27             return u3m_bail(c3__exit);
     28           }
     29           else switch ( hor ) {
     30             default:
     31               return u3m_bail(c3__exit);
     32 
     33             case c3__tip: {
     34               u3_noun p_l, k_l, v_l;
     35               u3_noun p_r, k_r, v_r;
     36 
     37               u3x_trel(u3t(l), &k_l, &p_l, &v_l);
     38               u3x_trel(u3t(r), &k_r, &p_r, &v_r);
     39 
     40               if ( c3y == u3qdu_lex(p_l, k_l, p_r, k_r) ) {
     41                 return u3nq(c3__bin, u3k(k_l), u3k(p_l),
     42                          u3nq(u3k(v_l), u3k(m), u3_nul, u3k(r)));
     43               }
     44               else {
     45                 return u3nq(c3__bin, u3k(k_r), u3k(p_r),
     46                          u3nq(u3k(v_r), u3k(m), u3k(l), u3_nul));
     47               }
     48             }
     49 
     50             case c3__bin: {
     51               u3_noun p_l, k_l, v_l;
     52               u3_noun p_r, k_r, v_r, m_r, l_r, r_r;
     53               u3_noun q_r;
     54 
     55               u3x_trel(u3t(l), &k_l, &p_l, &v_l);
     56               u3x_qual(u3t(r), &k_r, &p_r, &v_r, &q_r);
     57               u3x_trel(q_r, &m_r, &l_r, &r_r);
     58 
     59               if ( c3y == u3qdu_lex(p_l, k_l, p_r, k_r) ) {
     60                 return u3nq(c3__bin, u3k(k_l), u3k(p_l),
     61                          u3nq(u3k(v_l), u3k(m), u3_nul, u3k(r)));
     62               }
     63               else {
     64                 return u3nq(c3__bin, u3k(k_r), u3k(p_r),
     65                          u3nq(u3k(v_r), u3k(m), u3k(l),
     66                            u3qdu_fuse(m_r, l_r, r_r)));
     67               }
     68             }
     69           }
     70         }
     71       }
     72 
     73       case c3__bin: {
     74         if ( u3_nul == r ) {
     75           return u3k(l);
     76         }
     77         else {
     78           u3_noun hor = u3h(r);
     79 
     80           if ( c3n == u3ud(hor) )  {
     81             return u3m_bail(c3__exit);
     82           }
     83           else switch ( hor ) {
     84             default:
     85               return u3m_bail(c3__exit);
     86 
     87             case c3__tip: {
     88               u3_noun p_l, k_l, v_l, m_l, l_l, r_l;
     89               u3_noun p_r, k_r, v_r;
     90               u3_noun q_l;
     91 
     92               u3x_trel(u3t(r), &k_r, &p_r, &v_r);
     93               u3x_qual(u3t(l), &k_l, &p_l, &v_l, &q_l);
     94               u3x_trel(q_l, &m_l, &l_l, &r_l);
     95 
     96               if ( c3y == u3qdu_lex(p_l, k_l, p_r, k_r) ) {
     97                 return u3nq(c3__bin, u3k(k_l), u3k(p_l),
     98                          u3nq(u3k(v_l), u3k(m),
     99                             u3qdu_fuse(m_l, l_l, r_l), u3k(r)));
    100               }
    101               else {
    102                 return u3nq(c3__bin, u3k(k_r), u3k(p_r),
    103                          u3nq(u3k(v_r), u3k(m), u3k(l), u3_nul));
    104               }
    105             }
    106 
    107             case c3__bin: {
    108               u3_noun p_l, k_l, v_l, m_l, l_l, r_l;
    109               u3_noun p_r, k_r, v_r, m_r, l_r, r_r;
    110               u3_noun q_l;
    111               u3_noun q_r;
    112 
    113               u3x_qual(u3t(l), &k_l, &p_l, &v_l, &q_l);
    114               u3x_qual(u3t(r), &k_r, &p_r, &v_r, &q_r);
    115               u3x_trel(q_l, &m_l, &l_l, &r_l);
    116               u3x_trel(q_r, &m_r, &l_r, &r_r);
    117 
    118               if ( c3y == u3qdu_lex(p_l, k_l, p_r, k_r) ) {
    119                 return u3nq(c3__bin, u3k(k_l), u3k(p_l),
    120                          u3nq(u3k(v_l), u3k(m),
    121                            u3qdu_fuse(m_l, l_l, r_l), u3k(r)));
    122               }
    123               else {
    124                 return u3nq(c3__bin, u3k(k_r), u3k(p_r),
    125                          u3nq(u3k(v_r), u3k(m), u3k(l),
    126                            u3qdu_fuse(m_r, l_r, r_r)));
    127               }
    128             }
    129           }
    130         }
    131       }
    132     }
    133   }
    134 }
    135 
    136 u3_noun
    137 u3wdu_fuse(u3_noun cor)
    138 {
    139   u3_noun m, l, r;
    140 
    141   if (
    142     (c3n == u3r_mean(cor, u3x_sam_2, &m, u3x_sam_6, &l, u3x_sam_7, &r, 0 )) ||
    143     (c3n == u3ud(m)) )
    144   {
    145     return u3m_bail(c3__exit);
    146   } else {
    147     return u3qdu_fuse(m, l, r);
    148   }
    149 }
    150