diff --git a/examples/aoc2023/day07/part1.jou b/examples/aoc2023/day07/part1.jou index 60c0eea9..989dc94a 100644 --- a/examples/aoc2023/day07/part1.jou +++ b/examples/aoc2023/day07/part1.jou @@ -1,5 +1,6 @@ import "stdlib/io.jou" import "stdlib/str.jou" +import "stdlib/mem.jou" class Hand: letters: byte[6] @@ -110,18 +111,12 @@ class Hand: return 0 -def swap(h1: Hand*, h2: Hand*) -> None: - temp = *h1 - *h1 = *h2 - *h2 = temp - - def sort(hands: Hand*, nhands: int) -> None: # bubble sort go brrr for sorted_part_len = 1; sorted_part_len < nhands; sorted_part_len++: i = sorted_part_len while i > 0 and hands[i-1].compare(&hands[i]) == 1: - swap(&hands[i-1], &hands[i]) + memswap(&hands[i-1], &hands[i], sizeof(hands[i])) i-- diff --git a/examples/aoc2023/day07/part2.jou b/examples/aoc2023/day07/part2.jou index 205edb6c..7f2f7216 100644 --- a/examples/aoc2023/day07/part2.jou +++ b/examples/aoc2023/day07/part2.jou @@ -145,18 +145,12 @@ class Hand: return 0 -def swap(h1: Hand*, h2: Hand*) -> None: - temp = *h1 - *h1 = *h2 - *h2 = temp - - def sort(hands: Hand*, nhands: int) -> None: # bubble sort go brrr for sorted_part_len = 1; sorted_part_len < nhands; sorted_part_len++: i = sorted_part_len while i > 0 and hands[i-1].compare(&hands[i]) == 1: - swap(&hands[i-1], &hands[i]) + memswap(&hands[i-1], &hands[i], sizeof(hands[i])) i-- diff --git a/examples/aoc2023/day08/part2.jou b/examples/aoc2023/day08/part2.jou index a75311b6..efebad69 100644 --- a/examples/aoc2023/day08/part2.jou +++ b/examples/aoc2023/day08/part2.jou @@ -62,18 +62,13 @@ class List: self->append(*v) -def swap(a: long*, b: long*) -> None: - temp = *a - *a = *b - *b = temp - def gcd(a: long, b: long) -> long: assert a > 0 and b > 0 while True: a %= b if a == 0: return b - swap(&a, &b) + memswap(&a, &b, sizeof(a)) def lcm(a: long, b: long) -> long: return (a/gcd(a,b)) * b diff --git a/examples/aoc2023/day09/part2.jou b/examples/aoc2023/day09/part2.jou index f3521c2c..8f852776 100644 --- a/examples/aoc2023/day09/part2.jou +++ b/examples/aoc2023/day09/part2.jou @@ -23,17 +23,11 @@ def predict_next(nums: long*, len: int) -> long: return result -def swap(a: long*, b: long*) -> None: - tmp = *a - *a = *b - *b = tmp - - def reverse(nums: long*, len: int) -> None: p = nums q = &nums[len-1] while p < q: - swap(p++, q--) + memswap(p++, q--, sizeof(*p)) # return value is an array terminated by nums_len=-1 diff --git a/examples/aoc2023/day20/part2.jou b/examples/aoc2023/day20/part2.jou index 7060bf9f..9b698717 100644 --- a/examples/aoc2023/day20/part2.jou +++ b/examples/aoc2023/day20/part2.jou @@ -171,19 +171,13 @@ def run_part_of_input(start_flip_flop: Module*, end: Module*) -> int[5]: assert False -def swap(a: long*, b: long*) -> None: - old_a = *a - *a = *b - *b = old_a - - def gcd(a: long, b: long) -> long: assert a > 0 and b > 0 while True: a %= b if a == 0: return b - swap(&a, &b) + memswap(&a, &b, sizeof(a)) def lcm(a: long, b: long) -> long: return (a/gcd(a,b)) * b diff --git a/examples/aoc2024/day01/part1.jou b/examples/aoc2024/day01/part1.jou index 3cd813c1..cd6ff781 100644 --- a/examples/aoc2024/day01/part1.jou +++ b/examples/aoc2024/day01/part1.jou @@ -1,11 +1,6 @@ import "stdlib/io.jou" import "stdlib/math.jou" - - -def swap(a: int*, b: int*) -> None: - temp = *a - *a = *b - *b = temp +import "stdlib/mem.jou" def sort(array: int*, length: int) -> None: @@ -15,7 +10,7 @@ def sort(array: int*, length: int) -> None: for i = 1; i < length; i++: if array[i] < array[smallest]: smallest = i - swap(&array[0], &array[smallest]) + memswap(&array[0], &array[smallest], sizeof(array[0])) array++ length-- diff --git a/examples/aoc2024/day05/part2.jou b/examples/aoc2024/day05/part2.jou index aa8efa9d..25755f9c 100644 --- a/examples/aoc2024/day05/part2.jou +++ b/examples/aoc2024/day05/part2.jou @@ -76,12 +76,6 @@ def job_is_valid(rules: int[2]*, nrules: int, job: int*) -> bool: return True -def swap(a: int*, b: int*) -> None: - old_a = *a - *a = *b - *b = old_a - - def fix_job(rules: int[2]*, nrules: int, job: int*) -> None: length = 0 while job[length] != -1: @@ -94,7 +88,7 @@ def fix_job(rules: int[2]*, nrules: int, job: int*) -> None: first_idx = find_page(job, rule[0]) second_idx = find_page(job, rule[1]) if first_idx != -1 and second_idx != -1 and first_idx >= second_idx: - swap(&job[first_idx], &job[second_idx]) + memswap(&job[first_idx], &job[second_idx], sizeof(job[0])) def main() -> int: diff --git a/examples/quicksort.jou b/examples/quicksort.jou index b7cf5a98..0f3d5754 100644 --- a/examples/quicksort.jou +++ b/examples/quicksort.jou @@ -5,12 +5,7 @@ # Maybe this will some day become a part of the standard library :) import "stdlib/io.jou" - - -def swap(a: int*, b: int*) -> None: - temp = *a - *a = *b - *b = temp +import "stdlib/mem.jou" def print_array(prefix: byte*, arr: int*, length: int) -> None: @@ -90,7 +85,7 @@ def quicksort(arr: int*, length: int, depth: int) -> None: # neither range can expand because of >pivot and pivot assert arr[start_of_big - 1] < pivot - swap(&arr[end_of_small], &arr[start_of_big - 1]) + memswap(&arr[end_of_small], &arr[start_of_big - 1], sizeof(arr[0])) # Add back the removed pivot. It becomes a part of the overlap. arr[length++] = arr[end_of_small]