//! What the pictures MEAN — the port of `packages/core/src/engine/media/`. //! //! Not to be confused with [`crate::media`], the top-level object-key minter //! (`packages/core/src/media/keys.ts`). That one decides where the BYTES live; //! these three decide what a library of pictures adds up to: which are copies //! of which ([`duplicates`]), whose face a face looks like ([`people`]), and //! which of them make an album ([`albums`]). //! //! All three are pure functions rather than workers: they decide and the //! workers write. Their outputs are proposals the owner decides on — deleting //! a picture is `remove_photo`, keeping an album is the person's tap — with //! ONE act (owner, 14 Oct 2026): a face that clearly matches a person's face //! print is linked to them ([`people::match_people`]), so a person's page and //! album fill without a tap, and the owner's own answer about a face always //! stands over the link. [`embedding`] is how every vector is written into a //! column. //! //! A fourth media engine, `rules::media_items`, sits one level up because the //! TypeScript keeps it there (`engine/mediaItems.ts`): it derives photo and //! video ROWS from a chat message, which is a different job from judging the //! rows once they exist. Its `MediaFamily` is reused by [`albums`] rather than //! redeclared — `MEDIA_RECORD_KINDS` and that enum are the same two strings. use std::cmp::Ordering; pub mod albums; pub mod duplicates; pub mod people; /// A stable sort that cannot panic on an inconsistent comparator. /// /// Every comparator in this directory is ported from a JavaScript one written /// as `x < y ? -1 : 1` — a shape that answers "greater" for two values it /// considers EQUAL, because `Array.prototype.sort` only cares about the sign /// and a tie is unreachable once the earlier clauses have run. Rust's /// `slice::sort_by` is entitled to PANIC when handed a comparator that is not /// a total order, and a pure library in a server may not panic on rows a /// caller handed it — two photos sharing an id, or a group of proposals with /// identical anchors, would be enough. /// /// So this is a plain bottom-up merge sort over an index permutation: stable, /// O(n log n), and defined for any comparator at all. For a comparator that IS /// a total order it agrees with `sort_by` and with V8's TimSort exactly, which /// is every input these engines can meet from a well-formed file. For one that /// is not, the two runtimes' orders are both arbitrary and need not coincide; /// what matters is that neither aborts. pub(crate) fn stable_sort_by(items: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Ordering, { let n = items.len(); if n < 2 { return; } let mut order: Vec = (0..n).collect(); let mut merged: Vec = vec![0; n]; let mut width = 1usize; while width < n { let mut start = 0usize; while start < n { let middle = start.saturating_add(width).min(n); let end = start.saturating_add(width.saturating_mul(2)).min(n); let (mut left, mut right, mut cursor) = (start, middle, start); while left < middle && right < end { // Take from the right half only when it is STRICTLY less — // that is what makes the sort stable. if compare(&items[order[right]], &items[order[left]]) == Ordering::Less { merged[cursor] = order[right]; right += 1; } else { merged[cursor] = order[left]; left += 1; } cursor += 1; } while left < middle { merged[cursor] = order[left]; left += 1; cursor += 1; } while right < end { merged[cursor] = order[right]; right += 1; cursor += 1; } start = end; } order.copy_from_slice(&merged); width = width.saturating_mul(2); } // `order[target]` is the element that belongs at `target`; invert it and // walk the cycles, so the permutation is applied without cloning `T`. let mut destination = vec![0usize; n]; for (target, &source) in order.iter().enumerate() { destination[source] = target; } for i in 0..n { while destination[i] != i { let j = destination[i]; items.swap(i, j); destination.swap(i, j); } } }