Better handling of list.remove()
This commit is contained in:
parent
768c02c482
commit
30177e6c1e
|
@ -34,6 +34,11 @@ namespace Upsilon.BaseTypes
|
|||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Delete(ScriptType key)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
internal class UpTillNullPairsScriptIterator : ScriptIterator
|
||||
|
|
|
@ -41,6 +41,7 @@ namespace Upsilon.BaseTypes.ScriptTable
|
|||
EvaluationScope.CreateLocal(new VariableSymbol(s, value.Type, false), value);
|
||||
}
|
||||
|
||||
|
||||
public ScriptType GetValueFromIndex(ScriptType index)
|
||||
{
|
||||
if (EvaluationScope.TryGet(index.ToString(), out var result))
|
||||
|
@ -52,6 +53,10 @@ namespace Upsilon.BaseTypes.ScriptTable
|
|||
|
||||
public abstract IEnumerator<ScriptType> GetScriptEnumerator();
|
||||
public abstract bool Contains(ScriptType obj);
|
||||
public void Delete(ScriptType key)
|
||||
{
|
||||
EvaluationScope.Delete(key.ToString());
|
||||
}
|
||||
|
||||
public ScriptNumberLong Length()
|
||||
{
|
||||
|
|
|
@ -7,5 +7,6 @@ namespace Upsilon.BaseTypes.ScriptTypeInterfaces
|
|||
ScriptType GetValueFromIndex(ScriptType index);
|
||||
IEnumerator<ScriptType> GetScriptEnumerator();
|
||||
bool Contains(ScriptType obj);
|
||||
void Delete(ScriptType key);
|
||||
}
|
||||
}
|
|
@ -114,5 +114,10 @@ namespace Upsilon.BaseTypes.UserData
|
|||
var cSharpObj = obj.ToCSharpObject();
|
||||
return Dictionary.Contains(cSharpObj);
|
||||
}
|
||||
|
||||
public void Delete(ScriptType key)
|
||||
{
|
||||
Dictionary.Remove(key.ToCSharpObject());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -144,6 +144,11 @@ namespace Upsilon.BaseTypes.UserData
|
|||
return List.Contains(cSharpObj);
|
||||
}
|
||||
|
||||
public void Delete(ScriptType key)
|
||||
{
|
||||
List.Remove(key.ToCSharpObject());
|
||||
}
|
||||
|
||||
public ScriptNumberLong Length()
|
||||
{
|
||||
return new ScriptNumberLong(List.Count);
|
||||
|
|
|
@ -16,29 +16,9 @@ namespace Upsilon.StandardLibraries
|
|||
}
|
||||
|
||||
[ScriptFunction("remove", "Removes an object from a table.", directScriptManipulation: true)]
|
||||
public void Remove(ListUserData userList, ScriptType obj)
|
||||
public void Remove(IIterable scriptTable, ScriptType obj)
|
||||
{
|
||||
userList.List.Remove(obj);
|
||||
}
|
||||
|
||||
[ScriptFunction("remove", "Removes an object from a table.", directScriptManipulation: true)]
|
||||
public void Remove(NumeratedScriptTable scriptTable, ScriptType obj)
|
||||
{
|
||||
var enumerator = scriptTable.GetEnumerator();
|
||||
var toRemove = new List<int>();
|
||||
var i = 1;
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
var current = enumerator.Current;
|
||||
if (current == obj)
|
||||
toRemove.Add(i);
|
||||
i++;
|
||||
}
|
||||
enumerator.Dispose();
|
||||
foreach (var index in toRemove)
|
||||
{
|
||||
scriptTable.Set(null, new TextSpan(), index.ToScriptType(), new ScriptNull());
|
||||
}
|
||||
scriptTable.Delete(obj);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue