Skip to content

Commit

Permalink
breaking changes, ISynchronizedView<T> -> ISynchronizedView<T, TView>
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Oct 18, 2024
1 parent bd7cc35 commit 73c5b8e
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/ObservableCollections/IObservableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public enum RejectedViewChangedAction
public interface ISynchronizedView<T, TView> : IReadOnlyCollection<TView>, IDisposable
{
object SyncRoot { get; }
ISynchronizedViewFilter<T> Filter { get; }
ISynchronizedViewFilter<T, TView> Filter { get; }
IEnumerable<(T Value, TView View)> Filtered { get; }
IEnumerable<(T Value, TView View)> Unfiltered { get; }
int UnfilteredCount { get; }
Expand All @@ -44,7 +44,7 @@ public interface ISynchronizedView<T, TView> : IReadOnlyCollection<TView>, IDisp
event Action<RejectedViewChangedAction, int, int>? RejectedViewChanged;
event Action<NotifyCollectionChangedAction>? CollectionStateChanged;

void AttachFilter(ISynchronizedViewFilter<T> filter);
void AttachFilter(ISynchronizedViewFilter<T, TView> filter);
void ResetFilter();
ISynchronizedViewList<TView> ToViewList();
NotifyCollectionChangedSynchronizedViewList<TView> ToNotifyCollectionChanged();
Expand Down
28 changes: 21 additions & 7 deletions src/ObservableCollections/ISynchronizedViewFilter.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
using System;
using System.Collections.Specialized;

namespace ObservableCollections
{
// Obsolete...
[Obsolete("this interface is obsoleted. Use ISynchronizedViewFilter<T, TView> instead.")]
public interface ISynchronizedViewFilter<T>
{
bool IsMatch(T value);
}

public class SynchronizedViewFilter<T>(Func<T, bool> isMatch) : ISynchronizedViewFilter<T>
public interface ISynchronizedViewFilter<T, TView>
{
public static readonly ISynchronizedViewFilter<T> Null = new NullViewFilter();
bool IsMatch(T value, TView view);
}

public bool IsMatch(T value) => isMatch(value);
internal class SynchronizedViewValueOnlyFilter<T, TView>(Func<T, bool> isMatch) : ISynchronizedViewFilter<T, TView>
{
public bool IsMatch(T value, TView view) => isMatch(value);

class NullViewFilter : ISynchronizedViewFilter<T>
class NullViewFilter : ISynchronizedViewFilter<T, TView>
{
public bool IsMatch(T value) => true;
public bool IsMatch(T value, TView view) => true;
}
}


public class SynchronizedViewFilter<T, TView>(Func<T, TView, bool> isMatch) : ISynchronizedViewFilter<T, TView>
{
public static readonly ISynchronizedViewFilter<T, TView> Null = new NullViewFilter();

public bool IsMatch(T value, TView view) => isMatch(value, view);

class NullViewFilter : ISynchronizedViewFilter<T, TView>
{
public bool IsMatch(T value, TView view) => true;
}
}
}
16 changes: 8 additions & 8 deletions src/ObservableCollections/ObservableDictionary.Views.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class View<TView> : ISynchronizedView<KeyValuePair<TKey, TValue>, TView>
{
readonly ObservableDictionary<TKey, TValue> source;
readonly Func<KeyValuePair<TKey, TValue>, TView> selector;
ISynchronizedViewFilter<KeyValuePair<TKey, TValue>> filter;
ISynchronizedViewFilter<KeyValuePair<TKey, TValue>, TView> filter;
readonly Dictionary<TKey, (TValue, TView)> dict;
int filteredCount;

public View(ObservableDictionary<TKey, TValue> source, Func<KeyValuePair<TKey, TValue>, TView> selector)
{
this.source = source;
this.selector = selector;
this.filter = SynchronizedViewFilter<KeyValuePair<TKey, TValue>>.Null;
this.filter = SynchronizedViewFilter<KeyValuePair<TKey, TValue>, TView>.Null;
this.SyncRoot = new object();
lock (source.SyncRoot)
{
Expand All @@ -42,7 +42,7 @@ public View(ObservableDictionary<TKey, TValue> source, Func<KeyValuePair<TKey, T
public event Action<RejectedViewChangedAction, int, int>? RejectedViewChanged;
public event Action<NotifyCollectionChangedAction>? CollectionStateChanged;

public ISynchronizedViewFilter<KeyValuePair<TKey, TValue>> Filter
public ISynchronizedViewFilter<KeyValuePair<TKey, TValue>, TView> Filter
{
get { lock (SyncRoot) return filter; }
}
Expand Down Expand Up @@ -74,7 +74,7 @@ public void Dispose()
this.source.CollectionChanged -= SourceCollectionChanged;
}

public void AttachFilter(ISynchronizedViewFilter<KeyValuePair<TKey, TValue>> filter)
public void AttachFilter(ISynchronizedViewFilter<KeyValuePair<TKey, TValue>, TView> filter)
{
if (filter.IsNullFilter())
{
Expand All @@ -89,7 +89,7 @@ public void AttachFilter(ISynchronizedViewFilter<KeyValuePair<TKey, TValue>> fil
foreach (var v in dict)
{
var value = new KeyValuePair<TKey, TValue>(v.Key, v.Value.Item1);
if (filter.IsMatch(value))
if (filter.IsMatch(value, v.Value.Item2))
{
filteredCount++;
}
Expand All @@ -103,7 +103,7 @@ public void ResetFilter()
{
lock (SyncRoot)
{
this.filter = SynchronizedViewFilter<KeyValuePair<TKey, TValue>>.Null;
this.filter = SynchronizedViewFilter<KeyValuePair<TKey, TValue>, TView>.Null;
this.filteredCount = dict.Count;
ViewChanged?.Invoke(new SynchronizedViewChangedEventArgs<KeyValuePair<TKey, TValue>, TView>(NotifyCollectionChangedAction.Reset, true));
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public IEnumerator<TView> GetEnumerator()
foreach (var item in dict)
{
var v = (new KeyValuePair<TKey, TValue>(item.Key, item.Value.Item1), item.Value.Item2);
if (filter.IsMatch(v.Item1))
if (filter.IsMatch(v))
{
yield return v.Item2;
}
Expand All @@ -150,7 +150,7 @@ public IEnumerator<TView> GetEnumerator()
foreach (var item in dict)
{
var v = (new KeyValuePair<TKey, TValue>(item.Key, item.Value.Item1), item.Value.Item2);
if (filter.IsMatch(v.Item1))
if (filter.IsMatch(v))
{
yield return v;
}
Expand Down
16 changes: 8 additions & 8 deletions src/ObservableCollections/ObservableHashSet.Views.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ISynchronizedView<T, TView> CreateView<TView>(Func<T, TView> transform)

sealed class View<TView> : ISynchronizedView<T, TView>
{
public ISynchronizedViewFilter<T> Filter
public ISynchronizedViewFilter<T, TView> Filter
{
get { lock (SyncRoot) return filter; }
}
Expand All @@ -27,7 +27,7 @@ public ISynchronizedViewFilter<T> Filter
readonly Dictionary<T, (T, TView)> dict;
int filteredCount;

ISynchronizedViewFilter<T> filter;
ISynchronizedViewFilter<T, TView> filter;

public event NotifyViewChangedEventHandler<T, TView>? ViewChanged;
public event Action<RejectedViewChangedAction, int, int>? RejectedViewChanged;
Expand All @@ -39,7 +39,7 @@ public View(ObservableHashSet<T> source, Func<T, TView> selector)
{
this.source = source;
this.selector = selector;
this.filter = SynchronizedViewFilter<T>.Null;
this.filter = SynchronizedViewFilter<T, TView>.Null;
this.SyncRoot = new object();
lock (source.SyncRoot)
{
Expand Down Expand Up @@ -71,7 +71,7 @@ public int UnfilteredCount
}
}

public void AttachFilter(ISynchronizedViewFilter<T> filter)
public void AttachFilter(ISynchronizedViewFilter<T, TView> filter)
{
if (filter.IsNullFilter())
{
Expand All @@ -85,7 +85,7 @@ public void AttachFilter(ISynchronizedViewFilter<T> filter)
this.filteredCount = 0;
foreach (var (_, (value, view)) in dict)
{
if (filter.IsMatch(value))
if (filter.IsMatch(value, view))
{
filteredCount++;
}
Expand All @@ -98,7 +98,7 @@ public void ResetFilter()
{
lock (SyncRoot)
{
this.filter = SynchronizedViewFilter<T>.Null;
this.filter = SynchronizedViewFilter<T, TView>.Null;
this.filteredCount = dict.Count;
ViewChanged?.Invoke(new SynchronizedViewChangedEventArgs<T, TView>(NotifyCollectionChangedAction.Reset, true));
}
Expand All @@ -125,7 +125,7 @@ public IEnumerator<TView> GetEnumerator()
{
foreach (var item in dict)
{
if (filter.IsMatch(item.Value.Item1))
if (filter.IsMatch(item.Value))
{
yield return item.Value.Item2;
}
Expand All @@ -143,7 +143,7 @@ public IEnumerator<TView> GetEnumerator()
{
foreach (var item in dict)
{
if (filter.IsMatch(item.Value.Item1))
if (filter.IsMatch(item.Value))
{
yield return item.Value;
}
Expand Down
20 changes: 10 additions & 10 deletions src/ObservableCollections/ObservableList.Views.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public NotifyCollectionChangedSynchronizedViewList<TView> ToWritableNotifyCollec

internal sealed class View<TView> : ISynchronizedView<T, TView>, IWritableSynchronizedView<T, TView>
{
public ISynchronizedViewFilter<T> Filter
public ISynchronizedViewFilter<T, TView> Filter
{
get
{
Expand All @@ -61,7 +61,7 @@ public ISynchronizedViewFilter<T> Filter
internal readonly List<(T, TView)> list; // unsafe, be careful to use
int filteredCount;

ISynchronizedViewFilter<T> filter;
ISynchronizedViewFilter<T, TView> filter;

public event NotifyViewChangedEventHandler<T, TView>? ViewChanged;
public event Action<RejectedViewChangedAction, int, int>? RejectedViewChanged;
Expand All @@ -73,7 +73,7 @@ public View(ObservableList<T> source, Func<T, TView> selector)
{
this.source = source;
this.selector = selector;
this.filter = SynchronizedViewFilter<T>.Null;
this.filter = SynchronizedViewFilter<T, TView>.Null;
this.SyncRoot = new object();
lock (source.SyncRoot)
{
Expand Down Expand Up @@ -105,7 +105,7 @@ public int UnfilteredCount
}
}

public void AttachFilter(ISynchronizedViewFilter<T> filter)
public void AttachFilter(ISynchronizedViewFilter<T, TView> filter)
{
if (filter.IsNullFilter())
{
Expand All @@ -119,7 +119,7 @@ public void AttachFilter(ISynchronizedViewFilter<T> filter)
this.filteredCount = 0;
for (var i = 0; i < list.Count; i++)
{
if (filter.IsMatch(list[i].Item1))
if (filter.IsMatch(list[i]))
{
filteredCount++;
}
Expand All @@ -133,7 +133,7 @@ public void ResetFilter()
{
lock (SyncRoot)
{
this.filter = SynchronizedViewFilter<T>.Null;
this.filter = SynchronizedViewFilter<T, TView>.Null;
this.filteredCount = list.Count;
ViewChanged?.Invoke(new SynchronizedViewChangedEventArgs<T, TView>(NotifyCollectionChangedAction.Reset, true));
}
Expand All @@ -160,7 +160,7 @@ public IEnumerator<TView> GetEnumerator()
{
foreach (var item in list)
{
if (filter.IsMatch(item.Item1))
if (filter.IsMatch(item))
{
yield return item.Item2;
}
Expand All @@ -178,7 +178,7 @@ public IEnumerator<TView> GetEnumerator()
{
foreach (var item in list)
{
if (filter.IsMatch(item.Item1))
if (filter.IsMatch(item))
{
yield return item;
}
Expand Down Expand Up @@ -235,7 +235,7 @@ private void SourceCollectionChanged(in NotifyCollectionChangedEventArgs<T> e)
var view = selector(item);
views.Span[i] = view;
valueViews.Span[i] = (item, view);
var isMatch = matches.Span[i] = Filter.IsMatch(item);
var isMatch = matches.Span[i] = Filter.IsMatch(item, view);
if (isMatch)
{
filteredCount++; // increment in this process
Expand Down Expand Up @@ -271,7 +271,7 @@ private void SourceCollectionChanged(in NotifyCollectionChangedEventArgs<T> e)
var item = list[i];
values.Span[j] = item.Item1;
views.Span[j] = item.Item2;
var isMatch = matches.Span[j] = Filter.IsMatch(item.Item1);
var isMatch = matches.Span[j] = Filter.IsMatch(item);
if (isMatch)
{
filteredCount--; // decrement in this process
Expand Down
16 changes: 8 additions & 8 deletions src/ObservableCollections/ObservableQueue.Views.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class View<TView> : ISynchronizedView<T, TView>
protected readonly Queue<(T, TView)> queue;
int filteredCount;

ISynchronizedViewFilter<T> filter;
ISynchronizedViewFilter<T, TView> filter;

public event NotifyViewChangedEventHandler<T, TView>? ViewChanged;
public event Action<RejectedViewChangedAction, int, int>? RejectedViewChanged;
public event Action<NotifyCollectionChangedAction>? CollectionStateChanged;

public object SyncRoot { get; }

public ISynchronizedViewFilter<T> Filter
public ISynchronizedViewFilter<T, TView> Filter
{
get { lock (SyncRoot) return filter; }
}
Expand All @@ -39,7 +39,7 @@ public View(ObservableQueue<T> source, Func<T, TView> selector)
{
this.source = source;
this.selector = selector;
this.filter = SynchronizedViewFilter<T>.Null;
this.filter = SynchronizedViewFilter<T, TView>.Null;
this.SyncRoot = new object();
lock (source.SyncRoot)
{
Expand Down Expand Up @@ -71,7 +71,7 @@ public int UnfilteredCount
}
}

public void AttachFilter(ISynchronizedViewFilter<T> filter)
public void AttachFilter(ISynchronizedViewFilter<T, TView> filter)
{
if (filter.IsNullFilter())
{
Expand All @@ -85,7 +85,7 @@ public void AttachFilter(ISynchronizedViewFilter<T> filter)
this.filteredCount = 0;
foreach (var (value, view) in queue)
{
if (filter.IsMatch(value))
if (filter.IsMatch(value, view))
{
filteredCount++;
}
Expand All @@ -98,7 +98,7 @@ public void ResetFilter()
{
lock (SyncRoot)
{
this.filter = SynchronizedViewFilter<T>.Null;
this.filter = SynchronizedViewFilter<T, TView>.Null;
this.filteredCount = queue.Count;
ViewChanged?.Invoke(new SynchronizedViewChangedEventArgs<T, TView>(NotifyCollectionChangedAction.Reset, true));
}
Expand All @@ -125,7 +125,7 @@ public IEnumerator<TView> GetEnumerator()
{
foreach (var item in queue)
{
if (filter.IsMatch(item.Item1))
if (filter.IsMatch(item))
{
yield return item.Item2;
}
Expand All @@ -143,7 +143,7 @@ public IEnumerator<TView> GetEnumerator()
{
foreach (var item in queue)
{
if (filter.IsMatch(item.Item1))
if (filter.IsMatch(item))
{
yield return item;
}
Expand Down
Loading

0 comments on commit 73c5b8e

Please sign in to comment.