IEquatable impl for IPredicate impls
This commit is contained in:
1 parent
50f6ce9fb4
commit
25dfa9a0a2
1 file changed
+151
-40
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace mbts {
|
||||
interface IPredicate {
|
||||
@@ -97,7 +98,7 @@ namespace mbts {
|
||||
}
|
||||
|
||||
#region Implementation
|
||||
class EqualsTrigger<T> : IPredicate<T> {
|
||||
class EqualsTrigger<T> : IPredicate<T>, IEquatable<EqualsTrigger<T>> {
|
||||
readonly T _value;
|
||||
readonly IEqualityComparer<T> _eq_comparer;
|
||||
|
||||
@@ -106,12 +107,26 @@ namespace mbts {
|
||||
_eq_comparer = eq_comparer;
|
||||
}
|
||||
|
||||
public bool trigger(T value) => _eq_comparer.Equals(value, _value);
|
||||
public bool trigger(T value) =>
|
||||
_eq_comparer.Equals(value, _value);
|
||||
|
||||
public override string ToString() => $"EqualsTrigger ({typeof(T)})";
|
||||
public override string ToString() =>
|
||||
$"EqualsTrigger ({typeof(T)})";
|
||||
|
||||
public bool Equals(EqualsTrigger<T> other) =>
|
||||
other != null &&
|
||||
// check equality with both IEqualityComparers
|
||||
_eq_comparer.Equals(_value, other._value) &&
|
||||
other._eq_comparer.Equals(_value, other._value);
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
Equals(obj as EqualsTrigger<T>);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
_eq_comparer.GetHashCode(_value);
|
||||
}
|
||||
|
||||
class AndTrigger<T> : IPredicate<T> {
|
||||
class AndTrigger<T> : IPredicate<T>, IEquatable<AndTrigger<T>> {
|
||||
readonly IPredicate<T> _first, _second;
|
||||
|
||||
public AndTrigger(IPredicate<T> first, IPredicate<T> second) {
|
||||
@@ -119,18 +134,28 @@ namespace mbts {
|
||||
_second = second;
|
||||
}
|
||||
|
||||
public bool trigger(T value) => _first.trigger(value) && _second.trigger(value);
|
||||
public bool trigger(T value) =>
|
||||
_first.trigger(value) &&
|
||||
_second.trigger(value);
|
||||
|
||||
public override string ToString() => $"AndTrigger ({_first}, {_second})";
|
||||
public bool Equals(AndTrigger<T> other) =>
|
||||
other != null &&
|
||||
_first.Equals(other._first) &&
|
||||
_second.Equals(other._second);
|
||||
|
||||
public override string ToString() =>
|
||||
$"AndTrigger ({_first}, {_second})";
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
obj is AndTrigger<T> t && _first.Equals(t._first) && _second.Equals(t._second);
|
||||
Equals(obj as AndTrigger<T>);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
EqualityComparer.aggregate_hash(_first.GetHashCode(), _second.GetHashCode());
|
||||
EqualityComparer.aggregate_hash(
|
||||
_first.GetHashCode(),
|
||||
_second.GetHashCode());
|
||||
}
|
||||
|
||||
class AndTrigger<T1, T2> : IPredicate<(T1, T2)> {
|
||||
class AndTrigger<T1, T2> : IPredicate<(T1, T2)>, IEquatable<AndTrigger<T1, T2>> {
|
||||
readonly IPredicate<T1> _first;
|
||||
readonly IPredicate<T2> _second;
|
||||
|
||||
@@ -139,18 +164,28 @@ namespace mbts {
|
||||
_second = second;
|
||||
}
|
||||
|
||||
public bool trigger((T1, T2) value) => _first.trigger(value.Item1) && _second.trigger(value.Item2);
|
||||
public bool trigger((T1, T2) value) =>
|
||||
_first.trigger(value.Item1) &&
|
||||
_second.trigger(value.Item2);
|
||||
|
||||
public bool Equals(AndTrigger<T1, T2> other) =>
|
||||
other != null &&
|
||||
_first.Equals(other._first) &&
|
||||
_second.Equals(other._second);
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
obj is AndTrigger<T1, T2> t && _first.Equals(t._first) && _second.Equals(t._second);
|
||||
Equals(obj as AndTrigger<T1, T2>);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
EqualityComparer.aggregate_hash(_first.GetHashCode(), _second.GetHashCode());
|
||||
EqualityComparer.aggregate_hash(
|
||||
_first.GetHashCode(),
|
||||
_second.GetHashCode());
|
||||
|
||||
public override string ToString() => $"AndTrigger ({_first}, {_second})";
|
||||
public override string ToString() =>
|
||||
$"AndTrigger ({_first}, {_second})";
|
||||
}
|
||||
|
||||
class OrTrigger<T> : IPredicate<T> {
|
||||
class OrTrigger<T> : IPredicate<T>, IEquatable<OrTrigger<T>> {
|
||||
readonly IPredicate<T> _first, _second;
|
||||
|
||||
public OrTrigger(IPredicate<T> first, IPredicate<T> second) {
|
||||
@@ -160,16 +195,24 @@ namespace mbts {
|
||||
|
||||
public bool trigger(T value) => _first.trigger(value) || _second.trigger(value);
|
||||
|
||||
public bool Equals(OrTrigger<T> other) =>
|
||||
other != null &&
|
||||
_first.Equals(other._first) &&
|
||||
_second.Equals(other._second);
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
obj is OrTrigger<T> t && _first.Equals(t._first) && _second.Equals(t._second);
|
||||
Equals(obj is OrTrigger<T>);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
EqualityComparer.aggregate_hash(_first.GetHashCode(), _second.GetHashCode());
|
||||
EqualityComparer.aggregate_hash(
|
||||
_first.GetHashCode(),
|
||||
_second.GetHashCode());
|
||||
|
||||
public override string ToString() => $"OrTrigger ({_first}, {_second})";
|
||||
public override string ToString() =>
|
||||
$"OrTrigger ({_first}, {_second})";
|
||||
}
|
||||
|
||||
class OrTrigger<T1, T2> : IPredicate<(T1, T2)> {
|
||||
class OrTrigger<T1, T2> : IPredicate<(T1, T2)>, IEquatable<OrTrigger<T1, T2>> {
|
||||
readonly IPredicate<T1> _first;
|
||||
readonly IPredicate<T2> _second;
|
||||
|
||||
@@ -178,15 +221,25 @@ namespace mbts {
|
||||
_second = second;
|
||||
}
|
||||
|
||||
public bool trigger((T1, T2) value) => _first.trigger(value.Item1) || _second.trigger(value.Item2);
|
||||
public bool trigger((T1, T2) value) =>
|
||||
_first.trigger(value.Item1) ||
|
||||
_second.trigger(value.Item2);
|
||||
|
||||
public bool Equals(OrTrigger<T1, T2> other) =>
|
||||
other != null &&
|
||||
_first.Equals(other._first) &&
|
||||
_second.Equals(other._second);
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
obj is OrTrigger<T1, T2> t && _first.Equals(t._first) && _second.Equals(t._second);
|
||||
Equals(obj as OrTrigger<T1, T2>);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
EqualityComparer.aggregate_hash(_first.GetHashCode(), _second.GetHashCode());
|
||||
EqualityComparer.aggregate_hash(
|
||||
_first.GetHashCode(),
|
||||
_second.GetHashCode());
|
||||
|
||||
public override string ToString() => $"OrTrigger ({_first}, {_second})";
|
||||
public override string ToString() =>
|
||||
$"OrTrigger ({_first}, {_second})";
|
||||
}
|
||||
|
||||
class GreaterThanTrigger<T> : IPredicate<T> {
|
||||
@@ -198,29 +251,66 @@ namespace mbts {
|
||||
_comparer = comparer;
|
||||
}
|
||||
|
||||
public bool trigger(T value) => _comparer.Compare(value, _value) > 0;
|
||||
public bool trigger(T value) =>
|
||||
_comparer.Compare(value, _value) > 0;
|
||||
|
||||
public override string ToString() => $"GreaterThanTrigger ({_value})";
|
||||
public override string ToString() =>
|
||||
$"GreaterThanTrigger ({_value})";
|
||||
}
|
||||
|
||||
class InSetTrigger<T>: IPredicate<T> {
|
||||
class GreaterThanOrEqualToTrigger<T> : IPredicate<T>, IEquatable<GreaterThanOrEqualToTrigger<T>> {
|
||||
readonly T _value;
|
||||
readonly IComparer<T> _comparer;
|
||||
|
||||
public GreaterThanOrEqualToTrigger(T value, IComparer<T> comparer) {
|
||||
_value = value;
|
||||
_comparer = comparer;
|
||||
}
|
||||
|
||||
public bool trigger(T value) =>
|
||||
_comparer.Compare(value, _value) >= 0;
|
||||
|
||||
public bool Equals(GreaterThanOrEqualToTrigger<T> other) =>
|
||||
other != null &&
|
||||
_comparer.Equals(other._comparer) &&
|
||||
_comparer.Compare(_value, other._value) == 0;
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
Equals(obj as GreaterThanOrEqualToTrigger<T>);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
_value.GetHashCode();
|
||||
|
||||
public override string ToString() =>
|
||||
$"GreaterThanTrigger ({_value})";
|
||||
}
|
||||
|
||||
class InSetTrigger<T> : IPredicate<T>, IEquatable<InSetTrigger<T>> {
|
||||
readonly ISet<T> _set;
|
||||
|
||||
public InSetTrigger(ISet<T> set) =>
|
||||
_set = set;
|
||||
|
||||
public bool trigger(T value) => _set.Contains(value);
|
||||
public bool trigger(T value) =>
|
||||
_set.Contains(value);
|
||||
|
||||
public bool Equals(InSetTrigger<T> other) =>
|
||||
other != null &&
|
||||
// check SetEquals with both sets (IEqualityComparers may differ)
|
||||
_set.SetEquals(other._set) &&
|
||||
other._set.SetEquals(_set);
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
obj is InSetTrigger<T> t && _set.SetEquals(t._set);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
_set.GetHashCode();
|
||||
EqualityComparer.auto_phase(_set.Take(8));
|
||||
|
||||
public override string ToString() => $"InSetTrigger (Count: {_set.Count}, type: {typeof(T)})";
|
||||
public override string ToString() =>
|
||||
$"InSetTrigger (Count: {_set.Count}, type: {typeof(T)})";
|
||||
}
|
||||
|
||||
class WrapperTrigger<TOuter, TInner> : IPredicate<TOuter> {
|
||||
class WrapperTrigger<TOuter, TInner> : IPredicate<TOuter>, IEquatable<WrapperTrigger<TOuter, TInner>> {
|
||||
readonly IPredicate<TInner> _inner_trigger;
|
||||
readonly Func<TOuter, TInner> _selector;
|
||||
|
||||
@@ -229,26 +319,41 @@ namespace mbts {
|
||||
_selector = selector;
|
||||
}
|
||||
|
||||
public bool trigger(TOuter value) => _inner_trigger.trigger(_selector(value));
|
||||
public bool trigger(TOuter value) =>
|
||||
_inner_trigger.trigger(_selector(value));
|
||||
|
||||
public bool Equals(WrapperTrigger<TOuter, TInner> other) =>
|
||||
other != null &&
|
||||
_selector.Equals(other._selector) &&
|
||||
_inner_trigger.Equals(other._inner_trigger);
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
obj is WrapperTrigger<TOuter, TInner> t && _inner_trigger.Equals(t._inner_trigger) && _selector.Equals(t._selector);
|
||||
Equals(obj as WrapperTrigger<TOuter, TInner>);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
EqualityComparer.aggregate_hash(_inner_trigger.GetHashCode(), _selector.GetHashCode());
|
||||
EqualityComparer.aggregate_hash(
|
||||
_inner_trigger.GetHashCode(),
|
||||
_selector.GetHashCode());
|
||||
|
||||
public override string ToString() => $"WrapperTrigger (outer-type {typeof(TOuter)}, inner-trigger: {_inner_trigger})";
|
||||
public override string ToString() =>
|
||||
$"WrapperTrigger (outer-type {typeof(TOuter)}, inner-trigger: {_inner_trigger})";
|
||||
}
|
||||
|
||||
class NegateTrigger<T> : IPredicate<T> {
|
||||
class NegateTrigger<T> : IPredicate<T>, IEquatable<NegateTrigger<T>> {
|
||||
readonly IPredicate<T> _trigger;
|
||||
|
||||
public NegateTrigger(IPredicate<T> trigger) =>
|
||||
_trigger = trigger;
|
||||
|
||||
public bool trigger(T value) => !_trigger.trigger(value);
|
||||
public bool trigger(T value) =>
|
||||
!_trigger.trigger(value);
|
||||
|
||||
public bool Equals(NegateTrigger<T> other) =>
|
||||
other != null &&
|
||||
_trigger.Equals(other._trigger);
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
obj is NegateTrigger<T> t && _trigger.Equals(t._trigger);
|
||||
Equals(obj as NegateTrigger<T>);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
-_trigger.GetHashCode();
|
||||
@@ -256,16 +361,22 @@ namespace mbts {
|
||||
public override string ToString() => $"NegateTrigger ({_trigger})";
|
||||
}
|
||||
|
||||
class AnonymousTrigger<T> : IPredicate<T> {
|
||||
class AnonymousTrigger<T> : IPredicate<T>, IEquatable<AnonymousTrigger<T>> {
|
||||
readonly Func<T, bool> _trigger;
|
||||
|
||||
public AnonymousTrigger(Func<T, bool> trigger) =>
|
||||
_trigger = trigger;
|
||||
|
||||
public bool trigger(T value) => _trigger(value);
|
||||
public bool Equals(AnonymousTrigger<T> other) =>
|
||||
other != null &&
|
||||
_trigger.Equals(other._trigger);
|
||||
|
||||
public override string ToString() => $"AnonymousTrigger ({typeof(T)})";
|
||||
public bool trigger(T value) =>
|
||||
_trigger(value);
|
||||
|
||||
public override string ToString() =>
|
||||
$"AnonymousTrigger ({typeof(T)})";
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user