namespace bts.utils { using bts.collections; using Spark.Core.Collections; using Spark.Core.Disposables; using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Threading.Tasks; public static class CollectionHelper { public const string indexer_name = "Item[]"; public static int binary_search(IList items, T value) => binary_search(items, value, 0, items.Count, Comparer.Default); public static int binary_search(IList items, T value, IComparer comparer) => binary_search(items, value, 0, items.Count, comparer); public static int binary_search(IList items, T value, int index, int length, IComparer comparer) { if (comparer == null) comparer = Comparer.Default; int lo = index; int hi = index + length - 1; while (lo <= hi) { int i = lo + ((hi - lo) >> 1); int c = comparer.Compare(items[i], value); if (c == 0) return i; if (c < 0) { lo = i + 1; } else { hi = i - 1; } } return ~lo; } public static int binary_search(IList items, TKey value, Func key_selector) => binary_search(items, value, 0, items.Count, Comparer.Default, key_selector); public static int binary_search(IList items, TKey value, IComparer comparer, Func key_selector) => binary_search(items, value, 0, items.Count, comparer, key_selector); public static int binary_search(IList items, TKey key, int index, int length, IComparer comparer, Func key_selector) => binary_search(new SelectorList(items, key_selector, null), key, index, length, comparer); /// /// Like , but doesn't require deterministic sort /// (i.e., a comparison of adjacent items in the collection may yield '0'). /// public static int binary_search(IList items, T value, IComparer comparer, IEqualityComparer eq_comparer) => binary_search(items, value, 0, items.Count, comparer, eq_comparer); /// /// Like , but doesn't require deterministic sort /// (i.e., a comparison of adjacent items in the collection may yield '0'). /// public static int binary_search(IList items, T value, int index, int length, IComparer comparer, IEqualityComparer eq_comparer) { if (comparer == null) comparer = Comparer.Default; int lo = index; int hi = index + length - 1; while (lo <= hi) { int i = lo + ((hi - lo) >> 1); int c = comparer.Compare(items[i], value); if (c == 0) { if (eq_comparer.Equals(items[i], value)) { return i; } else { lo = i; goto try_find_exact; } } if (c < 0) { lo = i + 1; } else { hi = i - 1; } } return ~lo; try_find_exact: // move right for (hi = lo + 1; hi < items.Count && comparer.Compare(items[hi], value) == 0; hi++) { if (eq_comparer.Equals(items[hi], value)) return hi; } hi = lo; // move left while (--lo >= 0 && comparer.Compare(items[lo], value) == 0) { if (eq_comparer.Equals(items[lo], value)) return lo; } return ~hi; } public static IDisposable buffer_collection_changed_callback(INotifyCollectionChanged source, Action callback, TimeSpan delay_before_callback, TimeSpan delay_after_callback) { // return CallbackBuffer.buffer_callback(attach_callback, callback, delay_before_callback, delay_after_callback); // IDisposable attach_callback(Action cb) { // source.CollectionChanged += on_collection_changed; // return Disposable.Create(() => source.CollectionChanged -= on_collection_changed); // void on_collection_changed(object sender, NotifyCollectionChangedEventArgs e) => // cb(); // } return CallbackBuffer.buffer_async_callback(attach_callback, impl); IDisposable attach_callback(Action cb) { source.CollectionChanged += on_collection_changed; return Disposable.Create(() => source.CollectionChanged -= on_collection_changed); void on_collection_changed(object? sender, NotifyCollectionChangedEventArgs e) => cb(); } async Task impl() { if (delay_before_callback > TimeSpan.Zero) await Task.Delay(delay_before_callback); callback(); if (delay_after_callback > TimeSpan.Zero) await Task.Delay(delay_after_callback); } } public static ICollection empty() => Array.Empty(); public static string fmt_collection_change(NotifyCollectionChangedEventArgs e) { return e.Action switch { NotifyCollectionChangedAction.Add => $"Insert at {e.NewStartingIndex} [{fmt_items(e.NewItems!)}]", NotifyCollectionChangedAction.Remove => $"Remove from {e.OldStartingIndex} [{fmt_items(e.OldItems!)}]", NotifyCollectionChangedAction.Move => $"Move from {e.OldStartingIndex} to {e.NewStartingIndex} [{fmt_items(e.NewItems!)}]", NotifyCollectionChangedAction.Replace => $"Replace at {e.OldStartingIndex} [{fmt_items(e.OldItems!)}] with [{fmt_items(e.NewItems!)}]", NotifyCollectionChangedAction.Reset => "Reset", _ => $"Unknown-action {e.Action}" }; static string fmt_items(IList items) => string.Join(", ", items.Cast()); } public static void reflect_change(IList target, IEnumerable source, Func create_item, Action on_removed, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: int i = e.NewStartingIndex; foreach (TData d in e.NewItems!) { T item = create_item(d); target.Insert(i++, item); } break; case NotifyCollectionChangedAction.Move: if (e.OldStartingIndex > e.NewStartingIndex) { i = e.OldStartingIndex; foreach (TData d in e.OldItems!) { var v = target[i]; target.RemoveAt(i++); target.Insert(e.NewStartingIndex, v); } } else { foreach (TData d in e.OldItems!) { var v = target[e.OldStartingIndex]; target.RemoveAt(e.OldStartingIndex); target.Insert(e.NewStartingIndex, v); } } break; case NotifyCollectionChangedAction.Remove: for (int x = 0; x < e.OldItems!.Count; x++) { T item = target[e.OldStartingIndex]; target.RemoveAt(e.OldStartingIndex); on_removed?.Invoke(item); } break; case NotifyCollectionChangedAction.Replace: i = e.NewStartingIndex; foreach (TData d in e.NewItems!) { T old_item = target[i]; target[i++] = create_item(d); on_removed?.Invoke(old_item); } break; case NotifyCollectionChangedAction.Reset: var old_views = target.ToArray(); target.Clear(); if (on_removed != null) foreach (T item in old_views) on_removed(item); foreach (TData d in source) target.Add(create_item(d)); break; } } public static void reflect_change(IList target, IEnumerable source, Func create_item, Action on_removed, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: { int i = e.NewStartingIndex; foreach (TData d in e.NewItems!) { T item = create_item(d, i); target.Insert(i++, item); } } break; case NotifyCollectionChangedAction.Move: { if (e.OldStartingIndex > e.NewStartingIndex) { int i = e.OldStartingIndex; foreach (TData d in e.OldItems!) { var v = target[i]; target.RemoveAt(i++); target.Insert(e.NewStartingIndex, v); } } else { foreach (TData d in e.OldItems!) { var v = target[e.OldStartingIndex]; target.RemoveAt(e.OldStartingIndex); target.Insert(e.NewStartingIndex, v); } } } break; case NotifyCollectionChangedAction.Remove: { for (int i = 0; i < e.OldItems!.Count; i++) { T item = target[e.OldStartingIndex]; target.RemoveAt(e.OldStartingIndex); on_removed?.Invoke(item); } } break; case NotifyCollectionChangedAction.Replace: { int i = e.NewStartingIndex; foreach (TData d in e.NewItems!) { T old_item = target[i]; target[i] = create_item(d, i); on_removed?.Invoke(old_item); i++; } } break; case NotifyCollectionChangedAction.Reset: { var old_views = target.ToArray(); target.Clear(); int i; if (on_removed != null) { for (i = 0; i < old_views.Length; i++) on_removed(old_views[i]); } i = 0; foreach (TData d in source) target.Add(create_item(d, i++)); } break; } } public static void process_new_items(IEnumerable source, Action on_new_item, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: case NotifyCollectionChangedAction.Replace: foreach (T v in e.NewItems!) on_new_item(v); break; case NotifyCollectionChangedAction.Reset: foreach (T v in source) on_new_item(v); break; } } public static void sync_with_never_duplicate( IList dest, IEnumerable src, Func create, Func key, Func src_key) => sync_with_never_duplicate(dest, src, create, key, src_key, EqualityComparer.Default); // Note: this is not optimized, be wary of use with large collections public static void sync_with_never_duplicate( IList dest, IEnumerable src, Func create, Func key_selector, Func src_key_selector, IEqualityComparer key_comparer) { key_comparer ??= EqualityComparer.Default; var src_list = src as IList ?? new List(src); #nullable disable var src_map = src_list.ToDictionary(src_key_selector, key_comparer); #nullable enable for (int i = 0; i < dest.Count; i++) { var k = key_selector(dest[i]); if (!src_map.ContainsKey(k)) dest.RemoveAt(i); } #nullable disable var dest_map = dest.ToDictionary(key_selector, key_comparer); #nullable enable for (int i = 0; i < src_list.Count; i++) { TKey key = src_key_selector(src_list[i]); if (dest_map.TryGetValue(key, out T? dval)) { int curr_index = dest.IndexOf(dval); if (curr_index != i) { dest.RemoveAt(curr_index); dest.Insert(i, dval); } } else { dest.Insert(i, create(src_list[i])); } } } public static TResult[] convert_array(T[] source, Func selector) { TResult[] rv = new TResult[source.Length]; for (int i = 0; i < rv.Length; i++) rv[i] = selector(source[i]); return rv; } public static ICollection wrap_selector(ICollection source, Func convert, Func? convert_back) => new CollectionWrapper(source, convert, convert_back); public static void wrap_selector(IReadOnlyCollection source, Func selector, out IReadOnlyCollection as_ro_collection, out ICollection as_collection) { var wrapper = new ROCollectionWrapper(source, selector); as_ro_collection = wrapper; as_collection = wrapper; } public static IReadOnlyCollection wrap_mutable_as_read_only(ICollection source) => new MutableToReadOnlyCollectionWrapper(source); public static IReadOnlyCollection wrap_mutable_as_read_only(ICollection source, Func selector) => new MutableToReadOnlyCollectionWrapper(source, selector); class CollectionWrapper : ICollection, IReadOnlyCollection { readonly ICollection _source; readonly Func _convert; readonly Func? convertBackBacking; public CollectionWrapper(ICollection source, Func convert, Func? convert_back) { _source = source; _convert = convert; convertBackBacking = convert_back; } public int Count => _source.Count; public bool IsReadOnly => _source.IsReadOnly || _convert_back == null; Func _convert_back => convertBackBacking ?? throw new InvalidOperationException("ConvertBack delegate is null"); public void Add(TOut item) => _source.Add(_convert_back(item)); public void Clear() => _source.Clear(); public bool Contains(TOut item) => _source.Contains(_convert_back(item)); public void CopyTo(TOut[] array, int arrayIndex) { foreach (var v in _source) array[arrayIndex++] = _convert(v); } public IEnumerator GetEnumerator() { foreach (var v in _source) yield return _convert(v); } public bool Remove(TOut item) => _source.Remove(_convert_back(item)); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } class ROCollectionWrapper : IReadOnlyCollection, ICollection { readonly Func _convert; readonly IReadOnlyCollection _source; public ROCollectionWrapper(IReadOnlyCollection source, Func convert) { _source = source; _convert = convert; } public int Count => _source.Count; public bool IsReadOnly => true; void ICollection.Add(TOut item) => throw new NotImplementedException(); bool ICollection.Remove(TOut item) => throw new NotImplementedException(); void ICollection.Clear() => throw new NotImplementedException(); public bool Contains(TOut item) { foreach (var v in _source) { if (Equals(v, _convert(v))) return true; } return false; } public void CopyTo(TOut[] array, int arrayIndex) { foreach (var v in _source) array[arrayIndex++] = _convert(v); } public IEnumerator GetEnumerator() { foreach (var v in _source) yield return _convert(v); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } /// /// An wrapper for an underlying . /// class MutableToReadOnlyCollectionWrapper : IReadOnlyCollection { readonly ICollection _source; public MutableToReadOnlyCollectionWrapper(ICollection source) => _source = source ?? throw new ArgumentNullException(nameof(source)); public int Count => _source.Count; public IEnumerator GetEnumerator() => _source.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => _source.GetEnumerator(); } /// /// An wrapper for an underlying . /// class MutableToReadOnlyCollectionWrapper : IReadOnlyCollection { readonly ICollection _source; readonly Func _convert; public MutableToReadOnlyCollectionWrapper(ICollection source, Func convert) { _source = source ?? throw new ArgumentNullException(nameof(source)); _convert = convert ?? throw new ArgumentNullException(nameof(convert)); } public int Count => _source.Count; public IEnumerator GetEnumerator() { foreach (var v in _source) yield return _convert(v); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public static void compare_hashsets(HashSet new_d, HashSet old_d, out HashSet deleted_d, out HashSet added_d) { added_d = new HashSet(); deleted_d = new HashSet(); foreach (T t in new_d) if (!old_d.Contains(t)) added_d.Add(t); foreach (T t in old_d) if (!new_d.Contains(t)) deleted_d.Add(t); } public static IEnumerable append_arrays(params IEnumerable[] ienum_list) { List rv = new List(); foreach (IEnumerable ienum in ienum_list) rv.AddRange(ienum); return rv; } public static bool hashset_equals(HashSet new_d, HashSet old_d) { HashSet deleted_d, added_d; compare_hashsets(new_d, old_d, out deleted_d, out added_d); return deleted_d.Count == 0 && added_d.Count == 0; } /// If key_selector returns the same key this /// functions crushes it (in the Dict) public static IDictionary ToDictionaryDupes(this IEnumerable items, Func key_selector, Func value_selector) { IDictionary rv = new Dictionary(); //(items ?? new List()).ForEach(t => rv[key_selector(t)] = value_selector(t)); if (items != null) { foreach (var t in items) { rv[key_selector(t)] = value_selector(t); } } return rv; } /// /// Gets coordinates for an index in the imaginary sorted list that results from merging /// and . The "X" coordinate is either 0 /// (list1) or 1 (list2), and the "Y" coordinate is the index in that list. /// /// The type of elements in the list /// One of two lists making up the imaginary sorted merged list /// The index in the imaginary sorted merged list for which to get coordinates /// One of two lists making up the imaginary sorted merged list /// The comparer by which the elements are sorted /// /// /// public static (int X, int Y) BinarySearchMergedIndex(IList list1, int index, IList list2, IComparer comparer) { if (index < 0 || index >= list1.Count + list2.Count) { throw new IndexOutOfRangeException(); } if (list1.Count == 0) { return (1, index); } if (list2.Count == 0) { return (0, index); } int list1Start = binarySearchPosition(list2, list1[0], comparer); if (index < list1Start) { return (1, index); } if (index == list1Start) { return (0, 0); } if (list1.Count == 1) { return (1, index - 1); } list2 = list2.WrapSubRange(list1Start, list2.Count - list1Start); index -= list1Start; var res = binarySearchMergedIndex(list1, index, list2, comparer); if (res.X == 1) { res = (res.X, res.Y + list1Start); } return res; static (int X, int Y) binarySearchMergedIndex(IList list1, int index, IList list2, IComparer comparer) { if (index < 0 || index >= list1.Count + list2.Count) { throw new IndexOutOfRangeException(); } int list1Count = list1.Count; if (list1Count == 0) { return (1, index); } int list2Count = list2.Count; if (list2Count == 0) { return (0, index); } if (list1Count > index + 1) { list1 = list1.WrapSubRange(0, index + 1); } if (list2Count > index + 1) { list2 = list2.WrapSubRange(0, index + 1); } int mid1 = list1.Count / 2; int mid1Pos2 = binarySearchPosition(list2, list1[mid1], comparer); int shift1 = 0; int shift2 = 0; if ((mid1 + mid1Pos2) > index) { list1 = list1.WrapSubRange(0, mid1); list2 = list2.WrapSubRange(0, mid1Pos2); } else if ((mid1 + mid1Pos2) < index) { index -= mid1 + mid1Pos2 + 1; if (index < 0) { throw new InvalidOperationException("Bad state (index became negative)"); } shift1 += mid1 + 1; list1 = list1.WrapSubRange(mid1 + 1); shift2 += mid1Pos2; list2 = list2.WrapSubRange(mid1Pos2); } else { return (0, mid1); } var (x, y) = binarySearchMergedIndex(list1, index, list2, comparer); return x == 0 ? (0, y + shift1) : (1, y + shift2); } } static int binarySearchPosition(IList list, T value, IComparer comparer) { int rv = list.BinarySearch(value, comparer); if (rv < 0) { rv = ~rv; } return rv; } } }