Exemple #1
0
static int			*tabmax_fill(int *arr, t_dlist *list, size_t block_len)
{
	size_t			tmp_len;
	t_dcell			*tmp;
	int				i;

	i = 0;
	tmp = list->head;
	arr[i] = ft_strlen((char *)tmp->data);
	tmp = tmp->next;
	tmp_len = 2;
	while (tmp != list->head)
	{
		while (tmp_len < block_len && tmp != list->head)
		{
			if ((int)(ft_strlen((char *)tmp->data)) > arr[i])
				arr[i] = ft_strlen((char *)tmp->data);
			tmp = tmp->next;
			tmp_len++;
		}
		tmp_len = 1;
		i++;
	}
	return (cumulate(arr, i));
}
Exemple #2
0
uint DecodeBlock(uchar *bin, FILE *fo)	{
	register int i,pos; step();
	if(!(n=px.Decode(bin))) return 0;
	baza = px.ran_decode(0);
	memset(rb, 0, 0x100*sizeof(int));
	step(); //Heh
	for(i=0; i<n; i++) rb[bin[i]]++;
	#define nextp(id) p[rb[bin[id]]++] = id
	cumulate(); nextp(baza);
	for(i=0; i<baza; i++)	nextp(i);
	for(i=baza+1; i<n; i++)	nextp(i);
	#undef nextp
	for(pos=baza,i=0; i<n; i++)	{
		putc(bin[pos = p[pos]],fo);
		assert(pos>=0 && pos<n);
	}return n;
}
Exemple #3
0
int update(ENVELOPE *env, POINT *p, FUNBAG *lpdf, METROPOLIS *metrop)

/* to update envelope to incorporate new point on log density*/
/* *env          : envelope attributes */
/* *p            : point to be incorporated */
/* *lpdf         : to evaluate log-density */
/* *metrop       : for metropolis step */

{
  POINT *m,*ql,*qr,*q;

  if(!(p->f) || (env->cpoint > env->npoint - 2)){
    /* y-value has not been evaluated or no room for further points */
    /* ignore this point */
    return 0;
  }

  /* copy working POINT p to a new POINT q */
  q = env->p + env->cpoint++;
  q->x = p->x;
  q->y = p->y;
  q->f = 1;

  /* allocate an unused POINT for a new intersection */
  m = env->p + env->cpoint++;
  m->f = 0;
  if((p->pl->f) && !(p->pr->f)){
    /* left end of piece is on log density; right end is not */
    /* set up new intersection in interval between p->pl and p */
    m->pl = p->pl;
    m->pr = q;
    q->pl = m;
    q->pr = p->pr;
    m->pl->pr = m;
    q->pr->pl = q;
  } else if (!(p->pl->f) && (p->pr->f)){
    /* left end of interval is not on log density; right end is */
    /* set up new intersection in interval between p and p->pr */
    m->pr = p->pr;
    m->pl = q;
    q->pr = m;
    q->pl = p->pl;
    m->pr->pl = m;
    q->pl->pr = q;
  } else {
    /* this should be impossible */
    exit(10);
  }

  /* now adjust position of q within interval if too close to an endpoint */
  if(q->pl->pl != NULL){
    ql = q->pl->pl;
  } else {
    ql = q->pl;
  }
  if(q->pr->pr != NULL){
    qr = q->pr->pr;
  } else {
    qr = q->pr;
  }
  if (q->x < (1. - XEPS) * ql->x + XEPS * qr->x){
    /* q too close to left end of interval */
    q->x = (1. - XEPS) * ql->x + XEPS * qr->x;
    q->y = perfunc(lpdf,env,q->x);
  } else if (q->x > XEPS * ql->x + (1. - XEPS) * qr->x){
    /* q too close to right end of interval */
    q->x = XEPS * ql->x + (1. - XEPS) * qr->x;
    q->y = perfunc(lpdf,env,q->x);
  }

  /* revise intersection points */
  if(meet(q->pl,env,metrop)){
    /* envelope violation without metropolis */
    return 1;
  }
  if(meet(q->pr,env,metrop)){
    /* envelope violation without metropolis */
    return 1;
  }
  if(q->pl->pl != NULL){
    if(meet(q->pl->pl->pl,env,metrop)){
      /* envelope violation without metropolis */
      return 1;
    }
  }
  if(q->pr->pr != NULL){
    if(meet(q->pr->pr->pr,env,metrop)){
      /* envelope violation without metropolis */
      return 1;
    }
  }

  /* exponentiate and integrate new envelope */
  cumulate(env);

  return 0;
}
Exemple #4
0
int initial (double *xinit, int ninit, double xl, double xr, int npoint,
	     FUNBAG *lpdf, ENVELOPE *env, double *convex, int *neval,
             METROPOLIS *metrop)

/* to set up initial envelope */
/* xinit        : initial x-values */
/* ninit        : number of initial x-values */
/* xl,xr        : lower and upper x-bounds */
/* npoint       : maximum number of POINTs allowed in envelope */
/* *lpdf        : to evaluate log density */
/* *env         : rejection envelope attributes */
/* *convex      : adjustment for convexity */
/* *neval       : current number of function evaluations */
/* *metrop      : for metropolis step */

{
  int i,j,k,mpoint;
  POINT *q;

  if(ninit<3){
    /* too few initial points */
    return 1001;
  }

  mpoint = 2*ninit + 1;
  if(npoint < mpoint){
    /* too many initial points */
    return 1002;
  }

  if((xinit[0] <= xl) || (xinit[ninit-1] >= xr)){
    /* initial points do not satisfy bounds */
    return 1003;
  }

  for(i=1; i<ninit; i++){
    if(xinit[i] <= xinit[i-1]){
      /* data not ordered */
      return 1004;
    }
  }

  if(*convex < 0.0){
    /* negative convexity parameter */
    return 1008;
  }

  /* copy convexity address to env */
  env->convex = convex;

  /* copy address for current number of function evaluations */
  env->neval = neval;
  /* initialise current number of function evaluations */
  *(env->neval) = 0;

  /* set up space for envelope POINTs */
  env->npoint = npoint;
  env->p = (POINT *)malloc(npoint*sizeof(POINT));
  if(env->p == NULL){
    /* insufficient space */
    return 1006;
  }

  /* set up envelope POINTs */
  q = env->p;
  /* left bound */
  q->x = xl;
  q->f = 0;
  q->pl = NULL;
  q->pr = q+1;
  for(j=1, k=0; j<mpoint-1; j++){
    q++;
    if(j%2){
      /* point on log density */
      q->x = xinit[k++];
      q->y = perfunc(lpdf,env,q->x);
      q->f = 1;
    } else {
      /* intersection point */
      q->f = 0;
    }
    q->pl = q-1;
    q->pr = q+1;
  }
  /* right bound */
  q++;
  q->x = xr;
  q->f = 0;
  q->pl = q-1;
  q->pr = NULL;

  /* calculate intersection points */
  q = env->p;
  for (j=0; j<mpoint; j=j+2, q=q+2){
    if(meet(q,env,metrop)){
      /* envelope violation without metropolis */
      return 2000;
    }
  }

  /* exponentiate and integrate envelope */
  cumulate(env);

  /* note number of POINTs currently in envelope */
  env->cpoint = mpoint;

  return 0;
}
Exemple #5
0
/* Radix sort part of an array of positions based on the given radix. */
void MSD_radixsort(int *positions, int start, int end) {
    int sub_bucket_size = 0;
    int occ[ASCII] = {0};
    /* Scan the array and find every sub-bucket that is not fully resolved. */
    /* At the same time, keep a record of occurrence of every char, which will */
    /* be used to determine their location when sorted in-place. */
    for (int i = start; i <= end; ++i) {
        /* A bucket end is encountered. */
        if ((is_bucket_end[i / 8] & 1 << (i % 8))) {
            /* Skip sub-buckets that are fully resolved. */
            if (sub_bucket_size == 0)
                continue;
            sub_bucket_size++;
            /* Quicksort sufficiently small sub-buckets to improve efficiency. */
            if (sub_bucket_size <= 1024 * 8) {
                nb_of_new_buckets += (sub_bucket_size - 1);
                for (int j = i - sub_bucket_size + 1; j < i; ++j)
                    is_bucket_end[j / 8] |= 1 << (j % 8);
                qsort(&positions[i - sub_bucket_size + 1], sub_bucket_size, sizeof(int), compare);
                for (int j = 0; j < ASCII; ++j)
                    occ[j] = 0;
                sub_bucket_size = 0;
                continue;
            }
            /* In-place bucket sort the sub-bucket. */
            int sorted_position;
            bool *sorted = calloc(1, sub_bucket_size);
            occ[original[(positions[i] + radix) % file_size]]++;
            int cumulate_occ[ASCII];
            int bucket_index[ASCII] = {0};
            cumulate(occ, cumulate_occ);
            for (int j = i - sub_bucket_size + 1; j <= i; ++j) {
                if (sorted[j - i + sub_bucket_size - 1])
                    continue;
                int pending_position = positions[j];
                unsigned char radix_char = original[(pending_position + radix) % file_size];
                sorted_position = i - sub_bucket_size + 1 + cumulate_occ[radix_char] + (bucket_index[radix_char]++);
                while (sorted_position > j) {
                    int tmp = positions[sorted_position];
                    sorted[sorted_position - i + sub_bucket_size - 1] = true;
                    positions[sorted_position] = pending_position;
                    /* Label the end of new sub-buckets. */
                    if (bucket_index[radix_char] == occ[radix_char] && !(is_bucket_end[sorted_position / 8] & 1 << (sorted_position % 8))) {
                        nb_of_new_buckets++;
                        is_bucket_end[sorted_position / 8] |= 1 << (sorted_position % 8);
                    }
                    pending_position = tmp;
                    radix_char = original[(pending_position + radix) % file_size];
                    sorted_position = i - sub_bucket_size + 1 + cumulate_occ[radix_char] + (bucket_index[radix_char]++);
                }
                sorted[sorted_position - i + sub_bucket_size - 1] = true;
                positions[sorted_position] = pending_position;
                if (bucket_index[radix_char] == occ[radix_char] && !(is_bucket_end[sorted_position / 8] & 1 << (sorted_position % 8))) {
                    nb_of_new_buckets++;
                    is_bucket_end[sorted_position / 8] |= 1 << (sorted_position % 8);
                }
            }
            /* Resolve the '[' sub-bucket using quicksort. */
            if (occ['['] > 1) {
                qsort(&positions[i - sub_bucket_size + 1], occ['['], sizeof(int), compare_position_after_shift);
                for (int j = i - sub_bucket_size + 1; j <= i - sub_bucket_size + occ['[']; ++j)
                    is_bucket_end[j / 8] |= 1 << (j % 8);
                nb_of_new_buckets += (occ['['] - 1);
            }
            free(sorted);
            for (int j = 0; j < ASCII; ++j)
                occ[j] = 0;
            sub_bucket_size = 0;
        }
        /* A bucket end is not yet encountered. */
        else {
            sub_bucket_size++;
            occ[original[(positions[i] + radix) % file_size]]++;
        }
    }
}