laidout 0.3.0

Standalone prepared-document layout kernel with reusable allocation-controlled workspaces
Documentation
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Allocating compatibility engines for arbitrary cost models.
//!
//! This module is separate from the compact prepared consumer
//! kernel. It favors a small, inspectable oracle implementation over bounded
//! storage and is available only with the `research` feature.

use std::ops::Range;
use std::rc::Rc;

pub use crate::cost::research::{
    CostModel, LawfulCostModel, OverflowThenHeight, ResearchConsumerCost, ResearchConsumerCostModel,
};
pub use crate::doc::count_choices;
use crate::doc::{Doc, Node, TagId, TextRun};

#[derive(Clone, Debug)]
/// Persistent output tree used by allocating research engines.
pub enum Out {
    /// Empty output.
    Empty,
    /// Measured text.
    Text(TextRun),
    /// A newline followed by indentation.
    Newline {
        /// Indentation columns emitted after the newline.
        indent: u32,
    },
    /// Concatenated output.
    Cat(Rc<Out>, Rc<Out>),
    /// Tagged child output.
    Tagged {
        /// Numeric tag applied to the child output.
        tag: TagId,
        /// Tagged output.
        child: Rc<Out>,
    },
    /// Penalized child output.
    Penalty {
        /// Additional consumer cost.
        amount: u32,
        /// Penalized output.
        child: Rc<Out>,
    },
}

/// Constructs empty research output.
pub fn out_empty() -> Rc<Out> {
    Rc::new(Out::Empty)
}

/// Constructs research text output.
pub fn out_text(text: TextRun) -> Rc<Out> {
    Rc::new(Out::Text(text))
}

/// Constructs a research newline.
pub fn out_newline(indent: u32) -> Rc<Out> {
    Rc::new(Out::Newline { indent })
}

/// Concatenates two research outputs, eliminating empty identities.
pub fn out_cat(left: Rc<Out>, right: Rc<Out>) -> Rc<Out> {
    match (&*left, &*right) {
        (Out::Empty, _) => right,
        (_, Out::Empty) => left,
        _ => Rc::new(Out::Cat(left, right)),
    }
}

/// Wraps research output in a numeric tag.
pub fn out_tagged(tag: TagId, child: Rc<Out>) -> Rc<Out> {
    Rc::new(Out::Tagged { tag, child })
}

/// Wraps research output in a penalty.
pub fn out_penalty(amount: u32, child: Rc<Out>) -> Rc<Out> {
    Rc::new(Out::Penalty { amount, child })
}

#[derive(Clone, Debug, Eq, PartialEq)]
/// Materialized numeric-tag span produced by a research engine.
pub struct AnnotationSpan {
    /// Numeric tag.
    pub tag: TagId,
    /// UTF-8 byte range.
    pub range: Range<usize>,
    /// Parent span index, when nested.
    pub parent: Option<usize>,
}

#[derive(Clone, Debug)]
/// Materialized research output and its cost.
pub struct Rendered<C> {
    /// Rendered UTF-8 text.
    pub text: String,
    /// Structural tag spans.
    pub spans: Vec<AnnotationSpan>,
    /// Model-specific cost.
    pub cost: C,
}

#[derive(Clone)]
struct State<C> {
    cost: C,
    column: u32,
    out: Rc<Out>,
}

fn append<M: CostModel>(model: &M, left: State<M::Cost>, right: State<M::Cost>) -> State<M::Cost> {
    State {
        cost: model.add(&left.cost, &right.cost),
        column: right.column,
        out: out_cat(left.out, right.out),
    }
}

fn apply<M: CostModel>(
    model: &M,
    doc: &Doc<TagId>,
    state: State<M::Cost>,
    indent: u32,
) -> Vec<State<M::Cost>> {
    match doc.root().as_ref() {
        Node::Empty => vec![state],
        Node::Text(text) => {
            let end = state
                .column
                .checked_add(text.columns())
                .expect("research layout column overflow");
            let piece = State {
                cost: model.text(state.column, text.columns()),
                column: end,
                out: out_text(text.clone()),
            };
            vec![append(model, state, piece)]
        }
        Node::Break(_) | Node::HardLine => {
            let newline = State {
                cost: model.add(&model.newline(), &model.text(0, indent)),
                column: indent,
                out: out_newline(indent),
            };
            vec![append(model, state, newline)]
        }
        Node::Seq(children) => apply_sequence(model, children, vec![state], indent, false),
        Node::Group(child) => {
            let mut layouts = Vec::new();
            if let Some(flat) = child.flatten() {
                layouts.extend(apply(model, &flat, state.clone(), indent));
            }
            layouts.extend(apply(model, child, state, indent));
            layouts
        }
        Node::Fill(children) => apply_sequence(model, children, vec![state], indent, true),
        Node::Nest {
            indent: amount,
            child,
        } => apply(
            model,
            child,
            state,
            indent
                .checked_add(*amount)
                .expect("research indentation overflow"),
        ),
        Node::Align(child) => {
            let aligned = state.column;
            apply(model, child, state, aligned)
        }
        Node::Choice {
            preferred,
            alternative,
        } => {
            let mut layouts = apply(model, preferred, state.clone(), indent);
            layouts.extend(apply(model, alternative, state, indent));
            layouts
        }
        Node::Annotate { annotation, child } => {
            let local = State {
                cost: model.zero(),
                column: state.column,
                out: out_empty(),
            };
            apply(model, child, local, indent)
                .into_iter()
                .map(|mut child| {
                    child.out = out_tagged(**annotation, child.out);
                    append(model, state.clone(), child)
                })
                .collect()
        }
        Node::Penalty { amount, child } => {
            let local = State {
                cost: model.penalty(*amount),
                column: state.column,
                out: out_penalty(*amount, out_empty()),
            };
            apply(model, child, local, indent)
                .into_iter()
                .map(|child| append(model, state.clone(), child))
                .collect()
        }
    }
}

fn apply_sequence<M: CostModel>(
    model: &M,
    children: &[Doc<TagId>],
    mut states: Vec<State<M::Cost>>,
    indent: u32,
    fill: bool,
) -> Vec<State<M::Cost>> {
    for (index, child) in children.iter().enumerate() {
        if fill && index != 0 {
            let mut with_boundary = Vec::with_capacity(states.len().saturating_mul(2));
            for state in states {
                let space_end = state
                    .column
                    .checked_add(1)
                    .expect("research fill column overflow");
                with_boundary.push(append(
                    model,
                    state.clone(),
                    State {
                        cost: model.text(state.column, 1),
                        column: space_end,
                        out: out_text(TextRun::trusted_ascii(" ")),
                    },
                ));
                with_boundary.push(append(
                    model,
                    state,
                    State {
                        cost: model.add(&model.newline(), &model.text(0, indent)),
                        column: indent,
                        out: out_newline(indent),
                    },
                ));
            }
            states = with_boundary;
        }
        let mut next = Vec::new();
        for state in states {
            next.extend(apply(model, child, state, indent));
        }
        states = next;
    }
    states
}

fn all_layouts<M: CostModel>(model: &M, doc: &Doc<TagId>) -> Vec<State<M::Cost>> {
    apply(
        model,
        doc,
        State {
            cost: model.zero(),
            column: 0,
            out: out_empty(),
        },
        0,
    )
}

fn materialize<C>(cost: C, out: Rc<Out>) -> Rendered<C> {
    enum Work {
        Node(Rc<Out>),
        ExitTag(usize),
    }

    let mut text = String::new();
    let mut spans = Vec::<AnnotationSpan>::new();
    let mut open = Vec::<usize>::new();
    let mut work = vec![Work::Node(out)];
    while let Some(item) = work.pop() {
        match item {
            Work::Node(out) => match &*out {
                Out::Empty => {}
                Out::Text(run) => text.push_str(run.value()),
                Out::Newline { indent } => {
                    text.push('\n');
                    for _ in 0..*indent {
                        text.push(' ');
                    }
                }
                Out::Cat(left, right) => {
                    work.push(Work::Node(right.clone()));
                    work.push(Work::Node(left.clone()));
                }
                Out::Tagged { tag, child } => {
                    let span = spans.len();
                    spans.push(AnnotationSpan {
                        tag: *tag,
                        range: text.len()..text.len(),
                        parent: open.last().copied(),
                    });
                    open.push(span);
                    work.push(Work::ExitTag(span));
                    work.push(Work::Node(child.clone()));
                }
                Out::Penalty { child, .. } => work.push(Work::Node(child.clone())),
            },
            Work::ExitTag(span) => {
                assert_eq!(open.pop(), Some(span));
                spans[span].range.end = text.len();
            }
        }
    }
    Rendered { text, spans, cost }
}

/// Recomputes the model cost of a research output tree.
pub fn cost_of_out<M: CostModel>(model: &M, out: &Rc<Out>) -> M::Cost {
    let mut cost = model.zero();
    let mut column = 0u32;
    let mut work = vec![out.clone()];
    while let Some(out) = work.pop() {
        match &*out {
            Out::Empty => {}
            Out::Text(text) => {
                cost = model.add(&cost, &model.text(column, text.columns()));
                column = column
                    .checked_add(text.columns())
                    .expect("research output column overflow");
            }
            Out::Newline { indent } => {
                cost = model.add(&cost, &model.newline());
                cost = model.add(&cost, &model.text(0, *indent));
                column = *indent;
            }
            Out::Cat(left, right) => {
                work.push(right.clone());
                work.push(left.clone());
            }
            Out::Tagged { child, .. } => work.push(child.clone()),
            Out::Penalty { amount, child } => {
                cost = model.add(&cost, &model.penalty(*amount));
                work.push(child.clone());
            }
        }
    }
    cost
}

/// Materializes a research output tree as text.
pub fn to_string(out: &Rc<Out>) -> String {
    materialize((), out.clone()).text
}

/// Materializes and splits a research output tree into lines.
pub fn to_lines(out: &Rc<Out>) -> Vec<String> {
    to_string(out).split('\n').map(str::to_owned).collect()
}

/// Exhaustive allocating oracle.
pub mod brute {
    use super::*;

    #[derive(Clone, Debug)]
    /// Best exhaustive rendering and its structural witness.
    pub struct Rendering<C> {
        /// Model-specific cost.
        pub cost: C,
        /// Persistent output witness.
        pub out: Rc<Out>,
        /// Materialized tag spans.
        pub spans: Vec<AnnotationSpan>,
        /// Materialized output lines.
        pub lines: Vec<String>,
    }

    impl<C> Rendering<C> {
        /// Joins output lines with newline characters.
        pub fn text(&self) -> String {
            self.lines.join("\n")
        }
    }

    /// Enumerates every layout and returns the minimum-cost rendering.
    pub fn best<M: CostModel>(
        model: &M,
        doc: &Doc<TagId>,
        max_choices: usize,
    ) -> Rendering<M::Cost> {
        let choices = crate::doc::count_choices(doc);
        assert!(
            choices <= max_choices,
            "brute-force oracle refused: {choices} choices exceeds cap {max_choices}"
        );
        let state = all_layouts(model, doc)
            .into_iter()
            .min_by(|left, right| left.cost.cmp(&right.cost))
            .expect("a document always has one layout");
        let out = state.out;
        let rendered = materialize(state.cost.clone(), out.clone());
        Rendering {
            cost: state.cost,
            spans: rendered.spans,
            lines: rendered.text.split('\n').map(str::to_owned).collect(),
            out,
        }
    }
}

/// First-fitting allocating compatibility engine.
pub mod greedy {
    use super::*;

    #[derive(Clone, Debug)]
    /// Greedy rendering and its structural witness.
    pub struct GreedyResult<C> {
        /// Model-specific cost.
        pub cost: C,
        /// Persistent output witness.
        pub out: Rc<Out>,
        /// Materialized tag spans.
        pub spans: Vec<AnnotationSpan>,
        /// Materialized output lines.
        pub lines: Vec<String>,
    }

    fn fits_width(out: &Rc<Out>, width: u32) -> bool {
        let mut column = 0u32;
        let mut work = vec![out.clone()];
        while let Some(out) = work.pop() {
            match &*out {
                Out::Empty | Out::Penalty { .. } => {
                    if let Out::Penalty { child, .. } = &*out {
                        work.push(child.clone());
                    }
                }
                Out::Text(text) => {
                    column = match column.checked_add(text.columns()) {
                        Some(column) if column <= width => column,
                        _ => return false,
                    };
                }
                Out::Newline { indent } => column = *indent,
                Out::Cat(left, right) => {
                    work.push(right.clone());
                    work.push(left.clone());
                }
                Out::Tagged { child, .. } => work.push(child.clone()),
            }
        }
        true
    }

    /// Selects the first enumerated layout that fits `width`.
    pub fn layout<M: CostModel>(model: &M, doc: &Doc<TagId>, width: u32) -> GreedyResult<M::Cost> {
        let layouts = all_layouts(model, doc);
        let state = layouts
            .iter()
            .find(|state| fits_width(&state.out, width))
            .cloned()
            .unwrap_or_else(|| layouts[0].clone());
        let out = state.out;
        let rendered = materialize(state.cost.clone(), out.clone());
        GreedyResult {
            cost: state.cost,
            spans: rendered.spans,
            lines: rendered.text.split('\n').map(str::to_owned).collect(),
            out,
        }
    }
}

/// Sealed lawful-model frontier compatibility engine.
pub mod frontier {
    use super::*;

    #[derive(Clone, Debug)]
    /// Winning frontier candidate.
    pub struct Cand<C> {
        /// Model-specific cost.
        pub cost: C,
        /// Final display column.
        pub last: u32,
        /// Persistent output witness.
        pub out: Rc<Out>,
    }

    /// Returns the minimum-cost lawful candidate.
    pub fn best<M: LawfulCostModel>(model: &M, doc: &Doc<TagId>) -> Cand<M::Cost> {
        let state = all_layouts(model, doc)
            .into_iter()
            .min_by(|left, right| left.cost.cmp(&right.cost))
            .expect("a document always has one layout");
        Cand {
            cost: state.cost,
            last: state.column,
            out: state.out,
        }
    }
}