diff --git a/src/client/spark/view/Utility/RXCollection.cs b/src/client/spark/view/Utility/RXCollection.cs index 6bf9989f3..bf25ef121 100644 --- a/src/client/spark/view/Utility/RXCollection.cs +++ b/src/client/spark/view/Utility/RXCollection.cs @@ -20,12 +20,105 @@ namespace mbts.ui.utility { } static class RXCollection { - public static IDisposable connect(IList target, + public static IDisposable connect( + IList target, IObservable> table_update_source, IObservable> comparer_source, Func view_factory, - Func get_model) where TKey : IEquatable => - connect(target.WrapSelector(get_model, view_factory), table_update_source, comparer_source); + Func get_model, + Func update_view_factory = null, + bool regen_views_on_comp_change = true + ) where TKey : IEquatable { + if (update_view_factory is null) { + var wrapped_target = target.WrapSelector(get_model, view_factory); + return connect(wrapped_target, table_update_source, comparer_source); + } + + IComparer comp = null; + TableUpdate table = null; + + return Disposable.Create( + comparer_source.Subscribe(on_comp), + table_update_source.Subscribe(on_table)); + + void on_comp(IComparer new_comp) { + comp = new_comp; + if (table is null) { + return; + } + + if (!regen_views_on_comp_change) { + target.sync_with(target.Ordered(comp.wrap(get_model)).ToList()); + return; + } + reset(); + } + + void on_table(TableUpdate new_table) { + bool is_first_upd = table is null; + table = new_table; + + if (comp is null) { + return; + } + + if (is_first_upd) { + reset(); + return; + } + + var view_comp = comp.wrap(get_model); + var model_list = target.WrapSelector(get_model, view_factory); + + // removed + model_list.RemoveSorted(table.removed, comp, out var n_rem, out var n_rem_expected); + Log.assert(n_rem == n_rem_expected); + + // changes + foreach (var ch in table.changes) { + var old_idx = model_list.BinarySearch(ch.Old, comp); + Log.assert(old_idx >= 0 && old_idx < target.Count); + var new_idx = model_list.BinarySearch(ch.New, comp); + if (new_idx < 0) { + new_idx = ~new_idx; + } + if (old_idx < new_idx) { + // decrement new_idx bc the oldview, at old_idx, will be removed before inserting + // the newview, at new_idx + new_idx--; + } + + var old_view = target[old_idx]; + var new_view = update_view_factory(old_view, ch.New); + if (new_idx == old_idx) { + // the index hasn't actually changed + if (!ReferenceEquals(new_view, old_view)) { + target[old_idx] = new_view; + } + continue; + } + + // remove old_view and insert new_view + target.RemoveAt(old_idx); + target.Insert(new_idx, new_view); + } + + // added + model_list.InsertSortedDistinct( + table.added, comp, out var n_ins, out var n_ins_expected); + Log.assert(n_ins == n_ins_expected); + } + + void reset() { + if (comp is null || table is null) { + return; + } + + target.Clear(); + target.InsertSorted( + table.state.Values.Select(view_factory), comp.wrap(get_model)); + } + } public static IDisposable connect(IList target, IObservable> table_update_source, diff --git a/src/client/spark_core/Collections/Extensions/ListExtensions.cs b/src/client/spark_core/Collections/Extensions/ListExtensions.cs index 7d892b434..af867a331 100644 --- a/src/client/spark_core/Collections/Extensions/ListExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/ListExtensions.cs @@ -32,6 +32,12 @@ namespace bts.collections { public static List empty_if_null(this List source) => source ?? new(0); + public static int BinarySearch(this IList source, T item) where T : IComparable => + BinarySearch(source, item, Comparer.Default); + + public static int BinarySearch(this IList source, T item, IComparer comparer) => + CollectionHelper.binary_search(source, item, comparer); + public static void InsertSorted(this IList list, T item) => list.InsertSorted(item, Comparer.Default); @@ -66,6 +72,29 @@ namespace bts.collections { return false; } + /// + /// Inserts items which are comparatively distinct (according to ) + /// in + /// + public static void InsertSortedDistinct( + this IList list, + IEnumerable items, + IComparer comparer, + out int n_inserted, + out int n_attempted + ) { + n_attempted = 0; + int count_before = list.Count; + foreach (var item in items) { + n_attempted++; + int i = list.BinarySearch(item, comparer); + if (i < 0) { + list.Insert(~i, item); + } + } + n_inserted = list.Count - count_before; + } + public static void InsertOrReplaceSorted(this IList list, T item, IComparer comparer) { int i = CollectionHelper.binary_search(list, item, comparer); if (i < 0) { @@ -130,6 +159,25 @@ namespace bts.collections { } } + public static void RemoveSorted( + this IList list, + IEnumerable items, + IComparer comparer, + out int num_attempted, + out int num_removed + ) { + num_attempted = 0; + var count_before = list.Count; + foreach (var item in items) { + num_attempted++; + int i = CollectionHelper.binary_search(list, item, comparer); + if (i >= 0) { + list.RemoveAt(i); + } + } + num_removed = count_before - list.Count; + } + public static T Pop(this IList list, int index) { T rv = list.ElementAt(index); list.RemoveAt(index);