From a68efec5e8e7e2e2dad1aec5a9515a69720a627d Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 14 Jul 2022 14:58:11 +0200 Subject: [PATCH] Added swap and list swap to stdlib. --- lib/std/core/builtin.c3 | 7 +++++++ lib/std/list.c3 | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/lib/std/core/builtin.c3 b/lib/std/core/builtin.c3 index c07cf75f3..378866f79 100644 --- a/lib/std/core/builtin.c3 +++ b/lib/std/core/builtin.c3 @@ -27,6 +27,13 @@ macro void @scope(&variable; @body) @autoimport @body(); } +macro void @swap(&a, &b) @autoimport +{ + $typeof(a) temp = a; + a = b; + b = temp; +} + /** * Convert a variant type to a type, returning an failure if there is a type mismatch. * diff --git a/lib/std/list.c3 b/lib/std/list.c3 index 45757fecc..c8901b3b1 100644 --- a/lib/std/list.c3 +++ b/lib/std/list.c3 @@ -115,6 +115,10 @@ fn void List.free(List *list) list.size = 0; } +fn void List.swap(List *list, usize i, usize j) +{ + @swap(list.entries[i], list.entries[j]); +} macro Type List.@item_at(List &list, usize index) @operator(elementat) {