diff --git a/examples/aoc2023/day07/part1.jou b/examples/aoc2023/day07/part1.jou index 60c0eea93..989dc94ae 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 205edb6c0..7f2f72160 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 a75311b6c..efebad69d 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 f3521c2c3..8f8527768 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 7060bf9f8..9b6987170 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 3cd813c1e..cd6ff7815 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 aa8efa9de..25755f9cd 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/aoc2024/day09/part1.jou b/examples/aoc2024/day09/part1.jou index 12e30b946..063307faa 100644 --- a/examples/aoc2024/day09/part1.jou +++ b/examples/aoc2024/day09/part1.jou @@ -31,12 +31,6 @@ def parse_disk_map(disk_map_string: byte*) -> int*: return result -def swap(a: int*, b: int*) -> None: - temp = *a - *a = *b - *b = temp - - def fill_slots(disk_map: int*) -> None: blankptr = disk_map @@ -51,7 +45,7 @@ def fill_slots(disk_map: int*) -> None: elif *last == -1: last-- else: - swap(blankptr, last) + memswap(blankptr, last, sizeof(*blankptr)) def compute_checksum(disk_map: int*) -> long: diff --git a/examples/aoc2024/day09/part2.jou b/examples/aoc2024/day09/part2.jou index 67b53e347..c80afcbc9 100644 --- a/examples/aoc2024/day09/part2.jou +++ b/examples/aoc2024/day09/part2.jou @@ -31,17 +31,6 @@ def parse_disk_map(disk_map_string: byte*) -> int*: return result -def swap(a: int*, b: int*) -> None: - temp = *a - *a = *b - *b = temp - - -def swap_slice(a: int*, b: int*, length: int) -> None: - for i = 0; i < length; i++: - swap(&a[i], &b[i]) - - def find_blank_spot(start: int*, end: int*, length_required: int) -> int*: for p = start; p <= &end[-length_required]; p++: ok = True @@ -74,7 +63,7 @@ def fill_slots(disk_map: int*) -> None: blank_spot = find_blank_spot(disk_map, start, length) if blank_spot != NULL: - swap_slice(blank_spot, start, length) + memswap(blank_spot, start, length * sizeof(disk_map[0])) def compute_checksum(disk_map: int*) -> long: diff --git a/examples/quicksort.jou b/examples/quicksort.jou index b7cf5a983..0f3d57549 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]