amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

513 lines
13 KiB
C

#include "segment.h"
#include <string.h>
typedef struct {
unsigned label;
size_t width;
int *values;
} StRow;
struct StTable {
StAllocator allocator;
StRow *rows;
size_t count;
size_t capacity;
size_t cells;
uint64_t revision;
};
static int valid_allocator(StAllocator allocator)
{
return allocator.allocate != NULL && allocator.release != NULL;
}
static int valid_values(const int *values, size_t width)
{
size_t index;
if (width > ST_MAX_WIDTH || (width != 0 && values == NULL)) {
return 0;
}
for (index = 0; index < width; ++index) {
if (values[index] < -ST_VALUE_LIMIT || values[index] > ST_VALUE_LIMIT) {
return 0;
}
}
return 1;
}
static int valid_range(const StTable *table, size_t first, size_t count)
{
return table != NULL && first <= table->count && count <= table->count - first;
}
static void release_row(StTable *table, StRow *row)
{
if (row->values != NULL) {
table->allocator.release(table->allocator.context, row->values);
}
row->values = NULL;
row->width = 0;
row->label = 0;
}
static StResult copy_row(StTable *table, unsigned label, const int *values,
size_t width, StRow *output)
{
int *copy = NULL;
if (!valid_values(values, width)) {
return ST_INVALID;
}
if (width != 0) {
copy = table->allocator.allocate(table->allocator.context, width * sizeof(*copy));
if (copy == NULL) {
return ST_NOMEM;
}
memcpy(copy, values, width * sizeof(*copy));
}
output->label = label;
output->width = width;
output->values = copy;
return ST_OK;
}
static StResult reserve_rows(StTable *table, size_t required)
{
size_t capacity;
size_t index;
StRow *replacement;
if (required <= table->capacity) {
return ST_OK;
}
capacity = table->capacity == 0 ? 4 : table->capacity * 2;
if (capacity < required) {
capacity = required;
}
if (capacity > ST_MAX_ROWS) {
capacity = ST_MAX_ROWS;
}
replacement = table->allocator.allocate(table->allocator.context,
capacity * sizeof(*replacement));
if (replacement == NULL) {
return ST_NOMEM;
}
for (index = 0; index < table->count; ++index) {
replacement[index] = table->rows[index];
}
for (; index < capacity; ++index) {
replacement[index].label = 0;
replacement[index].width = 0;
replacement[index].values = NULL;
}
if (table->rows != NULL) {
table->allocator.release(table->allocator.context, table->rows);
}
table->rows = replacement;
table->capacity = capacity;
return ST_OK;
}
StResult st_create(StAllocator allocator, StTable **output)
{
StTable *table;
if (!valid_allocator(allocator) || output == NULL || *output != NULL) {
return ST_INVALID;
}
table = allocator.allocate(allocator.context, sizeof(*table));
if (table == NULL) {
return ST_NOMEM;
}
table->allocator = allocator;
table->rows = NULL;
table->count = 0;
table->capacity = 0;
table->cells = 0;
table->revision = 0;
*output = table;
return ST_OK;
}
void st_destroy(StTable **pointer)
{
StTable *table;
size_t index;
StAllocator allocator;
if (pointer == NULL || *pointer == NULL) {
return;
}
table = *pointer;
allocator = table->allocator;
for (index = 0; index < table->count; ++index) {
release_row(table, &table->rows[index]);
}
if (table->rows != NULL) {
allocator.release(allocator.context, table->rows);
}
allocator.release(allocator.context, table);
*pointer = NULL;
}
StResult st_clear(StTable *table)
{
size_t index;
if (table == NULL) {
return ST_INVALID;
}
for (index = 0; index < table->count; ++index) {
release_row(table, &table->rows[index]);
}
table->count = 0;
table->cells = 0;
++table->revision;
return ST_OK;
}
StResult st_append(StTable *table, unsigned label, const int *values, size_t width)
{
StRow row;
StResult result;
if (table == NULL || !valid_values(values, width)) {
return ST_INVALID;
}
if (table->count == ST_MAX_ROWS) {
return ST_RANGE;
}
result = copy_row(table, label, values, width, &row);
if (result != ST_OK) {
return result;
}
result = reserve_rows(table, table->count + 1);
if (result != ST_OK) {
release_row(table, &row);
return result;
}
table->rows[table->count] = row;
++table->count;
table->cells += width;
++table->revision;
return ST_OK;
}
StResult st_replace(StTable *table, size_t index, unsigned label,
const int *values, size_t width)
{
StRow replacement;
StResult result;
if (table == NULL || !valid_values(values, width)) {
return ST_INVALID;
}
if (index >= table->count) {
return ST_RANGE;
}
result = copy_row(table, label, values, width, &replacement);
if (result != ST_OK) {
return result;
}
table->cells -= table->rows[index].width;
release_row(table, &table->rows[index]);
table->rows[index] = replacement;
table->cells += width;
++table->revision;
return ST_OK;
}
StResult st_erase(StTable *table, size_t first, size_t count)
{
size_t index;
if (table == NULL) {
return ST_INVALID;
}
if (!valid_range(table, first, count)) {
return ST_RANGE;
}
for (index = first; index < first + count; ++index) {
table->cells -= table->rows[index].width;
release_row(table, &table->rows[index]);
}
for (index = first + count; index < table->count; ++index) {
table->rows[index - count] = table->rows[index];
}
table->count -= count;
for (index = table->count; index < table->count + count; ++index) {
table->rows[index].label = 0;
table->rows[index].width = 0;
table->rows[index].values = NULL;
}
++table->revision;
return ST_OK;
}
StResult st_get(const StTable *table, size_t row, size_t column, int *output)
{
if (table == NULL || output == NULL) {
return ST_INVALID;
}
if (row >= table->count || column >= table->rows[row].width) {
return ST_RANGE;
}
*output = table->rows[row].values[column];
return ST_OK;
}
StResult st_set(StTable *table, size_t row, size_t column, int value)
{
if (table == NULL || value < -ST_VALUE_LIMIT || value > ST_VALUE_LIMIT) {
return ST_INVALID;
}
if (row >= table->count || column >= table->rows[row].width) {
return ST_RANGE;
}
table->rows[row].values[column] = value;
++table->revision;
return ST_OK;
}
StResult st_shape(const StTable *table, size_t row, unsigned *label, size_t *width)
{
if (table == NULL || label == NULL || width == NULL) {
return ST_INVALID;
}
if (row >= table->count) {
return ST_RANGE;
}
*label = table->rows[row].label;
*width = table->rows[row].width;
return ST_OK;
}
static void stats_reset(StStats *stats)
{
stats->cells = 0;
stats->sum = 0;
stats->minimum = 0;
stats->maximum = 0;
}
static void stats_push(StStats *stats, int value)
{
if (stats->cells == 0) {
stats->minimum = value;
stats->maximum = value;
} else {
if (value < stats->minimum) {
stats->minimum = value;
}
if (value > stats->maximum) {
stats->maximum = value;
}
}
++stats->cells;
stats->sum += value;
}
StResult st_stats(const StTable *table, size_t first, size_t count, StStats *output)
{
StStats stats;
size_t row;
size_t column;
if (table == NULL || output == NULL) {
return ST_INVALID;
}
if (!valid_range(table, first, count)) {
return ST_RANGE;
}
stats_reset(&stats);
for (row = first; row < first + count; ++row) {
for (column = 0; column < table->rows[row].width; ++column) {
stats_push(&stats, table->rows[row].values[column]);
}
}
*output = stats;
return ST_OK;
}
StResult st_select(const StTable *table, size_t first, size_t count,
int64_t threshold, size_t *output)
{
size_t index;
size_t best = 0;
int found = 0;
int64_t best_sum = 0;
if (table == NULL || output == NULL) {
return ST_INVALID;
}
if (!valid_range(table, first, count)) {
return ST_RANGE;
}
for (index = first; index < first + count; ++index) {
StStats stats;
st_stats(table, index, 1, &stats);
if (stats.sum >= threshold && (!found || stats.sum >= best_sum)) {
found = 1;
best = index;
best_sum = stats.sum;
}
}
if (!found) {
return ST_EMPTY;
}
*output = best;
return ST_OK;
}
StResult st_rebuild(const StTable *table, size_t first, size_t count,
int reverse, StTable **output)
{
StTable *built = NULL;
StResult result;
size_t offset;
if (table == NULL || output == NULL || *output != NULL || (reverse != 0 && reverse != 1)) {
return ST_INVALID;
}
if (!valid_range(table, first, count)) {
return ST_RANGE;
}
result = st_create(table->allocator, &built);
if (result != ST_OK) {
return result;
}
result = reserve_rows(built, count);
if (result != ST_OK) {
st_destroy(&built);
return result;
}
for (offset = 0; offset < count; ++offset) {
size_t source = reverse ? first + count - 1 - offset : first + offset;
StRow *row = &table->rows[source];
result = copy_row(built, row->label, row->values, row->width,
&built->rows[offset]);
if (result != ST_OK) {
st_destroy(&built);
return result;
}
++built->count;
built->cells += row->width;
}
built->revision = 0;
*output = built;
return ST_OK;
}
StResult st_flatten(const StTable *table, int *values, size_t capacity, size_t *needed)
{
size_t row;
size_t column;
size_t offset = 0;
if (table == NULL || needed == NULL || (capacity != 0 && values == NULL)) {
return ST_INVALID;
}
*needed = table->cells;
if (capacity < table->cells) {
return ST_RANGE;
}
for (row = 0; row < table->count; ++row) {
for (column = 0; column < table->rows[row].width; ++column) {
values[offset++] = table->rows[row].values[column];
}
}
return ST_OK;
}
StResult st_cursor_begin(const StTable *table, StCursor *cursor)
{
if (table == NULL || cursor == NULL) {
return ST_INVALID;
}
cursor->row = 0;
cursor->column = 0;
cursor->revision = table->revision;
return ST_OK;
}
StResult st_cursor_next(const StTable *table, StCursor *cursor,
size_t *row, size_t *column, int *value)
{
if (table == NULL || cursor == NULL || row == NULL || column == NULL || value == NULL) {
return ST_INVALID;
}
if (cursor->revision != table->revision) {
return ST_INVALID;
}
while (cursor->row < table->count) {
if (cursor->column < table->rows[cursor->row].width) {
*row = cursor->row;
*column = cursor->column;
*value = table->rows[cursor->row].values[cursor->column];
++cursor->column;
return ST_OK;
}
++cursor->row;
cursor->column = 0;
}
return ST_EMPTY;
}
StResult st_equal(const StTable *left, const StTable *right, int *output)
{
size_t row;
size_t column;
int equal = 1;
if (left == NULL || right == NULL || output == NULL) {
return ST_INVALID;
}
if (left->count != right->count || left->cells != right->cells) {
*output = 0;
return ST_OK;
}
for (row = 0; row < left->count && equal; ++row) {
const StRow *a = &left->rows[row];
const StRow *b = &right->rows[row];
if (a->label != b->label || a->width != b->width) {
equal = 0;
break;
}
for (column = 0; column < a->width; ++column) {
if (a->values[column] != b->values[column]) {
equal = 0;
break;
}
}
}
*output = equal;
return ST_OK;
}
StResult st_check(const StTable *table)
{
size_t row;
size_t cells = 0;
if (table == NULL || !valid_allocator(table->allocator)) {
return ST_INVALID;
}
if (table->count > table->capacity || table->capacity > ST_MAX_ROWS) {
return ST_INVALID;
}
if (table->capacity != 0 && table->rows == NULL) {
return ST_INVALID;
}
for (row = 0; row < table->count; ++row) {
if (!valid_values(table->rows[row].values, table->rows[row].width)) {
return ST_INVALID;
}
cells += table->rows[row].width;
}
if (cells != table->cells) {
return ST_INVALID;
}
return ST_OK;
}
size_t st_rows(const StTable *table)
{
return table == NULL ? 0 : table->count;
}
size_t st_cells(const StTable *table)
{
return table == NULL ? 0 : table->cells;
}
uint64_t st_revision(const StTable *table)
{
return table == NULL ? 0 : table->revision;
}