Пример #1
0
            public int Compare(object x, object y)
            {
                //??? Putting this optimization here is awfully special case, but comes close to halving sort time for int lists
                //				if (x is int && y is int) {
                //					int xi = (int)x;
                //					int yi = (int)y;
                //					return xi == yi ? 0 : (xi < yi ? -1 : +1);
                //				}

                return(Ops.Compare(x, y));
            }
Пример #2
0
        public object RichEquals(object other)
        {
            Slice s = other as Slice;

            if (s == null)
            {
                return(Ops.FALSE);
            }
            return(Ops.Bool2Object((Ops.Compare(start, s.start) == 0) &&
                                   (Ops.Compare(stop, s.stop) == 0) &&
                                   (Ops.Compare(step, s.step) == 0)));
        }
Пример #3
0
        public static object Compare(int self, object obj)
        {
            if (obj == null)
            {
                return(1);
            }

            int otherInt;

            if (obj is int)
            {
                otherInt = (int)obj;
            }
            else if (obj is ExtensibleInt)
            {
                otherInt = ((ExtensibleInt)obj).value;
            }
            else if (obj is bool)
            {
                otherInt = ((bool)obj) ? 1 : 0;
            }
            else if (obj is double)
            {
                // compare as double to avoid truncation issues
                return(FloatOps.Compare((double)self, (double)obj));
            }
            else if (obj is ExtensibleFloat)
            {
                // compare as double to avoid truncation issues
                return(FloatOps.Compare((double)self, ((ExtensibleFloat)obj).value));
            }
            else if (obj is Decimal)
            {
                return(FloatOps.Compare((double)self, (double)(decimal)obj));
            }
            else
            {
                Conversion conv;
                otherInt = Converter.TryConvertToInt32(obj, out conv);
                if (conv == Conversion.None)
                {
                    object res = Ops.GetDynamicType(obj).Coerce(obj, self);
                    if (res != Ops.NotImplemented && !(res is OldInstance))
                    {
                        return(Ops.Compare(((Tuple)res)[1], ((Tuple)res)[0]));
                    }
                    return(Ops.NotImplemented);
                }
            }

            return(self == otherInt ? 0 : (self < otherInt ? -1 : +1));
        }
Пример #4
0
        public static object Compare(double self, object other)
        {
            if (other == null)
            {
                return(1);
            }

            Conversion conv;
            double     val = Converter.TryConvertToDouble(other, out conv);

            if (conv == Conversion.None)
            {
                object res = Ops.GetDynamicType(other).Coerce(other, self);
                if (res != Ops.NotImplemented && !(res is OldInstance))
                {
                    return(Ops.Compare(((Tuple)res)[1], ((Tuple)res)[0]));
                }

                Complex64 c64 = Converter.TryConvertToComplex64(other, out conv);
                if (conv != Conversion.None)
                {
                    return(ComplexOps.TrueCompare(c64, new Complex64(self)) * -1);
                }

                return(Ops.NotImplemented);
            }


            if (val == self)
            {
                return(0);
            }
            if (self < val)
            {
                return(-1);
            }
            return(1);
        }
Пример #5
0
        public static object Compare(BigInteger x, object y)
        {
            if (y == null)
            {
                return(1);
            }

            int intVal;

            if (y is int)
            {
                if (x.AsInt32(out intVal))
                {
                    return(IntOps.Compare(intVal, y));
                }
            }
            else if (y is ExtensibleInt)
            {
                if (x.AsInt32(out intVal))
                {
                    return(IntOps.Compare(intVal, ((ExtensibleInt)y).value));
                }
            }
            else if (y is double)
            {
                double dbl = x.ToFloat64();
                return(FloatOps.Compare(dbl, y));
            }
            else if (y is ExtensibleFloat)
            {
                double dbl = x.ToFloat64();
                return(FloatOps.Compare(dbl, ((ExtensibleFloat)y).value));
            }
            else if (y is bool)
            {
                if (x.AsInt32(out intVal))
                {
                    return(IntOps.Compare(intVal, ((bool)y) ? 1 : 0));
                }
            }
            else if (y is decimal)
            {
                double dbl = x.ToFloat64();
                return(FloatOps.Compare(dbl, y));
            }

            Conversion conv;
            BigInteger bi = Converter.TryConvertToBigInteger(y, out conv);

            if (conv == Conversion.None)
            {
                object res = Ops.GetDynamicType(y).Coerce(y, x);
                if (res != Ops.NotImplemented && !(res is OldInstance))
                {
                    return(Ops.Compare(((Tuple)res)[1], ((Tuple)res)[0]));
                }
                return(Ops.NotImplemented);
            }

            BigInteger diff = x - bi;

            if (diff == 0)
            {
                return(0);
            }
            else if (diff < 0)
            {
                return(-1);
            }
            else
            {
                return(1);
            }
        }
Пример #6
0
 public static int Compare(object x, object y)
 {
     return(Ops.Compare(x, y));
 }
Пример #7
0
        /// <summary>
        /// Used when user calls cmp(x,y) versus x > y, if the values are the same we return 0.
        /// </summary>
        public static int TrueCompare(object x, object y)
        {
            // Complex vs. null is 1 (when complex is on the lhs)
            // Complex vs. another type is -1 (when complex is on the lhs)
            // If two complex values are equal we return 0
            // Otherwise we throw because it's an un-ordered comparison
            if (x is Complex64)
            {
                Complex64 us = (Complex64)x;

                // Complex vs null, 1
                if (y == null)
                {
                    return(1);
                }

                // Compex vs Complex, if they're equal we return 0, otherwize we throw
                Complex64 them      = new Complex64();
                bool      haveOther = false;
                if (y is Complex64)
                {
                    them      = (Complex64)y;
                    haveOther = true;
                }
                else if (y is ExtensibleComplex)
                {
                    them      = ((ExtensibleComplex)y).value;
                    haveOther = true;
                }
                else
                {
                    object res = Ops.GetDynamicType(y).Coerce(y, x);
                    if (res != Ops.NotImplemented && !(res is OldInstance))
                    {
                        return(Ops.Compare(((Tuple)res)[1], ((Tuple)res)[0]));
                    }
                }


                if (haveOther)
                {
                    if (us.imag == them.imag && us.real == them.real)
                    {
                        return(0);
                    }
                    throw Ops.TypeError("complex is not an ordered type");
                }

                // Complex vs user type, check what the user type says
                object ret = Ops.GetDynamicType(y).CompareTo(y, x);
                if (ret != Ops.NotImplemented)
                {
                    return(((int)ret) * -1);
                }

                // Otherwise all types are less than complex
                return(-1);
            }
            else
            {
                System.Diagnostics.Debug.Assert(y is Complex64);
                return(-1 * TrueCompare(y, x));
            }
        }
Пример #8
0
            public int CompareTo(object obj)
            {
                PythonDequeCollection otherDeque = obj as PythonDequeCollection;

                if (otherDeque == null)
                {
                    throw new ArgumentException("expected deque");
                }

                if (otherDeque.itemCnt == 0 && itemCnt == 0)
                {
                    // comparing two empty deques
                    return(0);
                }

                ArrayList infinite = Ops.GetCmpInfinite();

                if (infinite.Contains(this))
                {
                    return(0);
                }

                int index = infinite.Add(this);

                try {
                    int otherIndex = otherDeque.head, ourIndex = head;

                    for (; ;)
                    {
                        int result = Ops.Compare(data[ourIndex], otherDeque.data[otherIndex]);
                        if (result != 0)
                        {
                            return(result);
                        }

                        // advance both indexes
                        otherIndex++;
                        if (otherIndex == otherDeque.data.Length)
                        {
                            otherIndex = 0;
                        }
                        else if (otherIndex == otherDeque.tail)
                        {
                            break;
                        }

                        ourIndex++;
                        if (ourIndex == data.Length)
                        {
                            ourIndex = 0;
                        }
                        else if (ourIndex == tail)
                        {
                            break;
                        }
                    }
                    // all items are equal, but # of items may be different.

                    if (otherDeque.itemCnt == itemCnt)
                    {
                        // same # of items, all items are equal
                        return(0);
                    }

                    return(itemCnt > otherDeque.itemCnt ? 1 : -1);
                } finally {
                    System.Diagnostics.Debug.Assert(infinite.Count == index + 1);
                    infinite.RemoveAt(index);
                }
            }