up

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

up_put.c (1715B)


      1 #include "all.h"
      2 
      3 u3_noun
      4 u3qdu_put(u3_noun a, u3_noun k, u3_atom p, u3_noun v)
      5 {
      6   u3_noun ded = u3qdu_qat_dew(a, k);
      7 
      8   if (u3_nul == ded) {
      9     u3_noun buc = u3nc(u3k(v), u3_nul);
     10     u3_noun pro = u3qdu_qat_raw(a, k, p, buc);
     11 
     12     u3z(buc);
     13     return pro;
     14   }
     15   else {
     16     u3_noun pv, qv, rv, sv;
     17     u3_noun vrv, trv;
     18 
     19     u3x_qual(u3t(ded), &pv, &qv, &rv, &sv);
     20     u3x_cell(rv, &vrv, &trv);
     21 
     22     if (c3y == u3r_sing(k, pv)) {
     23       u3_noun sun = u3qdu_qor_sink(trv, k, p, v);
     24 
     25       u3_noun ps, qs, rs;
     26       u3x_trel(sun, &ps, &qs, &rs);
     27 
     28       u3_noun pro = u3qdu_qat_raw(sv, ps, qs, rs);
     29 
     30       u3z(ded);
     31       u3z(sun);
     32 
     33       return pro;
     34     }
     35     else if ( (c3y == u3qa_lth(qv, p)) ||
     36               ( (c3y == u3r_sing(p, qv)) && (c3y == u3qc_gor(pv, k)) ) ) {
     37 
     38       u3_noun buc = u3nc(u3k(vrv), u3qdu_qor_put(trv, k, p, v));
     39       u3_noun pro = u3qdu_qat_raw(sv, pv, qv, buc);
     40 
     41       u3z(ded);
     42       u3z(buc);
     43 
     44       return pro;
     45     }
     46     else if (c3y == u3qdu_qor_has(trv, k)) {
     47       u3_noun dud = u3qdu_qor_del(trv, k);
     48       u3_noun buc = u3nc(u3k(v), u3qdu_qor_put(dud, pv, qv, vrv));
     49       u3_noun pro = u3qdu_qat_raw(sv, k, p, buc);
     50 
     51       u3z(ded);
     52       u3z(dud);
     53       u3z(buc);
     54 
     55       return pro;
     56     }
     57     else {
     58       u3_noun buc = u3nc(u3k(v), u3qdu_qor_put(trv, pv, qv, vrv));
     59       u3_noun pro = u3qdu_qat_raw(sv, k, p, buc);
     60 
     61       u3z(ded);
     62       u3z(buc);
     63 
     64       return pro;
     65     }
     66   }
     67 }
     68 
     69 u3_noun
     70 u3wdu_put(u3_noun cor)
     71 {
     72   u3_noun a, k, p, v;
     73 
     74   if ( (c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_6, &k, u3x_sam_14, &p,
     75                           u3x_sam_15, &v, 0)) ||
     76        (c3n == u3ud(p)) )
     77   {
     78     return u3m_bail(c3__exit);
     79   } else {
     80     return u3qdu_put(a, k, p, v);
     81   }
     82 }
     83