Beispiel #1
0
        private bool ValueEqualsPythonDict(PythonDictionary pd, IEqualityComparer comparer)
        {
            List myKeys = keys();

            foreach (object o in myKeys)
            {
                object res;
                if (!pd.TryGetValueNoMissing(o, out res))
                {
                    return(false);
                }

                CompareUtil.Push(res);
                try {
                    if (comparer == null)
                    {
                        if (!PythonOps.EqualRetBool(res, this[o]))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (!comparer.Equals(res, this[o]))
                        {
                            return(false);
                        }
                    }
                } finally {
                    CompareUtil.Pop(res);
                }
            }
            return(true);
        }
Beispiel #2
0
        public object __cmp__(CodeContext context, object other)
        {
            IDictionary <object, object> oth = other as IDictionary <object, object>;

            // CompareTo is allowed to throw (string, int, etc... all do it if they don't get a matching type)
            if (oth == null)
            {
                object len, iteritems;
                if (!PythonOps.TryGetBoundAttr(context, other, "__len__", out len) ||
                    !PythonOps.TryGetBoundAttr(context, other, "iteritems", out iteritems))
                {
                    return(NotImplementedType.Value);
                }

                // user-defined dictionary...
                int lcnt = Count;
                int rcnt = PythonContext.GetContext(context).ConvertToInt32(PythonOps.CallWithContext(context, len));

                if (lcnt != rcnt)
                {
                    return(lcnt > rcnt ? 1 : -1);
                }

                return(DictionaryOps.CompareToWorker(context, this, new List(PythonOps.CallWithContext(context, iteritems))));
            }

            CompareUtil.Push(this, oth);
            try {
                return(DictionaryOps.CompareTo(context, this, oth));
            } finally {
                CompareUtil.Pop(this, oth);
            }
        }
Beispiel #3
0
        private bool EqualsWorker(object other, IEqualityComparer comparer)
        {
            if (Object.ReferenceEquals(this, other))
            {
                return(true);
            }

            IDictionary <object, object> oth = other as IDictionary <object, object>;

            if (oth == null)
            {
                return(false);
            }
            if (oth.Count != Count)
            {
                return(false);
            }

            PythonDictionary pd = other as PythonDictionary;

            if (pd != null)
            {
                return(ValueEqualsPythonDict(pd, comparer));
            }
            // we cannot call Compare here and compare against zero because Python defines
            // value equality even if the keys/values are unordered.
            List myKeys = keys();

            foreach (object o in myKeys)
            {
                object res;
                if (!oth.TryGetValue(o, out res))
                {
                    return(false);
                }

                CompareUtil.Push(res);
                try {
                    if (comparer == null)
                    {
                        if (!PythonOps.EqualRetBool(res, this[o]))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (!comparer.Equals(res, this[o]))
                        {
                            return(false);
                        }
                    }
                } finally {
                    CompareUtil.Pop(res);
                }
            }
            return(true);
        }
Beispiel #4
0
 public int __cmp__(CodeContext /*!*/ context, [NotNull] PythonDictionary /*!*/ other)
 {
     CompareUtil.Push(this, other);
     try {
         return(DictionaryOps.CompareTo(context, this, other));
     } finally {
         CompareUtil.Pop(this, other);
     }
 }
Beispiel #5
0
        int IStructuralEquatable.GetHashCode(IEqualityComparer /*!*/ comparer)
        {
            if (CompareUtil.Check(this))
            {
                return(0);
            }

            int res;

            CompareUtil.Push(this);
            try {
                res = ((IStructuralEquatable) new FrozenSetCollection(_items)).GetHashCode(comparer);
            } finally {
                CompareUtil.Pop(this);
            }

            return(res);
        }
Beispiel #6
0
        int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
        {
            if (CompareUtil.Check(this))
            {
                return(0);
            }

            int  res;
            ISet pairs = new FrozenSetCollection();

            foreach (KeyValuePair <object, object> kvp in _storage.GetItems())
            {
                pairs.PrivAdd(PythonTuple.MakeTuple(kvp.Key, kvp.Value));
            }

            CompareUtil.Push(this);
            try {
                res = pairs.GetHashCode(comparer);
            } finally {
                CompareUtil.Pop(this);
            }

            return(res);
        }