Skip to content

Commit

Permalink
Implements list.RemoveAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 committed Jan 22, 2024
1 parent 2c1c73c commit c4e8176
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Content.Tests/DMProject/Tests/Stdlib/List/remove.dm
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@
ASSERT(L[2] == 2)
ASSERT(L[3] == 3)
ASSERT(L[4] == 1)

L = list(1,2,3,2,1)
L.Remove(list(2))
ASSERT(L[2] == 2)
ASSERT(L[3] == 3)
ASSERT(L[4] == 1)
13 changes: 13 additions & 0 deletions Content.Tests/DMProject/Tests/Stdlib/List/removeall.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/proc/RunTest()
var/list/L = list(1,2,3,2,1)
L.RemoveAll(2)
ASSERT(L[2] == 3)
ASSERT(L[3] == 1)
ASSERT(length(L) == 3)

L = list(1,2,3,2,1)
L.RemoveAll(list(2))
ASSERT(L[2] == 3)
ASSERT(L[3] == 1)
ASSERT(length(L) == 3)
1 change: 1 addition & 0 deletions DMCompiler/DMStandard/Types/List.dm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
proc/Insert(Index, Item1)
proc/Join(Glue, Start = 1, End = 0)
proc/Remove(Item1)
proc/RemoveAll(Item1)
proc/Swap(Index1, Index2)

proc/Splice(Start=1,End=0, ...)
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
objectTree.SetNativeProc(objectTree.List, DreamProcNativeList.NativeProc_Insert);
objectTree.SetNativeProc(objectTree.List, DreamProcNativeList.NativeProc_Join);
objectTree.SetNativeProc(objectTree.List, DreamProcNativeList.NativeProc_Remove);
objectTree.SetNativeProc(objectTree.List, DreamProcNativeList.NativeProc_RemoveAll);
objectTree.SetNativeProc(objectTree.List, DreamProcNativeList.NativeProc_Swap);

objectTree.SetNativeProc(objectTree.Matrix, DreamProcNativeMatrix.NativeProc_Add);
Expand Down
27 changes: 22 additions & 5 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,44 @@ public static DreamValue NativeProc_Join(NativeProc.Bundle bundle, DreamObject?
[DreamProcParameter("Item1")]
public static DreamValue NativeProc_Remove(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
DreamList list = (DreamList)src!;
bool itemRemoved = false;
return new DreamValue(ListRemove(list, bundle.Arguments) > 0 ? 1 : 0);
}

foreach (var argument in bundle.Arguments) {
[DreamProc("RemoveAll")]
[DreamProcParameter("Item1")]
public static DreamValue NativeProc_RemoveAll(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
DreamList list = (DreamList)src!;
var totalRemoved = 0;
var removed = 0;
do {
removed = ListRemove(list, bundle.Arguments);
totalRemoved += removed;
} while (removed > 0);

return new DreamValue(totalRemoved);
}

private static int ListRemove(DreamList list, ReadOnlySpan<DreamValue> args) {
var itemRemoved = 0;
foreach (var argument in args) {
if (argument.TryGetValueAsDreamList(out var argumentList)) {
foreach (DreamValue value in argumentList.GetValues()) {
if (list.ContainsValue(value)) {
list.RemoveValue(value);

itemRemoved = true;
itemRemoved++;
}
}
} else {
if (list.ContainsValue(argument)) {
list.RemoveValue(argument);

itemRemoved = true;
itemRemoved++;
}
}
}

return new DreamValue(itemRemoved ? 1 : 0);
return itemRemoved;
}

[DreamProc("Swap")]
Expand Down

0 comments on commit c4e8176

Please sign in to comment.