From 6c2f13c2ffc49a175ecb13be65fc543cc2776960 Mon Sep 17 00:00:00 2001 From: liuchang Date: Tue, 5 Sep 2023 07:38:47 +0000 Subject: [PATCH] replace fopen by directly oprate memory --- src/nonOverlappingTemplateMatchings.c | 172 ++++++++++++++++++++++++-- 1 file changed, 161 insertions(+), 11 deletions(-) diff --git a/src/nonOverlappingTemplateMatchings.c b/src/nonOverlappingTemplateMatchings.c index 7f5d24b..1bc47db 100644 --- a/src/nonOverlappingTemplateMatchings.c +++ b/src/nonOverlappingTemplateMatchings.c @@ -8,6 +8,157 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * N O N O V E R L A P P I N G T E M P L A T E T E S T * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +static unsigned int template9[] = { +0,0,0,0,0,0,0,0,1, +0,0,0,0,0,0,0,1,1, +0,0,0,0,0,0,1,0,1, +0,0,0,0,0,0,1,1,1, +0,0,0,0,0,1,0,0,1, +0,0,0,0,0,1,0,1,1, +0,0,0,0,0,1,1,0,1, +0,0,0,0,0,1,1,1,1, +0,0,0,0,1,0,0,0,1, +0,0,0,0,1,0,0,1,1, +0,0,0,0,1,0,1,0,1, +0,0,0,0,1,0,1,1,1, +0,0,0,0,1,1,0,0,1, +0,0,0,0,1,1,0,1,1, +0,0,0,0,1,1,1,0,1, +0,0,0,0,1,1,1,1,1, +0,0,0,1,0,0,0,1,1, +0,0,0,1,0,0,1,0,1, +0,0,0,1,0,0,1,1,1, +0,0,0,1,0,1,0,0,1, +0,0,0,1,0,1,0,1,1, +0,0,0,1,0,1,1,0,1, +0,0,0,1,0,1,1,1,1, +0,0,0,1,1,0,0,1,1, +0,0,0,1,1,0,1,0,1, +0,0,0,1,1,0,1,1,1, +0,0,0,1,1,1,0,0,1, +0,0,0,1,1,1,0,1,1, +0,0,0,1,1,1,1,0,1, +0,0,0,1,1,1,1,1,1, +0,0,1,0,0,0,0,1,1, +0,0,1,0,0,0,1,0,1, +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,0, +1,1,1,1,1,0,0,0,0, +1,1,1,1,1,0,0,1,0, +1,1,1,1,1,0,1,0,0, +1,1,1,1,1,0,1,1,0, +1,1,1,1,1,1,0,0,0, +1,1,1,1,1,1,0,1,0, +1,1,1,1,1,1,1,0,0, +1,1,1,1,1,1,1,1,0, +}; +static size_t template_size = sizeof(template9)/sizeof(template9[0]); int NonOverlappingTemplateMatchings(int m, int n, BitSequence *epsilon) @@ -21,11 +172,10 @@ NonOverlappingTemplateMatchings(int m, int n, BitSequence *epsilon) number of nonperiodic templates for that file be stored in the m-th position in the numOfTemplates variable. ----------------------------------------------------------------------------*/ - unsigned int bit, W_obs, nu[6], *Wj = NULL; - FILE *fp = NULL; + unsigned int W_obs, nu[6], *Wj = NULL; + size_t template_idx = 0; double sum, chi2, p_value, lambda, pi[6], varWj; int i, j, jj, k, match, SKIP, M, N, K = 5; - char directory[100]; BitSequence *sequence = NULL; N = 8; @@ -36,10 +186,8 @@ NonOverlappingTemplateMatchings(int m, int n, BitSequence *epsilon) } lambda = (M-m+1)/pow(2, m); varWj = M*(1.0/pow(2.0, m) - (2.0*m-1.0)/pow(2.0, 2.0*m)); - sprintf(directory, "templates/template%d", m); if ( ((isNegative(lambda)) || (isZero(lambda))) || - ((fp = fopen(directory, "r")) == NULL) || ((sequence = (BitSequence *) calloc(m, sizeof(BitSequence))) == NULL) ) { goto end; } @@ -66,8 +214,13 @@ NonOverlappingTemplateMatchings(int m, int n, BitSequence *epsilon) sum = 0; for ( k=0; k 1 ) - fseek(fp, (long)(SKIP-1)*2*m, SEEK_CUR); + template_idx += (SKIP-1)*2*m; // fprintf(results[TEST_NONPERIODIC], "%f\n", p_value); fflush(results[TEST_NONPERIODIC]); if (p_value < ALPHA) { goto end; @@ -121,9 +274,6 @@ NonOverlappingTemplateMatchings(int m, int n, BitSequence *epsilon) if (Wj != NULL) { free(Wj); } - if (fp != NULL) { - fclose(fp); - } if (sequence != NULL) { free(sequence); }