forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IComparable and IEquatable implementations for PyInt, PyFloat, and Py…
…String for primitive .NET types
- Loading branch information
Showing
10 changed files
with
331 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
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
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); | ||
} |
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
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
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); | ||
} |
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
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
Oops, something went wrong.