public object CompareTo(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 (!Ops.TryGetAttr(other, SymbolTable.Length, out len) || !Ops.TryGetAttr(other, SymbolTable.StringToId("iteritems"), out iteritems)) { return(Ops.NotImplemented); } // user-defined dictionary... int lcnt = this.Count; int rcnt = Converter.ConvertToInt32(Ops.Call(len)); if (lcnt != rcnt) { return(lcnt > rcnt ? 1 : -1); } return(DictOps.CompareToWorker(this, rcnt, new List(Ops.Call(iteritems)))); } return(DictOps.CompareTo(this, oth)); }
public static int CompareToWorker(IDictionary <object, object> left, int rightLen, List ritems) { List litems = DictOps.Items(left); litems.Sort(); ritems.Sort(); return(litems.CompareTo(ritems)); }
public void CopyTo(KeyValuePair <object, object>[] array, int arrayIndex) { lock (this) { Dictionary <object, object> .Enumerator ie = data.GetEnumerator(); while (ie.MoveNext()) { array[arrayIndex++] = new KeyValuePair <object, object>(DictOps.ObjToNull(ie.Current.Key), ie.Current.Value); } } }
int IComparable.CompareTo(object obj) { IDictionary <object, object> other = obj as IDictionary <object, object>; // CompareTo is allowed to throw (string, int, etc... all do it if they don't get a matching type) if (other == null) { throw Ops.TypeError("CompareTo argument must be a Dictionary"); } return(DictOps.CompareTo(this, other)); }
public override object this[object key] { set { data[DictOps.NullToObj(key)] = value; string s1 = key as string; string s2 = value as string; if (s1 != null && s2 != null) { Environment.SetEnvironmentVariable(s1, s2); } } }
public static List Keys(IDictionary <object, object> self) { List l = List.Make(self.Keys); for (int i = 0; i < l.Count; i++) { if (l[i] == nullObject) { l[i] = DictOps.ObjToNull(l[i]); break; } } return(l); }
public virtual object this[object key] { get { object realKey = DictOps.NullToObj(key); object ret; lock (this) if (TryGetValue(realKey, out ret)) { return(ret); } throw Ops.KeyError("'{0}'", key); } set { lock (this) data[DictOps.NullToObj(key)] = value; } }
public static int CompareTo(IDictionary <object, object> left, IDictionary <object, object> right) { int lcnt = left.Count; int rcnt = right.Count; if (lcnt != rcnt) { return(lcnt > rcnt ? 1 : -1); } //!!! too expensive List ritems = DictOps.Items(right); return(CompareToWorker(left, rcnt, ritems)); }
public static void Update(IDictionary <object, object> self, object b) { object keysFunc; IDictionary dict = b as IDictionary; if (dict != null) { IDictionaryEnumerator e = dict.GetEnumerator(); while (e.MoveNext()) { self[DictOps.NullToObj(e.Key)] = e.Value; } } else if (Ops.TryGetAttr(b, SymbolTable.Keys, out keysFunc)) { // user defined dictionary IEnumerator i = Ops.GetEnumerator(Ops.Call(keysFunc)); while (i.MoveNext()) { self[DictOps.NullToObj(i.Current)] = Ops.GetIndex(b, i.Current); } } else { // list of lists (key/value pairs), list of tuples, // tuple of tuples, etc... IEnumerator i = Ops.GetEnumerator(b); int index = 0; while (i.MoveNext()) { if (!AddKeyValue(self, i.Current)) { throw Ops.ValueError("dictionary update sequence element #{0} has bad length; 2 is required", index); } index++; } } }
public bool ContainsValue(object value) { return(DictOps.Contains(this, value)); }
public void Add(object key, object value) { lock (this) data.Add(DictOps.NullToObj(key), value); }
public object HasKey(object key) { return(DictOps.HasKey(this, key)); }
public override string ToString() { return(DictOps.ToString(this)); }
public object SetDefault(object key) { return(DictOps.SetDefault(this, key)); }
void IDictionary.Clear() { DictOps.Clear(this); }
public IEnumerator IterValues() { return(DictOps.IterValues(this)); }
public int GetLength() { return(DictOps.Length(this)); }
public bool Remove(object key) { lock (this) return(data.Remove(DictOps.NullToObj(key))); }
public object Pop(object key, object defaultValue) { return(DictOps.Pop(this, key, defaultValue)); }
public List values() { return(DictOps.Values(this)); }
public List keys() { return(DictOps.Keys(this)); }
public object SetDefault(object key, object defaultValue) { return(DictOps.SetDefault(this, key, defaultValue)); }
void Replace(object key, object value) { lock (this) data[DictOps.NullToObj(key)] = value; }
public void Update(object b) { DictOps.Update(this, b); }
public bool ContainsKey(object key) { lock (this) return(data.ContainsKey(DictOps.NullToObj(key))); }
public object Pop(object key) { return(DictOps.Pop(this, key)); }
public List Items() { return(DictOps.Items(this)); }
public Tuple PopItem() { return(DictOps.PopItem(this)); }
public bool TryGetValue(object key, out object value) { lock (this) return(data.TryGetValue(DictOps.NullToObj(key), out value)); }
public IEnumerator IterKeys() { return(DictOps.IterKeys(this)); }