Repository navigation
Expand file tree
/
Copy pathMaxEx.cs
More file actions
181 lines (160 loc) · 7.84 KB
/
Copy pathMaxEx.cs
File metadata and controls
181 lines (160 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved.
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Reactive.Linq;
namespace DynamicData.Aggregation;
/// <summary>
/// Maximum and minimum value extensions.
/// </summary>
public static class MaxEx
{
private enum MaxOrMin
{
Max,
Min
}
/// <summary>
/// Continually calculates the maximum value from the underlying data source.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="source">The source.</param>
/// <param name="valueSelector">The value selector.</param>
/// <param name="emptyValue">The value to use when the underlying collection is empty.</param>
/// <returns>
/// A distinct observable of the maximum item.
/// </returns>
public static IObservable<TResult> Maximum<TObject, TResult>(this IObservable<IChangeSet<TObject>> source, Func<TObject, TResult> valueSelector, TResult emptyValue = default)
where TObject : notnull
where TResult : struct, IComparable<TResult> => source.ToChangesAndCollection().Calculate(valueSelector, MaxOrMin.Max, emptyValue);
/// <summary>
/// Continually calculates the maximum value from the underlying data source.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="source">The source.</param>
/// <param name="valueSelector">The value selector.</param>
/// <param name="emptyValue">The value to use when the underlying collection is empty.</param>
/// <returns>
/// A distinct observable of the maximum item.
/// </returns>
public static IObservable<TResult> Maximum<TObject, TKey, TResult>(this IObservable<IChangeSet<TObject, TKey>> source, Func<TObject, TResult> valueSelector, TResult emptyValue = default)
where TObject : notnull
where TKey : notnull
where TResult : struct, IComparable<TResult> => source.ToChangesAndCollection().Calculate(valueSelector, MaxOrMin.Max, emptyValue);
/// <summary>
/// Continually calculates the minimum value from the underlying data source.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="source">The source.</param>
/// <param name="valueSelector">The value selector.</param>
/// <param name="emptyValue">The value to use when the underlying collection is empty.</param>
/// <returns>A distinct observable of the minimums item.</returns>
public static IObservable<TResult> Minimum<TObject, TResult>(this IObservable<IChangeSet<TObject>> source, Func<TObject, TResult> valueSelector, TResult emptyValue = default)
where TObject : notnull
where TResult : struct, IComparable<TResult> => source.ToChangesAndCollection().Calculate(valueSelector, MaxOrMin.Min, emptyValue);
/// <summary>
/// Continually calculates the minimum value from the underlying data source.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="source">The source.</param>
/// <param name="valueSelector">The value selector.</param>
/// <param name="emptyValue">The value to use when the underlying collection is empty.</param>
/// <returns>
/// A distinct observable of the minimums item.
/// </returns>
public static IObservable<TResult> Minimum<TObject, TKey, TResult>(this IObservable<IChangeSet<TObject, TKey>> source, Func<TObject, TResult> valueSelector, TResult emptyValue = default)
where TObject : notnull
where TKey : notnull
where TResult : struct, IComparable<TResult> => source.ToChangesAndCollection().Calculate(valueSelector, MaxOrMin.Min, emptyValue);
private static IObservable<TResult> Calculate<TObject, TResult>(this IObservable<ChangesAndCollection<TObject>> source, Func<TObject, TResult> valueSelector, MaxOrMin maxOrMin, TResult emptyValue = default)
where TResult : struct, IComparable<TResult>
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
valueSelector.ThrowArgumentNullExceptionIfNull(nameof(valueSelector));
return source.Scan(
default(TResult?),
(state, latest) =>
{
var current = state;
var requiresReset = false;
foreach (var change in latest.Changes)
{
var value = valueSelector(change.Item);
current ??= value;
if (change.Type == AggregateType.Add)
{
if (maxOrMin is MaxOrMin.Max)
{
if (value.CompareTo(current.Value) > 0)
{
current = value;
}
}
else if (value.CompareTo(current.Value) < 0)
{
current = value;
}
}
else
{
// check whether the max / min has been removed. If so we need to look
// up the latest from the underlying collection
if (value.CompareTo(current.Value) != 0)
{
continue;
}
requiresReset = true;
break;
}
}
if (requiresReset)
{
var collection = latest.Collection;
if (collection.Count == 0)
{
current = default;
}
else
{
current = maxOrMin == MaxOrMin.Max ? collection.Max(valueSelector) : collection.Min(valueSelector);
}
}
return current;
}).Select(t => t ?? emptyValue).DistinctUntilChanged();
}
private static IObservable<ChangesAndCollection<TObject>> ToChangesAndCollection<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source)
where TObject : notnull
where TKey : notnull
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
return source.Publish(
shared =>
{
var changes = shared.ForAggregation();
var data = shared.ToCollection();
return data.Zip(changes, (d, c) => new ChangesAndCollection<TObject>(c, d));
});
}
private static IObservable<ChangesAndCollection<TObject>> ToChangesAndCollection<TObject>(this IObservable<IChangeSet<TObject>> source)
where TObject : notnull
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
return source.Publish(
shared =>
{
var changes = shared.ForAggregation();
var data = shared.ToCollection();
return data.Zip(changes, (d, c) => new ChangesAndCollection<TObject>(c, d));
});
}
private sealed class ChangesAndCollection<T>(IAggregateChangeSet<T> changes, IReadOnlyCollection<T> collection)
{
public IAggregateChangeSet<T> Changes { get; } = changes;
public IReadOnlyCollection<T> Collection { get; } = collection;
}
}