up

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

up_see.c (3375B)


      1 #include "all.h"
      2 
      3 static u3_noun
      4 _stir(u3_noun k, u3_atom p, u3_noun l, u3_atom q, u3_noun buc)
      5 {
      6   u3_noun vb, tb;
      7   u3x_cell(buc, &vb, &tb);
      8 
      9   if (c3y == u3r_sing(k, l)) {
     10     if (u3_nul == tb) {
     11       return u3nc(u3nt(u3_nul, u3k(q), u3k(vb)),
     12                u3qdu_qor_sink(tb, l, p, vb));
     13     }
     14     else {
     15       return u3nc(u3nt(u3_nul, u3k(q), u3k(vb)),
     16                u3qdu_qor_sink(tb, k, p, vb));
     17     }
     18   }
     19   else {
     20     u3_noun val = u3qdu_qor_get(tb, k);
     21 
     22     if (u3_nul == val) {
     23       return u3nq(u3_nul, u3k(l), u3k(q), u3k(buc));
     24     }
     25     else {
     26       u3_noun pro = u3nq(u3k(val), u3k(l), u3k(q),
     27                       u3nc(u3k(vb),
     28                         u3qdu_qor_put(tb, k, p, u3t(u3t(val)))));
     29 
     30       u3z(val);
     31       return pro;
     32     }
     33   }
     34 }
     35 
     36 u3_noun
     37 u3qdu_see(u3_noun a, u3_noun k, u3_noun p)
     38 {
     39   if (u3_nul == a) {
     40     return u3nc(u3_nul, u3_nul);
     41   }
     42 
     43   u3_atom hoc = u3h(a);
     44 
     45   if (c3n == u3ud(hoc)) {
     46     return u3m_bail(c3__exit);
     47   }
     48   else switch ( hoc ) {
     49     default:
     50       return u3m_bail(c3__exit);
     51 
     52     case c3__tip: {
     53       u3_noun ka, pa, va;
     54       u3x_trel(u3t(a), &ka, &pa, &va);
     55 
     56       u3_atom mk  = u3r_mug(k);
     57       u3_atom mka = u3r_mug(ka);
     58 
     59       if (mk != mka) {
     60         return u3nc(u3_nul, u3k(a));
     61       }
     62       else {
     63         u3_noun mud = _stir(k, p, ka, pa, va);
     64 
     65         u3_noun pm, qm, rm, sm;
     66         u3x_qual(mud, &pm, &qm, &rm, &sm);
     67 
     68         u3_noun pro = u3nc(u3k(pm), u3nq(c3__tip, u3k(qm), u3k(rm), u3k(sm)));
     69 
     70         u3z(mud);
     71 
     72         return pro;
     73       }
     74     }
     75 
     76     case c3__bin: {
     77       u3_noun ka, pa, va, ta, ma, la, ra;
     78       u3x_qual(u3t(a), &ka, &pa, &va, &ta);
     79       u3x_trel(ta, &ma, &la, &ra);
     80 
     81       if (c3y == u3qdu_feud(ma, k, ka)) {
     82         return u3nc(u3_nul, u3k(a));
     83       }
     84 
     85       u3_atom mk  = u3r_mug(k);
     86       u3_atom mka = u3r_mug(ka);
     87 
     88       if (mk == mka) {
     89         u3_noun mud = _stir(k, p, ka, pa, va);
     90         u3_noun pm, qm, rm, sm;
     91         u3x_qual(mud, &pm, &qm, &rm, &sm);
     92 
     93         if (c3y == u3qdu_zero(ma, k)) {
     94           u3_noun lef = u3qdu_qat_raw(la, qm, rm, sm);
     95           u3_noun pro = u3nc(u3k(pm), u3qdu_fuse(ma, lef, ra));
     96 
     97           u3z(mud);
     98           u3z(lef);
     99 
    100           return pro;
    101         }
    102         else {
    103           u3_noun rye = u3qdu_qat_raw(ra, qm, rm, sm);
    104           u3_noun pro = u3nc(u3k(pm), u3qdu_fuse(ma, la, rye));
    105 
    106           u3z(mud);
    107           u3z(rye);
    108 
    109           return pro;
    110         }
    111       }
    112       else if (c3y == u3qdu_zero(ma, k)) {
    113         u3_noun val = u3qdu_see(la, k, p);
    114 
    115         u3_noun pv, qv;
    116         u3x_cell(val, &pv, &qv);
    117 
    118         u3_noun pro = u3nc(u3k(pv),
    119                         u3nq(c3__bin, u3k(ka), u3k(pa),
    120                           u3nq(u3k(va), u3k(ma), u3k(qv), u3k(ra))));
    121 
    122         u3z(val);
    123         return pro;
    124       }
    125       else {
    126         u3_noun val = u3qdu_see(ra, k, p);
    127 
    128         u3_noun pv, qv;
    129         u3x_cell(val, &pv, &qv);
    130 
    131         u3_noun pro = u3nc(u3k(pv),
    132                         u3nq(c3__bin, u3k(ka), u3k(pa),
    133                           u3nq(u3k(va), u3k(ma), u3k(la), u3k(qv))));
    134 
    135         u3z(val);
    136         return pro;
    137       }
    138     }
    139   }
    140 }
    141 
    142 u3_noun
    143 u3wdu_see(u3_noun cor)
    144 {
    145   u3_noun a, k, p;
    146 
    147   if ( (c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_6, &k,
    148                           u3x_sam_7, &p, 0)) ||
    149        (c3n == u3ud(p)) )
    150   {
    151     return u3m_bail(c3__exit);
    152   } else {
    153     return u3qdu_see(a, k, p);
    154   }
    155 }
    156 
    157