Пример #1
0
int main(int argc, char* argv[])
{
    int n,k,nc,i,j;

    int done;       /* boolean for more combinations to compute */
    int *a;         /* list of elements in the current combination (not needed at startup) */
    int **mask;

    sf_axis areplic;
    sf_file in,out;

    sf_init(argc,argv);

    in = sf_input("in");
    out = sf_output("out");
    sf_settype(out,SF_INT);

    if (!sf_getint("k",&k)) sf_error("Need k=");
    /* combination of k elements */

    /* input file */
    if (!sf_histint(in,"n1",&n)) sf_error("No n1=");

    nc = binomial(n,k);
    sf_warning("Number of combinations is %3d",nc);

    /* output file parameters */
    areplic = sf_maxa(nc,0,1);
    sf_oaxa(out,areplic,2);

    sf_putstring (out,"label2", "replication");

    /* memory allocations */
    a = sf_intalloc(k);
    mask = sf_intalloc2(n,nc);

    done = 1;
    j = 0;

    while (1) {

        /* Combination of k elements out of n */
	comb_next(n,k,a,&done);

	if (done) break;
        /* done = 1 if more combinations to compute */
        /* done = 0 when the list is exhausted. */

	for (i = 0; i < k; i++) fprintf(stderr," %3d",a[i]);
	fprintf(stderr," \n");

	for (i = 0; i < n; i++) mask[j][i] = 1;
	for (i = 0; i < k; i++) mask[j][a[i]-1] = 0;

	j++;

    }

    /* output */ 
    sf_warning("Number of combinations is %3d",nc);
    sf_intwrite(mask[0],n*nc,out);

    exit(0);
}
Пример #2
0
/**
   Create combination of stars to form asterism. It has the option to put TTF
   always on the brightest for testing purpose.  */
ASTER_S *setup_aster_comb(int *naster, int nstar, const PARMS_S *parms){
    if(nstar==0){
	*naster=0;
	return NULL;
    }else if(parms->skyc.keeporder){
	/*Use the same order as input stars.*/
	ASTER_S *aster=calloc(1, sizeof(ASTER_S));
	*naster=1;
	int npowfs=parms->maos.npowfs;
	int nleft=nstar;
	int stars[npowfs];
	for(int ipowfs=0; ipowfs<npowfs; ipowfs++){
	    stars[ipowfs]=MIN(nleft, parms->skyc.nwfsmax[ipowfs]);
	    nleft-=stars[ipowfs];
	}
	if(nleft>0){
	    warning("skyc.keeporder is set, but there are more stars than needed, dropped the extra\n");
	}
	int ntot=nstar-nleft;
	aster[0].nwfs=ntot;
	aster[0].wfs=calloc(ntot, sizeof(WFS_S));
	aster[0].iaster=0;
	int count=0;
	for(int ipowfs=0; ipowfs<npowfs; ipowfs++){
	    for(int istar=0; istar<stars[ipowfs]; istar++){
		aster[0].wfs[count].ipowfs=ipowfs;
		aster[0].wfs[count].istar=count;
		count++;
	    }
	}
	return aster;
    }
    
    int ncomb=1;
    ASTER_S *aster;
    int npowfs=parms->skyc.npowfs;
    int nwfs[npowfs];
    int nleft;
    int nwfstot=0;
    nleft=nstar;
    for(int ipowfs=0; ipowfs<npowfs; ipowfs++){
	if(nleft>=parms->skyc.nwfsmax[ipowfs]){
	    nwfs[ipowfs]=parms->skyc.nwfsmax[ipowfs];
	}else{
	    nwfs[ipowfs]=nleft;
	}
	nwfstot+=nwfs[ipowfs];
	ncomb*=comb_select(nleft,nwfs[ipowfs]);
	nleft-=nwfs[ipowfs];
    }
    if(parms->skyc.ttfbrightest){
	if(parms->maos.msa[0]==2){
	    ncomb/=comb_select(nwfstot,nwfs[0]);
	}else{
	    error("Please revise\n");
	}
    }
    if(parms->skyc.verbose){
	info2("Number of stars: %d, number of asterisms: %d\n", nstar, ncomb);
    }
    aster=calloc(ncomb, sizeof(ASTER_S));
    int count=0;
    int *comb=comb_init(nwfstot);
    do{
	if(npowfs==1){
	    aster[count].nwfs=nwfs[0];
	    aster[count].wfs=calloc(nwfs[0], sizeof(WFS_S));
	    aster[count].iaster=count;
	    for(int iwfs=0; iwfs<nwfs[0]; iwfs++){
		aster[count].wfs[iwfs].istar=comb[iwfs];
		aster[count].wfs[iwfs].ipowfs=0;
	    }
	    count++;
	}else if(npowfs==2){
	    int mask[nwfstot];
	    int *comb2=comb_init(nwfs[0]);
	    do{
		memset(mask, 0, sizeof(int)*nwfstot);
		aster[count].nwfs=nwfstot;
		aster[count].wfs=calloc(nwfstot, sizeof(WFS_S));
		aster[count].iaster=count;
		for(int iwfs=0; iwfs<nwfs[0]; iwfs++){
		    aster[count].wfs[iwfs].ipowfs=0;
		    aster[count].wfs[iwfs].istar=comb[comb2[iwfs]];
		    mask[comb2[iwfs]]=1;
		}
		int jstar=0;
		for(int iwfs=0; iwfs<nwfs[1]; iwfs++){
		    aster[count].wfs[iwfs+nwfs[0]].ipowfs=1;
		    while(mask[jstar]) jstar++;
		    aster[count].wfs[iwfs+nwfs[0]].istar=comb[jstar];
		    mask[jstar]=1;
		}
		count++;
	    }while(comb_next(comb2,nwfstot,nwfs[0]) && !parms->skyc.ttfbrightest);
	    free(comb2);
	}
    }while(comb_next(comb,nstar,nwfstot));
    free(comb);
    if(count!=ncomb){
	warning("ncomb=%d, count=%d. They should equal.\n", ncomb, count);
    }
    *naster=count;
    return aster;
}
Пример #3
0
int main()
{
	combiter_t ci;

	assert(comb_begin(3, 2, &ci) == 0);
	
	// index of loop
	int s = 0;

	do {
		comb_t comb;
		// get current combination
		if (comb_get(&ci, &comb))
			break;

		// enumerates all elements contained in this combination
		int elem;
		int t = 0;
		while ((elem = comb_elem(&comb)) != -1) {
			switch (s) {
			case 0: // first round 011
				switch (t) {
				case 0: assert(elem == 1);
					break;
				case 1: assert(elem == 2);
					break;
				default: fprintf(stderr, "elem error\n");
					exit(EXIT_FAILURE);
				}
				break;
			case 1: // second round 101
				switch (t) {
				case 0: assert(elem == 1);
					break;
				case 1: assert(elem == 3);
					break;
				default: fprintf(stderr, "elem error\n");
					exit(EXIT_FAILURE);
				}
				break;
			case 2: // third round 110
				switch (t) {
				case 0: assert(elem == 2);
					break;
				case 1: assert(elem == 3);
					break;
				default: fprintf(stderr, "elem error\n");
					exit(EXIT_FAILURE);
				}
				break;
			default: // no more rounds
				fprintf(stderr, "combination error\n");
				exit(EXIT_FAILURE);
			}
			t++;
		}
		assert(t == 2);
		s++;
	} while (!comb_next(&ci));

	// exactly three combinations for choosing 2 from 3 elements
	assert(s == 3);

	printf("passed\n");

	return 0;
}