Type Function EnumMultiset [src]
A multiset of enum elements up to a count of usize. Backed
by an EnumArray. This type does no dynamic allocation and can
be copied by value.
Prototype
pub fn EnumMultiset(comptime E: type) type
Parameters
E: type
Example
test EnumMultiset {
const Ball = enum { red, green, blue };
const empty = EnumMultiset(Ball).initEmpty();
const r0_g1_b2 = EnumMultiset(Ball).init(.{
.red = 0,
.green = 1,
.blue = 2,
});
const ten_of_each = EnumMultiset(Ball).initWithCount(10);
try testing.expectEqual(empty.count(), 0);
try testing.expectEqual(r0_g1_b2.count(), 3);
try testing.expectEqual(ten_of_each.count(), 30);
try testing.expect(!empty.contains(.red));
try testing.expect(!empty.contains(.green));
try testing.expect(!empty.contains(.blue));
try testing.expect(!r0_g1_b2.contains(.red));
try testing.expect(r0_g1_b2.contains(.green));
try testing.expect(r0_g1_b2.contains(.blue));
try testing.expect(ten_of_each.contains(.red));
try testing.expect(ten_of_each.contains(.green));
try testing.expect(ten_of_each.contains(.blue));
{
var copy = ten_of_each;
copy.removeAll(.red);
try testing.expect(!copy.contains(.red));
// removeAll second time does nothing
copy.removeAll(.red);
try testing.expect(!copy.contains(.red));
}
{
var copy = ten_of_each;
copy.addAssertSafe(.red, 6);
try testing.expectEqual(copy.getCount(.red), 16);
}
{
var copy = ten_of_each;
try copy.add(.red, 6);
try testing.expectEqual(copy.getCount(.red), 16);
try testing.expectError(error.Overflow, copy.add(.red, std.math.maxInt(usize)));
}
{
var copy = ten_of_each;
copy.remove(.red, 4);
try testing.expectEqual(copy.getCount(.red), 6);
// subtracting more it contains does not underflow
copy.remove(.green, 14);
try testing.expectEqual(copy.getCount(.green), 0);
}
try testing.expectEqual(empty.getCount(.green), 0);
try testing.expectEqual(r0_g1_b2.getCount(.green), 1);
try testing.expectEqual(ten_of_each.getCount(.green), 10);
{
var copy = empty;
copy.setCount(.red, 6);
try testing.expectEqual(copy.getCount(.red), 6);
}
{
var copy = r0_g1_b2;
copy.addSetAssertSafe(ten_of_each);
try testing.expectEqual(copy.getCount(.red), 10);
try testing.expectEqual(copy.getCount(.green), 11);
try testing.expectEqual(copy.getCount(.blue), 12);
}
{
var copy = r0_g1_b2;
try copy.addSet(ten_of_each);
try testing.expectEqual(copy.getCount(.red), 10);
try testing.expectEqual(copy.getCount(.green), 11);
try testing.expectEqual(copy.getCount(.blue), 12);
const full = EnumMultiset(Ball).initWithCount(std.math.maxInt(usize));
try testing.expectError(error.Overflow, copy.addSet(full));
}
{
var copy = ten_of_each;
copy.removeSet(r0_g1_b2);
try testing.expectEqual(copy.getCount(.red), 10);
try testing.expectEqual(copy.getCount(.green), 9);
try testing.expectEqual(copy.getCount(.blue), 8);
copy.removeSet(ten_of_each);
try testing.expectEqual(copy.getCount(.red), 0);
try testing.expectEqual(copy.getCount(.green), 0);
try testing.expectEqual(copy.getCount(.blue), 0);
}
try testing.expect(empty.eql(empty));
try testing.expect(r0_g1_b2.eql(r0_g1_b2));
try testing.expect(ten_of_each.eql(ten_of_each));
try testing.expect(!empty.eql(r0_g1_b2));
try testing.expect(!r0_g1_b2.eql(ten_of_each));
try testing.expect(!ten_of_each.eql(empty));
try testing.expect(empty.subsetOf(empty));
try testing.expect(r0_g1_b2.subsetOf(r0_g1_b2));
try testing.expect(empty.subsetOf(r0_g1_b2));
try testing.expect(r0_g1_b2.subsetOf(ten_of_each));
try testing.expect(!ten_of_each.subsetOf(r0_g1_b2));
try testing.expect(!r0_g1_b2.subsetOf(empty));
try testing.expect(empty.supersetOf(empty));
try testing.expect(r0_g1_b2.supersetOf(r0_g1_b2));
try testing.expect(r0_g1_b2.supersetOf(empty));
try testing.expect(ten_of_each.supersetOf(r0_g1_b2));
try testing.expect(!r0_g1_b2.supersetOf(ten_of_each));
try testing.expect(!empty.supersetOf(r0_g1_b2));
{
// with multisets it could be the case where two
// multisets are neither subset nor superset of each
// other.
const r10 = EnumMultiset(Ball).init(.{
.red = 10,
});
const b10 = EnumMultiset(Ball).init(.{
.blue = 10,
});
try testing.expect(!r10.subsetOf(b10));
try testing.expect(!b10.subsetOf(r10));
try testing.expect(!r10.supersetOf(b10));
try testing.expect(!b10.supersetOf(r10));
}
{
const result = r0_g1_b2.plusAssertSafe(ten_of_each);
try testing.expectEqual(result.getCount(.red), 10);
try testing.expectEqual(result.getCount(.green), 11);
try testing.expectEqual(result.getCount(.blue), 12);
}
{
const result = try r0_g1_b2.plus(ten_of_each);
try testing.expectEqual(result.getCount(.red), 10);
try testing.expectEqual(result.getCount(.green), 11);
try testing.expectEqual(result.getCount(.blue), 12);
const full = EnumMultiset(Ball).initWithCount(std.math.maxInt(usize));
try testing.expectError(error.Overflow, result.plus(full));
}
{
const result = ten_of_each.minus(r0_g1_b2);
try testing.expectEqual(result.getCount(.red), 10);
try testing.expectEqual(result.getCount(.green), 9);
try testing.expectEqual(result.getCount(.blue), 8);
}
{
const result = ten_of_each.minus(r0_g1_b2).minus(ten_of_each);
try testing.expectEqual(result.getCount(.red), 0);
try testing.expectEqual(result.getCount(.green), 0);
try testing.expectEqual(result.getCount(.blue), 0);
}
{
var copy = empty;
var it = copy.iterator();
var entry = it.next().?;
try testing.expectEqual(entry.key, .red);
try testing.expectEqual(entry.value.*, 0);
entry = it.next().?;
try testing.expectEqual(entry.key, .green);
try testing.expectEqual(entry.value.*, 0);
entry = it.next().?;
try testing.expectEqual(entry.key, .blue);
try testing.expectEqual(entry.value.*, 0);
try testing.expectEqual(it.next(), null);
}
{
var copy = r0_g1_b2;
var it = copy.iterator();
var entry = it.next().?;
try testing.expectEqual(entry.key, .red);
try testing.expectEqual(entry.value.*, 0);
entry = it.next().?;
try testing.expectEqual(entry.key, .green);
try testing.expectEqual(entry.value.*, 1);
entry = it.next().?;
try testing.expectEqual(entry.key, .blue);
try testing.expectEqual(entry.value.*, 2);
try testing.expectEqual(it.next(), null);
}
}
Source
pub fn EnumMultiset(comptime E: type) type {
return BoundedEnumMultiset(E, usize);
}