big: Add swap.

This commit is contained in:
Jeroen van Rijn
2021-08-11 20:59:51 +02:00
parent f34ba44bf8
commit 0254057f1b
3 changed files with 88 additions and 29 deletions
+18 -1
View File
@@ -20,7 +20,10 @@ int_destroy :: proc(integers: ..^Int) {
for a in &integers {
mem.zero_slice(a.digit[:]);
free(&a.digit[0]);
raw := transmute(mem.Raw_Dynamic_Array)a.digit;
if raw.cap > 0 {
free(&a.digit[0]);
}
a = &Int{};
}
}
@@ -84,6 +87,20 @@ int_copy :: proc(dest, src: ^Int, allocator := context.allocator) -> (err: Error
}
copy :: proc { int_copy, };
/*
In normal code, you can also write `a, b = b, a`.
However, that only swaps within the current scope.
This helper swaps completely.
*/
int_swap :: proc(a, b: ^Int) {
a := a; b := b;
a.used, b.used = b.used, a.used;
a.sign, b.sign = b.sign, a.sign;
a.digit, b.digit = b.digit, a.digit;
}
swap :: proc { int_swap, };
/*
Set `dest` to |`src`|.
*/