public static string SetToString(CodeContext /*!*/ context, object set, CommonDictionaryStorage items) { string setTypeStr; Type setType = set.GetType(); if (setType == typeof(SetCollection)) { setTypeStr = "set"; } else if (setType == typeof(FrozenSetCollection)) { setTypeStr = "frozenset"; } else { setTypeStr = PythonTypeOps.GetName(set); } StringBuilder sb = new StringBuilder(); sb.Append(setTypeStr); sb.Append("(["); string comma = ""; foreach (object o in items.GetKeys()) { sb.Append(comma); sb.Append(PythonOps.Repr(context, o)); comma = ", "; } sb.Append("])"); return(sb.ToString()); }
internal SetIterator(CommonDictionaryStorage items, bool mutable) { _items = items; if (mutable) { lock (items) { _version = _items.Version; _enumerator = items.GetKeys().GetEnumerator(); } } else { _version = _items.Version; _enumerator = items.GetKeys().GetEnumerator(); } }
public object pop() { foreach (object o in _items.GetKeys()) { _items.Remove(o); return(o); } throw PythonOps.KeyError("pop from an empty set"); }
public static PythonTuple Reduce(CommonDictionaryStorage items, PythonType type) { object[] keys = new object[items.Count]; int i = 0; foreach (object key in items.GetKeys()) { keys[i++] = key; } return(PythonTuple.MakeTuple(type, PythonTuple.MakeTuple(List.FromArrayNoCopy(keys)), null)); }
private int CalculateHashCode(IEqualityComparer /*!*/ comparer) { Assert.NotNull(comparer); HashCache curHashCache = _hashCache; if (curHashCache != null && object.ReferenceEquals(comparer, curHashCache.Comparer)) { return(curHashCache.HashCode); } // hash code needs be stable across collections (even if keys are // added in different order) and needs to be fairly collision free. int[] hash_codes = new int[_items.Count]; int i = 0; foreach (object o in _items.GetKeys()) { hash_codes[i++] = comparer.GetHashCode(o); } Array.Sort(hash_codes); int hash1 = 6551; int hash2 = hash1; for (i = 0; i < hash_codes.Length; i += 2) { hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ hash_codes[i]; if (i == hash_codes.Length - 1) { break; } hash2 = ((hash2 << 5) + hash2 + (hash2 >> 27)) ^ hash_codes[i + 1]; } hash1 += hash2 * 1566083941; _hashCache = new HashCache(hash1, comparer); return(hash1); }