oxilean_parse/pattern/patterncompiler_select_column_group.rs
1//! # PatternCompiler - select_column_group Methods
2//!
3//! This module contains method implementations for `PatternCompiler`.
4//!
5//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
6
7use crate::Pattern;
8
9use super::types::PatternRow;
10
11use super::patterncompiler_type::PatternCompiler;
12
13impl PatternCompiler {
14 /// Select the best column to split on.
15 ///
16 /// Uses a simple heuristic: pick the column with the most constructor
17 /// patterns. This tends to produce smaller decision trees.
18 #[allow(dead_code)]
19 pub fn select_column(&self, rows: &[PatternRow], num_cols: usize) -> usize {
20 let mut best_col = 0;
21 let mut best_score: usize = 0;
22 for col in 0..num_cols {
23 let mut score: usize = 0;
24 for row in rows {
25 if col < row.patterns.len() {
26 match &row.patterns[col] {
27 Pattern::Ctor(_, _) | Pattern::Lit(_) | Pattern::Or(_, _) => {
28 score += 1;
29 }
30 Pattern::Wild | Pattern::Var(_) => {}
31 }
32 }
33 }
34 if score > best_score {
35 best_score = score;
36 best_col = col;
37 }
38 }
39 best_col
40 }
41}