namespace Titano.Core.Project; /// /// A photo as every copy of a sequence knows it (BRIEF §14.5: originals are content-addressed): its SHA-256 once hashed, /// else its size and capture time (a folder adopted and not verified yet). Frame ids and positions differ between a /// server sequence and a PC's copy of it; the photo does not. /// /// Hex SHA-256; null or empty while the copy has not hashed the file. public sealed record FramePhoto(string? Sha256, long Size, DateTime? CapturedAt) { public bool IsHashed => !string.IsNullOrEmpty(Sha256); /// /// The same photo: the same SHA-256 when both are hashed, otherwise the same size and capture time (to the /// millisecond, as databases keep it). /// public bool Matches(FramePhoto other) { ArgumentNullException.ThrowIfNull(other); if (IsHashed && other.IsHashed) return string.Equals(Sha256, other.Sha256, StringComparison.OrdinalIgnoreCase); return Size == other.Size && CapturedAt is { } mine && other.CapturedAt is { } theirs && Millisecond(mine) == Millisecond(theirs); } internal static long Millisecond(DateTime time) => time.Ticks / TimeSpan.TicksPerMillisecond; } /// /// The photos of one copy's frames by frame index (positions in capture order, BRIEF §5), and where a photo is among /// them. A copy may hold one photo more than once (a folder adopted with 'IMG_0001.JPG' and 'IMG_0001 (1).JPG'); those /// frames are told apart by their order (). Immutable once built, so parallel workers may share it. /// public sealed class FramePhotos { private readonly FramePhoto?[] _byIndex; private readonly Dictionary> _bySha = new(StringComparer.Ordinal); private readonly Dictionary<(long Size, long Time), List> _bySizeAndTime = []; /// Each frame's index and photo, in any order. public FramePhotos(IEnumerable<(int Index, FramePhoto Photo)> frames) { ArgumentNullException.ThrowIfNull(frames); var list = frames.Where(f => f.Index >= 0).ToList(); _byIndex = new FramePhoto?[list.Count == 0 ? 0 : list.Max(f => f.Index) + 1]; foreach (var (index, photo) in list) _byIndex[index] = photo; for (var i = 0; i < _byIndex.Length; i++) { if (_byIndex[i] is not { } photo) continue; if (photo.IsHashed) { var sha = photo.Sha256!.ToLowerInvariant(); if (!_bySha.TryGetValue(sha, out var copies)) _bySha[sha] = copies = []; copies.Add(i); } if (photo.CapturedAt is not { } time) continue; var key = (photo.Size, FramePhoto.Millisecond(time)); if (!_bySizeAndTime.TryGetValue(key, out var same)) _bySizeAndTime[key] = same = []; same.Add(i); } } public int Count => _byIndex.Length; /// The photo at ; null when no frame has that index. public FramePhoto? this[int index] => (uint)index < (uint)_byIndex.Length ? _byIndex[index] : null; /// Some frame is not hashed yet: matching it needs capture times. public bool HasUnhashed => _byIndex.Any(p => p is { IsHashed: false }); /// /// The index of the first frame holding (), or with /// n of the (n+1)-th in index order; null when fewer frames hold it. /// public int? IndexOf(FramePhoto photo, int occurrence = 0) { ArgumentNullException.ThrowIfNull(photo); ArgumentOutOfRangeException.ThrowIfNegative(occurrence); var holding = Holding(photo); return occurrence < holding.Count ? holding[occurrence] : null; } /// /// Which of the frames holding the photo at it is, in index order: 0 for the first (and for /// a photo held once), 1 for the second, … A position translated to another copy names the same one there. /// public int Occurrence(int index) => this[index] is { } photo ? Math.Max(Holding(photo).BinarySearch(index), 0) : 0; /// /// The indices of the frames holding , ascending. A hashed photo is the frames with its SHA-256 /// when there are any: a frame not hashed yet with the same size and capture time may be another photo (a RAW of /// constant size in a burst without sub-second times), so it counts only when no frame has the hash. /// private List Holding(FramePhoto photo) { if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) return hashed; var holding = new List(1); if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return holding; foreach (var index in candidates) { if (_byIndex[index]!.Matches(photo)) holding.Add(index); } return holding; } /// Both lists hold the same photos at the same indices: a position names the same photo on both. public bool SameAs(FramePhotos other) { ArgumentNullException.ThrowIfNull(other); if (other.Count != Count) return false; for (var i = 0; i < Count; i++) { var (mine, theirs) = (this[i], other[i]); if (mine is null != theirs is null || (mine is not null && !mine.Matches(theirs!))) return false; } return true; } }