#include #include #include #include #include #include #include void getStatsOnDistribution (int readmode, int* theValue, int* ww_length, int* XlenFile, int* YminFile, int* YmaxFile, char* filename) ; //int dist_arb ( int mode, int xmin, int xmax ) // for C-only testing // A mode of 0 means just return the length to allows the SV side to resize the array. // A mode of 1 then fills out that array. // The xmin and xmax tell this function what the resulting bounds need to be. int dist_arb ( // returns an int between xmin and int mode, // and xmax according to the distribution int xmin, // in the csv file arbitrary_distribution_file.txt. int xmax) // Call this first and once with mode==0 to set up. { int i, j, k ; static firsttime = 1 ; static int XlenFile, YminFile, YmaxFile, *X ; static int ww_length = 0 ; // the sum of all y values, scaled int theData ; // This is the return value requested int theValue ; // This is for the value read from the file each time. int scaledVal ; // Conversion between the requested xmin-xmax range and the native // values in the arbitrary_distribution_file.txt file. Note that // the values in the file not only provide an arbitrary curve, but // also have 'Y' values that have no relation to the desired data // value between xmin and xmax ... it is the *relative* values of // 'Y' in the arbitrary_distribution_file.txt w.r.t each other that // provide the distribution. if (mode == 0) { // This firstime business is tied into the expected operation of the randomize. // There is no reason to create the distribution more than once, since it itself is // not randomized. So even if the pre-randomize function fails to have a switch to // stop it from doing two file reads each time randomize is called, the firsttime // flag should stop it doing so. if (firsttime == 1) { firsttime = 0 ; ww_length = 0 ; XlenFile = 0 ; YminFile = 100000 ; YmaxFile = 0 ; getStatsOnDistribution(0, &theValue, &ww_length, &XlenFile, &YminFile, &YmaxFile, "arbitrary_distribution_file.txt") ; X = (int*)malloc(ww_length * sizeof(int)) ; j = 0 ; for (i = 0 ; i < XlenFile; i++) { getStatsOnDistribution(1, &theValue, &ww_length, &XlenFile, &YminFile, &YmaxFile, "arbitrary_distribution_file.txt") ; scaledVal = (int) ((float)i * (float)(xmax-xmin)/(float)XlenFile) + xmin ; for (k=0; k < theValue ; k++) { if ((j+k) > ww_length) break ; //*(int*)svGetArrElemPtr1(X,j+k) = scaledVal ; // for the dynamic array solution X[j+k] = scaledVal ; } j = j + k ; } } } theData = X[rand() % ww_length] ; // ignore bias for now return theData ; } /* -------------------------------------------------------------------------- This opens the file arbitrary_distribution_file.txt and does one of two things depending on the value of 'readmode'. If readmode == 0, it gathers the following values: The actual length of the file, the length of the X-axis, XlenFile, and the maximum and minimum Y values, YminFile, YmaxFile. These will be used during the second pass to scale all values and lengths in the file with the requirements from the caller. The value of 'theValue' is not meant to be used. If readmode == 1: it performs a read of a single line of the file and returns the value on that line in 'theValue'. The number of lines should be known already, so don't ask it to read too many lines. ----------------------------------------------------------------------- */ void getStatsOnDistribution(int readmode, int* theValue, int* ww_length, int* XlenFile, int* YminFile, int* YmaxFile, char* filename) { static int firsttimethrough = 1 ; int temp ; char* aptr ; char thefile[100] ; char line [100]; // These are just ints really strcpy(thefile, filename) ; FILE *fp_R = NULL; static FILE *fp_R2 = NULL; if (readmode == 0) { fp_R = fopen ( thefile, "r" ); if (fp_R == NULL) { perror(thefile); } else { while (fgets(line, sizeof line, fp_R)!= NULL) { temp = (int)strtol(line, &aptr, 10) ; if (temp < *YminFile) *YminFile = temp ; if (temp > *YmaxFile) *YmaxFile = temp ; *ww_length += temp ; (*XlenFile)++ ; } fclose(fp_R); } } else // readmode == 1 { if (firsttimethrough == 1) { firsttimethrough = 0 ; fp_R2 = fopen ( thefile, "r" ); } if (fp_R2 == NULL) { perror(thefile); } else { if (fgets(line, sizeof line, fp_R2) != NULL) { temp = (int)strtol(line, &aptr, 10) ; *theValue = temp ; } else { printf("\nUh Oh.") ; exit ; } } } }