pub fn BoundedEnumMultiset(comptime E: type, comptime CountSize: type) type counts: EnumArray(E, CountSize)pub fn init(init_counts: EnumFieldStruct(E, CountSize, 0)) SelfInitializes the multiset using a struct of counts.
pub fn initWithCount(comptime c: CountSize) SelfInitializes the multiset with all keys at the same count.
pub fn count(self: Self) usizeReturns the total number of key counts in the multiset.
pub fn contains(self: Self, key: E) boolChecks if at least one key in multiset.
pub fn removeAll(self: *Self, key: E) voidRemoves all instance of a key from multiset. Same as setCount(key, 0).
pub fn addAssertSafe(self: *Self, key: E, c: CountSize) voidIncreases the key count by given amount. Caller asserts operation will not overflow.
pub fn add(self: *Self, key: E, c: CountSize) errorIncreases the key count by given amount.
pub fn remove(self: *Self, key: E, c: CountSize) voidDecreases the key count by given amount. If amount is greater than the number of keys in multset, then key count will be set to zero.
pub fn getCount(self: Self, key: E) CountSizeReturns the count for a key.
pub fn setCount(self: *Self, key: E, c: CountSize) voidSet the count for a key.
pub fn addSetAssertSafe(self: *Self, other: Self) voidIncreases the all key counts by given multiset. Caller asserts operation will not overflow any key.
pub fn addSet(self: *Self, other: Self) errorIncreases the all key counts by given multiset.
pub fn removeSet(self: *Self, other: Self) voidDecreases the all key counts by given multiset. If the given multiset has more key counts than this, then that key will have a key count of zero.
pub fn eql(self: Self, other: Self) boolReturns true iff all key counts are the same as given multiset.
pub fn subsetOf(self: Self, other: Self) boolReturns true iff all key counts less than or equal to the given multiset.
pub fn supersetOf(self: Self, other: Self) boolReturns true iff all key counts greater than or equal to the given multiset.
pub fn plusAssertSafe(self: Self, other: Self) SelfReturns a multiset with the total key count of this multiset and the other multiset. Caller asserts operation will not overflow any key.
pub fn plus(self: Self, other: Self) errorReturns a multiset with the total key count of this multiset and the other multiset.
pub fn minus(self: Self, other: Self) SelfReturns a multiset with the key count of this multiset minus the corresponding key count in the other multiset. If the other multiset contains more key count than this set, that key will have a count of zero.
pub fn iterator(self: *Self) IteratorReturns an iterator over this multiset. Keys with zero counts are included. Modifications to the set during iteration may or may not be observed by the iterator, but will not invalidate it.
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848pub fn BoundedEnumMultiset(comptime E: type, comptime CountSize: type) type {
return struct {
const Self = @This();
counts: EnumArray(E, CountSize),
/// Initializes the multiset using a struct of counts.
pub fn init(init_counts: EnumFieldStruct(E, CountSize, 0)) Self {
@setEvalBranchQuota(2 * @typeInfo(E).@"enum".field_names.len);
var self = initWithCount(0);
const info = @typeInfo(E).@"enum";
inline for (info.field_names, info.field_values) |field_name, field_value| {
const c = @field(init_counts, field_name);
const key: E = @fromBackingInt(@intCast(field_value));
self.counts.set(key, c);
}
return self;
}
/// A multiset with a count of zero.
pub const empty: Self = .initWithCount(0);
/// Initializes the multiset with all keys at the
/// same count.
pub fn initWithCount(comptime c: CountSize) Self {
return .{
.counts = .initDefault(c, .{}),
};
}
/// Returns the total number of key counts in the multiset.
pub fn count(self: Self) usize {
var sum: usize = 0;
for (self.counts.values) |c| {
sum += c;
}
return sum;
}
/// Checks if at least one key in multiset.
pub fn contains(self: Self, key: E) bool {
return self.counts.get(key) > 0;
}
/// Removes all instance of a key from multiset. Same as
/// setCount(key, 0).
pub fn removeAll(self: *Self, key: E) void {
return self.counts.set(key, 0);
}
/// Increases the key count by given amount. Caller asserts
/// operation will not overflow.
pub fn addAssertSafe(self: *Self, key: E, c: CountSize) void {
self.counts.getPtr(key).* += c;
}
/// Increases the key count by given amount.
pub fn add(self: *Self, key: E, c: CountSize) error{Overflow}!void {
self.counts.set(key, try std.math.add(CountSize, self.counts.get(key), c));
}
/// Decreases the key count by given amount. If amount is
/// greater than the number of keys in multset, then key count
/// will be set to zero.
pub fn remove(self: *Self, key: E, c: CountSize) void {
self.counts.getPtr(key).* -= @min(self.getCount(key), c);
}
/// Returns the count for a key.
pub fn getCount(self: Self, key: E) CountSize {
return self.counts.get(key);
}
/// Set the count for a key.
pub fn setCount(self: *Self, key: E, c: CountSize) void {
self.counts.set(key, c);
}
/// Increases the all key counts by given multiset. Caller
/// asserts operation will not overflow any key.
pub fn addSetAssertSafe(self: *Self, other: Self) void {
inline for (@typeInfo(E).@"enum".field_values) |field_value| {
const key = @as(E, @fromBackingInt(@intCast(field_value)));
self.addAssertSafe(key, other.getCount(key));
}
}
/// Increases the all key counts by given multiset.
pub fn addSet(self: *Self, other: Self) error{Overflow}!void {
inline for (@typeInfo(E).@"enum".field_values) |field_value| {
const key = @as(E, @fromBackingInt(@intCast(field_value)));
try self.add(key, other.getCount(key));
}
}
/// Decreases the all key counts by given multiset. If
/// the given multiset has more key counts than this,
/// then that key will have a key count of zero.
pub fn removeSet(self: *Self, other: Self) void {
inline for (@typeInfo(E).@"enum".field_values) |field_value| {
const key = @as(E, @fromBackingInt(@intCast(field_value)));
self.remove(key, other.getCount(key));
}
}
/// Returns true iff all key counts are the same as
/// given multiset.
pub fn eql(self: Self, other: Self) bool {
inline for (@typeInfo(E).@"enum".field_values) |field_value| {
const key = @as(E, @fromBackingInt(@intCast(field_value)));
if (self.getCount(key) != other.getCount(key)) {
return false;
}
}
return true;
}
/// Returns true iff all key counts less than or
/// equal to the given multiset.
pub fn subsetOf(self: Self, other: Self) bool {
inline for (@typeInfo(E).@"enum".field_values) |field_value| {
const key = @as(E, @fromBackingInt(@intCast(field_value)));
if (self.getCount(key) > other.getCount(key)) {
return false;
}
}
return true;
}
/// Returns true iff all key counts greater than or
/// equal to the given multiset.
pub fn supersetOf(self: Self, other: Self) bool {
inline for (@typeInfo(E).@"enum".field_values) |field_value| {
const key = @as(E, @fromBackingInt(@intCast(field_value)));
if (self.getCount(key) < other.getCount(key)) {
return false;
}
}
return true;
}
/// Returns a multiset with the total key count of this
/// multiset and the other multiset. Caller asserts
/// operation will not overflow any key.
pub fn plusAssertSafe(self: Self, other: Self) Self {
var result = self;
result.addSetAssertSafe(other);
return result;
}
/// Returns a multiset with the total key count of this
/// multiset and the other multiset.
pub fn plus(self: Self, other: Self) error{Overflow}!Self {
var result = self;
try result.addSet(other);
return result;
}
/// Returns a multiset with the key count of this
/// multiset minus the corresponding key count in the
/// other multiset. If the other multiset contains
/// more key count than this set, that key will have
/// a count of zero.
pub fn minus(self: Self, other: Self) Self {
var result = self;
result.removeSet(other);
return result;
}
pub const Entry = EnumArray(E, CountSize).Entry;
pub const Iterator = EnumArray(E, CountSize).Iterator;
/// Returns an iterator over this multiset. Keys with zero
/// counts are included. 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 {
return self.counts.iterator();
}
};
}
A multiset of enum elements up to CountSize. Backed by an EnumArray. This type does no dynamic allocation and can be copied by value.