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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! Host-side prefix array builder.
//!
//! NOTE: This is NOT part of the vyre IR. It runs on the CPU once before a
//! GPU dispatch needs the prefix arrays as input buffers. It does not produce
//! a Program, does not go through validate or lower, and is not registered in
//! the op registry. Callers compose it manually with their dispatch flow.
//!
//! The IR-side equivalents are in `vyre_spec::string::prefix_brace` (and
//! related ops in the `string` and `match_ops` domains) -- those are real
//! Category A compositions that lower to GPU code.
use crate;
/// Maximum input bytes accepted by CPU prefix builders.
///
/// I10: prefix builders allocate one `u32` per input byte, plus one sentinel
/// for newline prefix sums. The 64 MiB bound keeps each prefix array bounded
/// before `Vec::with_capacity` and structurally prevents `bytes.len() + 1`
/// overflow in newline prefix construction. Prevents OOM on the host.
pub const MAX_PREFIX_INPUT_BYTES: usize = 64 * 1024 * 1024;
/// Compute brace `{`/`}` depth at each byte offset.
///
/// `result[i]` = nesting depth BEFORE byte `i`.
/// Already existed as `compute_file_brace_depths` — moved here for reuse.
///
/// # Errors
///
/// Returns `Error::Prefix` if the input exceeds `MAX_PREFIX_INPUT_BYTES`.
///
/// # Examples
///
/// ```
/// use vyre_foundation::engine::prefix::brace_depth_prefix;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let input = b"{a{b}c}";
/// let depths = brace_depth_prefix(input)?;
/// assert_eq!(depths, vec![0, 1, 1, 2, 2, 1, 1]);
/// # Ok(())
/// # }
/// ```
/// Compute combined brace `{`/`}` and paren `(`/`)` nesting depth at each byte offset.
///
/// `result[i]` = combined nesting depth BEFORE byte `i`.
/// Replaces the O(n) `nested_depth()` shader function.
///
/// # Errors
///
/// Returns `Error::Prefix` if the input exceeds `MAX_PREFIX_INPUT_BYTES`.
///
/// # Examples
///
/// ```
/// use vyre_foundation::engine::prefix::nested_depth_prefix;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let input = b"f({x})";
/// let depths = nested_depth_prefix(input)?;
/// assert_eq!(depths, vec![0, 0, 1, 2, 2, 1]);
/// # Ok(())
/// # }
/// ```
/// Compute newline prefix sum for O(1) line-distance queries.
///
/// `result[i]` = number of `\n` bytes in `bytes[0..i]`.
/// `newline_count_between(a, b) = result[b] - result[a]` — O(1).
///
/// # Errors
///
/// Returns `Error::Prefix` if the input exceeds `MAX_PREFIX_INPUT_BYTES`
/// or if the prefix capacity overflows `usize`.
///
/// # Examples
///
/// ```
/// use vyre_foundation::engine::prefix::newline_prefix_sum;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let input = b"a\nb\nc";
/// let sums = newline_prefix_sum(input)?;
/// assert_eq!(sums, vec![0, 0, 1, 1, 2, 2]);
/// # Ok(())
/// # }
/// ```
/// `validate_prefix_input` function.