/*************************************/ //long *res = (long*)malloc(tag.size * gsm.size * sizeof(long)); long **res = (long **)calloc(tag.size, sizeof(long *)); for (i = 0; i < tag.size; i++) { res[i] = (long *)calloc(gsm.size, sizeof(long)); } while ( blk = depile(blk, &i, &j, &freq) ) res[i][j] = freq; /*************************************/ FILE *fpo = stdout; fprintf(fpo, "etiquette"); for (j=0; j<gsm.size; j++) fprintf(fpo, " %s", gsm.array[j]); fprintf(fpo, "\n"); for (i=0; i<tag.size; i++) { fprintf(fpo, "%s", tag.array[i]); // for (j=0; j<gsm.size; j++) // fprintf(fpo, " %d", res[i*gsm.size+j]); for (j=0; j<gsm.size; j++) fprintf(fpo, " %d", res[i][j]); fprintf(fpo, "\n"); free(res[i]); } hashtable_destroy(tag.h, 0); free(tag.array); hashtable_destroy(gsm.h, 0); free(gsm.array); free(res); }
for (i = 0; i < tag.size; i++) { res[i] = (long *)calloc(gsm.size, sizeof(long)); } fprintf(stderr, "boucle dépile\n"); while ( blk = depile(blk, &i, &j, &freq) ) { fprintf(stderr, "%d %d %d\n", i, j, freq); res[i][j] = freq; } fprintf(stderr, "boucle dépile terminée\n"); /*************************************/ FILE *fpo = stdout; fprintf(fpo, "etiquette"); for (j=0; j<gsm.size; j++) fprintf(fpo, " %s", gsm.array[j]); fprintf(fpo, "\n"); // As-tu cette ligne d'entête (ci dessus) ? for (i=0; i<tag.size; i++) { fprintf(fpo, "%s", tag.array[i]); // for (j=0; j<gsm.size; j++) // fprintf(fpo, " %d", res[i*gsm.size+j]); for (j=0; j<gsm.size; j++) fprintf(fpo, " %d", res[i][j]); fprintf(fpo, "\n"); free(res[i]); } hashtable_destroy(tag.h, 0);
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionb->next = 0; } else if ( b->n == sizeof(b->tbfreq)/sizeof(FREQ) ) {
b->next = (BLOC *)0; } else if ( b->n == NFREQ ) {
#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "hashtable.h" #define NFREQ 1024 typedef struct { char **array; int size; int allocSize; struct hashtable *h; } ARR; typedef struct { long i; long j; long freq; } FREQ; typedef struct st_bloc { FREQ tbfreq[1024]; int n; struct st_bloc *next; } BLOC; static unsigned int hashKey(void* item) { unsigned int h = 0; char *p = (char *)item; while ( *p ) { h = ((h << 5) + h) + *p++; } return h; } static int compareKey(void* k1, void* k2) { return !strcmp(k1, k2); } void initArray(ARR* a, int sz, int hsz) { a->size = 0; a->allocSize = sz; a->array = (char **) malloc(sizeof(char *) * sz); a->h = create_hashtable(hsz, hashKey, compareKey); } long findOrAddArray(ARR *arr, char *item) { long * pi = (long *)hashtable_search(arr->h, item); if ( pi ) { return *pi; } long i = arr->size; int n = strlen(item); char * key = malloc(n + 1 + sizeof(long)); strcpy(key, item); pi = (long *)(&key[n + 1]); *pi = i; if ( arr->size == arr->allocSize ) { arr->allocSize *= 2; char ** p = (char **) malloc(sizeof(char *) * arr->allocSize); memcpy(p, arr->array, sizeof(char *) * arr->size); free(arr->array); arr->array = p; } arr->array[i] = key; arr->size++; hashtable_insert(arr->h, key, pi); return i; } BLOC* empile(BLOC *b, long i, long j, long freq) { if ( !b ) { b = (BLOC *)malloc(sizeof(BLOC)); b->n = 0; b->next = (BLOC *)0; } else if ( b->n == NFREQ ) { BLOC *x = b; b = (BLOC *)malloc(sizeof(BLOC)); b->next = x; b->n = 0; } FREQ* pf = &b->tbfreq[ b->n++ ]; pf->i = i; pf->j = j; pf->freq = freq; return b; } BLOC* depile(BLOC *b, long *i, long *j, long * freq) { if ( !b ) return NULL; if ( b->n > 0 ) { FREQ* pf = &b->tbfreq[ --b->n ]; *i = pf->i; *j = pf->j; *freq = pf->freq; } else { BLOC *x = b->next; free(b); b = depile(x, i, j, freq); } return b; } main(int argc, char *argv[]) { BLOC *blk = NULL; FILE *fp; ARR tag; ARR gsm; initArray(&tag, 10000, 1000000); initArray(&gsm, 500, 1000); if ( argc != 2 ) { fprintf(stderr, "il manque le fichier a traiter."); exit(1); } char bufTag[512], bufGsm[512]; long freq; long i, j; if ( !(fp = fopen(argv[1],"r")) ) { fprintf(stderr, "%s: erreur d'ouverture"); exit(1); } struct stat st; stat(argv[1], &st); long fsz = st.st_size; fprintf(stderr, "size = %d\n", fsz); float n = 0.1; while ( !feof(fp) ) { if ( fscanf(fp, "%s %s %d", bufGsm, bufTag, &freq) == 3 ) { i = findOrAddArray(&tag, bufTag); j = findOrAddArray(&gsm, bufGsm); blk = empile(blk, i, j, freq); } if ( fsz > 0 && ftell(fp)*100.0/fsz > n ) { fprintf(stderr, "%.1f%% tag:%d gsm:%d\r", n,tag.size,gsm.size); fflush(stderr); n += 0.1; } } fprintf(stderr, "100.0%% tag:%d gsm:%d\r", tag.size,gsm.size); fclose(fp); /*************************************/ //long *res = (long*)malloc(tag.size * gsm.size * sizeof(long)); long **res = (long **)calloc(tag.size, sizeof(long *)); for (i = 0; i < tag.size; i++) { res[i] = (long *)calloc(gsm.size, sizeof(long)); } fprintf(stderr, "boucle depilee\n"); while ( blk = depile(blk, &i, &j, &freq) ) { fprintf(stderr, "%d %d %d\n", i, j, freq); res[i][j] = freq; } fprintf(stderr, "boucle depilee terminee"); /*************************************/ FILE *fpo = stdout; fprintf(fpo, "etiquette"); for (j=0; j<gsm.size; j++) fprintf(fpo, " %s", gsm.array[j]); fprintf(fpo, "\n"); // As-tu cette ligne d'entê (ci dessus) ? for (i=0; i<tag.size; i++) { fprintf(fpo, "%s", tag.array[i]); // for (j=0; j<gsm.size; j++) // fprintf(fpo, " %d", res[i*gsm.size+j]); for (j=0; j<gsm.size; j++) fprintf(fpo, " %d", res[i][j]); fprintf(fpo, "\n"); free(res[i]); } hashtable_destroy(tag.h, 0); free(tag.array); hashtable_destroy(gsm.h, 0); free(gsm.array); free(res); }
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "hashtable.h" typedef struct { char **array; int size; int allocSize; struct hashtable *h; } ARR; static unsigned int hashKey(void* item) { unsigned int h = 0; char *p = (char *)item; while ( *p ) { h = ((h << 5) + h) + *p++; } return h; } static int compareKey(void* k1, void* k2) { return !strcmp(k1, k2); } void initArray(ARR* a, int sz, int hsz) { a->size = 0; a->allocSize = sz; a->array = (char **) malloc(sizeof(char *) * sz); a->h = create_hashtable(hsz, hashKey, compareKey); } long findOrAddArray(ARR *arr, char *item) { long * pi = (long *)hashtable_search(arr->h, item); if ( pi ) { return *pi; } long i = arr->size; int n = strlen(item); char * key = malloc(n + 1 + sizeof(long)); strcpy(key, item); pi = (long *)(&key[n + 1]); *pi = i; if ( arr->size == arr->allocSize ) { arr->allocSize *= 2; char ** p = (char **) malloc(sizeof(char *) * arr->allocSize); memcpy(p, arr->array, sizeof(char *) * arr->size); free(arr->array); arr->array = p; } arr->array[i] = key; arr->size++; hashtable_insert(arr->h, key, pi); return i; } main(int argc, char *argv[]) { ARR gsm; initArray(&gsm, 500, 1000); if ( argc != 2 ) { fprintf(stderr, "il manque le fichier à traiter."); exit(1); } long freq; long i, j; int fd; if ( !(fd = open(argv[1],O_RDONLY)) ) { fprintf(stderr, "%s: erreur d'ouverture"); exit(1); } struct stat st; stat(argv[1], &st); unsigned long fsz = st.st_size; fprintf(stderr, "size = %d\n", fsz); char *bufFile = malloc(st.st_size); if ( !bufFile ) { fprintf(stderr, "Erreur malloc bufFile\n"); exit(2); } int nr; char *pbf, *p; int n = 1; long nl = 0; long ncur = 0; for (pbf = bufFile; (nr=read(fd, pbf, 1024)) > 0; pbf += nr ) { for (p=pbf; p 0 && ncur*100.0/fsz > n ) { fprintf(stderr, "lecture %d%%\r", n); fflush(stderr); n += 1; } } *pbf = 0; if ( pbf>bufFile && pbf[-1] ) nl++; fprintf(stderr, "lecture 100.0%%\r\n"); close(fd); char **row = (char **)malloc(nl*sizeof(char *)); if ( !row ) { fprintf(stderr, "Erreur malloc row\n"); exit(2); } long *vgsm = (long *)malloc(nl*sizeof(long)); if ( !vgsm ) { fprintf(stderr, "Erreur malloc vgsm\n"); exit(2); } char **prow = row; long *pvgsm = vgsm; j = 0; for (pbf=bufFile; *pbf; ) { *pvgsm = 0; *prow = NULL; i = strlen(pbf)+1; if ( p = strtok(pbf, " ") ) { *pvgsm = findOrAddArray(&gsm, p); if ( (p=strtok(NULL, " ")) && strtok(NULL, " ") ) *prow = p; // pointe sur tag } *prow++; *pvgsm++; pbf += i; } /*************************************/ long *res = (long *)malloc(gsm.size * sizeof(long)); FILE *fpo = stdout; fprintf(fpo, "etiquette"); for (j=0; j<gsm.size; j++) fprintf(fpo, " %s", gsm.array[j]); fprintf(fpo, "\n"); char **irow; char **jrow; char *curTag; //tag en cours long *ivgsm = vgsm; for (i=0, irow = row; i<nl; i++, irow++, ivgsm++) { if ( *irow ) { memset(res, 0, gsm.size*sizeof(long)); // remise à 0 curTag = *irow; p = curTag + strlen(curTag) + 1; res[*ivgsm] = atol(p); long *jvgsm = ivgsm + 1; for (j = i+1, jrow=irow+1; j < nl; j++, jrow++, jvgsm++) { if ( (p = *jrow) && !strcmp(curTag, p) ) { p += strlen(p) + 1; res[*jvgsm] = atol(p); *jrow = (char *)0; } } fprintf(fpo, "%s", curTag); for (j=0; j<gsm.size; j++) fprintf(fpo, " %d", res[j]); fprintf(fpo, "\n"); } } /*************************************/ hashtable_destroy(gsm.h, 0); free(gsm.array); free(res); free(bufFile); exit(0); }
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "hashtable.h" typedef struct { char **array; int size; int allocSize; struct hashtable *h; } ARR; static unsigned int hashKey(void* item) { unsigned int h = 0; char *p = (char *)item; while ( *p ) { h = ((h << 5) + h) + *p++; } return h; } static int compareKey(void* k1, void* k2) { return !strcmp(k1, k2); } void initArray(ARR* a, int sz, int hsz) { a->size = 0; a->allocSize = sz; a->array = (char **) malloc(sizeof(char *) * sz); a->h = create_hashtable(hsz, hashKey, compareKey); } void sortie(char *msg, int nret) { fprintf(stderr, msg); exit(nret); } long findOrAddArray(ARR *arr, char *item) { long * pi = (long *)hashtable_search(arr->h, item); if ( pi ) return *pi; long i = arr->size; int n = strlen(item); char * key = malloc(n + 1 + sizeof(long)); strcpy(key, item); pi = (long *)(&key[n + 1]); *pi = i; if ( arr->size == arr->allocSize ) { arr->allocSize *= 2; char ** p = (char **) malloc(sizeof(char *) * arr->allocSize); memcpy(p, arr->array, sizeof(char *) * arr->size); free(arr->array); arr->array = p; } arr->array[i] = key; arr->size++; hashtable_insert(arr->h, key, pi); return i; } main(int argc, char *argv[]) { ARR gsm; initArray(&gsm, 500, 1000); if ( argc != 2 ) sortie("il manque le fichier à traiter.", 1); long i, j; int fd; if ( !(fd = open(argv[1],O_RDONLY)) ) sortie("erreur d'ouverture", 1); struct stat st; stat(argv[1], &st); unsigned long fsz = st.st_size; if ( fsz == 0 ) sortie("la taille du fichier est nulle", 1); fprintf(stderr, "size = %d\n", fsz); char *bufFile = malloc(st.st_size); if ( !bufFile ) sortie("Erreur malloc bufFile\n", 2); /*************************************/ int nr; char *pbf, *p; int n = 1; long nl = 0; long ncur = 0; for (pbf = bufFile; (nr=read(fd, pbf, 1024)) > 0; pbf += nr ) { for (p=pbf; p n*fsz ) { fprintf(stderr, "lecture %d%%\r", n); fflush(stderr); n += 1; } } *pbf = 0; if ( pbf>bufFile && pbf[-1] ) nl++; close(fd); fprintf(stderr, "lecture 100%% NL=%d\r\n", nl); if ( nl == 0 ) sortie("Fichier vide\n", 4); /*************************************/ char **row = (char **)malloc(nl*sizeof(char *)); if ( !row ) sortie("Erreur malloc row\n", 2); long *vgsm = (long *)malloc(nl*sizeof(long)); if ( !vgsm ) sortie("Erreur malloc vgsm\n", 2); char **prow = row; long *pvgsm = vgsm; pbf = bufFile; while ( *pbf ) { *pvgsm = 0; *prow = NULL; i = strlen(pbf)+1; if ( p = strtok(pbf, " ") ) { *pvgsm = findOrAddArray(&gsm, p); if ( (p=strtok(NULL, " ")) && strtok(NULL, " ") ) *prow = p; // pointe sur tag } prow++; pvgsm++; pbf += i; } if ( gsm.size == 0 ) sortie("Pas de gsm\n", 3); /*************************************/ long *res = (long *)malloc(gsm.size * sizeof(long)); if ( !res ) sortie("Erreur malloc res\n", 2); FILE *fpo = stdout; fprintf(fpo, "etiquette"); for (j=0; j<gsm.size; j++) fprintf(fpo, " %s", gsm.array[j]); fprintf(fpo, "\n"); char **irow; char *curTag; //tag en cours long *ivgsm = vgsm; char *prevTag = NULL; for (i=0, irow = row; i < nl && !(prevTag = *irow); i++, irow++) ; memset(res, 0, gsm.size*sizeof(long)); // remise à 0 n = 1; for (i=0, irow = row; i < nl; i++, irow++, ivgsm++) { if ( curTag = *irow ) { if ( strcmp(curTag, prevTag) ) { fprintf(fpo, "%s", prevTag); for (j=0; j<gsm.size; j++) fprintf(fpo, " %d", res[j]); fprintf(fpo, "\n"); // Réinitialisation prevTag = curTag; memset(res, 0, gsm.size*sizeof(long)); // remise à 0 } res[*ivgsm] = atol(curTag + strlen(curTag) + 1); } if ( i*100.0 > n*nl ) { fprintf(stderr, "écriture %d%%\r", n); fflush(stderr); n += 1; } } if ( nl > 0 ) { fprintf(fpo, "%s", curTag); for (j=0; j<gsm.size; j++) fprintf(fpo, " %d", res[j]); fprintf(fpo, "\n"); } fprintf(stderr, "écriture 100%%\r\n"); /*************************************/ hashtable_destroy(gsm.h, 0); free(gsm.array); free(res); free(bufFile); exit(0); }