forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTInt.cs
More file actions
71 lines (58 loc) · 2.23 KB
/
TInt.cs
File metadata and controls
71 lines (58 loc) · 2.23 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Linq;
namespace System
{
// A wrapped integer that invokes a custom delegate every time IEquatable<TInt>.Equals() is invoked.
internal struct TInt : IEquatable<TInt>, IComparable<TInt>
{
public TInt(int value)
: this(value, (Action<int, int>)null)
{
// This constructor does not report comparisons but is still useful for catching uses of the boxing Equals().
}
public TInt(int value, Action<int, int> onCompare)
{
Value = value;
_onCompare = onCompare;
}
public TInt(int value, TIntLog log)
{
Value = value;
_onCompare = (x, y) => log.Add(x, y);
}
public bool Equals(TInt other)
{
_onCompare?.Invoke(Value, other.Value);
return Value == other.Value;
}
public int CompareTo(TInt other)
{
_onCompare?.Invoke(Value, other.Value);
return Value.CompareTo(other.Value);
}
#pragma warning disable 0809 // Obsolete member 'TInt.Equals(object)' overrides non-obsolete member 'object.Equals(object)'
[Obsolete("Don't call this. Call IEquatable<T>.Equals(T)")]
public override bool Equals(object obj)
{
throw new NotSupportedException("Unexpected use of boxing Equals().");
}
#pragma warning restore 0809
public override int GetHashCode() => Value.GetHashCode();
public override string ToString()
{
return Value.ToString();
}
public int Value { get; }
private Action<int, int> _onCompare;
}
internal sealed class TIntLog
{
public void Add(int x, int y) => _log.Add(Tuple.Create(x, y));
public int Count => _log.Count;
public int CountCompares(int x, int y) => _log.Where(t => (t.Item1 == x && t.Item2 == y) || (t.Item1 == y && t.Item2 == x)).Count();
private List<Tuple<int, int>> _log = new List<Tuple<int, int>>();
}
}