620 lines
28 KiB
C#
620 lines
28 KiB
C#
using mbts.utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Collections.Specialized;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
|
|
namespace System.Collections.Generic {
|
|
public static class EnumerableExtensions {
|
|
#region Ordered
|
|
public static IOrderedEnumerable<T> Ordered<T>(this IEnumerable<T> enumerable) =>
|
|
enumerable.OrderBy(t => t);
|
|
|
|
public static IEnumerable<T> Ordered<T>(this IEnumerable<T> enumerable, Func<T, T, int> compare) =>
|
|
enumerable.Ordered(Comparer<T>.Create(new Comparison<T>(compare)));
|
|
|
|
public static IEnumerable<T> Ordered<T>(this IEnumerable<T> enumerable, IComparer<T> comparer) =>
|
|
enumerable.OrderBy(t => t, comparer);
|
|
#endregion
|
|
|
|
public static IEnumerable<T> DistinctOn<T, TKey>(this IEnumerable<T> enumerable, Func<T, TKey> key_selector) =>
|
|
enumerable.Distinct(EqualityComparer.Create<T>((x, y) => key_selector(x).Equals(key_selector(y)), x => key_selector(x).GetHashCode()));
|
|
|
|
public static IEnumerable<T> Except<T, TKey>(this IEnumerable<T> enumerable, IEnumerable<TKey> second, Func<T, TKey> key_selector) =>
|
|
enumerable.Where(t => !second.Contains(key_selector(t)));
|
|
|
|
public static IEnumerable<T> Intersect<T, TKey>(this IEnumerable<T> enumerable, IEnumerable<TKey> second, Func<T, TKey> key_selector) =>
|
|
enumerable.Where(t => second.Contains(key_selector(t)));
|
|
|
|
public static T[] ToLArray<T>(this IEnumerable<T> enumerable) =>
|
|
enumerable.ToList().ToArray();
|
|
|
|
public static TAccumulate AggregateWhile<T, TAccumulate>(this IEnumerable<T> source, TAccumulate seed, Func<TAccumulate, T, TAccumulate> aggregate, Func<TAccumulate, T, bool> predicate) {
|
|
TAccumulate rv = seed;
|
|
foreach (T el in source) {
|
|
if (!predicate(rv, el))
|
|
break;
|
|
rv = aggregate(rv, el);
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
public static IEnumerable<T> ToIEnumerable<T>(this T item) {
|
|
yield return item;
|
|
}
|
|
|
|
public static IEnumerable<T> TakeWhile<T, TAccumulate>(this IEnumerable<T> source, TAccumulate seed, Func<TAccumulate, T, TAccumulate> accumulate, Func<TAccumulate, T, bool> predicate) {
|
|
TAccumulate a = seed;
|
|
foreach (T el in source) {
|
|
if (!predicate(a, el))
|
|
break;
|
|
a = accumulate(a, el);
|
|
yield return el;
|
|
}
|
|
}
|
|
|
|
public static bool TryGetFirst<T>(this IEnumerable<T> source, Func<T, bool> predicate, out T result) {
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
foreach (T element in source)
|
|
if (predicate(element)) {
|
|
result = element;
|
|
return true;
|
|
}
|
|
result = default;
|
|
return false;
|
|
}
|
|
|
|
public static bool TryGetFirst<T, TResult>(this IEnumerable<T> source, Func<T, bool> predicate, Func<T, TResult> select, out TResult result) {
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
foreach (T element in source)
|
|
if (predicate(element)) {
|
|
result = select(element);
|
|
return true;
|
|
}
|
|
result = default;
|
|
return false;
|
|
}
|
|
|
|
public static T FirstOrDefault<T>(this IEnumerable<T> source, Func<T, bool> predicate, T default_rv) {
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
foreach (T element in source)
|
|
if (predicate(element))
|
|
return element;
|
|
return default_rv;
|
|
}
|
|
|
|
public static TResult FirstOrDefault<T, TResult>(this IEnumerable<T> source, Func<T, bool> predicate, Func<T, TResult> select) {
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
foreach (T element in source)
|
|
if (predicate(element))
|
|
return select(element);
|
|
return default;
|
|
}
|
|
|
|
public static TResult FirstOrDefault<T, TResult>(this IEnumerable<T> source, Func<T, bool> predicate, Func<T, TResult> select, TResult default_rv) {
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
foreach (T element in source)
|
|
if (predicate(element))
|
|
return select(element);
|
|
return default_rv;
|
|
}
|
|
|
|
public static T LastOrDefault<T>(this IEnumerable<T> source, T default_rv) {
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
var list = source as IList<T>;
|
|
if (list != null) {
|
|
int count = list.Count;
|
|
if (count > 0) return list[count - 1];
|
|
} else {
|
|
using (var e = source.GetEnumerator())
|
|
if (e.MoveNext()) {
|
|
T result;
|
|
do result = e.Current;
|
|
while (e.MoveNext());
|
|
return result;
|
|
}
|
|
}
|
|
return default_rv;
|
|
}
|
|
|
|
public static T LastOrDefault<T>(this IEnumerable<T> source, Func<T, bool> predicate, T default_rv) {
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
|
|
T rv = default_rv;
|
|
foreach (T element in source)
|
|
if (predicate(element)) rv = element;
|
|
return rv;
|
|
}
|
|
|
|
public static TResult LastOrDefault<T, TResult>(this IEnumerable<T> source, Func<T, TResult> select, Func<TResult, bool> predicate) =>
|
|
LastOrDefault(source, select, predicate, default);
|
|
|
|
public static TResult LastOrDefault<T, TResult>(this IEnumerable<T> source, Func<T, TResult> select, Func<TResult, bool> predicate, TResult default_rv) {
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
if (select == null) throw new ArgumentNullException(nameof(select));
|
|
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
|
|
TResult rv = default;
|
|
foreach (T element in source) {
|
|
TResult r = select(element);
|
|
if (predicate(r)) rv = r;
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Simply returns enumerable.ToList(), under the guise ofo IReadOnlyList<>
|
|
/// </summary>
|
|
public static IReadOnlyList<T> ToReadOnlyList<T>(this IEnumerable<T> enumerable) =>
|
|
enumerable.ToList();
|
|
|
|
public static Dictionary<TKey, T> ToDictionary<T, TKey>(this IEnumerable<KeyValuePair<TKey, T>> key_values) =>
|
|
key_values.ToDictionary(kv => kv.Key, kv => kv.Value);
|
|
|
|
public static ReadOnlyDictionary<TKey, T> ToReadOnlyDictionary<T, TKey>(this IEnumerable<T> enumerable, Func<T, TKey> key_selector) =>
|
|
new ReadOnlyDictionary<TKey, T>(enumerable.ToDictionary(key_selector));
|
|
|
|
public static ReadOnlyDictionary<TKey, T> ToReadOnlyDictionary<T, TKey>(this IEnumerable<T> enumerable, Func<T, TKey> key_selector, IEqualityComparer<TKey> comparer) =>
|
|
new ReadOnlyDictionary<TKey, T>(enumerable.ToDictionary(key_selector, comparer));
|
|
|
|
public static ReadOnlyDictionary<TKey, TElement> ToReadOnlyDictionary<T, TKey, TElement>(this IEnumerable<T> enumerable, Func<T, TKey> key_selector, Func<T, TElement> element_selector) =>
|
|
new ReadOnlyDictionary<TKey, TElement>(enumerable.ToDictionary(key_selector, element_selector));
|
|
|
|
public static ReadOnlyDictionary<TKey, TElement> ToReadOnlyDictionary<T, TKey, TElement>(this IEnumerable<T> enumerable, Func<T, TKey> key_selector, Func<T, TElement> element_selector, IEqualityComparer<TKey> comparer) =>
|
|
new ReadOnlyDictionary<TKey, TElement>(enumerable.ToDictionary(key_selector, element_selector, comparer));
|
|
|
|
#region Min
|
|
public static T Min<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value) where T : IComparable<T> =>
|
|
Min(enumerable, select_value, default(T));
|
|
|
|
public static T Min<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, T return_if_empty) where T : IComparable<T> {
|
|
var etr = enumerable.GetEnumerator();
|
|
if (!etr.MoveNext()) return return_if_empty;
|
|
T min = select_value(etr.Current);
|
|
while (etr.MoveNext()) {
|
|
T val = select_value(etr.Current);
|
|
if (val.CompareTo(min) == -1) min = val;
|
|
}
|
|
return min;
|
|
}
|
|
|
|
public static T Min<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> comparison) =>
|
|
Min(enumerable, select_value, comparison, default);
|
|
|
|
public static T Min<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> comparison, T return_if_empty) {
|
|
var etr = enumerable.GetEnumerator();
|
|
if (!etr.MoveNext()) return return_if_empty;
|
|
T min = select_value(etr.Current);
|
|
while (etr.MoveNext()) {
|
|
T val = select_value(etr.Current);
|
|
if (comparison(val, min) == -1) min = val;
|
|
}
|
|
return min;
|
|
}
|
|
#endregion
|
|
|
|
#region MinElement
|
|
public static TElement MinElement<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value) where T : IComparable<T> =>
|
|
MinElement(enumerable, select_value, default(TElement));
|
|
|
|
public static TElement MinElement<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, TElement return_if_empty) where T : IComparable<T> {
|
|
var etr = enumerable.GetEnumerator();
|
|
if (!etr.MoveNext()) return return_if_empty;
|
|
TElement min_el = etr.Current;
|
|
T min = select_value(min_el);
|
|
while (etr.MoveNext()) {
|
|
T val = select_value(etr.Current);
|
|
if (val.CompareTo(min) == -1) {
|
|
min_el = etr.Current;
|
|
min = val;
|
|
}
|
|
}
|
|
return min_el;
|
|
}
|
|
|
|
public static TElement MinElement<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> comparison) =>
|
|
MinElement(enumerable, select_value, comparison, default);
|
|
|
|
public static TElement MinElement<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> comparison, TElement return_if_empty) {
|
|
var etr = enumerable.GetEnumerator();
|
|
if (!etr.MoveNext()) return return_if_empty;
|
|
TElement min_el = etr.Current;
|
|
T min = select_value(etr.Current);
|
|
while (etr.MoveNext()) {
|
|
T val = select_value(etr.Current);
|
|
if (comparison(val, min) == -1) {
|
|
min_el = etr.Current;
|
|
min = val;
|
|
}
|
|
}
|
|
return min_el;
|
|
}
|
|
#endregion
|
|
|
|
#region Max
|
|
public static T Max<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value) where T : IComparable<T> =>
|
|
Max(enumerable, select_value, default(T));
|
|
|
|
public static T Max<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, T return_if_empty) where T : IComparable<T> {
|
|
var etr = enumerable.GetEnumerator();
|
|
if (!etr.MoveNext()) return return_if_empty;
|
|
T max = select_value(etr.Current);
|
|
while (etr.MoveNext()) {
|
|
T val = select_value(etr.Current);
|
|
if (val.CompareTo(max) == 1) max = val;
|
|
}
|
|
return max;
|
|
}
|
|
|
|
public static T Max<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> comparison) =>
|
|
Max(enumerable, select_value, comparison, default);
|
|
|
|
|
|
public static T Max<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> comparison, T return_if_empty) {
|
|
var etr = enumerable.GetEnumerator();
|
|
if (!etr.MoveNext()) return return_if_empty;
|
|
T max = select_value(etr.Current);
|
|
while (etr.MoveNext()) {
|
|
T val = select_value(etr.Current);
|
|
if (comparison(val, max) == 1) max = val;
|
|
}
|
|
return max;
|
|
}
|
|
#endregion
|
|
|
|
#region MaxElement
|
|
public static TElement MaxElement<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value) where T : IComparable<T> =>
|
|
MaxElement(enumerable, select_value, default(TElement));
|
|
|
|
public static TElement MaxElement<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, TElement return_if_empty) where T : IComparable<T> {
|
|
var etr = enumerable.GetEnumerator();
|
|
if (!etr.MoveNext()) return return_if_empty;
|
|
TElement max_el = etr.Current;
|
|
T max = select_value(max_el);
|
|
while (etr.MoveNext()) {
|
|
T val = select_value(etr.Current);
|
|
if (val.CompareTo(max) == 1) {
|
|
max_el = etr.Current;
|
|
max = val;
|
|
}
|
|
}
|
|
return max_el;
|
|
}
|
|
|
|
public static TElement MaxElement<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> comparison) =>
|
|
MaxElement(enumerable, select_value, comparison, default);
|
|
|
|
public static TElement MaxElement<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> comparison, TElement return_if_empty) {
|
|
var etr = enumerable.GetEnumerator();
|
|
if (!etr.MoveNext()) return return_if_empty;
|
|
TElement max_el = etr.Current;
|
|
T max = select_value(etr.Current);
|
|
while (etr.MoveNext()) {
|
|
T val = select_value(etr.Current);
|
|
if (comparison(val, max) == 1) {
|
|
max_el = etr.Current;
|
|
max = val;
|
|
}
|
|
}
|
|
return max_el;
|
|
}
|
|
#endregion
|
|
|
|
#region Sum
|
|
public static uint Sum<T>(this IEnumerable<T> enumerable, Func<T, uint> select) =>
|
|
Sum(enumerable.Select(select));
|
|
|
|
public static uint Sum(this IEnumerable<uint> enumerable) {
|
|
uint sum = 0;
|
|
foreach (uint val in enumerable) sum += val;
|
|
return sum;
|
|
}
|
|
#endregion
|
|
|
|
public static void DisposeAll(this IEnumerable<IDisposable> source) {
|
|
foreach (IDisposable d in source)
|
|
d.Dispose();
|
|
}
|
|
}
|
|
|
|
public static class CollectionExtensions
|
|
{
|
|
#region AddRange
|
|
public static void AddRange<T>(this ICollection<T> collection, params T[] items) {
|
|
foreach (T item in items) collection.Add(item);
|
|
}
|
|
|
|
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> items) {
|
|
foreach (T item in items) collection.Add(item);
|
|
}
|
|
#endregion
|
|
|
|
public static void SetItems<T>(this ICollection<T> collection, IEnumerable<T> items) {
|
|
collection.Clear();
|
|
collection.AddRange(items);
|
|
}
|
|
}
|
|
|
|
public static class DictionaryExtensions
|
|
{
|
|
#region TryGetValue
|
|
public static bool TryGetValue<K, VK1, VK2, V>(this Dictionary<K, Dictionary<VK1, Dictionary<VK2, V>>> target, K k1, VK1 k2, VK2 k3, out V value) {
|
|
if (target.TryGetValue(k1, out Dictionary<VK1, Dictionary<VK2, V>> d1) && d1.TryGetValue(k2, out Dictionary<VK2, V> d2))
|
|
return d2.TryGetValue(k3, out value);
|
|
value = default;
|
|
return false;
|
|
}
|
|
|
|
public static bool TryGetValue<K, VK, V, VKD>(this IDictionary<K, VKD> target, K k1, VK k2, out V value) where VKD : IDictionary<VK, V> {
|
|
if (target.TryGetValue(k1, out VKD d))
|
|
return d.TryGetValue(k2, out value);
|
|
value = default;
|
|
return false;
|
|
}
|
|
#endregion
|
|
|
|
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> add_value_factory, Action<TKey, TValue> update_value) {
|
|
if (!dictionary.TryGetValue(key, out TValue value))
|
|
dictionary.Add(key, add_value_factory(key));
|
|
else update_value(key, value);
|
|
}
|
|
|
|
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> add_value_factory, Func<TKey, TValue, TValue> update_value) {
|
|
if (!dictionary.TryGetValue(key, out TValue value))
|
|
dictionary.Add(key, add_value_factory(key));
|
|
dictionary[key] = update_value(key, value);
|
|
}
|
|
|
|
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> add_value_factory) {
|
|
if (!dictionary.TryGetValue(key, out TValue value))
|
|
dictionary.Add(key, value = add_value_factory(key));
|
|
return value;
|
|
}
|
|
|
|
public static void SetValue<K, VK, V, VKD>(this IDictionary<K, VKD> target, K k1, VK k2, V value) where VKD : IDictionary<VK, V> {
|
|
if (!target.TryGetValue(k1, out VKD d))
|
|
target.Add(k1, d = Activator.CreateInstance<VKD>());
|
|
d[k2] = value;
|
|
}
|
|
|
|
public static IReadOnlyDictionary<TKey, (TValue1, TValue2)> Join<TKey, TValue1, TValue2>(this IReadOnlyDictionary<TKey, TValue1> dictionary, IReadOnlyDictionary<TKey, TValue2> merge_target) =>
|
|
Join(dictionary, merge_target, (v1, v2) => (v1, v2));
|
|
|
|
public static IReadOnlyDictionary<TKey, TResult> Join<TKey, TValue1, TValue2, TResult>(this IReadOnlyDictionary<TKey, TValue1> dictionary, IReadOnlyDictionary<TKey, TValue2> merge_target, Func<TValue1, TValue2, TResult> select) {
|
|
var iks = dictionary.Keys.Intersect(merge_target.Keys);
|
|
return new ReadOnlyDictionary<TKey, TResult>(iks.ToDictionary(k => k, k => select(dictionary[k], merge_target[k])));
|
|
}
|
|
|
|
public static IReadOnlyDictionary<TKey, TResult> LeftJoin<TKey, TValue1, TValue2, TResult>(this IReadOnlyDictionary<TKey, TValue1> dictionary, IReadOnlyDictionary<TKey, TValue2> merge_target,
|
|
Func<TValue1, TValue2, TResult> select, Func<TValue1, TResult> select_left_only) =>
|
|
dictionary.ToDictionary(kv => kv.Key, kv => {
|
|
if (merge_target.TryGetValue(kv.Key, out TValue2 v2))
|
|
return select(kv.Value, v2);
|
|
else
|
|
return select_left_only(kv.Value);
|
|
});
|
|
|
|
public static IReadOnlyDictionary<TKey, (TValue1, TValue2)> SuperJoin<TKey, TValue1, TValue2>(this IReadOnlyDictionary<TKey, TValue1> dictionary, IReadOnlyDictionary<TKey, TValue2> merge_target) {
|
|
var eks1 = dictionary.Keys.Except(merge_target.Keys);
|
|
var eks2 = merge_target.Keys.Except(dictionary.Keys);
|
|
var iks = dictionary.Keys.Intersect(merge_target.Keys);
|
|
return new ReadOnlyDictionary<TKey, (TValue1, TValue2)>(eks1
|
|
.Select(k => (k, dictionary[k], default(TValue2)))
|
|
.Concat(eks2.Select(k => (k, default(TValue1), merge_target[k])))
|
|
.Concat(iks.Select(k => (k, dictionary[k], merge_target[k])))
|
|
.ToDictionary(vals => vals.k, vals => (vals.Item2, vals.Item3))
|
|
);
|
|
}
|
|
}
|
|
|
|
public static class ListExtensions
|
|
{
|
|
#region InserSorted
|
|
public static void InsertSorted<T>(this IList<T> list, T item) =>
|
|
list.InsertSorted(item, Comparer<T>.Default);
|
|
|
|
public static void InsertSorted<T>(this IList<T> list, T item, IComparer<T> comparer) {
|
|
for (int i = 0; i < list.Count; i++)
|
|
if (comparer.Compare(list[i], item) > 0) {
|
|
list.Insert(i, item);
|
|
return;
|
|
}
|
|
list.Add(item);
|
|
}
|
|
#endregion
|
|
|
|
#region Pop
|
|
public static T Pop<T>(this IList<T> list, int index) {
|
|
T rv = list.ElementAt(index);
|
|
list.RemoveAt(index);
|
|
return rv;
|
|
}
|
|
|
|
public static bool Pop<T, TKey>(this IList<T> list, TKey key, Func<T, TKey> key_selector, out T result) {
|
|
for(int i = 0; i < list.Count; i++) {
|
|
if (key_selector(list[i]).Equals(key)) {
|
|
result = list[i];
|
|
return true;
|
|
}
|
|
}
|
|
result = default(T);
|
|
return false;
|
|
}
|
|
#endregion
|
|
|
|
public static bool PopDispose<T, TKey>(this IList<T> list, TKey key, Func<T, TKey> key_selector) where T : IDisposable {
|
|
if (list.Pop(key, key_selector, out T result)) {
|
|
result.Dispose();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace System.Collections.ObjectModel
|
|
{
|
|
public static class ObservableCollectionExtensions
|
|
{
|
|
public static int RemoveAll<T>(this ObservableCollection<T> collection, Func<T, bool> match) {
|
|
if (collection == null) return 0;
|
|
// In terms of allocations we could do this more efficiently by copying items from the end of the
|
|
// collection in (see https://referencesource.microsoft.com/#q=RemoveAll) but that would allow
|
|
// transient duplication in the list which could be observed from the outside
|
|
// Better to generate Remove events rather than move+delete events
|
|
var to_remove = collection.Where(match).ToList();
|
|
foreach (var item in to_remove) {
|
|
collection.Remove(item);
|
|
}
|
|
return to_remove.Count;
|
|
}
|
|
|
|
public static void ReflectChange<T, TData>(this ObservableCollection<T> target, IEnumerable<TData> source, Func<TData, T> create_item, NotifyCollectionChangedEventArgs e) =>
|
|
ReflectChange(target, source, create_item, null, e);
|
|
|
|
public static void ReflectChange<T, TData>(this ObservableCollection<T> target, IEnumerable<TData> source, Func<TData, T> create_item, Action<T> item_destroyed, NotifyCollectionChangedEventArgs e) {
|
|
try {
|
|
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: target.Move(e.OldStartingIndex, e.NewStartingIndex); break;
|
|
case NotifyCollectionChangedAction.Remove:
|
|
for (int x = 0; x < e.OldItems.Count; x++) {
|
|
T item = target[e.OldStartingIndex];
|
|
target.RemoveAt(e.OldStartingIndex);
|
|
item_destroyed?.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);
|
|
item_destroyed?.Invoke(old_item);
|
|
}
|
|
break;
|
|
case NotifyCollectionChangedAction.Reset:
|
|
var old_views = target.ToArray();
|
|
target.Clear();
|
|
if (item_destroyed != null) foreach (T item in old_views) item_destroyed(item);
|
|
foreach (TData d in source) target.Add(create_item(d));
|
|
break;
|
|
}
|
|
}catch (Exception ex) {
|
|
if (ex == null) {
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IObservable<IReadOnlyList<T>> ObserveSelf<T>(this ObservableCollection<T> collection) =>
|
|
Observable.FromEvent<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(cb => (s, e) => cb(e), h => collection.CollectionChanged += h, h => collection.CollectionChanged -= h).Select(_ => collection);
|
|
|
|
public static IObservable<IReadOnlyList<T>> ObserveSelf<T>(this ObservableCollection<T> collection, bool start_with_current) {
|
|
var src = ObserveSelf(collection);
|
|
return start_with_current? src.StartWith(collection) : src;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace mbts.collections.extensions
|
|
{
|
|
public static class HashSetExtensions
|
|
{
|
|
public static void sync_with<T>(this HashSet<T> set, IEnumerable<T> values) {
|
|
set.RemoveWhere(v => !values.Contains(v));
|
|
var adds = values.Distinct().Except(set).ToList();
|
|
foreach (var a in adds)
|
|
set.Add(a);
|
|
}
|
|
}
|
|
|
|
public static class ListExtensions
|
|
{
|
|
#region RemoveWhere
|
|
public static void RemoveWhere<T>(this IList<T> list, Func<T, bool> where) { foreach (var r in list.Where(where).ToList()) list.Remove(r); }
|
|
|
|
public static void RemoveWhere<T>(this IList<T> list, Func<T, bool> where, Action<T> on_remove) {
|
|
int count = list.Count;
|
|
for (int i = 0; i < count; i++) {
|
|
var t = list[i];
|
|
if (where(t)) {
|
|
list.RemoveAt(i--);
|
|
--count;
|
|
on_remove(t);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static Random rng = new Random();
|
|
|
|
public static void Shuffle<T>(this IList<T> list) {
|
|
int n = list.Count;
|
|
while (n > 1) {
|
|
n--;
|
|
int k = rng.Next(n + 1);
|
|
T value = list[k];
|
|
list[k] = list[n];
|
|
list[n] = value;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
public static class DictionaryExtensions
|
|
{
|
|
#region GetValueOrDefault
|
|
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key) =>
|
|
dictionary.TryGetValue(key, out TValue result) ? result : default;
|
|
|
|
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) =>
|
|
dictionary.TryGetValue(key, out TValue result) ? result : default;
|
|
|
|
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue default_value) =>
|
|
dictionary.TryGetValue(key, out TValue result) ? result : default_value;
|
|
|
|
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> default_factory) =>
|
|
dictionary.TryGetValue(key, out TValue result) ? result : default_factory(key);
|
|
#endregion
|
|
|
|
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Action<TValue> update_existing, Func<TValue> create_new) {
|
|
if (dictionary.TryGetValue(key, out TValue curr)) update_existing(curr);
|
|
else dictionary.Add(key, create_new());
|
|
}
|
|
|
|
// this overload prevents 'ambiguous calls' compilation errors due to Dictionary implementing IDictionary, and IReadOnlyDictionary.
|
|
public static void sync_with<TKey, TValue, TSourceValue>(this IDictionary<TKey, TValue> dictionary, Dictionary<TKey, TSourceValue> source, Func<TKey, TSourceValue, TValue> selector) =>
|
|
_sync_with(dictionary, source, selector);
|
|
|
|
public static void sync_with<TKey, TValue, TSourceValue>(this IDictionary<TKey, TValue> dictionary, IDictionary<TKey, TSourceValue> source, Func<TKey, TSourceValue, TValue> selector) =>
|
|
_sync_with(dictionary, source, selector);
|
|
|
|
public static void sync_with<TKey, TValue, TSourceValue>(this IDictionary<TKey, TValue> dictionary, IReadOnlyDictionary<TKey, TSourceValue> source, Func<TKey, TSourceValue, TValue> selector) =>
|
|
_sync_with_ro(dictionary, source, selector);
|
|
|
|
static void _sync_with<TKey, TValue, TSourceValue>(this IDictionary<TKey, TValue> dictionary, IDictionary<TKey, TSourceValue> source, Func<TKey, TSourceValue, TValue> selector) {
|
|
var removes = dictionary.Keys.Except(source.Keys).ToList();
|
|
foreach (var k in removes)
|
|
dictionary.Remove(k);
|
|
var adds = source.Except(dictionary.Keys, kv => kv.Key).ToList();
|
|
foreach (var kv in adds)
|
|
dictionary.Add(kv.Key, selector(kv.Key, kv.Value));
|
|
}
|
|
|
|
static void _sync_with_ro<TKey, TValue, TSourceValue>(this IDictionary<TKey, TValue> dictionary, IReadOnlyDictionary<TKey, TSourceValue> source, Func<TKey, TSourceValue, TValue> selector) {
|
|
var removes = dictionary.Keys.Except(source.Keys).ToList();
|
|
foreach (var k in removes)
|
|
dictionary.Remove(k);
|
|
var adds = source.Except(dictionary.Keys, kv => kv.Key).ToList();
|
|
foreach (var kv in adds)
|
|
dictionary.Add(kv.Key, selector(kv.Key, kv.Value));
|
|
}
|
|
}
|
|
} |