Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/plans/aexpr/minterm_iter.rs
6940 views
1
use polars_utils::arena::{Arena, Node};
2
use polars_utils::idx_vec::UnitVec;
3
use polars_utils::unitvec;
4
5
use super::{AExpr, Operator};
6
7
/// An iterator over all the minterms in a boolean expression boolean.
8
///
9
/// In other words, all the terms that can `AND` together to form this expression.
10
///
11
/// # Example
12
///
13
/// ```
14
/// a & (b | c) & (b & (c | (a & c)))
15
/// ```
16
///
17
/// Gives terms:
18
///
19
/// ```
20
/// a
21
/// b | c
22
/// b
23
/// c | (a & c)
24
/// ```
25
pub struct MintermIter<'a> {
26
stack: UnitVec<Node>,
27
expr_arena: &'a Arena<AExpr>,
28
}
29
30
impl Iterator for MintermIter<'_> {
31
type Item = Node;
32
33
fn next(&mut self) -> Option<Self::Item> {
34
let mut top = self.stack.pop()?;
35
36
while let AExpr::BinaryExpr {
37
left,
38
op: Operator::And | Operator::LogicalAnd,
39
right,
40
} = self.expr_arena.get(top)
41
{
42
self.stack.push(*right);
43
top = *left;
44
}
45
46
Some(top)
47
}
48
}
49
50
impl<'a> MintermIter<'a> {
51
pub fn new(root: Node, expr_arena: &'a Arena<AExpr>) -> Self {
52
Self {
53
stack: unitvec![root],
54
expr_arena,
55
}
56
}
57
}
58
59