From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27, 11 canonical families), 38 skipped for scope or duplication. Each task has at least one independent contract-edge defect, boundary types rotate, and public docs state trap clauses as plain contract prose. Claude reviewed 63/63 USABLE against current digests. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6.9 KiB
6.9 KiB
Reduction interface
Values and ownership
reduction.hdefinesRP_MAX_ITEMS=4096,RP_MAX_WORKERS=64,RP_OK=0,RP_INVALID=1, andRP_OVERFLOW=2. Arithmetic uses mathematical signed integers. Anint64_toutput must be representable from -9223372036854775808 through 9223372036854775807. A transient sum outside this range does not itself cause failure if the completed sum is representable.- Each
rp_spancontainssize_t begin,end; eachrp_chunkcontainssize_t countandint64_t sum,minimum,maximum. An empty chunk has all four fields zero. A nonempty chunk records its item count, sum, smallest value and largest value. - For the arithmetic APIs in clauses 5–10, every argument marked
constis unchanged over its complete object or supplied byte span, including unused array elements. Output objects may start with arbitrary values. A failed library call leaves its complete output object unchanged. Successful calls replace the documented fields; no output is a view into input storage. Calls have no persistent state and separate objects remain independent when interleaved or reused. Input and output storage do not overlap. - In clauses 5–10, a null required pointer returns
RP_INVALIDwithout dereferencing it. Counts and capacities refer to actual caller-provided arrays, and callers provide storage for the declared count or capacity. There is no artificial limit on repeated calls or console requests.
Library calls
int rp_token_i64(const char *text, size_t length, int64_t *out)reads exactlylengthbytes, without requiring a trailing NUL. Its accepted grammar is an optional ASCII+or-followed by one or more ASCII digits. Leading zeros and signed zero are allowed. Whitespace, embedded NUL, other bytes, missing digits, or an unrepresentable value returnRP_INVALID; success writes the value and returnsRP_OK. Both pointers are required.int rp_span_for(size_t count, size_t workers, size_t index, struct rp_span *out)requirescount<=4096,1<=workers<=64,index<workers, and a nonnull output. Invalid arguments returnRP_INVALID. On success workers partition indexes[0,count)in order. Withq=count/workersandr=count%workers, the firstrworkers receiveq+1items and the remaining workers receiveqitems. The result contains the begin index and the end index after the worker's final item. Empty spans are allowed. ReturnRP_OK.int rp_plan(size_t count, size_t workers, struct rp_span *spans, size_t capacity)returns all spans from clause 6. The output pointer is required andcapacity>=workers; other size rules are those of clause 6. It returnsRP_INVALIDon invalid arguments, otherwiseRP_OK. On success it writes the firstworkerselements and leaves every later element throughcapacityunchanged. On failure it writes no elements.int rp_partial(const int64_t *values, size_t count, struct rp_span span, struct rp_chunk *out)requirescount<=4096and0<=span.begin<=span.end<=count. The output is required;valuesmay be null only when count is zero. It summarizes values in[span.begin,span.end)according to clause 2. Other values are ignored. Invalid arguments returnRP_INVALID. A completed sum outside int64 returnsRP_OVERFLOW; otherwise it writes the chunk and returnsRP_OK.int rp_merge(const struct rp_chunk *chunks, size_t workers, struct rp_chunk *out)requires both pointers and1<=workers<=64. Every chunk has count at most 4096; the combined count is at most 4096. Empty chunks have four zero fields. A nonempty chunk hasminimum<=maximumandcount*minimum<=sum<=count*maximum, with those comparisons performed in mathematical integers. Violation of any of these rules returnsRP_INVALIDbefore sum overflow is considered. The merged count and sum are the sums of those fields, and extrema come from nonempty chunks. A combined sum outside int64 returnsRP_OVERFLOW; success writes the merged chunk and returnsRP_OK.int rp_run(const struct rp_request *request, struct rp_result *out)requires both pointers. A request containssize_t count,workers,int64_t lower,upper, andint64_t values[4096]; it requirescount<=4096,1<=workers<=64, andlower<upper. Each of the first count values satisfieslower<=value<upper. Unused value elements do not affect computation. Validate the whole request before computing any worker result. Any invalid request returnsRP_INVALID, including an invalid value after an earlier group that would overflow. Then compute clause 6 spans, clause 8 chunks and clause 9 total. A worker sum or merged sum outside int64 returnsRP_OVERFLOW. On success returnRP_OK, write result.workers, total, and chunks in worker order, and set all inactive chunk fields through index 63 to zero.
Console
int rp_console(FILE *input, FILE *output)reads requests until EOF without closing either stream. A null stream returns 2. Every LF-terminated input line is one request; a nonempty final line without LF is also a request. Empty stdin yields zero stdout bytes. There is no fixed line or token length limit apart from available memory. NUL is an input byte, not an end marker. Space, tab and CR are the only separators within a line.- Each line contains decimal integers
count workers lower upper value0 .... Integer tokens follow clause 5. Exactly count values must follow the four header tokens. Empty lines, missing or extra tokens, malformed tokens, negative count/workers, and values or header fields outside their declared ranges produceRP_INVALID. The reduction then follows clause 10, including zero count and workers exceeding count. - Emit one JSON object per request in the same order. A rejected request has integer
status1 or 2. A successful object has integerstatus:0, integerworkers, objecttotal, and arraychunksof exactly workers objects. Each chunk object has integercount,sum,minimum,maximum. These fields equal the library result. Additional object members and JSON whitespace are allowed. Consecutive objects are separated by JSON whitespace; NaN and Infinity are not JSON values. The output stream consists only of these JSON values and whitespace and is encoded in UTF-8. JSON booleans are not integers. - Input errors and arithmetic overflow are per-request results; continue processing later lines and return 0 on normal EOF. Return 2 for a stream read/write failure or an allocation failure; already emitted results may remain. Stderr is diagnostic text with no required content or encoding. For the executable, normal request processing returns 0; it uses stdin/stdout with the same contract.
Delivery
makebuilds the complete program from the declared files with the provided C11/POSIX Linux toolchain and no downloads.make smokerunssmoke.txt. The sample's three completed totals are respectively 16, 0, and 6. Those examples do not replace any part of this contract. All behavior in this document is required. Only the files listed in README.md are collected.