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
//! Discretize binning — continuous → discrete via interval matching.
//!
//! Implements `Discretize` semantics from PMML 4.4: a continuous input is tested
//! against an ordered list of intervals (`low`/`high` with open/closed closure).
//! The first matching interval yields a discrete symbol. This module provides
//! a standalone helper exposed for unit tests; the hot-path `Op::Discretize`
//! evaluation in `transform::vm` uses [`crate::ir::DiscretizeBin`] directly.
//!
//! # What belongs here
//!
//! - [`eval_discretize`] — pure function mapping `Value::Continuous(x)` to a bin.
//!
//! # Performance
//!
//! `O(bins)` linear scan. Bins are typically 2–7 intervals; binary search would
//! not improve the constant factor for this size.
use crateValue;
/// Map a continuous [`Value`] into a bin defined by intervals.
///
/// Tests `value` against `bins` in order. Each bin is `(low, high, left_closed, right_closed)`:
/// the interval is inclusive (`>=`/`<=`) when closed, exclusive (`>`/`<`) otherwise.
/// The first matching bin returns `Continuous(idx as f64)` where `idx` is the bin's position;
/// the VM layer then maps `idx` to the actual [`crate::base::SymbolId`] (`bin_value`) or to
/// `default_value` / `map_missing_to`.
///
/// # Parameters
///
/// - `value`: Input value. Only [`Value::Continuous`] is binned; `Missing` or `Discrete` returns `Missing`.
/// - `bins`: Ordered intervals to test top-to-bottom. `low == -inf` / `high == inf` are permitted via `f64::NEG_INFINITY` / `INFINITY`.
///
/// # Returns
///
/// `Continuous(bin_index)` when a bin matches, otherwise [`Value::Missing`]. `Missing` input always yields `Missing`.
///
/// # Panics
///
/// Never panics.
///
/// # Performance
///
/// `O(bins)` linear scan. No allocation.
///
/// # Examples
///
/// ```
/// use pmmlruntime::base::Value;
/// use pmmlruntime::engine::transform::discretize::eval_discretize;
///
/// let bins = vec![(0.0, 10.0, true, false), (10.0, 20.0, true, true)];
/// assert_eq!(eval_discretize(Value::Continuous(5.0), &bins), Value::Continuous(0.0));
/// assert_eq!(eval_discretize(Value::Continuous(10.0), &bins), Value::Continuous(1.0));
/// assert_eq!(eval_discretize(Value::Continuous(25.0), &bins), Value::Missing);
/// assert_eq!(eval_discretize(Value::Missing, &bins), Value::Missing);
/// ```