Paragraph: threaded Kurikaeshi Moji Shori leads come from one pass over the frames

A thread of frames that hold only repeat marks or soft returns made each
frame look back through every frame before it, so opening or flowing such
a story took quadratic time, with the setting off as well. The leads are
now carried forward in one pass.
This commit is contained in:
dotCooCoo
2026-11-15 09:13:16 -08:00
parent c08b4d932f
commit b6463e53cc
2 changed files with 41 additions and 16 deletions
+23 -16
View File
@@ -404,23 +404,22 @@ pub(crate) fn mark_continued(doc: &mut Document) {
/// [`TextObject::first_para_lead`] for each frame of a thread, from the frames' plain `texts` and
/// whether each one's first paragraph `continues` from the frame before: the last character of
/// that paragraph in the frames before that a repeat mark could stand for, or None.
/// that paragraph in the frames before that a repeat mark could stand for, or None. One pass over
/// the frames: each frame's lead is carried forward to the next.
fn first_para_leads(texts: &[String], continues: &[bool]) -> Vec<Option<char>> {
(0..texts.len())
.map(|k| {
let mut j = k;
while continues.get(j) == Some(&true) && j > 0 {
j -= 1;
let text = texts.get(j)?;
let tail = text.rfind('\n').map_or(Some(text.as_str()), |at| text.get(at + 1..))?;
if let Some(c) = vectorcraft_text::repeat_base(tail) {
return Some(c);
}
if tail.len() != text.len() {
return None;
}
}
None
// The lead the next frame takes when its first paragraph continues from this one.
let mut carry = None;
texts
.iter()
.enumerate()
.map(|(k, text)| {
#[cfg(test)]
LEAD_SCANS.with(|c| c.set(c.get() + 1));
let lead = carry.filter(|_| continues.get(k) == Some(&true));
let tail = text.rsplit('\n').next().unwrap_or(text);
// A paragraph break in this frame ends the paragraph the lead came from.
carry = vectorcraft_text::repeat_base(tail).or(lead.filter(|_| tail.len() == text.len()));
lead
})
.collect()
}
@@ -432,6 +431,8 @@ thread_local! {
static THREAD_SCANS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
/// Tests: how many times [`story_in`] went through a thread's frames on this thread.
static STORY_BUILDS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
/// Tests: how many frame texts [`first_para_leads`] went through on this thread.
static LEAD_SCANS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// Tests: how many times [`story_of`] and [`ThreadIndex::of`] have gone through the document's
@@ -447,6 +448,12 @@ pub(crate) fn story_builds() -> usize {
STORY_BUILDS.with(std::cell::Cell::get)
}
/// Tests: how many frame texts [`first_para_leads`] has gone through on this thread.
#[cfg(test)]
pub(crate) fn lead_scans() -> usize {
LEAD_SCANS.with(std::cell::Cell::get)
}
/// The frames of the thread that `id` is a frame of, in thread order, each area type or type on a
/// path; empty when `id` is in no thread or the thread has fewer than two such frames.
pub(crate) fn story_of(d: &Document, id: NodeId) -> Vec<NodeId> {
+18
View File
@@ -2262,3 +2262,21 @@ fn a_continued_frame_carries_the_character_a_repeat_mark_stands_for() {
assert!(tb.first_para_continues);
assert_eq!(tb.first_para_lead, vectorcraft_text::repeat_base(&ta.plain_text()));
}
/// Kurikaeshi Moji Shori: the leads of a thread whose frames hold only repeat marks come from one
/// pass over the frames, each frame's text gone through once, not once for every frame after it.
#[test]
fn continued_frame_leads_go_through_each_frame_once() {
let n = 2000;
let mut texts = vec!["時".to_string()];
texts.extend(std::iter::repeat_n("々\u{2028}".to_string(), n - 1));
let continues: Vec<bool> = (0..n).map(|k| k > 0).collect();
let before = lead_scans();
let leads = first_para_leads(&texts, &continues);
assert!(lead_scans() - before <= n, "{} frame texts gone through for {n} frames", lead_scans() - before);
assert_eq!(leads.first(), Some(&None));
assert!(leads.iter().skip(1).all(|l| *l == Some('時')));
// A paragraph break stops the carry, and a frame that starts a paragraph carries nothing.
let leads = first_para_leads(&["時".into(), "々\nす".into(), "々".into(), "日".into(), "々".into()], &[false, true, true, false, true]);
assert_eq!(leads, [None, Some('時'), Some('す'), None, Some('日')]);
}