Skip to content

Commit

Permalink
.github, docs, exercises: bump Zig from 0.11.0 to 0.12.0 (#392)
Browse files Browse the repository at this point in the history
Zig 0.12.0 was released on 2024-04-20. See the release notes [1].

Also simplify the sieve example implementation again for now,
essentially reverting a previous commit [2]. With 0.12.0, the previous
implementation produced the error:

    Running tests for sieve...
    ./bin/run-tests: line 23:  2287 Trace/breakpoint trap   (core dumped) zig test "test_${exercise_name}.zig"

[1] https://ziglang.org/download/0.12.0/release-notes.html
[2] 1bcf8f1, 2023-03-23, "exercises(sieve): example: generate primes at compile time"
  • Loading branch information
ee7 authored Apr 21, 2024
1 parent 7151ce9 commit ebb9581
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/bin/install-zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ esac

arch="$(uname -m)"

version='0.11.0'
version='0.12.0'
url="https://ziglang.org/download/${version}/zig-${os}-${arch}-${version}.${ext}"

curlopts=(
Expand Down
2 changes: 1 addition & 1 deletion docs/INSTALLATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Fortunately, all of the popular installation methods are listed on the [Zig inst

## Zig version

Exercism currently supports Zig 0.11.0 (released on 2023-08-04) only.
Exercism currently supports Zig 0.12.0 (released on 2024-04-20) only.

An exercise may be compatible with a different Zig version, but that isn't guaranteed.
Zig has not yet reached version 1.0, and breaking changes are common.
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/allergies/.docs/instructions.append.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

It may be helpful to look at the implementation of [`std.enums.EnumSet`][enumset].

[enumset]: https://github.com/ziglang/zig/blob/0.11.0/lib/std/enums.zig#L236-L262
[enumset]: https://github.com/ziglang/zig/blob/0.12.0/lib/std/enums.zig#L243-L247
2 changes: 1 addition & 1 deletion exercises/practice/anagram/.docs/instructions.append.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ For more details on hash maps in Zig, see:
- [Zighelp - Hash Maps][zighelp]
- [Zig Standard Library - `buf_set.zig`][buf-set]

[buf-set]: https://github.com/ziglang/zig/blob/0.11.0/lib/std/buf_set.zig
[buf-set]: https://github.com/ziglang/zig/blob/0.12.0/lib/std/buf_set.zig
[zig-hashmaps-explained]: https://devlog.hexops.com/2022/zig-hashmaps-explained/
[zighelp]: https://zighelp.org/chapter-2/#hash-maps
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ For more details, see the [Zig Language Reference][zig-reference] and the implem

However, note that this exercise does not currently test an input of 0 (because `std.testing` does [not yet support expecting a panic][proposal]).

[zig-reference]: https://ziglang.org/documentation/0.11.0/#unreachable
[assert]: https://github.com/ziglang/zig/blob/0.11.0/lib/std/debug.zig#L332-L344
[zig-reference]: https://ziglang.org/documentation/0.12.0/#unreachable
[assert]: https://github.com/ziglang/zig/blob/0.12.0/lib/std/debug.zig#L392-L404
[proposal]: https://github.com/ziglang/zig/issues/1356
72 changes: 24 additions & 48 deletions exercises/practice/sieve/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -1,53 +1,29 @@
const std = @import("std");
const assert = std.debug.assert;
const sqrt = std.math.sqrt;

/// Returns bools, where the value at index `i` is `true` iff `i` is a prime number.
fn eratosthenes(comptime n: u32) []const bool {
var result = [_]bool{false} ** (n + 1);
result[2] = true;

var i = 3;
while (i <= n) : (i += 2) {
result[i] = true;
}

i = 3;
while (i <= sqrt(n)) : (i += 2) { // Optimization: stop at sqrt(n)
if (result[i]) {
var j = i * i; // Optimization: start at i*i
while (j < n) : (j += 2 * i) {
result[j] = false;
}
}
}
return &result;
}

/// Returns the prime numbers up to `limit`.
fn genPrimes(comptime limit: u32) []const u32 {
@setEvalBranchQuota(limit * 3);
const bools = comptime eratosthenes(limit);
// See https://en.wikipedia.org/wiki/Prime-counting_function#Inequalities
const upper_bound_count = @ceil(1.25506 * @as(f64, limit) / @log(@as(f64, limit)));
var result: [upper_bound_count]u32 = undefined;
var j = 0;
for (bools, 0..) |b, i| {
if (b) {
result[j] = i;
j += 1;
}
}
return result[0..j];
}
const assert = @import("std").debug.assert;

pub fn primes(buffer: []u32, comptime limit: u32) []const u32 {
const sieve_limit = 1000;
comptime assert(limit <= sieve_limit);
const p = comptime genPrimes(sieve_limit); // The primes up to the sieve limit.
comptime assert(limit <= 1000);
const primes_under_1000 = [_]u32{
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997,
};
_ = buffer;
if (limit >= p[p.len - 1]) return p;
if (limit >= primes_under_1000[primes_under_1000.len - 1]) return &primes_under_1000;
var i: usize = 0;
while (p[i] <= limit) : (i += 1) {}
return p[0..i];
while (primes_under_1000[i] <= limit) : (i += 1) {}
return primes_under_1000[0..i];
}

0 comments on commit ebb9581

Please sign in to comment.