| ... | ... | @@ -420,6 +420,8 @@ pub fn EnumSet(comptime E: type) type { |
| 420 | 420 | /// If the enum is exhaustive but not dense, a mapping will be constructed from |
| 421 | 421 | /// enum values to dense indices. This type does no dynamic |
| 422 | 422 | /// allocation and can be copied by value. |
| 423 | /// |
| 424 | /// Default initialization is deprecated; use .empty instead. |
| 423 | 425 | pub fn EnumMap(comptime E: type, comptime V: type) type { |
| 424 | 426 | return struct { |
| 425 | 427 | const Self = @This(); |
| ... | ... | @@ -441,6 +443,11 @@ pub fn EnumMap(comptime E: type, comptime V: type) type { |
| 441 | 443 | /// bit is zero, the value is undefined. |
| 442 | 444 | values: [Indexer.count]Value = undefined, |
| 443 | 445 | |
| 446 | pub const empty: Self = .{ |
| 447 | .bits = .empty, |
| 448 | .values = undefined, |
| 449 | }; |
| 450 | |
| 444 | 451 | /// Initializes the map using a sparse struct of optionals |
| 445 | 452 | pub fn init(init_values: EnumFieldStruct(E, ?Value, @as(?Value, null))) Self { |
| 446 | 453 | @setEvalBranchQuota(2 * @typeInfo(E).@"enum".field_names.len); |
| ... | ... | @@ -470,12 +477,10 @@ pub fn EnumMap(comptime E: type, comptime V: type) type { |
| 470 | 477 | /// Initializes a full mapping with all keys set to value. |
| 471 | 478 | /// Consider using EnumArray instead if the map will remain full. |
| 472 | 479 | pub fn initFull(value: Value) Self { |
| 473 | | var result: Self = .{ |
| 480 | return .{ |
| 474 | 481 | .bits = .full, |
| 475 | | .values = undefined, |
| 482 | .values = @splat(value), |
| 476 | 483 | }; |
| 477 | | @memset(&result.values, value); |
| 478 | | return result; |
| 479 | 484 | } |
| 480 | 485 | |
| 481 | 486 | /// Initializes a full mapping with supplied values. |
| ... | ... | @@ -645,7 +650,10 @@ pub fn EnumMap(comptime E: type, comptime V: type) type { |
| 645 | 650 | test EnumMap { |
| 646 | 651 | const Ball = enum { red, green, blue }; |
| 647 | 652 | |
| 648 | | const some = EnumMap(Ball, u8).init(.{ |
| 653 | const none: EnumMap(Ball, u8) = .empty; |
| 654 | try testing.expectEqual(0, none.count()); |
| 655 | |
| 656 | const some: EnumMap(Ball, u8) = .init(.{ |
| 649 | 657 | .green = 0xff, |
| 650 | 658 | .blue = 0x80, |
| 651 | 659 | }); |
| ... | ... | @@ -653,6 +661,9 @@ test EnumMap { |
| 653 | 661 | try testing.expectEqual(null, some.get(.red)); |
| 654 | 662 | try testing.expectEqual(0xff, some.get(.green)); |
| 655 | 663 | try testing.expectEqual(0x80, some.get(.blue)); |
| 664 | |
| 665 | const all: EnumMap(Ball, u8) = .initFull(0); |
| 666 | try testing.expectEqual(3, all.count()); |
| 656 | 667 | } |
| 657 | 668 | |
| 658 | 669 | /// A multiset of enum elements up to a count of usize. Backed |