fix: Use IEqualityComparer arg in sync_with_never_duplicate
This commit is contained in:
1 parent
17f10b2450
commit
11f345f3cd
1 file changed
+467
-458
@@ -1,458 +1,467 @@
|
||||
using mbts;
|
||||
using bts.collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bts.utils {
|
||||
static class CollectionHelper {
|
||||
public const string indexer_name = "Item[]";
|
||||
|
||||
public static int binary_search<T>(IList<T> items, T value) =>
|
||||
binary_search(items, value, 0, items.Count, Comparer<T>.Default);
|
||||
|
||||
public static int binary_search<T>(IList<T> items, T value, IComparer<T> comparer) =>
|
||||
binary_search(items, value, 0, items.Count, comparer);
|
||||
|
||||
public static int binary_search<T>(IList<T> items, T value, int index, int length, IComparer<T> comparer) {
|
||||
if (comparer == null) comparer = Comparer<T>.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<T, TKey>(IList<T> items, TKey value, Func<T, TKey> key_selector) =>
|
||||
binary_search(items, value, 0, items.Count, Comparer<TKey>.Default, key_selector);
|
||||
|
||||
public static int binary_search<T, TKey>(IList<T> items, TKey value, IComparer<TKey> comparer, Func<T, TKey> key_selector) =>
|
||||
binary_search(items, value, 0, items.Count, comparer, key_selector);
|
||||
|
||||
public static int binary_search<T, TKey>(IList<T> items, TKey key, int index, int length, IComparer<TKey> comparer, Func<T, TKey> key_selector) =>
|
||||
binary_search(items.WrapSelector(key_selector, null), key, index, length, comparer);
|
||||
|
||||
/// <summary>
|
||||
/// Like <see cref="binary_search{T}(IList{T}, T, IComparer{T})"/>, but doesn't require deterministic sort
|
||||
/// (i.e., a comparison of adjacent items in the collection may yield '0').
|
||||
/// </summary>
|
||||
public static int binary_search<T>(IList<T> items, T value, IComparer<T> comparer, IEqualityComparer<T> eq_comparer) =>
|
||||
binary_search(items, value, 0, items.Count, comparer, eq_comparer);
|
||||
|
||||
/// <summary>
|
||||
/// Like <see cref="binary_search{T}(IList{T}, T, int, int, IComparer{T})"/>, but doesn't require deterministic sort
|
||||
/// (i.e., a comparison of adjacent items in the collection may yield '0').
|
||||
/// </summary>
|
||||
public static int binary_search<T>(IList<T> items, T value, int index, int length, IComparer<T> comparer, IEqualityComparer<T> eq_comparer) {
|
||||
if (comparer == null) comparer = Comparer<T>.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 void reflect_change<T, TData>(IList<T> target, IEnumerable<TData> source, Func<TData, T> create_item, Action<T> 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<T, TData>(IList<T> target, IEnumerable<TData> source, Func<TData, int, T> create_item, Action<T> 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<T>(IEnumerable<T> source, Action<T> 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;
|
||||
}
|
||||
}
|
||||
|
||||
// Note: this is not optimized, be wary of use with large collections
|
||||
public static void sync_with_never_duplicate<T>(IList<T> dest, IEnumerable<T> src) =>
|
||||
sync_with_never_duplicate(dest, src, EqualityComparer<T>.Default);
|
||||
|
||||
// Note: this is not optimized, be wary of use with large collections
|
||||
public static void sync_with_never_duplicate<T>(IList<T> dest, IEnumerable<T> src, IEqualityComparer<T> comp) {
|
||||
var src_list = src as IList<T> ?? new List<T>(src);
|
||||
var src_set = src as ISet<T> ?? new HashSet<T>(src_list);
|
||||
for (int i = 0; i < dest.Count; i++) {
|
||||
if (!src_set.Contains(dest[i]))
|
||||
dest.RemoveAt(i);
|
||||
}
|
||||
for (int i = 0; i < src_list.Count; i++) {
|
||||
int curr_index = dest.IndexOf(src_list[i]);
|
||||
if (curr_index == i) {
|
||||
continue;
|
||||
} else if (curr_index >= 0 && curr_index < dest.Count) {
|
||||
dest.RemoveAt(curr_index);
|
||||
}
|
||||
dest.Insert(i, src_list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public static void sync_with_never_duplicate<T, TSource, TKey>(ObservableCollection<T> dest, IEnumerable<TSource> src, Func<TSource, T> create, Func<T, TKey> key, Func<TSource, TKey> src_key) =>
|
||||
sync_with_never_duplicate(dest, src, create, key, src_key, EqualityComparer<TKey>.Default);
|
||||
|
||||
// Note: this is not optimized, be wary of use with large collections
|
||||
public static void sync_with_never_duplicate<T, TSource, TKey>(ObservableCollection<T> dest, IEnumerable<TSource> src, Func<TSource, T> create, Func<T, TKey> key_selector, Func<TSource, TKey> src_key_selector, IEqualityComparer<TKey> key_comparer) {
|
||||
var src_list = src as IList<TSource> ?? new List<TSource>(src);
|
||||
var src_map = src_list.ToDictionary(src_key_selector);
|
||||
for (int i = 0; i < dest.Count; i++) {
|
||||
var k = key_selector(dest[i]);
|
||||
if (!src_map.ContainsKey(k))
|
||||
dest.RemoveAt(i);
|
||||
}
|
||||
var dest_map = dest.ToDictionary(key_selector);
|
||||
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, TResult>(T[] source, Func<T, TResult> 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<TResult> wrap_selector<T, TResult>(ICollection<T> source, Func<T, TResult> convert, Func<TResult, T> convert_back) =>
|
||||
new CollectionWrapper<T, TResult>(source, convert, convert_back);
|
||||
|
||||
public static void wrap_selector<T, TResult>(IReadOnlyCollection<T> source, Func<T, TResult> selector, out IReadOnlyCollection<TResult> as_ro_collection, out ICollection<TResult> as_collection) {
|
||||
var wrapper = new ROCollectionWrapper<T, TResult>(source, selector);
|
||||
as_ro_collection = wrapper;
|
||||
as_collection = wrapper;
|
||||
}
|
||||
|
||||
public static IReadOnlyCollection<T> wrap_mutable_as_read_only<T>(ICollection<T> source) =>
|
||||
new MutableToReadOnlyCollectionWrapper<T>(source);
|
||||
|
||||
public static IReadOnlyCollection<TResult> wrap_mutable_as_read_only<T, TResult>(ICollection<T> source, Func<T, TResult> selector) =>
|
||||
new MutableToReadOnlyCollectionWrapper<T, TResult>(source, selector);
|
||||
|
||||
class CollectionWrapper<TIn, TOut> : ICollection<TOut>, IReadOnlyCollection<TOut> {
|
||||
readonly ICollection<TIn> _source;
|
||||
readonly Func<TIn, TOut> _convert;
|
||||
readonly Func<TOut, TIn> _convert_back;
|
||||
|
||||
public CollectionWrapper(ICollection<TIn> source, Func<TIn, TOut> convert, Func<TOut, TIn> convert_back) {
|
||||
_source = source;
|
||||
_convert = convert;
|
||||
_convert_back = convert_back;
|
||||
}
|
||||
|
||||
public int Count => _source.Count;
|
||||
public bool IsReadOnly => _source.IsReadOnly || _convert_back == 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<TOut> 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<TIn, TOut> : IReadOnlyCollection<TOut>, ICollection<TOut> {
|
||||
readonly Func<TIn, TOut> _convert;
|
||||
readonly IReadOnlyCollection<TIn> _source;
|
||||
|
||||
public ROCollectionWrapper(IReadOnlyCollection<TIn> source, Func<TIn, TOut> convert) {
|
||||
_source = source;
|
||||
_convert = convert;
|
||||
}
|
||||
|
||||
public int Count => _source.Count;
|
||||
public bool IsReadOnly => true;
|
||||
|
||||
void ICollection<TOut>.Add(TOut item) => throw new NotImplementedException();
|
||||
bool ICollection<TOut>.Remove(TOut item) => throw new NotImplementedException();
|
||||
void ICollection<TOut>.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<TOut> GetEnumerator() {
|
||||
foreach (var v in _source)
|
||||
yield return _convert(v);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IReadOnlyCollection{T}"/> wrapper for an underlying <see cref="ICollection{T}"/>.
|
||||
/// </summary>
|
||||
class MutableToReadOnlyCollectionWrapper<T> : IReadOnlyCollection<T> {
|
||||
readonly ICollection<T> _source;
|
||||
|
||||
public MutableToReadOnlyCollectionWrapper(ICollection<T> source) =>
|
||||
_source = source ?? throw new ArgumentNullException(nameof(source));
|
||||
|
||||
public int Count => _source.Count;
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => _source.GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => _source.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IReadOnlyCollection{TOut}"/> wrapper for an underlying <see cref="ICollection{TIn}"/>.
|
||||
/// </summary>
|
||||
class MutableToReadOnlyCollectionWrapper<TIn, TOut> : IReadOnlyCollection<TOut> {
|
||||
readonly ICollection<TIn> _source;
|
||||
readonly Func<TIn, TOut> _convert;
|
||||
|
||||
public MutableToReadOnlyCollectionWrapper(ICollection<TIn> source, Func<TIn, TOut> convert) {
|
||||
_source = source ?? throw new ArgumentNullException(nameof(source));
|
||||
_convert = convert ?? throw new ArgumentNullException(nameof(convert));
|
||||
}
|
||||
|
||||
public int Count => _source.Count;
|
||||
|
||||
public IEnumerator<TOut> GetEnumerator() {
|
||||
foreach (var v in _source)
|
||||
yield return _convert(v);
|
||||
}
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
|
||||
public static void compare_hashsets<T>(HashSet<T> new_d, HashSet<T> old_d,
|
||||
out HashSet<T> deleted_d, out HashSet<T> added_d)
|
||||
{
|
||||
added_d = new HashSet<T>();
|
||||
deleted_d = new HashSet<T>();
|
||||
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<T> append_arrays<T>(params IEnumerable<T>[] ienum_list)
|
||||
{
|
||||
List<T> rv = new List<T>();
|
||||
foreach (IEnumerable<T> ienum in ienum_list)
|
||||
rv.AddRange(ienum);
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static bool hashset_equals<T>(HashSet<T> new_d, HashSet<T> old_d)
|
||||
{
|
||||
HashSet<T> 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<K, V> ToDictionaryDupes<K, V, T>(this IEnumerable<T> items, Func<T, K> key_selector, Func<T, V> value_selector) {
|
||||
IDictionary<K, V> rv = new Dictionary<K, V>();
|
||||
//(items ?? new List<T>()).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;
|
||||
}
|
||||
}
|
||||
}
|
||||
using mbts;
|
||||
using bts.collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bts.utils {
|
||||
static class CollectionHelper {
|
||||
public const string indexer_name = "Item[]";
|
||||
|
||||
public static int binary_search<T>(IList<T> items, T value) =>
|
||||
binary_search(items, value, 0, items.Count, Comparer<T>.Default);
|
||||
|
||||
public static int binary_search<T>(IList<T> items, T value, IComparer<T> comparer) =>
|
||||
binary_search(items, value, 0, items.Count, comparer);
|
||||
|
||||
public static int binary_search<T>(IList<T> items, T value, int index, int length, IComparer<T> comparer) {
|
||||
if (comparer == null) comparer = Comparer<T>.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<T, TKey>(IList<T> items, TKey value, Func<T, TKey> key_selector) =>
|
||||
binary_search(items, value, 0, items.Count, Comparer<TKey>.Default, key_selector);
|
||||
|
||||
public static int binary_search<T, TKey>(IList<T> items, TKey value, IComparer<TKey> comparer, Func<T, TKey> key_selector) =>
|
||||
binary_search(items, value, 0, items.Count, comparer, key_selector);
|
||||
|
||||
public static int binary_search<T, TKey>(IList<T> items, TKey key, int index, int length, IComparer<TKey> comparer, Func<T, TKey> key_selector) =>
|
||||
binary_search(items.WrapSelector(key_selector, null), key, index, length, comparer);
|
||||
|
||||
/// <summary>
|
||||
/// Like <see cref="binary_search{T}(IList{T}, T, IComparer{T})"/>, but doesn't require deterministic sort
|
||||
/// (i.e., a comparison of adjacent items in the collection may yield '0').
|
||||
/// </summary>
|
||||
public static int binary_search<T>(IList<T> items, T value, IComparer<T> comparer, IEqualityComparer<T> eq_comparer) =>
|
||||
binary_search(items, value, 0, items.Count, comparer, eq_comparer);
|
||||
|
||||
/// <summary>
|
||||
/// Like <see cref="binary_search{T}(IList{T}, T, int, int, IComparer{T})"/>, but doesn't require deterministic sort
|
||||
/// (i.e., a comparison of adjacent items in the collection may yield '0').
|
||||
/// </summary>
|
||||
public static int binary_search<T>(IList<T> items, T value, int index, int length, IComparer<T> comparer, IEqualityComparer<T> eq_comparer) {
|
||||
if (comparer == null) comparer = Comparer<T>.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 void reflect_change<T, TData>(IList<T> target, IEnumerable<TData> source, Func<TData, T> create_item, Action<T> 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<T, TData>(IList<T> target, IEnumerable<TData> source, Func<TData, int, T> create_item, Action<T> 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<T>(IEnumerable<T> source, Action<T> 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;
|
||||
}
|
||||
}
|
||||
|
||||
// Note: this is not optimized, be wary of use with large collections
|
||||
public static void sync_with_never_duplicate<T>(IList<T> dest, IEnumerable<T> src) =>
|
||||
sync_with_never_duplicate(dest, src, EqualityComparer<T>.Default);
|
||||
|
||||
// Note: this is not optimized, be wary of use with large collections
|
||||
public static void sync_with_never_duplicate<T>(IList<T> dest, IEnumerable<T> src, IEqualityComparer<T> comp) {
|
||||
comp ??= EqualityComparer<T>.Default;
|
||||
var src_list = src as IList<T> ?? new List<T>(src);
|
||||
var src_set = src as ISet<T> ?? new HashSet<T>(src_list, comp);
|
||||
for (int i = 0; i < dest.Count; i++) {
|
||||
if (!src_set.Contains(dest[i]))
|
||||
dest.RemoveAt(i);
|
||||
}
|
||||
for (int i = 0; i < src_list.Count; i++) {
|
||||
int curr_index = dest.IndexOf(src_list[i]);
|
||||
if (curr_index == i) {
|
||||
continue;
|
||||
} else if (curr_index >= 0 && curr_index < dest.Count) {
|
||||
dest.RemoveAt(curr_index);
|
||||
}
|
||||
dest.Insert(i, src_list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public static void sync_with_never_duplicate<T, TSource, TKey>(ObservableCollection<T> dest, IEnumerable<TSource> src, Func<TSource, T> create, Func<T, TKey> key, Func<TSource, TKey> src_key) =>
|
||||
sync_with_never_duplicate(dest, src, create, key, src_key, EqualityComparer<TKey>.Default);
|
||||
|
||||
// Note: this is not optimized, be wary of use with large collections
|
||||
public static void sync_with_never_duplicate<T, TSource, TKey>(
|
||||
ObservableCollection<T> dest,
|
||||
IEnumerable<TSource> src,
|
||||
Func<TSource, T> create,
|
||||
Func<T, TKey> key_selector,
|
||||
Func<TSource, TKey> src_key_selector,
|
||||
IEqualityComparer<TKey> key_comparer) {
|
||||
key_comparer ??= EqualityComparer<TKey>.Default;
|
||||
var src_list = src as IList<TSource> ?? new List<TSource>(src);
|
||||
var src_map = src_list.ToDictionary(src_key_selector, key_comparer);
|
||||
for (int i = 0; i < dest.Count; i++) {
|
||||
var k = key_selector(dest[i]);
|
||||
if (!src_map.ContainsKey(k))
|
||||
dest.RemoveAt(i);
|
||||
}
|
||||
|
||||
var dest_map = dest.ToDictionary(key_selector, key_comparer);
|
||||
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, TResult>(T[] source, Func<T, TResult> 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<TResult> wrap_selector<T, TResult>(ICollection<T> source, Func<T, TResult> convert, Func<TResult, T> convert_back) =>
|
||||
new CollectionWrapper<T, TResult>(source, convert, convert_back);
|
||||
|
||||
public static void wrap_selector<T, TResult>(IReadOnlyCollection<T> source, Func<T, TResult> selector, out IReadOnlyCollection<TResult> as_ro_collection, out ICollection<TResult> as_collection) {
|
||||
var wrapper = new ROCollectionWrapper<T, TResult>(source, selector);
|
||||
as_ro_collection = wrapper;
|
||||
as_collection = wrapper;
|
||||
}
|
||||
|
||||
public static IReadOnlyCollection<T> wrap_mutable_as_read_only<T>(ICollection<T> source) =>
|
||||
new MutableToReadOnlyCollectionWrapper<T>(source);
|
||||
|
||||
public static IReadOnlyCollection<TResult> wrap_mutable_as_read_only<T, TResult>(ICollection<T> source, Func<T, TResult> selector) =>
|
||||
new MutableToReadOnlyCollectionWrapper<T, TResult>(source, selector);
|
||||
|
||||
class CollectionWrapper<TIn, TOut> : ICollection<TOut>, IReadOnlyCollection<TOut> {
|
||||
readonly ICollection<TIn> _source;
|
||||
readonly Func<TIn, TOut> _convert;
|
||||
readonly Func<TOut, TIn> _convert_back;
|
||||
|
||||
public CollectionWrapper(ICollection<TIn> source, Func<TIn, TOut> convert, Func<TOut, TIn> convert_back) {
|
||||
_source = source;
|
||||
_convert = convert;
|
||||
_convert_back = convert_back;
|
||||
}
|
||||
|
||||
public int Count => _source.Count;
|
||||
public bool IsReadOnly => _source.IsReadOnly || _convert_back == 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<TOut> 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<TIn, TOut> : IReadOnlyCollection<TOut>, ICollection<TOut> {
|
||||
readonly Func<TIn, TOut> _convert;
|
||||
readonly IReadOnlyCollection<TIn> _source;
|
||||
|
||||
public ROCollectionWrapper(IReadOnlyCollection<TIn> source, Func<TIn, TOut> convert) {
|
||||
_source = source;
|
||||
_convert = convert;
|
||||
}
|
||||
|
||||
public int Count => _source.Count;
|
||||
public bool IsReadOnly => true;
|
||||
|
||||
void ICollection<TOut>.Add(TOut item) => throw new NotImplementedException();
|
||||
bool ICollection<TOut>.Remove(TOut item) => throw new NotImplementedException();
|
||||
void ICollection<TOut>.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<TOut> GetEnumerator() {
|
||||
foreach (var v in _source)
|
||||
yield return _convert(v);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IReadOnlyCollection{T}"/> wrapper for an underlying <see cref="ICollection{T}"/>.
|
||||
/// </summary>
|
||||
class MutableToReadOnlyCollectionWrapper<T> : IReadOnlyCollection<T> {
|
||||
readonly ICollection<T> _source;
|
||||
|
||||
public MutableToReadOnlyCollectionWrapper(ICollection<T> source) =>
|
||||
_source = source ?? throw new ArgumentNullException(nameof(source));
|
||||
|
||||
public int Count => _source.Count;
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => _source.GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => _source.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IReadOnlyCollection{TOut}"/> wrapper for an underlying <see cref="ICollection{TIn}"/>.
|
||||
/// </summary>
|
||||
class MutableToReadOnlyCollectionWrapper<TIn, TOut> : IReadOnlyCollection<TOut> {
|
||||
readonly ICollection<TIn> _source;
|
||||
readonly Func<TIn, TOut> _convert;
|
||||
|
||||
public MutableToReadOnlyCollectionWrapper(ICollection<TIn> source, Func<TIn, TOut> convert) {
|
||||
_source = source ?? throw new ArgumentNullException(nameof(source));
|
||||
_convert = convert ?? throw new ArgumentNullException(nameof(convert));
|
||||
}
|
||||
|
||||
public int Count => _source.Count;
|
||||
|
||||
public IEnumerator<TOut> GetEnumerator() {
|
||||
foreach (var v in _source)
|
||||
yield return _convert(v);
|
||||
}
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
|
||||
public static void compare_hashsets<T>(HashSet<T> new_d, HashSet<T> old_d,
|
||||
out HashSet<T> deleted_d, out HashSet<T> added_d)
|
||||
{
|
||||
added_d = new HashSet<T>();
|
||||
deleted_d = new HashSet<T>();
|
||||
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<T> append_arrays<T>(params IEnumerable<T>[] ienum_list)
|
||||
{
|
||||
List<T> rv = new List<T>();
|
||||
foreach (IEnumerable<T> ienum in ienum_list)
|
||||
rv.AddRange(ienum);
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static bool hashset_equals<T>(HashSet<T> new_d, HashSet<T> old_d)
|
||||
{
|
||||
HashSet<T> 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<K, V> ToDictionaryDupes<K, V, T>(this IEnumerable<T> items, Func<T, K> key_selector, Func<T, V> value_selector) {
|
||||
IDictionary<K, V> rv = new Dictionary<K, V>();
|
||||
//(items ?? new List<T>()).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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user