/** 
 * @brief  Store a score to the current list of computed Gaussians.
 * 
 * Store the calculated score of a Gaussian to OP_calced_score, with its
 * corresponding mixture id to OP_calced_id.
 *
 * The OP_calced_score and OP_calced_id always holds the
 * (OP_gprune_num)-best scores and ids.  If the number of stored
 * Gaussian from start has reached OP_gprune_num and the given score is
 * below the bottom, it will be dropped.  Else, the new
 * score will be inserted and the bottom will be dropped from the list.
 *
 * The OP_calced_score will always kept sorted by the scores.
 * 
 * @param wrk [i/o] HMM computation work area
 * @param id [in] mixture id of the Gaussian to store
 * @param score [in] score of the Gaussian to store
 * @param len [in] current number of stored scores in OP_calced_score
 * 
 * @return the resulting number of stored scores in OP_calced_score.
 */
int
cache_push(HMMWork *wrk, int id, LOGPROB score, int len)
{
  int insertp;
  LOGPROB *calced_score;
  int *calced_id;

  calced_score = wrk->OP_calced_score;
  calced_id = wrk->OP_calced_id;

  if (len == 0) {               /* first one */
    calced_score[0] = score;
    calced_id[0] = id;
    return(1);
  }
  if (calced_score[len-1] >= score) { /* bottom */
    if (len < wrk->OP_gprune_num) {          /* append to bottom */
      calced_score[len] = score;
      calced_id[len] = id;
      len++;
    }
    return len;
  }
  if (calced_score[0] < score) {
    insertp = 0;
  } else {
    insertp = find_insert_point(calced_score, score, len);
  }
  if (len < wrk->OP_gprune_num) {
    memmove(&(calced_score[insertp+1]), &(calced_score[insertp]), sizeof(LOGPROB)*(len - insertp));
    memmove(&(calced_id[insertp+1]), &(calced_id[insertp]), sizeof(int)*(len - insertp));    
  } else if (insertp < len - 1) {
    memmove(&(calced_score[insertp+1]), &(calced_score[insertp]), sizeof(LOGPROB)*(len - insertp - 1));
    memmove(&(calced_id[insertp+1]), &(calced_id[insertp]), sizeof(int)*(len - insertp - 1));
  }
  calced_score[insertp] = score;
  calced_id[insertp] = id;
  if (len < wrk->OP_gprune_num) len++;
  return(len);
}
Example #2
0
static int
get_insert_run_id(
        struct rldb_plugin_cnts *cdata,
        time_t create_time,
        int user_id,
        int create_nsec)
{
  struct rldb_mysql_cnts *cs = (struct rldb_mysql_cnts *) cdata;
  struct rldb_mysql_state *state = cs->plugin_state;
  struct common_mysql_iface *mi = state->mi;
  struct common_mysql_state *md = state->md;
  struct runlog_state *rls = cs->rl_state;
  struct run_entry_internal ri;
  struct run_entry *re;
  struct timeval curtime;
  int run_id, i;
  char *cmd_t = 0;
  size_t cmd_z = 0;
  FILE *cmd_f = 0;

  if ((run_id = find_insert_point(rls, create_time, create_nsec, user_id)) < 0)
    goto fail;
  ASSERT(run_id < rls->run_u);

  if (rls->runs[run_id].status != RUN_EMPTY) {
    // move [run_id, run_u - 1) one forward
    memmove(&rls->runs[run_id + 1], &rls->runs[run_id],
            (rls->run_u - run_id - 1) * sizeof(rls->runs[0]));
    for (i = run_id + 1; i < rls->run_u; ++i)
      rls->runs[i].run_id = i;
    if (mi->simple_fquery(md, "UPDATE %sruns SET run_id = run_id + 1 WHERE contest_id = %d AND run_id >= %d ORDER BY run_id DESC;", md->table_prefix, cs->contest_id, run_id) < 0)
      goto fail;
  }
  re = &rls->runs[run_id];
  memset(re, 0, sizeof(*re));
  re->run_id = run_id;
  re->time = create_time;
  re->nsec = create_nsec;
  //re->user_id = user_id;
  re->status = RUN_EMPTY;

  memset(&ri, 0, sizeof(ri));
  gettimeofday(&curtime, 0);
  ri.run_id = run_id;
  ri.contest_id = cs->contest_id;
  ri.create_time = create_time;
  ri.create_nsec = create_nsec;
  ri.status = RUN_EMPTY;
  //ri.user_id = user_id;
  ri.last_change_time = curtime.tv_sec;
  ri.last_change_nsec = curtime.tv_usec * 1000;

  cmd_f = open_memstream(&cmd_t, &cmd_z);
  fprintf(cmd_f, "INSERT INTO %sruns VALUES ( ", md->table_prefix);
  mi->unparse_spec(md, cmd_f, RUNS_ROW_WIDTH, runs_spec, &ri);
  fprintf(cmd_f, " ) ;");
  close_memstream(cmd_f); cmd_f = 0;
  if (mi->simple_query(md, cmd_t, cmd_z) < 0) goto fail;
  xfree(cmd_t); cmd_t = 0;
  return run_id;

 fail:
  if (cmd_f) fclose(cmd_f);
  xfree(cmd_t);
  return -1;
}