static void WriteHyperFile ( const char *szFilename, const hyperequity ahe[], const int nC ) { int nPos = Combination( 25 + nC, nC ); int i, j, k; char sz[ 41 ]; FILE *pf; if ( ! ( pf = g_fopen ( szFilename, "w+b" ) ) ) { perror ( szFilename ); return; } sprintf ( sz, "gnubg-H%dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", nC ); fputs ( sz, pf ); for ( i = 0; i < nPos; ++i ) for ( j = 0; j < nPos; ++j ) { for ( k = 0; k < NUM_OUTPUTS; ++k ) WriteProb ( pf, ahe[ i * nPos + j ].arOutput[ k ] ); for ( k = 1; k < 5; ++k ) WriteEquity ( pf, ahe[ i * nPos + j ].arEquity[ k ] ); putc(0,pf); /* padding */ } fclose ( pf ); }
static void generate_ts(const int nTSP, const int nTSC, const int fHeader, const int fCubeful, const int nHashSize, bearoffcontext * pbc, FILE * output) { int i, j, k; int iPos; int n; short int asiEquity[4]; xhash h; FILE *pfTmp; unsigned char ac[8]; char *tmpfile; int fTTY = isatty(STDERR_FILENO); pfTmp = GetTemporaryFile(NULL, &tmpfile); if (pfTmp == NULL) exit(2); /* initialise xhash */ if (XhashCreate(&h, nHashSize / (fCubeful ? 8 : 2))) { fprintf(stderr, _("Error creating xhash with %d elements\n"), nHashSize / (fCubeful ? 8 : 2)); exit(2); } XhashStatus(&h); /* write header information */ if (fHeader) { char sz[41]; sprintf(sz, "gnubg-TS-%02d-%02d-%1dxxxxxxxxxxxxxxxxxxxxxxx\n", nTSP, nTSC, fCubeful); fputs(sz, output); } /* generate bearoff database */ n = Combination(nTSP + nTSC, nTSC); iPos = 0; /* positions above diagonal */ for (i = 0; i < n; i++) { for (j = 0; j <= i; j++, ++iPos) { BearOff2(i - j, j, nTSP, nTSC, asiEquity, n, fCubeful, &h, pbc, pfTmp); for (k = 0; k < (fCubeful ? 4 : 1); ++k) WriteEquity(pfTmp, asiEquity[k]); XhashAdd(&h, (i - j) * n + j, asiEquity, fCubeful ? 8 : 2); } if (fTTY) fprintf(stderr, "%d/%d \r", iPos, n * n); } /* positions below diagonal */ for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++, ++iPos) { BearOff2(i + n - j, j, nTSP, nTSC, asiEquity, n, fCubeful, &h, pbc, pfTmp); for (k = 0; k < (fCubeful ? 4 : 1); ++k) WriteEquity(pfTmp, asiEquity[k]); XhashAdd(&h, (i + n - j) * n + j, asiEquity, fCubeful ? 8 : 2); } if (fTTY) fprintf(stderr, "%d/%d \r", iPos, n * n); } putc('\n', stderr); XhashStatus(&h); XhashDestroy(&h); /* sort file from ordering: * * 136 123 * 258 to 456 * 479 789 * */ for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { unsigned int count = fCubeful ? 8 : 2; k = CalcPosition(i, j, n); fseek(pfTmp, count * k, SEEK_SET); if (fread(ac, 1, count, pfTmp) != count || fwrite(ac, 1, count, output) != count) { fprintf(stderr, "failed to read from or write to database file\n"); exit(3); } } } fclose(pfTmp); g_unlink(tmpfile); }