1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Query methods for [`BitSet`] that make sense for sets over any universe.
//!
//! For methods specific to integer sets, see [`math`].
use crate::*;
impl<W: Word> BitSet<W> {
/// Number of elements in the set.
///
/// # Examples
///
/// ```
/// # use pibs::prelude::*;
/// assert_eq!(set![4..=6].len(), 3);
/// ```
#[inline]
pub fn len(self) -> usize {
self.0.count_ones() as usize
}
/// Whether the set contains no elements.
///
/// # Examples
///
/// ```
/// # use pibs::prelude::*;
/// assert!(Set::new().is_empty());
/// ```
#[inline]
pub fn is_empty(self) -> bool {
self.0 == W::ZERO
}
/// Whether the set contains a given element.
///
/// # Preconditions
///
/// The caller must ensure that `e <= Self::MAX`.
///
/// # Examples
///
/// ```
/// # use pibs::prelude::*;
/// assert_eq!(set![4, 5, 6].contains(5), true);
/// assert_eq!(set![4, 5, 6].contains(8), false);
/// ```
#[inline]
pub fn contains(self, e: Element) -> bool {
Self::debug_bound_check(e);
self.0 & (W::ONE << e) != W::ZERO
}
/// Whether `self` is a (non-strict) subset of `other`.
///
/// This can also be written as `self <= other`.
///
/// # Examples
///
/// ```
/// # use pibs::prelude::*;
/// assert_eq!(set![1, 2].is_subset(set![1, 2]), true);
/// assert_eq!(set![1, 2].is_subset(set![1, 2, 3]), true);
/// assert_eq!(set![1, 2, 3].is_subset(set![1, 2]), false);
/// ```
#[inline]
pub fn is_subset(self, other: Self) -> bool {
self.0 & !other.0 == W::ZERO
}
/// Whether `self` is a (non-strict) superset of `other`.
///
/// This can also be written as `self >= other`.
///
/// # Examples
///
/// ```
/// # use pibs::prelude::*;
/// assert_eq!(set![1, 2].is_superset(set![1, 2]), true);
/// assert_eq!(set![1, 2].is_superset(set![1, 2, 3]), false);
/// assert_eq!(set![1, 2, 3].is_superset(set![1, 2]), true);
/// ```
#[inline]
pub fn is_superset(self, other: Self) -> bool {
!self.0 & other.0 == W::ZERO
}
/// Whether `self` is a strict subset of `other`.
///
/// This can also be written as `self < other`.
///
/// # Examples
///
/// ```
/// # use pibs::prelude::*;
/// assert_eq!(set![1, 2].is_strict_subset(set![1, 2]), false);
/// assert_eq!(set![1, 2].is_strict_subset(set![1, 2, 3]), true);
/// assert_eq!(set![1, 2, 3].is_strict_subset(set![1, 2]), false);
/// ```
#[inline]
pub fn is_strict_subset(self, other: Self) -> bool {
self != other && self.is_subset(other)
}
/// Whether `self` is a strict superset of `other`.
///
/// This can also be written as `self > other`.
///
/// # Examples
///
/// ```
/// # use pibs::prelude::*;
/// assert_eq!(set![1, 2].is_strict_superset(set![1, 2]), false);
/// assert_eq!(set![1, 2].is_strict_superset(set![1, 2, 3]), false);
/// assert_eq!(set![1, 2, 3].is_strict_superset(set![1, 2]), true);
/// ```
#[inline]
pub fn is_strict_superset(self, other: Self) -> bool {
self != other && self.is_superset(other)
}
/// Whether `self` and `other` have elements in common.
///
/// # Examples
///
/// ```
/// # use pibs::prelude::*;
/// assert_eq!(set![1, 2].intersects(set![2, 3]), true);
/// assert_eq!(set![1, 2].intersects(set![3, 4]), false);
/// ```
#[inline]
pub fn intersects(self, other: Self) -> bool {
self.0 & other.0 != W::ZERO
}
/// Whether `self` and `other` have no elements in common.
///
/// # Examples
///
/// ```
/// # use pibs::prelude::*;
/// assert_eq!(set![1, 2].is_disjoint(set![2, 3]), false);
/// assert_eq!(set![1, 2].is_disjoint(set![3, 4]), true);
/// ```
#[inline]
pub fn is_disjoint(self, other: Self) -> bool {
self.0 & other.0 == W::ZERO
}
}