extern crate alloc;
use crate::kairos::{Clock, TickCounter};
use crate::metis::{Anchor, Dot, DotSet, Dotted, Locus, Rhapsody};
mod condense;
mod conformance;
mod core;
mod extent;
mod positional;
mod run;
mod visual;
mod walk;
type Seq = Dotted<Rhapsody>;
fn clock(station: u32) -> Clock<TickCounter> {
Clock::with_default_config(TickCounter::new(), station).unwrap()
}
fn woven(dot: Dot, locus: Locus) -> Seq {
let mut rhapsody = Rhapsody::new();
assert!(rhapsody.weave(dot, locus));
Dotted::from_store(rhapsody)
}
fn insert(replica: &mut Seq, clk: &Clock<TickCounter>, station: u32, anchor: Option<Dot>) -> Dot {
let dot = replica.next_dot(station);
let locus = Locus {
anchor: anchor.map_or(Anchor::Origin, |dot| Anchor::After(dot.into())),
rank: clk.now(0u16),
};
*replica = replica.merge(&woven(dot, locus));
dot
}
fn weave_at(replica: &mut Seq, clk: &Clock<TickCounter>, station: u32, anchor: Anchor) -> Dot {
let dot = replica.next_dot(station);
let locus = Locus {
anchor,
rank: clk.now(0u16),
};
*replica = replica.merge(&woven(dot, locus));
dot
}
fn prepend(replica: &mut Seq, clk: &Clock<TickCounter>, station: u32) -> Dot {
let anchor = replica.store().anchor_for_visual_insert(None);
if let Some(top) = replica.store().children_of(anchor).next()
&& let Some(locus) = replica.store().locus(top)
{
clk.observe(locus.rank);
}
weave_at(replica, clk, station, anchor)
}
fn delete(replica: &mut Seq, dot: Dot) {
let mut ctx = DotSet::new();
assert!(ctx.insert(dot));
*replica = replica.merge(&Dotted::from_context(ctx));
}
fn position_of_run(seq: &[Dot], run: &[Dot]) -> Option<usize> {
if run.is_empty() {
return Some(0);
}
seq.windows(run.len()).position(|w| w == run)
}