Example #1
0
        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));
        }
Example #2
0
        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));
        }
Example #3
0
 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);
         }
     }
 }
Example #4
0
        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));
        }
Example #5
0
        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);
                }
            }
        }
Example #6
0
        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);
        }
Example #7
0
 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;
     }
 }
Example #8
0
        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));
        }
Example #9
0
        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++;
                }
            }
        }
Example #10
0
 public bool ContainsValue(object value)
 {
     return(DictOps.Contains(this, value));
 }
Example #11
0
 public void Add(object key, object value)
 {
     lock (this) data.Add(DictOps.NullToObj(key), value);
 }
Example #12
0
 public object HasKey(object key)
 {
     return(DictOps.HasKey(this, key));
 }
Example #13
0
 public override string ToString()
 {
     return(DictOps.ToString(this));
 }
Example #14
0
 public object SetDefault(object key)
 {
     return(DictOps.SetDefault(this, key));
 }
Example #15
0
 void IDictionary.Clear()
 {
     DictOps.Clear(this);
 }
Example #16
0
 public IEnumerator IterValues()
 {
     return(DictOps.IterValues(this));
 }
Example #17
0
 public int GetLength()
 {
     return(DictOps.Length(this));
 }
Example #18
0
 public bool Remove(object key)
 {
     lock (this) return(data.Remove(DictOps.NullToObj(key)));
 }
Example #19
0
 public object Pop(object key, object defaultValue)
 {
     return(DictOps.Pop(this, key, defaultValue));
 }
Example #20
0
 public List values()
 {
     return(DictOps.Values(this));
 }
Example #21
0
 public List keys()
 {
     return(DictOps.Keys(this));
 }
Example #22
0
 public object SetDefault(object key, object defaultValue)
 {
     return(DictOps.SetDefault(this, key, defaultValue));
 }
Example #23
0
 void Replace(object key, object value)
 {
     lock (this) data[DictOps.NullToObj(key)] = value;
 }
Example #24
0
 public void Update(object b)
 {
     DictOps.Update(this, b);
 }
Example #25
0
 public bool ContainsKey(object key)
 {
     lock (this) return(data.ContainsKey(DictOps.NullToObj(key)));
 }
Example #26
0
 public object Pop(object key)
 {
     return(DictOps.Pop(this, key));
 }
Example #27
0
 public List Items()
 {
     return(DictOps.Items(this));
 }
Example #28
0
 public Tuple PopItem()
 {
     return(DictOps.PopItem(this));
 }
Example #29
0
 public bool TryGetValue(object key, out object value)
 {
     lock (this) return(data.TryGetValue(DictOps.NullToObj(key), out value));
 }
Example #30
0
 public IEnumerator IterKeys()
 {
     return(DictOps.IterKeys(this));
 }