public static object RejectMutate(RubyContext /*!*/ context, BlockParam block, IDictionary <object, object> /*!*/ self) { RubyUtils.RequiresNotFrozen(context, self); // Make a copy of the keys to delete, so we don't modify the collection // while iterating over it RubyArray keysToDelete = new RubyArray(); foreach (KeyValuePair <object, object> pair in self) { object result; if (block.Yield(CustomStringDictionary.ObjToNull(pair.Key), pair.Value, out result)) { return(result); } // Delete the key, unless 'false' or 'nil' is returned if (RubyOps.IsTrue(result)) { keysToDelete.Add(pair.Key); } } foreach (object key in keysToDelete) { self.Remove(key); } return(keysToDelete.Count == 0 ? null : self); }
public static object DeleteIf([NotNull] BlockParam /*!*/ block, IDictionary <object, object> /*!*/ self) { // Make a copy of the keys to delete, so we don't modify the collection // while iterating over it RubyArray keysToDelete = new RubyArray(); foreach (var pair in self) { object result; if (block.Yield(CustomStringDictionary.ObjToNull(pair.Key), pair.Value, out result)) { return(result); } // Delete the key, unless 'false' or 'nil' is returned if (RubyOps.IsTrue(result)) { keysToDelete.Add(pair.Key); } } foreach (object key in keysToDelete) { self.Remove(key); } return(self); }
public static object Update(RubyContext /*!*/ context, BlockParam block, IDictionary <object, object> /*!*/ self, [DefaultProtocol, NotNull] IDictionary <object, object> /*!*/ hash) { if (hash.Count > 0) { RubyUtils.RequiresNotFrozen(context, self); } if (block == null) { foreach (var pair in CopyKeyValuePairs(hash)) { self[CustomStringDictionary.NullToObj(pair.Key)] = pair.Value; } } else { foreach (var pair in CopyKeyValuePairs(hash)) { object key = pair.Key, newValue = pair.Value, oldValue; if (self.TryGetValue(key, out oldValue)) { if (block.Yield(CustomStringDictionary.ObjToNull(key), oldValue, pair.Value, out newValue)) { return(newValue); } } self[key] = newValue; } } return(self); }
public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, Hash /*!*/ self) { using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) { if (handle == null) { return(MutableString.CreateAscii("{...}")); } MutableString str = MutableString.CreateMutable(RubyEncoding.Binary); str.Append('{'); bool first = true; foreach (var entry in self) { if (first) { first = false; } else { str.Append(", "); } str.Append(context.Inspect(CustomStringDictionary.ObjToNull(entry.Key))); str.Append("=>"); str.Append(context.Inspect(entry.Value)); } str.Append('}'); return(str); } }
public static object Fetch(RubyContext /*!*/ context, BlockParam block, IDictionary <object, object> /*!*/ self, object key, [Optional] object defaultValue) { object result; if (self.TryGetValue(CustomStringDictionary.NullToObj(key), out result)) { return(result); } if (block != null) { if (defaultValue != Missing.Value) { context.ReportWarning("block supersedes default value argument"); } block.Yield(key, out result); return(result); } if (defaultValue == Missing.Value) { throw RubyExceptions.CreateIndexError("key not found"); } return(defaultValue); }
// Make a 2 element array internal static RubyArray /*!*/ MakeArray(KeyValuePair <object, object> pair) { RubyArray list = new RubyArray(2); list.Add(CustomStringDictionary.ObjToNull(pair.Key)); list.Add(pair.Value); return(list); }
internal static RubyArray /*!*/ MakeArray(object key, object value) { RubyArray list = new RubyArray(2); list.Add(CustomStringDictionary.ObjToNull(key)); list.Add(value); return(list); }
public static object GetElement(RubyContext /*!*/ context, IDictionary <object, object> /*!*/ self, object key) { object result; if (!self.TryGetValue(CustomStringDictionary.NullToObj(key), out result)) { return(null); } return(result); }
public static RubyArray /*!*/ GetKeys(IDictionary <object, object> /*!*/ self) { RubyArray keys = new RubyArray(self.Count); foreach (object key in self.Keys) { keys.Add(CustomStringDictionary.ObjToNull(key)); } return(keys); }
public static Hash /*!*/ Invert(RubyContext /*!*/ context, IDictionary <object, object> /*!*/ self) { // invert returns a Hash, even from subclasses Hash hash = new Hash(context.EqualityComparer, self.Count); foreach (KeyValuePair <object, object> pair in self) { hash[CustomStringDictionary.NullToObj(pair.Value)] = CustomStringDictionary.ObjToNull(pair.Key); } return(hash); }
public static object Index(BinaryOpStorage /*!*/ equals, IDictionary <object, object> /*!*/ self, object value) { foreach (KeyValuePair <object, object> pair in self) { if (Protocols.IsEqual(equals, pair.Value, value)) { return(CustomStringDictionary.ObjToNull(pair.Key)); } } return(null); }
public static object GetElement(BinaryOpStorage /*!*/ storage, IDictionary <object, object> /*!*/ self, object key) { object result; if (!self.TryGetValue(CustomStringDictionary.NullToObj(key), out result)) { var site = storage.GetCallSite("default", 1); return(site.Target(site, self, key)); } return(result); }
public override void Add(ref DictionaryStorage storage, object key, object value) { lock (this) { string strKey = key as string; if (strKey != null) { _dict[strKey] = value; } else { EnsureObjectDictionary(); _objDict[CustomStringDictionary.NullToObj(key)] = value; } } }
// Dictionary has an odd not-implemented check to support custom dictionaries and therefore // needs a custom __eq__ / __ne__ implementation. public static string /*!*/ __repr__(CodeContext /*!*/ context, IDictionary <object, object> self) { List <object> infinite = PythonOps.GetAndCheckInfinite(self); if (infinite == null) { return("{...}"); } int index = infinite.Count; infinite.Add(self); try { StringBuilder buf = new StringBuilder(); buf.Append("{"); bool first = true; foreach (KeyValuePair <object, object> kv in self) { if (first) { first = false; } else { buf.Append(", "); } if (CustomStringDictionary.IsNullObject(kv.Key)) { buf.Append("None"); } else { buf.Append(PythonOps.Repr(context, kv.Key)); } buf.Append(": "); buf.Append(PythonOps.Repr(context, kv.Value)); } buf.Append("}"); return(buf.ToString()); } finally { System.Diagnostics.Debug.Assert(index == infinite.Count - 1); infinite.RemoveAt(index); } }
public override bool Contains(object key) { lock (this) { string strKey = key as string; if (strKey != null) { return(_dict.ContainsKey(strKey)); } if (_objDict != null) { return(_objDict.ContainsKey(CustomStringDictionary.NullToObj(key))); } return(false); } }
public override bool Remove(ref DictionaryStorage storage, object key) { lock (this) { string strKey = key as string; if (strKey != null) { return(_dict.Remove(strKey)); } if (_objDict != null) { return(_objDict.Remove(CustomStringDictionary.NullToObj(key))); } return(false); } }
public static object Delete(BlockParam block, IDictionary <object, object> /*!*/ self, object key) { object value; if (!self.TryGetValue(CustomStringDictionary.NullToObj(key), out value)) { // key not found, call the block if it was passed in if (block != null) { object result; block.Yield(key, out result); return(result); } return(null); } self.Remove(CustomStringDictionary.NullToObj(key)); return(value); }
public override bool TryGetValue(object key, out object value) { lock (this) { string strKey = key as string; if (strKey != null) { return(_dict.TryGetValue(strKey, out value)); } if (_objDict != null) { return(_objDict.TryGetValue(CustomStringDictionary.NullToObj(key), out value)); } value = null; return(false); } }
public static object Select(RubyContext /*!*/ context, [NotNull] BlockParam /*!*/ block, IDictionary <object, object> /*!*/ self) { Hash result = new Hash(context); foreach (var pair in CopyKeyValuePairs(self)) { object blockResult; if (block.Yield(CustomStringDictionary.ObjToNull(pair.Key), pair.Value, out blockResult)) { return(blockResult); } if (RubyOps.IsTrue(blockResult)) { result[pair.Key] = pair.Value; } } return(result); }
public static object Select(RubyContext /*!*/ context, BlockParam block, IDictionary <object, object> /*!*/ self) { RubyArray list = new RubyArray(); foreach (var pair in CopyKeyValuePairs(self)) { object result; if (block.Yield(CustomStringDictionary.ObjToNull(pair.Key), pair.Value, out result)) { return(result); } // Select the key, unless 'false' or 'nil' is returned if (RubyOps.IsTrue(result)) { list.Add(MakeArray(pair)); } } return(list); }
public static object EachKey(RubyContext /*!*/ context, BlockParam block, IDictionary <object, object> /*!*/ self) { if (self.Count > 0) { // Must make a copy of the Keys array so that we can iterate over a static set of keys. Remember // that the block can modify the hash, hence the need for a copy of the keys object[] keys = new object[self.Count]; self.Keys.CopyTo(keys, 0); // TODO: what are all the scenarios where the block can mutate the hash? can it remove keys? if so, what happens? for (int i = 0; i < keys.Length; i++) { object result; if (block.Yield(CustomStringDictionary.ObjToNull(keys[i]), out result)) { return(result); } } } return(self); }
public static MutableString Inspect(RubyContext /*!*/ context, IDictionary <object, object> /*!*/ self) { using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) { if (handle == null) { return(MutableString.CreateAscii("{...}")); } MutableString str = MutableString.CreateMutable(RubyEncoding.Binary); str.Append('{'); foreach (KeyValuePair <object, object> pair in self) { if (str.Length != 1) { str.Append(", "); } str.Append(context.Inspect(CustomStringDictionary.ObjToNull(pair.Key))); str.Append("=>"); str.Append(context.Inspect(pair.Value)); } str.Append('}'); return(str); } }
public ExtraKeyEnumerator(CustomStringDictionary idDict) { _idDict = idDict; }
public static bool HasKey(IDictionary <object, object> /*!*/ self, object key) { return(self.ContainsKey(CustomStringDictionary.NullToObj(key))); }