Ejemplo n.º 1
0
/*
*  building optimum dichotomy tree on Burkov,Burkova's works
*/
node_t* optimal_dichotomic_tree ( const task_t *task){

  //{ finding q <= countItems - number of elements on which the maximal symmetric hierarchy must be created
    int q = find_q (task->b);
    q = (q > task->length) ? task->length : q;
  //}

  // maximum symmetric hierarchy must be created from top to down!
  // optimally sorting items
  item_t *diitems, // items for dichotomic part
         *dpitems; // items for dynamic programming branch
  prep_items(task->length, task->items, q, &diitems, &dpitems);

  // head of optimal dichotomic tree
  node_t* head = createnodes (2*task->length-1); // number of all nodes of any tree is doubled number of it's leafs minus one.
  head->hnode = NULL; // parentof the head of the tree

  // DP branch
  node_t *p = head;
  item_t *pl = dpitems, *tmp;
  int i;
  for ( i = 0 ; i < task->length-q ; i++ ) { // in fact p move as p = p + 2
    p->lnode = (p+1);
    p->lnode->items = NULL;
    tmp = copyitem(pl);
    HASH_ADD_KEYPTR ( hh, p->lnode->items, tmp->w, KNINT_SIZE, tmp);
    pl++;
    p->lnode->length = 1;
    p->lnode->hnode = p;
    
    p->rnode = (p+2);
    p->rnode->hnode = p;

    p = p->rnode;
  }

  // Dichotomic branch
  dicho_tree(p, q, diitems);

  return head;
}
Ejemplo n.º 2
0
// parse a segment, and store in a sorted array, also updates qarray
static void parse_seg(const char* s, long len, VALUE out) {
  double qval = 1;
  const char* q = find_q(s, len);
  if (q) {
    if (q == s) {
      return;
    }
    char* str_end = (char*)q + 3;
    qval = strtod(q + 3, &str_end);
    if (str_end == q + 3 || isnan(qval) || qval > 3) {
      qval = 1;
    } else if (qval <= 0) {
      return;
    }
    len = q - s;
  }
  long pos = qarray_insert(qval);
  rb_ary_push(out, Qnil); // just to increase cap
  VALUE* out_ptr = RARRAY_PTR(out);
  long out_len = RARRAY_LEN(out); // note this len is +1
  memmove(out_ptr + pos + 1, out_ptr + pos, sizeof(VALUE) * (out_len - pos - 1));
  rb_ary_store(out, pos, rb_enc_str_new(s, len, u8_encoding));
}