Better handling of list.remove()

This commit is contained in:
Deukhoofd 2019-02-25 13:46:06 +01:00
parent 768c02c482
commit 30177e6c1e
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
6 changed files with 23 additions and 22 deletions

View File

@ -34,6 +34,11 @@ namespace Upsilon.BaseTypes
{ {
return false; return false;
} }
public void Delete(ScriptType key)
{
throw new InvalidOperationException();
}
} }
internal class UpTillNullPairsScriptIterator : ScriptIterator internal class UpTillNullPairsScriptIterator : ScriptIterator

View File

@ -41,6 +41,7 @@ namespace Upsilon.BaseTypes.ScriptTable
EvaluationScope.CreateLocal(new VariableSymbol(s, value.Type, false), value); EvaluationScope.CreateLocal(new VariableSymbol(s, value.Type, false), value);
} }
public ScriptType GetValueFromIndex(ScriptType index) public ScriptType GetValueFromIndex(ScriptType index)
{ {
if (EvaluationScope.TryGet(index.ToString(), out var result)) if (EvaluationScope.TryGet(index.ToString(), out var result))
@ -52,6 +53,10 @@ namespace Upsilon.BaseTypes.ScriptTable
public abstract IEnumerator<ScriptType> GetScriptEnumerator(); public abstract IEnumerator<ScriptType> GetScriptEnumerator();
public abstract bool Contains(ScriptType obj); public abstract bool Contains(ScriptType obj);
public void Delete(ScriptType key)
{
EvaluationScope.Delete(key.ToString());
}
public ScriptNumberLong Length() public ScriptNumberLong Length()
{ {

View File

@ -7,5 +7,6 @@ namespace Upsilon.BaseTypes.ScriptTypeInterfaces
ScriptType GetValueFromIndex(ScriptType index); ScriptType GetValueFromIndex(ScriptType index);
IEnumerator<ScriptType> GetScriptEnumerator(); IEnumerator<ScriptType> GetScriptEnumerator();
bool Contains(ScriptType obj); bool Contains(ScriptType obj);
void Delete(ScriptType key);
} }
} }

View File

@ -114,5 +114,10 @@ namespace Upsilon.BaseTypes.UserData
var cSharpObj = obj.ToCSharpObject(); var cSharpObj = obj.ToCSharpObject();
return Dictionary.Contains(cSharpObj); return Dictionary.Contains(cSharpObj);
} }
public void Delete(ScriptType key)
{
Dictionary.Remove(key.ToCSharpObject());
}
} }
} }

View File

@ -144,6 +144,11 @@ namespace Upsilon.BaseTypes.UserData
return List.Contains(cSharpObj); return List.Contains(cSharpObj);
} }
public void Delete(ScriptType key)
{
List.Remove(key.ToCSharpObject());
}
public ScriptNumberLong Length() public ScriptNumberLong Length()
{ {
return new ScriptNumberLong(List.Count); return new ScriptNumberLong(List.Count);

View File

@ -16,29 +16,9 @@ namespace Upsilon.StandardLibraries
} }
[ScriptFunction("remove", "Removes an object from a table.", directScriptManipulation: true)] [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); scriptTable.Delete(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());
}
} }
} }
} }