int behindDue_Common(Task** tasks, float Cmax)
{
    int i;
    for (i = _nbTasks - 1; i >= 0; i--)
    {
        if(Cmax == tasks[i]->endingTime)
        {
            Cmax = tasks[i]->beginningTime;
            tasks[i]->nbWorkersToAssign = minInt(tasks[i]->nbWorkersToAssign + 1, minInt(_nbWorkers + 1, tasks[i]->workersMax));
        }
    }
    return _nbWorkers + 1;
}
예제 #2
0
void test_minInt(void) {
    int actual = minInt(0, 0);
    TEST_ASSERT_EQUAL(0, actual);

    actual = minInt(0, 25000);
    TEST_ASSERT_EQUAL(0, actual);

    actual = minInt(25000, 0);
    TEST_ASSERT_EQUAL(0, actual);

    actual = minInt(-25000, 0);
    TEST_ASSERT_EQUAL(-25000, actual);

    actual = minInt(-25000, 100);
    TEST_ASSERT_EQUAL(-25000, actual);

    actual = minInt(-25000, 30000);
    TEST_ASSERT_EQUAL(-25000, actual);

    actual = minInt(-25000, 22000);
    TEST_ASSERT_EQUAL(-25000, actual);

    actual = minInt(10, 30000);
    TEST_ASSERT_EQUAL(10, actual);
}
예제 #3
0
void OutlineElement::drawLine( WWindow * win, int & idx,
                                int width, int height )
//----------------------------------------------------------------
{
    OutlineElement * elm;
    WPoint           start( width * _level + width / 2, height * (idx + 1) );
    WPoint           end( start );
    WOrdinal         maxY = height * win->getRows();

    idx += 1;

    if( _state == ESExpanded ) {

        for( elm = _child; elm != NULL && idx < win->getRows() + 1;
            elm = elm->visibleSib() ) {

            end = WPoint( width * elm->_level,
                          height * idx + height / 2 );

            if( end.y() < maxY ) {
                win->drawLine( WPoint( start.x(), end.y() ), end, ColorBlack );
                elm->drawLine( win, idx, width, height );
            }

            if( elm->_lastSib ) {
                break;
            }
        }

        end.x( start.x() );
        end.y( minInt( end.y(), maxY ) );

        if( start.y() < maxY ) {
            win->drawLine( start, end, ColorBlack );
        }
    }
}
예제 #4
0
int CheckedFile::readNString( String & str )
//------------------------------------------
// read in a string in the form <uint_16>{<byte>}*
{
    const int   BufLen = 255;
    char        buffer[ BufLen + 1 ];
    uint_16     strLen;
    uint_16     amtRead = 0;
    int         maxRead;

    str = "";       // clear string

    read( &strLen, sizeof( uint_16 ) );

    while( amtRead < strLen ) {
        maxRead = minInt( BufLen, strLen - amtRead );
        read( buffer, maxRead );
        buffer[ maxRead ] = '\0';
        str += buffer;
        amtRead += maxRead;
    }

    return strLen;
}
int main(int argc, char **argv){
	uint32_t datarows, datacolumns;
	uint32_t indexrows, indexcolumns;
	uint32_t prows, pcolumns;
	uint32_t i, j, k, k1, k2;

	//Check input arguments
	if (argc != 4) {
		fprintf(stderr,"USAGE: %s <PerplexResults.tsv> <index.tsv> <pressure.tsv>\n", argv[0]);
		exit(1);
	}
	
	
	// Import data
	double* const data = csvparseflat(argv[1],'\t', &datarows, &datacolumns);
	double* const index = csvparseflat(argv[2],'\t', &indexrows, &indexcolumns);
	double* const pressure = csvparseflat(argv[3],'\t', &prows, &pcolumns);
//	printf("Data rows: %i, Data columns: %i\n",datarows,datacolumns);
//	printf("Index rows: %i, Index columns: %i\n",indexrows,indexcolumns);
//	printf("Pressure rows: %i, Pressure columns: %i\n",prows,pcolumns);


	// Convert indices to unsigned intergers for faster comparision
	uint32_t* restrict target = malloc(indexrows * sizeof(uint32_t));
	uint32_t* restrict source = malloc(datarows * sizeof(uint32_t));
	for (i=0; i<indexrows; i++){
		target[i] = (uint32_t) round(index[i]);
	}
	for (j=0; j<datarows; j++){
		source[j] = (uint32_t) round(data[0*datarows + j]);
	}

	
	uint32_t jmin, step;
	// Find first line of real output
	for (jmin=0; isnan(data[0*datarows+jmin]); jmin++){
	}
	// Find output step size
	for (step=1; source[jmin+step]==source[jmin]; step++) {
	}
//	printf("jmin: %i\n",jmin);
//	printf("Step size: %i\n",step);



	// Make target variables and initialize with NANs
	double* restrict Rho = malloc(indexrows * sizeof(double));
	for (i=0; i<indexrows; i++) Rho[i]=NAN;
	double* restrict Vp = malloc(indexrows * sizeof(double));
	for (i=0; i<indexrows; i++) Vp[i]=NAN;
	double* restrict VpVs = malloc(indexrows * sizeof(double));
	for (i=0; i<indexrows; i++) VpVs[i]=NAN;
	double* restrict RhoStdErr = malloc(indexrows * sizeof(double));
	for (i=0; i<indexrows; i++) RhoStdErr[i]=NAN;
	double* restrict VpStdErr = malloc(indexrows * sizeof(double));
	for (i=0; i<indexrows; i++) VpStdErr[i]=NAN;
	double* restrict VpVsStdErr = malloc(indexrows * sizeof(double));
	for (i=0; i<indexrows; i++) VpVsStdErr[i]=NAN;

	double r1, r2, rmin1, rmin2, p1, p2;

//	pcg32_random_t rng;
//	pcg32_srandom_r(&rng,time(NULL), clock());
	
	for (i=0; i<indexrows; i++){
		for (j=jmin; j<datarows; j += step){
			if (source[j]==target[i]){
				// Find best pressure match for first pressure
				p1 = pressure[0*prows + i];
				p2 = pressure[1*prows + i];
				rmin1 = p1;
				rmin2 = p2;
				k1 = 0;
				k2 = 0;
				for (k=0; k<100; k++){
					r1=fabs(p1 - data[1*datarows + j + k]);
					r2=fabs(p2 - data[1*datarows + j + k]);
					if (r1 < rmin1) {
						rmin1 = r1;
						k1 = k;
					}
					if (r2 < rmin2) {
						rmin2 = r2;
						k2 = k;
					}
				}
				Offset_nanstderr(&data[3*datarows + j + minInt(k1,k2)], (uint32_t)abs(k2-k1), &Rho[i], &RhoStdErr[i]);
				Offset_nanstderr(&data[4*datarows + j + minInt(k1,k2)], (uint32_t)abs(k2-k1), &Vp[i], &VpStdErr[i]);
				Offset_nanstderr(&data[5*datarows + j + minInt(k1,k2)], (uint32_t)abs(k2-k1), &VpVs[i], &VpVsStdErr[i]);


				// Save fitting results
//				Rho[i] =  data[3*datarows + j + kmin];// * (1 + pcg_gaussian_ziggurat(&rng, 0.005)); //Rho
//				Vp[i] = data[4*datarows + j + kmin];// * (1 + pcg_gaussian_ziggurat(&rng, 0.005)); //Vp
//				VpVs[i] = data[5*datarows + j + kmin];// * (1 + pcg_gaussian_ziggurat(&rng, 0.005)); //VpVs

				// And move to the next index
				break;
			} 
		}
	}

	FILE *fp;
	// Write results to file
	fp = fopen("Rho.csv","w");
	for (i=0; i<indexrows; i++) fprintf(fp, "%g\t%g\n", Rho[i], RhoStdErr[i]);
	fclose(fp);
	fp = fopen("Vp.csv","w");
	for (i=0; i<indexrows; i++) fprintf(fp, "%g\t%g\n", Vp[i], VpStdErr[i]);
	fclose(fp);
	fp = fopen("VpVs.csv","w");
	for (i=0; i<indexrows; i++) fprintf(fp, "%g\t%g\n", VpVs[i], VpStdErr[i]);
	fclose(fp);

		
return 0;
}
예제 #6
0
파일: diff.c 프로젝트: HardenedBSD/pkg
/*
** Compare two blocks of text on lines iS1 through iE1-1 of the aFrom[]
** file and lines iS2 through iE2-1 of the aTo[] file.  Locate a sequence
** of lines in these two blocks that are exactly the same.  Return
** the bounds of the matching sequence.
**
** If there are two or more possible answers of the same length, the
** returned sequence should be the one closest to the center of the
** input range.
**
** Ideally, the common sequence should be the longest possible common
** sequence.  However, an exact computation of LCS is O(N*N) which is
** way too slow for larger files.  So this routine uses an O(N)
** heuristic approximation based on hashing that usually works about
** as well.  But if the O(N) algorithm doesn't get a good solution
** and N is not too large, we fall back to an exact solution by
** calling optimalLCS().
*/
static void longestCommonSequence(
  DContext *p,               /* Two files being compared */
  int iS1, int iE1,          /* Range of lines in p->aFrom[] */
  int iS2, int iE2,          /* Range of lines in p->aTo[] */
  int *piSX, int *piEX,      /* Write p->aFrom[] common segment here */
  int *piSY, int *piEY       /* Write p->aTo[] common segment here */
){
  int i, j, k;               /* Loop counters */
  int n;                     /* Loop limit */
  DLine *pA, *pB;            /* Pointers to lines */
  int iSX, iSY, iEX, iEY;    /* Current match */
  int skew = 0;              /* How lopsided is the match */
  int dist = 0;              /* Distance of match from center */
  int mid;                   /* Center of the span */
  int iSXb, iSYb, iEXb, iEYb;   /* Best match so far */
  int iSXp, iSYp, iEXp, iEYp;   /* Previous match */
  int64_t bestScore;      /* Best score so far */
  int64_t score;          /* Score for current candidate LCS */
  int span;                     /* combined width of the input sequences */

  span = (iE1 - iS1) + (iE2 - iS2);
  bestScore = -10000;
  score = 0;
  iSXb = iSXp = iS1;
  iEXb = iEXp = iS1;
  iSYb = iSYp = iS2;
  iEYb = iEYp = iS2;
  mid = (iE1 + iS1)/2;
  for(i=iS1; i<iE1; i++){
    int limit = 0;
    j = p->aTo[p->aFrom[i].h % p->nTo].iHash;
    while( j>0
      && (j-1<iS2 || j>=iE2 || !p->same_fn(&p->aFrom[i], &p->aTo[j-1]))
    ){
      if( limit++ > 10 ){
        j = 0;
        break;
      }
      j = p->aTo[j-1].iNext;
    }
    if( j==0 ) continue;
    if( i<iEXb && j>=iSYb && j<iEYb ) continue;
    if( i<iEXp && j>=iSYp && j<iEYp ) continue;
    iSX = i;
    iSY = j-1;
    pA = &p->aFrom[iSX-1];
    pB = &p->aTo[iSY-1];
    n = minInt(iSX-iS1, iSY-iS2);
    for(k=0; k<n && p->same_fn(pA,pB); k++, pA--, pB--){}
    iSX -= k;
    iSY -= k;
    iEX = i+1;
    iEY = j;
    pA = &p->aFrom[iEX];
    pB = &p->aTo[iEY];
    n = minInt(iE1-iEX, iE2-iEY);
    for(k=0; k<n && p->same_fn(pA,pB); k++, pA++, pB++){}
    iEX += k;
    iEY += k;
    skew = (iSX-iS1) - (iSY-iS2);
    if( skew<0 ) skew = -skew;
    dist = (iSX+iEX)/2 - mid;
    if( dist<0 ) dist = -dist;
    score = (iEX - iSX)*(int64_t)span - (skew + dist);
    if( score>bestScore ){
      bestScore = score;
      iSXb = iSX;
      iSYb = iSY;
      iEXb = iEX;
      iEYb = iEY;
    }else if( iEX>iEXp ){
      iSXp = iSX;
      iSYp = iSY;
      iEXp = iEX;
      iEYp = iEY;
    }
  }
  if( iSXb==iEXb && (iE1-iS1)*(iE2-iS2)<400 ){
    /* If no common sequence is found using the hashing heuristic and
    ** the input is not too big, use the expensive exact solution */
    optimalLCS(p, iS1, iE1, iS2, iE2, piSX, piEX, piSY, piEY);
  }else{
    *piSX = iSXb;
    *piSY = iSYb;
    *piEX = iEXb;
    *piEY = iEYb;
  }
}
예제 #7
0
unsigned int MO::selectPluralForm(int n)
{
    auto index = onPluralExpression ? onPluralExpression(n) : MO::DEFAULT_PLURAL_EXPRESSION(n);
    return maxInt(0, minInt(index, nplurals-1));
}
예제 #8
0
bool TreeNode::resolveChildWard( TreeRect & r )
//---------------------------------------------
{
    bool       vert = _parent->getDirection() == TreeVertical;
    TreeCoord  minSib = vert ? r.x() : r.y();
    TreeCoord  maxSib = 0;
    TreeCoord  childOff = childSep / 2;
    TreeCoord  maxChild = 0;
    bool       minSet = false;
    int        maxLevel;
    int        minLevel;
    int        i;

    for( i = 0; i < getCount( FlatList ); i += 1 ) {
        TreeNode * node = getNode( FlatList, i );

        int tLevel = node->getLevel();
        if( node->_flags.enabled > Hidden && tLevel >= 0 ) {
            if( minSet ) {
                maxLevel = maxInt( maxLevel, tLevel );
                minLevel = minInt( minLevel, tLevel );
            } else {
                maxLevel = tLevel;
                minLevel = tLevel;
                minSet = true;
            }
        }
    }

    if( !minSet ) {
        return false;
    }

    for( int level = minLevel; level <= maxLevel; level += 1 ) {
        maxChild = 0;
        for( i = 0; i < getCount( FlatList ); i += 1 ) {
            TreeNode * node = getNode( FlatList, i );
            if( node->getLevel() == level && node->_flags.enabled > Hidden ) {
                vert ? node->_bounding.y( childOff )
                     : node->_bounding.x( childOff );

                maxChild = maxCoord( maxChild, vert ? node->_bounding.h()
                                                    : node->_bounding.w() );
                maxSib = maxCoord( maxSib, vert ? node->_descend.x() + node->_descend.w()
                                                : node->_descend.y() + node->_descend.h() );
                minSib = minCoord( minSib, vert ? node->_descend.x()
                                                : node->_descend.y() );
            }
        }
        childOff += maxChild + childSep;
    }

    if( vert ) {
        r.x( minSib );
        r.w( maxSib - r.x() );
        r.h( childOff );
        _descend.h( childOff );
    } else {
        r.w( childOff );
        r.y( minSib );
        r.h( maxSib - r.y() );
        _descend.w( childOff );
    }

    return true;
}
/*
 * caller free both sino and newSino
 * 
 * This function prepares a new set of sinograms for the next recursive call.
 * It DOES angularly downsample the sinograms.
 *
 */
sinograms* newSinoForNextIter_forw(sinograms* sino,int newSinoSize,myFloat constShiftingFactor,myFloat* nu_i)
{

  int P = sino->num;
  sinograms* newSino;
  int size = sino->size;
  int newNumSino;
  int m,n,k,p;
  myFloat kk,pp;
  myFloat sum;
  myFloat angle;
  
  myFloat temp1,temp2,temp3;
  myFloat shiftingFactor;
  myFloat radialShift;
  myFloat totalShift;
  myFloat temp;
  
  int n2;
  int lowerPBound,upperPBound,lowerKBound,upperKBound;
  myFloat angularSupport;
  myFloat radialSupport;

  //preparing new sinograms
  newSino = (sinograms*)malloc(1*sizeof(sinograms));
  newSino->T = T;
  newSino->size = newSinoSize; 
  newSino->num = ceilf((myFloat)P/2);

  newNumSino = newSino->num;
  (newSino->sino)  = (myFloat**)malloc(newNumSino*sizeof(myFloat*));
  (newSino->sine) = (myFloat*)malloc(newNumSino*sizeof(myFloat));
  (newSino->cosine) = (myFloat*)malloc(newNumSino*sizeof(myFloat));
  //done preparing spaces for new sinograms


  angularSupport = ANGULAR_SUPPORT;
  radialSupport = RADIAL_SUPPORT;


  for(n=0;n<newNumSino;n++){
    //for each new angle
    n2 = 2*n;                
    (newSino->sino)[n] = (myFloat*)malloc(newSinoSize*sizeof(myFloat));
    (newSino->sine)[n] = (sino->sine)[n2];
    (newSino->cosine)[n] = (sino->cosine)[n2];    
    
    lowerPBound = maxInt(n2-(int)(angularSupport),0);    
    upperPBound = minInt(n2+ceilf(angularSupport),P-1);  


    for(m=0;m<newSinoSize;m++){
      //for each new radial array index
      sum = 0;
      
      for(p=lowerPBound;p<=upperPBound;p++){
	    //for each old angle
	    temp1 = n2-p;
	    temp2 = ANGULAR_INTERP(temp1);

	    radialShift = m+nu_i[n2]-nu_i[p];
	    shiftingFactor = rintf(nu_i[p]) + constShiftingFactor;

	    lowerKBound = (minInt(maxInt(ceilf(radialShift-shiftingFactor-radialSupport),0),size-1));
	    upperKBound = (maxInt(minInt((int)(radialShift-shiftingFactor+radialSupport),size-1),0)); 
	
	    
        //this loop can be optimized more, but i didn't have time.
	    for(k=lowerKBound;k<=upperKBound;k++){ 
	        //for each old radial array index
	        temp3 = (sino->sino)[p][k];
	  
	        kk = k + shiftingFactor; 
	        temp = radialShift-kk;
	        temp1 = RADIAL_INTERP(temp);
	  
	        sum += temp1 * temp2 * temp3;

	    }//end for k
      }//end for p

      (newSino->sino)[n][m] =  sum;
        
    }//end for m
  }//end for n

  return newSino;
}
/*
 * direct_forw(the sinograms,the size of the image, tau's,output image)
 *
 * direct_forw corresponds EXACTLY to the direct_forw method. 
 *
 */
void direct_forw( sinograms* g , int size , myFloat* tau , image* ans )
{
  int m;        //x direct_forwion
  int n;        //y direct_forwion

  myFloat* curRow; 
  myFloat middle = ( ( myFloat )( g->size ) ) / 2 - 0.5;
  myFloat halfSize = 0.5 * size;
  int numSino = g->num;
  myFloat scale = M_PI/numSino;       //scale output by PI/(# of projections)  ; see derivation of inverse radon


  int p;
  int k;
  int P = g->num;
  int sinoSize;

  myFloat i;
  myFloat sum = 0;
  myFloat* curSino;

  myFloat temp;
  
  myFloat realm; 
  myFloat realn;

  myFloat scaledRealm;
  myFloat scaledRealn;

  myFloat lowerXLimit;
  myFloat upperXLimit;
  myFloat lowerYLimit;
  myFloat upperYLimit;
   
  myFloat spXSupport;
  myFloat spYSupport;
  myFloat intpSupport;

  myFloat curr;
  myFloat lowerKBound;
  myFloat upperKBound;

  int lowerKBound2;
  int upperKBound2;

  myFloat theta;

  myFloat exactRadialPosition;

  intpSupport = RADIAL_SUPPORT;


  sinoSize = g->size;   


  //for each pixel in the image
  for(n=0;n<size;n++){
    curRow = (ans->pixel)[n];
    
    for(m=0;m<size;m++){

      //for each sinogram
      sum = 0.0;

      for(p=0;p<P;p++){
        curSino = g->sino[p];
        
        realm = m - halfSize + 0.5; 
        realn = n - halfSize + 0.5;
        scaledRealm = realm * oneOverT;
        scaledRealn = realn * oneOverT;         
        
        exactRadialPosition = scaledRealm*(g->cosine)[p] + scaledRealn*(g->sine)[p];

        exactRadialPosition -= tau[p]-middle; 
        //exactRadialPosition is now in terms of array index! (although it's still a float)

        //since spSupport is zero
        lowerKBound = exactRadialPosition-intpSupport;
        upperKBound = exactRadialPosition+intpSupport;

        lowerKBound2 = maxInt((int)(lowerKBound-ERROR_TOLERANCE)+1,0);
        upperKBound2 = minInt((int)(upperKBound),(g->size)-1);

        //for nearest interpolator, this needs to be checked
        if(upperKBound2<lowerKBound2){
            upperKBound2 = lowerKBound2;
        }

        //for each entry in this sinogram
        for(k=lowerKBound2;k<=upperKBound2;k++){      
            temp = weight_forw( exactRadialPosition , k );
            curSino[k] += curRow[m] * temp * scale;
        }//end for k
      }//end for p     
    } //end for n
  } //end for m
}