Created
May 10, 2018 20:52
-
-
Save dharmatech/36efac147b2167276f84d43ba4472882 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace AbstractAlgebraFunctionIntInt | |
{ | |
public class FunctionSetComparer : IEqualityComparer<IEnumerable<FunctionIntInt>> | |
{ | |
public bool Equals(IEnumerable<FunctionIntInt> a, IEnumerable<FunctionIntInt> b) => new HashSet<FunctionIntInt>(a).SetEquals(b); | |
public int GetHashCode(IEnumerable<FunctionIntInt> seq) => string.Join(" ", seq.Select(elt => elt.GetHashCode())).GetHashCode(); | |
} | |
public class FunctionIntInt : IEquatable<FunctionIntInt> | |
{ | |
public readonly List<(int, int)> ls; | |
// ---------------------------------------------------------------------- | |
// public FunctionIntInt() { } | |
public FunctionIntInt(IEnumerable<(int, int)> items) { ls = items.ToList(); } | |
public FunctionIntInt(params (int, int)[] items) { ls = items.ToList(); } | |
public FunctionIntInt(string str) | |
{ | |
ls = Enumerable.Range(1, str.Count()) | |
.Zip(str.Select(elt => elt - '0'), (k, v) => (k, v)) | |
.ToList(); | |
} | |
// ---------------------------------------------------------------------- | |
public override string ToString() => string.Format("new FunctionIntInt({0})", string.Join(", ", ls)); | |
// ---------------------------------------------------------------------- | |
public bool Equals(FunctionIntInt obj) | |
{ | |
if (ReferenceEquals(obj, null)) return false; | |
if (ReferenceEquals(obj, this)) return true; | |
if (obj.GetType() != GetType()) return false; | |
return new HashSet<(int, int)>(ls).SetEquals(obj.ls); | |
} | |
public override bool Equals(object obj) => Equals(obj as FunctionIntInt); | |
public static bool operator ==(FunctionIntInt a, FunctionIntInt b) => | |
ReferenceEquals(a, null) ? ReferenceEquals(b, null) : a.Equals(b); | |
public static bool operator !=(FunctionIntInt a, FunctionIntInt b) => !(a == b); | |
public override int GetHashCode() => string.Join(" ", ls.SelectMany(elt => new[] { elt.Item1, elt.Item2 }).OrderBy(elt => elt)).GetHashCode(); | |
// ---------------------------------------------------------------------- | |
public static FunctionIntInt FromString(string s) => | |
new FunctionIntInt( | |
Enumerable.Range(1, s.Count()) | |
.Zip( | |
s.Select(elt => elt - '0'), | |
(k, v) => (k, v))); | |
public int Apply(int x) => ls.First(elt => elt.Item1 == x).Item2; | |
public FunctionIntInt Compose(FunctionIntInt g) => | |
new FunctionIntInt(g.ls.Select(elt => (elt.Item1, Apply(elt.Item2)))); | |
public FunctionIntInt Inverse() => | |
new FunctionIntInt(ls.Select(elt => (elt.Item2, elt.Item1)).OrderBy(elt => elt.Item1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment