Better handling of list.contains()

This commit is contained in:
Deukhoofd 2019-02-25 13:39:45 +01:00
parent 6bc3469860
commit 768c02c482
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
8 changed files with 29 additions and 9 deletions

View File

@ -30,7 +30,10 @@ namespace Upsilon.BaseTypes
} }
public abstract IEnumerator<ScriptType> GetScriptEnumerator(); public abstract IEnumerator<ScriptType> GetScriptEnumerator();
public bool Contains(ScriptType obj)
{
return false;
}
} }
internal class UpTillNullPairsScriptIterator : ScriptIterator internal class UpTillNullPairsScriptIterator : ScriptIterator

View File

@ -28,5 +28,10 @@ namespace Upsilon.BaseTypes.ScriptTable
yield return variable.Key.ToScriptType(); yield return variable.Key.ToScriptType();
} }
} }
public override bool Contains(ScriptType obj)
{
return EvaluationScope.TryGet(obj.ToString(), out _);
}
} }
} }

View File

@ -19,6 +19,11 @@ namespace Upsilon.BaseTypes.ScriptTable
} }
} }
public override bool Contains(ScriptType obj)
{
return EvaluationScope.Variables.Values.Contains(obj);
}
public override System.Type GetCSharpType() public override System.Type GetCSharpType()
{ {

View File

@ -51,6 +51,7 @@ namespace Upsilon.BaseTypes.ScriptTable
} }
public abstract IEnumerator<ScriptType> GetScriptEnumerator(); public abstract IEnumerator<ScriptType> GetScriptEnumerator();
public abstract bool Contains(ScriptType obj);
public ScriptNumberLong Length() public ScriptNumberLong Length()
{ {

View File

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

View File

@ -108,5 +108,11 @@ namespace Upsilon.BaseTypes.UserData
return (from object key in Dictionary.Keys return (from object key in Dictionary.Keys
select key.ToScriptType()).GetEnumerator(); select key.ToScriptType()).GetEnumerator();
} }
public bool Contains(ScriptType obj)
{
var cSharpObj = obj.ToCSharpObject();
return Dictionary.Contains(cSharpObj);
}
} }
} }

View File

@ -138,6 +138,12 @@ namespace Upsilon.BaseTypes.UserData
} }
} }
public bool Contains(ScriptType obj)
{
var cSharpObj = obj.ToCSharpObject();
return List.Contains(cSharpObj);
}
public ScriptNumberLong Length() public ScriptNumberLong Length()
{ {
return new ScriptNumberLong(List.Count); return new ScriptNumberLong(List.Count);

View File

@ -12,14 +12,7 @@ namespace Upsilon.StandardLibraries
[ScriptFunction("contains", "Returns a boolean that defines whether a table contains a value", directScriptManipulation: true)] [ScriptFunction("contains", "Returns a boolean that defines whether a table contains a value", directScriptManipulation: true)]
public ScriptBoolean Contains(IIterable table, ScriptType obj) public ScriptBoolean Contains(IIterable table, ScriptType obj)
{ {
var enumerator = table.GetScriptEnumerator(); return table.Contains(obj);
while (enumerator.MoveNext())
{
var current = enumerator.Current;
if (current == obj)
return true;
}
return false;
} }
[ScriptFunction("remove", "Removes an object from a table.", directScriptManipulation: true)] [ScriptFunction("remove", "Removes an object from a table.", directScriptManipulation: true)]