blob: 0b13434a76c87fe0de138d9dafbca512efc440b7 (
plain)
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
|
using System.Collections.Generic;
using System.Linq;
namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
{
/// <summary>A watcher which detects changes to a collection of values using a specified <see cref="IEqualityComparer{T}"/> instance.</summary>
/// <typeparam name="TValue">The value type within the collection.</typeparam>
internal class ComparableListWatcher<TValue> : BaseDisposableWatcher, ICollectionWatcher<TValue>
{
/*********
** Fields
*********/
/// <summary>The collection to watch.</summary>
private readonly ICollection<TValue> CurrentValues;
/// <summary>The values during the previous update.</summary>
private HashSet<TValue> LastValues;
/// <summary>The pairs added since the last reset.</summary>
private readonly List<TValue> AddedImpl = new();
/// <summary>The pairs removed since the last reset.</summary>
private readonly List<TValue> RemovedImpl = new();
/*********
** Accessors
*********/
/// <inheritdoc />
public string Name { get; }
/// <inheritdoc />
public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0;
/// <inheritdoc />
public IEnumerable<TValue> Added => this.AddedImpl;
/// <inheritdoc />
public IEnumerable<TValue> Removed => this.RemovedImpl;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="name">A name which identifies what the watcher is watching, used for troubleshooting.</param>
/// <param name="values">The collection to watch.</param>
/// <param name="comparer">The equality comparer which indicates whether two values are the same.</param>
public ComparableListWatcher(string name, ICollection<TValue> values, IEqualityComparer<TValue> comparer)
{
this.Name = name;
this.CurrentValues = values;
this.LastValues = new HashSet<TValue>(comparer);
}
/// <inheritdoc />
public void Update()
{
this.AssertNotDisposed();
// optimize for zero items
if (this.CurrentValues.Count == 0)
{
if (this.LastValues.Count > 0)
{
this.AddedImpl.AddRange(this.LastValues);
this.LastValues.Clear();
}
return;
}
// detect changes
HashSet<TValue> curValues = new HashSet<TValue>(this.CurrentValues, this.LastValues.Comparer);
this.RemovedImpl.AddRange(from value in this.LastValues where !curValues.Contains(value) select value);
this.AddedImpl.AddRange(from value in curValues where !this.LastValues.Contains(value) select value);
this.LastValues = curValues;
}
/// <inheritdoc />
public void Reset()
{
this.AssertNotDisposed();
this.AddedImpl.Clear();
this.RemovedImpl.Clear();
}
}
}
|