std.IndexedSet.iterator: allow iteration on const EnumSet

This commit is contained in:
Steven Kabbes 2022-12-17 02:37:03 -07:00 committed by GitHub
parent 270b6c4c2f
commit 90477e5c10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -878,7 +878,7 @@ pub fn IndexedSet(comptime I: type, comptime Ext: fn (type) type) type {
/// index order. Modifications to the set during iteration
/// may or may not be observed by the iterator, but will
/// not invalidate it.
pub fn iterator(self: *Self) Iterator {
pub fn iterator(self: *const Self) Iterator {
return .{ .inner = self.bits.iterator(.{}) };
}
@ -970,6 +970,24 @@ test "pure EnumSet fns" {
try testing.expect(full.differenceWith(black).eql(red));
}
test "std.enums.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));
}
/// A map from keys to values, using an index lookup. Uses a
/// bitfield to track presence and a dense array of values.
/// This type does no allocation and can be copied by value.