File size: 2,047 Bytes
1ddfac5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * dense.h — the dense body simulator: symbols x symbols x time.
 *
 * Nothing is materialized.  The cell count, the linear index, and the
 * decode are pure arithmetic; the structural mark (cut) is computed by
 * the mask callback on demand.  The body is dense in names and sparse
 * in memory: a search spends only the tree it actually walks.
 *
 * A cell is (a, b, t): symbol a at time t, symbol b at time t+1.  Every
 * cell is hard-connected to every other cell; no edge is stored, and a
 * DO names a cell by its linear index and jumps there in O(1).
 */
#ifndef DENSE_H
#define DENSE_H

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    uint32_t symbols;   /* X: the alphabet size */
    uint64_t steps;     /* T: the time slices */
    uint64_t cells;     /* X * X * T */
    /* The causal/domain mask: return non-zero to cut the cell.  It is
       evaluated on demand; no cut is ever stored. */
    int (*cut)(void *ctx, uint32_t a, uint32_t b, uint64_t t);
    void *ctx;
} dense;

/* Define the body.  No allocation: `cells` is computed, nothing is
 * stored.  `cut` may be NULL (nothing is cut).  Returns 0 or -1. */
int dense_init(dense *d, uint32_t symbols, uint64_t steps,
               int (*cut)(void *ctx, uint32_t a, uint32_t b, uint64_t t),
               void *ctx);
void dense_free(dense *d);

uint64_t dense_cells(const dense *d);

/* The O(1) jump: (a, b, t) -> linear cell, or UINT64_MAX if out of
 * range.  Any cell to any cell is one DO, so the target needs no edge. */
uint64_t dense_index(const dense *d, uint32_t a, uint32_t b, uint64_t t);

/* Decode a linear cell back to (a, b, t).  Any out pointer may be NULL.
 * Returns 0 or -1. */
int dense_cell(const dense *d, uint64_t cell, uint32_t *a, uint32_t *b,
               uint64_t *t);

/* Evaluate the mask for a cell: 1 = cut, 0 = open.  Nothing is written;
 * the whole body is computable at any time. */
int dense_cut(const dense *d, uint64_t cell);

#ifdef __cplusplus
}
#endif
#endif /* DENSE_H */