- Notifications
You must be signed in to change notification settings - Fork749
IComparable
andIEquatable
implementations#2322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletionsCHANGELOG.md
This file contains hidden or 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
27 changes: 27 additions & 0 deletionssrc/embed_tests/TestPyFloat.cs
This file contains hidden or 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
70 changes: 70 additions & 0 deletionssrc/embed_tests/TestPyInt.cs
This file contains hidden or 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
19 changes: 19 additions & 0 deletionssrc/embed_tests/TestPyString.cs
This file contains hidden or 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
34 changes: 34 additions & 0 deletionssrc/runtime/PythonTypes/PyFloat.IComparable.cs
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
namespace Python.Runtime; | ||
partial class PyFloat : IComparable<double>, IComparable<float> | ||
, IEquatable<double>, IEquatable<float> | ||
, IComparable<PyFloat?>, IEquatable<PyFloat?> | ||
{ | ||
public override bool Equals(object o) | ||
{ | ||
using var _ = Py.GIL(); | ||
return o switch | ||
{ | ||
double f64 => this.Equals(f64), | ||
float f32 => this.Equals(f32), | ||
_ => base.Equals(o), | ||
}; | ||
} | ||
public int CompareTo(double other) => this.ToDouble().CompareTo(other); | ||
public int CompareTo(float other) => this.ToDouble().CompareTo(other); | ||
public bool Equals(double other) => this.ToDouble().Equals(other); | ||
public bool Equals(float other) => this.ToDouble().Equals(other); | ||
public int CompareTo(PyFloat? other) | ||
{ | ||
return other is null ? 1 : this.CompareTo(other.BorrowNullable()); | ||
} | ||
public bool Equals(PyFloat? other) => base.Equals(other); | ||
} |
4 changes: 3 additions & 1 deletionsrc/runtime/PythonTypes/PyFloat.cs
This file contains hidden or 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
136 changes: 136 additions & 0 deletionssrc/runtime/PythonTypes/PyInt.IComparable.cs
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System; | ||
namespace Python.Runtime; | ||
partial class PyInt : IComparable<long>, IComparable<int>, IComparable<sbyte>, IComparable<short> | ||
, IComparable<ulong>, IComparable<uint>, IComparable<ushort>, IComparable<byte> | ||
, IEquatable<long>, IEquatable<int>, IEquatable<short>, IEquatable<sbyte> | ||
, IEquatable<ulong>, IEquatable<uint>, IEquatable<ushort>, IEquatable<byte> | ||
, IComparable<PyInt?>, IEquatable<PyInt?> | ||
{ | ||
public override bool Equals(object o) | ||
{ | ||
using var _ = Py.GIL(); | ||
return o switch | ||
{ | ||
long i64 => this.Equals(i64), | ||
int i32 => this.Equals(i32), | ||
short i16 => this.Equals(i16), | ||
sbyte i8 => this.Equals(i8), | ||
ulong u64 => this.Equals(u64), | ||
uint u32 => this.Equals(u32), | ||
ushort u16 => this.Equals(u16), | ||
byte u8 => this.Equals(u8), | ||
_ => base.Equals(o), | ||
}; | ||
} | ||
#region Signed | ||
public int CompareTo(long other) | ||
{ | ||
using var pyOther = Runtime.PyInt_FromInt64(other); | ||
return this.CompareTo(pyOther.BorrowOrThrow()); | ||
} | ||
public int CompareTo(int other) | ||
{ | ||
using var pyOther = Runtime.PyInt_FromInt32(other); | ||
return this.CompareTo(pyOther.BorrowOrThrow()); | ||
} | ||
public int CompareTo(short other) | ||
{ | ||
using var pyOther = Runtime.PyInt_FromInt32(other); | ||
return this.CompareTo(pyOther.BorrowOrThrow()); | ||
} | ||
public int CompareTo(sbyte other) | ||
{ | ||
using var pyOther = Runtime.PyInt_FromInt32(other); | ||
return this.CompareTo(pyOther.BorrowOrThrow()); | ||
} | ||
public bool Equals(long other) | ||
{ | ||
using var pyOther = Runtime.PyInt_FromInt64(other); | ||
return this.Equals(pyOther.BorrowOrThrow()); | ||
} | ||
public bool Equals(int other) | ||
{ | ||
using var pyOther = Runtime.PyInt_FromInt32(other); | ||
return this.Equals(pyOther.BorrowOrThrow()); | ||
} | ||
public bool Equals(short other) | ||
{ | ||
using var pyOther = Runtime.PyInt_FromInt32(other); | ||
return this.Equals(pyOther.BorrowOrThrow()); | ||
} | ||
public bool Equals(sbyte other) | ||
{ | ||
using var pyOther = Runtime.PyInt_FromInt32(other); | ||
return this.Equals(pyOther.BorrowOrThrow()); | ||
} | ||
#endregion Signed | ||
#region Unsigned | ||
public int CompareTo(ulong other) | ||
{ | ||
using var pyOther = Runtime.PyLong_FromUnsignedLongLong(other); | ||
return this.CompareTo(pyOther.BorrowOrThrow()); | ||
} | ||
public int CompareTo(uint other) | ||
{ | ||
using var pyOther = Runtime.PyLong_FromUnsignedLongLong(other); | ||
return this.CompareTo(pyOther.BorrowOrThrow()); | ||
} | ||
public int CompareTo(ushort other) | ||
{ | ||
using var pyOther = Runtime.PyLong_FromUnsignedLongLong(other); | ||
return this.CompareTo(pyOther.BorrowOrThrow()); | ||
} | ||
public int CompareTo(byte other) | ||
{ | ||
using var pyOther = Runtime.PyLong_FromUnsignedLongLong(other); | ||
return this.CompareTo(pyOther.BorrowOrThrow()); | ||
} | ||
public bool Equals(ulong other) | ||
{ | ||
using var pyOther = Runtime.PyLong_FromUnsignedLongLong(other); | ||
return this.Equals(pyOther.BorrowOrThrow()); | ||
} | ||
public bool Equals(uint other) | ||
{ | ||
using var pyOther = Runtime.PyLong_FromUnsignedLongLong(other); | ||
return this.Equals(pyOther.BorrowOrThrow()); | ||
} | ||
public bool Equals(ushort other) | ||
{ | ||
using var pyOther = Runtime.PyLong_FromUnsignedLongLong(other); | ||
return this.Equals(pyOther.BorrowOrThrow()); | ||
} | ||
public bool Equals(byte other) | ||
{ | ||
using var pyOther = Runtime.PyLong_FromUnsignedLongLong(other); | ||
return this.Equals(pyOther.BorrowOrThrow()); | ||
} | ||
#endregion Unsigned | ||
public int CompareTo(PyInt? other) | ||
{ | ||
return other is null ? 1 : this.CompareTo(other.BorrowNullable()); | ||
} | ||
public bool Equals(PyInt? other) => base.Equals(other); | ||
} |
2 changes: 1 addition & 1 deletionsrc/runtime/PythonTypes/PyInt.cs
This file contains hidden or 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
19 changes: 18 additions & 1 deletionsrc/runtime/PythonTypes/PyObject.cs
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.