Exemplo n.º 1
0
Arquivo: sdmap.c Projeto: ifzz/libsrt
sdm_t *sdm_dup(const sdm_t *src)
{
	ASSERT_RETURN_IF(!src, NULL);
	size_t nsubmaps = sdm_size(src);
	ASSERT_RETURN_IF(!nsubmaps, NULL);
	const sm_t **maps = sdm_submaps_r(src);
	ASSERT_RETURN_IF(!maps, NULL);
	ASSERT_RETURN_IF(!maps[0], NULL);
	enum eSM_Type t = (enum eSM_Type)maps[0]->f.type;
	ASSERT_RETURN_IF(t >= SM_TotalTypes, NULL);
	size_t alloc_size = sizeof(sdm_t) + sizeof(sm_t *) * (nsubmaps - 1);
	sdm_t *dm = (sdm_t *)__sd_malloc(alloc_size);
	ASSERT_RETURN_IF(!dm, NULL);
	size_t i = 0;
	for (; i < nsubmaps; i++ ) {
		if (!(dm->maps[i] = sm_dup(maps[i])))
			break; /* Allocation error */
	}
	if (i != nsubmaps) { /* Handle allocation error */
		for (; i < nsubmaps; i++ ) {
			if (dm->maps[i])
				sm_free(&dm->maps[i]);
			else
				break;
		}
		free(dm);
		dm = NULL;
	}
	return dm;
}
Exemplo n.º 2
0
char		*sm_splithead(char *str, char spliter)
{
  size_t	i;

  i = 0;
  while (str[i])
  {
    if (str[i] == spliter)
      return (sm_dupn(str, i));
    i++;
  }
  return (sm_dup(str));
}
Exemplo n.º 3
0
t_list		*sm_split(char *str, char spliter)
{
  int		i;
  int		j;
  t_list	*list;
  char		*n_str;

  i = -1;
  j = 0;
  if (!str)
    return (NULL);
  if (!(list = new_list()))
    return (NULL);
  while (str[++i])
    if (str[i] == spliter)
    {
      n_str = sm_dupn(&str[j], i - j);
      list->push(list, _wrap(n_str, sm_len(n_str)));
      j = i + 1;
    }
  if (j != i)
    list->push(list, _wrap(sm_dup(&str[j]), sm_len(&str[j])));
  return (list);
}
Exemplo n.º 4
0
  /* Check the matrix sparsity */
  nelem = 0;
  sm_foreach_row(A, prow) {
    nelem += prow->length;
  }
  sparsity = (double) nelem / (double) (A->nrows * A->ncols);

  /* Determine an upper bound on the solution */
  bound = 1;
  sm_foreach_col(A, pcol) {
    bound += WEIGHT(weight, pcol->col_num);
  }

  /* Perform the covering */
  select = solution_alloc();
  dup_A = sm_dup(A);
  best = sm_mincov(dup_A, select, weight, 0, bound, 0, &stats);
  sm_free(dup_A);
  solution_free(select);

  if (stats.debug) {
    if (stats.no_branching) {
      (void) printf("**** heuristic covering ...\n");
      (void) printf("lower bound = %d\n", stats.lower_bound);
    }
    (void) printf("matrix     = %d by %d with %d elements (%4.3f%%)\n",
        A->nrows, A->ncols, nelem, sparsity * 100.0);
    (void) printf("cover size = %d elements\n", best->row->length);
    (void) printf("cover cost = %d\n", best->cost);
    (void) printf("time       = %s\n", 
        util_print_time(util_cpu_time() - stats.start_time));