Skip to content

std.EnumArray: edit return of iterator.next() #23701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 120 additions & 107 deletions lib/std/enums.zig
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,115 @@ pub fn EnumSet(comptime E: type) type {
};
}

test "pure EnumSet fns" {
const Suit = enum { spades, hearts, clubs, diamonds };

const empty = EnumSet(Suit).initEmpty();
const full = EnumSet(Suit).initFull();
const black = EnumSet(Suit).initMany(&[_]Suit{ .spades, .clubs });
const red = EnumSet(Suit).initMany(&[_]Suit{ .hearts, .diamonds });

try testing.expect(empty.eql(empty));
try testing.expect(full.eql(full));
try testing.expect(!empty.eql(full));
try testing.expect(!full.eql(empty));
try testing.expect(!empty.eql(black));
try testing.expect(!full.eql(red));
try testing.expect(!red.eql(empty));
try testing.expect(!black.eql(full));

try testing.expect(empty.subsetOf(empty));
try testing.expect(empty.subsetOf(full));
try testing.expect(full.subsetOf(full));
try testing.expect(!black.subsetOf(red));
try testing.expect(!red.subsetOf(black));

try testing.expect(full.supersetOf(full));
try testing.expect(full.supersetOf(empty));
try testing.expect(empty.supersetOf(empty));
try testing.expect(!black.supersetOf(red));
try testing.expect(!red.supersetOf(black));

try testing.expect(empty.complement().eql(full));
try testing.expect(full.complement().eql(empty));
try testing.expect(black.complement().eql(red));
try testing.expect(red.complement().eql(black));

try testing.expect(empty.unionWith(empty).eql(empty));
try testing.expect(empty.unionWith(full).eql(full));
try testing.expect(full.unionWith(full).eql(full));
try testing.expect(full.unionWith(empty).eql(full));
try testing.expect(black.unionWith(red).eql(full));
try testing.expect(red.unionWith(black).eql(full));

try testing.expect(empty.intersectWith(empty).eql(empty));
try testing.expect(empty.intersectWith(full).eql(empty));
try testing.expect(full.intersectWith(full).eql(full));
try testing.expect(full.intersectWith(empty).eql(empty));
try testing.expect(black.intersectWith(red).eql(empty));
try testing.expect(red.intersectWith(black).eql(empty));

try testing.expect(empty.xorWith(empty).eql(empty));
try testing.expect(empty.xorWith(full).eql(full));
try testing.expect(full.xorWith(full).eql(empty));
try testing.expect(full.xorWith(empty).eql(full));
try testing.expect(black.xorWith(red).eql(full));
try testing.expect(red.xorWith(black).eql(full));

try testing.expect(empty.differenceWith(empty).eql(empty));
try testing.expect(empty.differenceWith(full).eql(empty));
try testing.expect(full.differenceWith(full).eql(empty));
try testing.expect(full.differenceWith(empty).eql(full));
try testing.expect(full.differenceWith(red).eql(black));
try testing.expect(full.differenceWith(black).eql(red));
}

test "EnumSet empty" {
const E = enum {};
const empty = EnumSet(E).initEmpty();
const full = EnumSet(E).initFull();

try testing.expect(empty.eql(full));
try testing.expect(empty.complement().eql(full));
try testing.expect(empty.complement().eql(full.complement()));
try testing.expect(empty.eql(full.complement()));
}

test "EnumSet const iterator" {
const Direction = enum { up, down, left, right };
const diag_move = init: {
var move = EnumSet(Direction).initEmpty();
move.insert(.right);
move.insert(.up);
break :init move;
};

var result = EnumSet(Direction).initEmpty();
var it = diag_move.iterator();
while (it.next()) |dir| {
result.insert(dir);
}

try testing.expect(result.eql(diag_move));
}

test "EnumSet non-exhaustive" {
const BitIndices = enum(u4) {
a = 0,
b = 1,
c = 4,
_,
};
const BitField = EnumSet(BitIndices);

var flags = BitField.init(.{ .a = true, .b = true });
flags.insert(.c);
flags.remove(.a);
try testing.expect(!flags.contains(.a));
try testing.expect(flags.contains(.b));
try testing.expect(flags.contains(.c));
}

/// A map keyed by an enum, backed by a bitfield and a dense array.
/// If the enum is exhaustive but not dense, a mapping will be constructed from
/// enum values to dense indices. This type does no dynamic
Expand Down Expand Up @@ -1140,7 +1249,7 @@ pub fn EnumArray(comptime E: type, comptime V: type) type {
key: Key,

/// A pointer to the value in the array associated
/// with this key. Modifications through this
/// with this key. Modifications through this
/// pointer will modify the underlying data.
value: *Value,
};
Expand All @@ -1153,7 +1262,7 @@ pub fn EnumArray(comptime E: type, comptime V: type) type {
const index = self.index;
if (index < Indexer.count) {
self.index += 1;
return Entry{
return .{
.key = Indexer.keyForIndex(index),
.value = &self.values[index],
};
Expand All @@ -1164,113 +1273,17 @@ pub fn EnumArray(comptime E: type, comptime V: type) type {
};
}

test "pure EnumSet fns" {
const Suit = enum { spades, hearts, clubs, diamonds };

const empty = EnumSet(Suit).initEmpty();
const full = EnumSet(Suit).initFull();
const black = EnumSet(Suit).initMany(&[_]Suit{ .spades, .clubs });
const red = EnumSet(Suit).initMany(&[_]Suit{ .hearts, .diamonds });

try testing.expect(empty.eql(empty));
try testing.expect(full.eql(full));
try testing.expect(!empty.eql(full));
try testing.expect(!full.eql(empty));
try testing.expect(!empty.eql(black));
try testing.expect(!full.eql(red));
try testing.expect(!red.eql(empty));
try testing.expect(!black.eql(full));

try testing.expect(empty.subsetOf(empty));
try testing.expect(empty.subsetOf(full));
try testing.expect(full.subsetOf(full));
try testing.expect(!black.subsetOf(red));
try testing.expect(!red.subsetOf(black));

try testing.expect(full.supersetOf(full));
try testing.expect(full.supersetOf(empty));
try testing.expect(empty.supersetOf(empty));
try testing.expect(!black.supersetOf(red));
try testing.expect(!red.supersetOf(black));

try testing.expect(empty.complement().eql(full));
try testing.expect(full.complement().eql(empty));
try testing.expect(black.complement().eql(red));
try testing.expect(red.complement().eql(black));

try testing.expect(empty.unionWith(empty).eql(empty));
try testing.expect(empty.unionWith(full).eql(full));
try testing.expect(full.unionWith(full).eql(full));
try testing.expect(full.unionWith(empty).eql(full));
try testing.expect(black.unionWith(red).eql(full));
try testing.expect(red.unionWith(black).eql(full));

try testing.expect(empty.intersectWith(empty).eql(empty));
try testing.expect(empty.intersectWith(full).eql(empty));
try testing.expect(full.intersectWith(full).eql(full));
try testing.expect(full.intersectWith(empty).eql(empty));
try testing.expect(black.intersectWith(red).eql(empty));
try testing.expect(red.intersectWith(black).eql(empty));

try testing.expect(empty.xorWith(empty).eql(empty));
try testing.expect(empty.xorWith(full).eql(full));
try testing.expect(full.xorWith(full).eql(empty));
try testing.expect(full.xorWith(empty).eql(full));
try testing.expect(black.xorWith(red).eql(full));
try testing.expect(red.xorWith(black).eql(full));

try testing.expect(empty.differenceWith(empty).eql(empty));
try testing.expect(empty.differenceWith(full).eql(empty));
try testing.expect(full.differenceWith(full).eql(empty));
try testing.expect(full.differenceWith(empty).eql(full));
try testing.expect(full.differenceWith(red).eql(black));
try testing.expect(full.differenceWith(black).eql(red));
}

test "EnumSet empty" {
const E = enum {};
const empty = EnumSet(E).initEmpty();
const full = EnumSet(E).initFull();

try std.testing.expect(empty.eql(full));
try std.testing.expect(empty.complement().eql(full));
try std.testing.expect(empty.complement().eql(full.complement()));
try std.testing.expect(empty.eql(full.complement()));
}

test "EnumSet const iterator" {
const Direction = enum { up, down, left, right };
const diag_move = init: {
var move = EnumSet(Direction).initEmpty();
move.insert(.right);
move.insert(.up);
break :init move;
};

var result = EnumSet(Direction).initEmpty();
var it = diag_move.iterator();
while (it.next()) |dir| {
result.insert(dir);
}

try testing.expect(result.eql(diag_move));
}
test "EnumArray iterator for one field" {
const Enum0 = std.meta.FieldEnum(struct { x: u32 });
const Enum1 = enum { x };

test "EnumSet non-exhaustive" {
const BitIndices = enum(u4) {
a = 0,
b = 1,
c = 4,
_,
};
const BitField = EnumSet(BitIndices);
var a = std.EnumArray(Enum0, u32).initFill(123);
var it0 = a.iterator();
try testing.expectEqual(it0.next().?.key, Enum0.x);

var flags = BitField.init(.{ .a = true, .b = true });
flags.insert(.c);
flags.remove(.a);
try testing.expect(!flags.contains(.a));
try testing.expect(flags.contains(.b));
try testing.expect(flags.contains(.c));
var b = std.EnumArray(Enum1, u32).initFill(123);
var it1 = b.iterator();
try testing.expectEqual(it1.next().?.key, Enum1.x);
}

pub fn EnumIndexer(comptime E: type) type {
Expand Down
8 changes: 5 additions & 3 deletions test/behavior/type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,18 @@ test "Type.Enum" {
try testing.expectEqual(@as(u32, 5), @intFromEnum(Bar.b));
try testing.expectEqual(@as(u32, 6), @intFromEnum(@as(Bar, @enumFromInt(6))));

{ // from https://github.com/ziglang/zig/issues/19985
{ // https://github.com/ziglang/zig/issues/19985
{ // enum with single field can be initialized.
const E = @Type(.{ .@"enum" = .{
.tag_type = u0,
.is_exhaustive = true,
.fields = &.{.{ .name = "foo", .value = 0 }},
.decls = &.{},
} });
const s: struct { E } = .{.foo};
try testing.expectEqual(.foo, s[0]);
const s0: struct { E } = .{.foo};
const s1: struct { k: E } = .{ .k = .foo };
try testing.expectEqual(.foo, s0[0]);
try testing.expectEqual(.foo, s1.k);
}

{ // meta.FieldEnum() with single field
Expand Down
Loading