#nullable enable
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using Microsoft.Scripting.Runtime;
namespace IronPython.Runtime {
public static class DictionaryOps {
#region Dictionary Public API Surface
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(": ");
try {
PythonOps.FunctionPushFrame(context.LanguageContext);
buf.Append(PythonOps.Repr(context, kv.Value));
} finally {
PythonOps.FunctionPopFrame();
}
}
buf.Append("}");
return buf.ToString();
} finally {
System.Diagnostics.Debug.Assert(index == infinite.Count - 1);
infinite.RemoveAt(index);
}
}
public static object? get(PythonDictionary self, object key, object? defaultValue = null) {
if (self.TryGetValueNoMissing(key, out object ret)) return ret;
return defaultValue;
}
private static PythonList ToList(IDictionary<object, object> self) {
PythonList ret = new PythonList(self.Count);
foreach (KeyValuePair<object, object> kv in self) {
ret.AddNoLock(PythonTuple.MakeTuple(kv.Key, kv.Value));
}
return ret;
}
public static object pop(PythonDictionary self, object key) {
if (self.TryGetValueNoMissing(key, out object ret)) {
self.RemoveDirect(key);
return ret;
} else {
throw PythonOps.KeyError(key);
}
}
public static object pop(PythonDictionary self, object key, object defaultValue) {
if (self.TryGetValueNoMissing(key, out object ret)) {
self.RemoveDirect(key);
return ret;
} else {
return defaultValue;
}
}
public static PythonTuple popitem(PythonDictionary self) {
using IEnumerator<KeyValuePair<object, object>> ie = self.GetEnumerator();
if (ie.MoveNext()) {
object key = ie.Current.Key;
object val = ie.Current.Value;
self.RemoveDirect(key);
return PythonTuple.MakeTuple(key, val);
}
throw PythonOps.KeyError("dictionary is empty");
}
public static object? setdefault(PythonDictionary self, object key) {
return setdefault(self, key, null);
}
public static object? setdefault(PythonDictionary self, object key, object? defaultValue) {
if (self.TryGetValueNoMissing(key, out object ret)) return ret;
self.SetItem(key, defaultValue);
return defaultValue;
}
public static void update(CodeContext context, PythonDictionary self, object other) {
if (other is PythonDictionary pyDict) {
pyDict._storage.CopyTo(ref self._storage);
} else {
SlowUpdate(context, self, other);
}
}
private static void SlowUpdate(CodeContext context, PythonDictionary self, object other) {
if (other is MappingProxy proxy) {
update(context, self, proxy.GetDictionary(context));
} else if (other is IDictionary dict) {
IDictionaryEnumerator e = dict.GetEnumerator();
while (e.MoveNext()) {
self._storage.Add(ref self._storage, e.Key, e.Value);
}
} else if (PythonOps.TryGetBoundAttr(other, "keys", out object? keysFunc)) {
IEnumerator i = PythonOps.GetEnumerator(context, PythonCalls.Call(context, keysFunc));
while (i.MoveNext()) {
self._storage.Add(ref self._storage, i.Current, PythonOps.GetIndex(context, other, i.Current));
}
} else {
IEnumerator i = PythonOps.GetEnumerator(context, other);
int index = 0;
while (i.MoveNext()) {
if (!AddKeyValue(context, self, i.Current)) {
throw PythonOps.ValueError("dictionary update sequence element #{0} has bad length; 2 is required", index);
}
index++;
}
}
}
#endregion
#region Dictionary Helper APIs
internal static bool TryGetValueVirtual(CodeContext context, PythonDictionary self, object key, ref object DefaultGetItem, out object? value) {
if (self is IPythonObject sdo) {
PythonType myType = sdo.PythonType;
PythonTypeSlot dts;
if (DefaultGetItem == null) {
TypeCache.Dict.TryLookupSlot(context, "__getitem__", out dts);
bool res = dts.TryGetValue(context, self, TypeCache.Dict, out DefaultGetItem);
Debug.Assert(res);
}
if (myType.TryLookupSlot(context, "__getitem__", out dts)) {
dts.TryGetValue(context, self, myType, out object ret);
if (ret != DefaultGetItem) {
try {
value = self[key];
return true;
} catch (KeyNotFoundException) {
value = null;
return false;
}
}
}
}
value = null;
return false;
}
internal static bool AddKeyValue(CodeContext context, PythonDictionary self, object o) {
IEnumerator i = PythonOps.GetEnumerator(context, o);
if (i.MoveNext()) {
object key = i.Current;
if (i.MoveNext()) {
object value = i.Current;
self._storage.Add(ref self._storage, key, value);
return !i.MoveNext();
}
}
return false;
}
#endregion
}
}