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
//! MapValues table lookup — discrete input → discrete output.
//!
//! Implements PMML `MapValues` (`InlineTable` / `TextIndex`) for single- and
//! multi-input tables. The evaluator variant here handles the single-column case
//! used by `DerivedField` expressions; the multi-column variant is evaluated
//! via [`crate::ir::Op::MapValuesMulti`] in the VM.
//!
//! # What belongs here
//!
//! - [`eval_mapvalues`] — linear search over `&[(SymbolId, SymbolId)]`.
//!
//! # Performance
//!
//! `O(table.len())` linear scan. Tables are small (typical <32 entries).
//! For larger tables a `HashMap` in `lower` would be generated, but the VM
//! retains the `Vec` form to avoid hashing on the hot path for small tables.
use crate;
/// Look up a discrete input in a `MapValues` table.
///
/// Mirrors PMML `MapValues` for a single `FieldColumnPair` plus
/// `InlineTable` / `TextIndex` derived fields. The table is an ordered
/// `Vec<(input SymbolId, output SymbolId)>` produced during lowering; search
/// is linear and returns the first match.
///
/// # Parameters
///
/// - `input`: Value to map. Only [`Value::Discrete`] is looked up; `Missing`
/// or `Continuous` immediately returns `default` (or `Missing` when `None`).
/// - `table`: Sorted map from input symbol to output symbol.
/// - `default`: Value when no key matches or when `input` is `Missing`/`Continuous`. `None` yields `Missing`.
///
/// # Returns
///
/// `Discrete(output)` when found, otherwise `Discrete(default)` when `default.is_some()`,
/// otherwise [`Value::Missing`].
///
/// # Panics
///
/// Never panics.
///
/// # Performance
///
/// `O(table.len())` with early exit on match. No allocation.
///
/// # Examples
///
/// ```
/// use pmmlruntime::base::{SymbolId, Value};
/// use pmmlruntime::engine::transform::mapvalues::eval_mapvalues;
///
/// let table = vec![(SymbolId(1), SymbolId(10)), (SymbolId(2), SymbolId(20))];
/// assert_eq!(eval_mapvalues(Value::Discrete(SymbolId(1)), &table, None), Value::Discrete(SymbolId(10)));
/// assert_eq!(eval_mapvalues(Value::Discrete(SymbolId(3)), &table, Some(SymbolId(99))), Value::Discrete(SymbolId(99)));
/// assert_eq!(eval_mapvalues(Value::Discrete(SymbolId(3)), &table, None), Value::Missing);
/// assert_eq!(eval_mapvalues(Value::Missing, &table, Some(SymbolId(5))), Value::Discrete(SymbolId(5)));
/// ```