MCPcopy Create free account
hub / github.com/argumentcomputer/ix / merge

Function merge

crates/kernel/src/canonical_check.rs:545–576  ·  view source on GitHub ↗

Merge two sorted slices of `(KId, &KConst)` pairs. Mirrors `merge` (`src/ix/compile.rs:2671`).

(
  left: Vec<(KId<M>, &'a KConst<M>)>,
  right: Vec<(KId<M>, &'a KConst<M>)>,
  ctx: &KMutCtx,
  resolve_ctor: &dyn Fn(&KId<M>) -> Option<KConst<M>>,
)

Source from the content-addressed store, hash-verified

543) -> Result<Vec<(KId<M>, &'a KConst<M>)>, TcError<M>> {
544 let mut result = Vec::with_capacity(left.len() + right.len());
545 let mut left_iter = left.into_iter();
546 let mut right_iter = right.into_iter();
547 let mut left_item = left_iter.next();
548 let mut right_item = right_iter.next();
549
550 while let (Some(l), Some(r)) = (&left_item, &right_item) {
551 let cmp = compare_kconst(l.1, r.1, ctx, resolve_ctor)?.ordering;
552 if cmp == Ordering::Greater {
553 result.push(right_item.take().unwrap());
554 right_item = right_iter.next();
555 } else {
556 result.push(left_item.take().unwrap());
557 left_item = left_iter.next();
558 }
559 }
560 if let Some(l) = left_item {
561 result.push(l);
562 result.extend(left_iter);
563 }
564 if let Some(r) = right_item {
565 result.push(r);
566 result.extend(right_iter);
567 }
568 Ok(result)
569}
570
571/// Merge-sort a class of `(KId, &KConst)` pairs by structural comparison.
572/// Mirrors `sort_by_compare` (`src/ix/compile.rs:2708`).
573fn sort_by_compare<'a, M: KernelMode>(
574 items: &[(KId<M>, &'a KConst<M>)],
575 ctx: &KMutCtx,
576 resolve_ctor: &dyn Fn(&KId<M>) -> Option<KConst<M>>,
577) -> Result<Vec<(KId<M>, &'a KConst<M>)>, TcError<M>> {
578 if items.len() <= 1 {
579 return Ok(items.to_vec());

Callers

nothing calls this directly

Calls 5

compare_kconstFunction · 0.85
nextMethod · 0.80
pushMethod · 0.80
lenMethod · 0.45
takeMethod · 0.45

Tested by

no test coverage detected