powerio-core 0.11.2

Shared source, diagnostic, module, collection, and output types for PowerIO.
Documentation
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;

use crate::Error;
use crate::validation::valid_nonempty_text;

/// Absolute tolerance for a complete probability sum.
pub const SCENARIO_PROBABILITY_TOLERANCE: f64 = 1e-12;

/// Case-sensitive stable identity of one scenario.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ScenarioId(Box<str>);

impl ScenarioId {
    pub fn new(id: impl Into<String>) -> Result<Self, Error> {
        let id = id.into();
        if !valid_nonempty_text(&id) {
            return Err(Error::new(
                &crate::codes::VALIDATE_SCENARIO_INVALID_ID,
                "a scenario ID must be nonempty and bounded",
            ));
        }
        Ok(Self(id.into_boxed_str()))
    }

    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for ScenarioId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

/// One named scenario and its optional probability.
#[derive(Clone, Debug)]
pub struct Scenario<T> {
    id: ScenarioId,
    probability: Option<f64>,
    value: T,
}

impl<T> Scenario<T> {
    #[must_use]
    pub const fn new(id: ScenarioId, probability: Option<f64>, value: T) -> Self {
        Self {
            id,
            probability,
            value,
        }
    }

    #[must_use]
    pub const fn id(&self) -> &ScenarioId {
        &self.id
    }

    #[must_use]
    pub const fn probability(&self) -> Option<f64> {
        self.probability
    }

    #[must_use]
    pub const fn value(&self) -> &T {
        &self.value
    }

    /// Mutably borrow the scenario value. Identity and probability stay
    /// immutable so a set's validated index and probability sum remain valid.
    pub const fn value_mut(&mut self) -> &mut T {
        &mut self.value
    }

    #[must_use]
    pub fn into_value(self) -> T {
        self.value
    }

    /// Consume the entry and map its value while retaining its identity and
    /// probability.
    #[must_use]
    pub fn map_value<U>(self, map: impl FnOnce(T) -> U) -> Scenario<U> {
        Scenario {
            id: self.id,
            probability: self.probability,
            value: map(self.value),
        }
    }
}

/// Named alternatives with no implied time order.
pub struct ScenarioSet<T> {
    scenarios: Arc<Vec<Scenario<T>>>,
    /// ID to position. Built once, so `get` does not scan the set.
    index: Arc<HashMap<Box<str>, usize>>,
}

impl<T> ScenarioSet<T> {
    pub fn new(scenarios: Vec<Scenario<T>>) -> Result<Self, Error> {
        let mut ids: HashMap<Box<str>, usize> = HashMap::new();
        ids.try_reserve(scenarios.len()).map_err(|cause| {
            Error::new(
                &crate::codes::VALIDATE_SCENARIO_ALLOCATION_REFUSED,
                format!(
                    "cannot reserve identity validation for {} scenarios",
                    scenarios.len()
                ),
            )
            .with_cause(cause)
        })?;
        for (position, scenario) in scenarios.iter().enumerate() {
            if ids.insert(scenario.id.as_str().into(), position).is_some() {
                return Err(Error::new(
                    &crate::codes::VALIDATE_SCENARIO_DUPLICATE_ID,
                    format!("duplicate scenario ID `{}`", scenario.id),
                ));
            }
        }

        let probability_count = scenarios
            .iter()
            .filter(|scenario| scenario.probability.is_some())
            .count();
        if probability_count != 0
            && probability_count != scenarios.len()
            && let Some(missing) = scenarios
                .iter()
                .find(|scenario| scenario.probability.is_none())
        {
            return Err(Error::new(
                &crate::codes::VALIDATE_SCENARIO_MISSING_PROBABILITY,
                format!("scenario `{}` has no probability", missing.id),
            ));
        }

        if probability_count != 0 {
            for scenario in &scenarios {
                let Some(probability) = scenario.probability else {
                    return Err(Error::new(
                        &crate::codes::VALIDATE_SCENARIO_MISSING_PROBABILITY,
                        format!("scenario `{}` has no probability", scenario.id),
                    ));
                };
                if !probability.is_finite() || probability < 0.0 {
                    return Err(Error::new(
                        &crate::codes::VALIDATE_SCENARIO_INVALID_PROBABILITY,
                        format!(
                            "scenario `{}` probability must be finite and nonnegative; found {probability}",
                            scenario.id
                        ),
                    ));
                }
            }
            let sum = compensated_sum(scenarios.iter().filter_map(|scenario| scenario.probability));
            if !sum.is_finite() || (sum - 1.0).abs() > SCENARIO_PROBABILITY_TOLERANCE {
                return Err(Error::new(
                    &crate::codes::VALIDATE_SCENARIO_PROBABILITY_SUM,
                    format!("scenario probabilities must sum to one; found {sum}"),
                ));
            }
        }

        Ok(Self {
            scenarios: Arc::new(scenarios),
            index: Arc::new(ids),
        })
    }

    #[must_use]
    /// Look one scenario value up by its stable ID. Constant time: IDs are
    /// case sensitive and never normalized, and the index is built once.
    pub fn get(&self, id: &str) -> Option<&T> {
        self.entry(id).map(Scenario::value)
    }

    /// Look up the complete scenario entry by its stable ID.
    #[must_use]
    pub fn entry(&self, id: &str) -> Option<&Scenario<T>> {
        self.entry_at(*self.index.get(id)?)
    }

    /// Look up one scenario value by its insertion position.
    #[must_use]
    pub fn get_at(&self, position: usize) -> Option<&T> {
        self.entry_at(position).map(Scenario::value)
    }

    /// Look up one complete scenario entry by its insertion position.
    #[must_use]
    pub fn entry_at(&self, position: usize) -> Option<&Scenario<T>> {
        self.scenarios.get(position)
    }

    pub fn iter(&self) -> impl ExactSizeIterator<Item = &Scenario<T>> {
        self.scenarios.iter()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.scenarios.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.scenarios.is_empty()
    }
}

impl<T: Clone> ScenarioSet<T> {
    /// Mutably borrow one scenario value by ID through copy on write.
    pub fn get_mut(&mut self, id: &str) -> Option<&mut T> {
        self.entry_mut(id).map(Scenario::value_mut)
    }

    /// Mutably borrow one complete scenario entry by ID through copy on
    /// write. Its public mutable surface exposes only the value, preserving
    /// the validated ID index and probability sum.
    pub fn entry_mut(&mut self, id: &str) -> Option<&mut Scenario<T>> {
        let position = *self.index.get(id)?;
        self.entry_at_mut(position)
    }

    /// Mutably borrow one scenario value by insertion position.
    pub fn get_at_mut(&mut self, position: usize) -> Option<&mut T> {
        self.entry_at_mut(position).map(Scenario::value_mut)
    }

    /// Mutably borrow one complete scenario entry by insertion position.
    pub fn entry_at_mut(&mut self, position: usize) -> Option<&mut Scenario<T>> {
        Arc::make_mut(&mut self.scenarios).get_mut(position)
    }

    /// Iterate over mutable scenario entries through one copy on write
    /// detachment. Entry identities and probabilities remain immutable.
    pub fn iter_mut(&mut self) -> impl ExactSizeIterator<Item = &mut Scenario<T>> {
        Arc::make_mut(&mut self.scenarios).iter_mut()
    }

    /// Consume the set and map each scenario value while retaining identities,
    /// probabilities, order, and the already validated identity index. A
    /// uniquely owned set moves its values without cloning them; a shared set
    /// first performs the copy on write split.
    #[must_use]
    pub fn map_values<U>(self, mut map: impl FnMut(T) -> U) -> ScenarioSet<U> {
        let scenarios = Arc::unwrap_or_clone(self.scenarios)
            .into_iter()
            .map(|scenario| scenario.map_value(&mut map))
            .collect();
        ScenarioSet {
            scenarios: Arc::new(scenarios),
            index: self.index,
        }
    }
}

impl<T> Clone for ScenarioSet<T> {
    fn clone(&self) -> Self {
        Self {
            scenarios: Arc::clone(&self.scenarios),
            index: Arc::clone(&self.index),
        }
    }
}

// The ID index is a derived view of `scenarios`, so printing it would only
// repeat the IDs already shown.
#[expect(
    clippy::missing_fields_in_debug,
    reason = "the index is derived from the scenarios it points into"
)]
impl<T: fmt::Debug> fmt::Debug for ScenarioSet<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ScenarioSet")
            .field("scenarios", &self.scenarios)
            .finish()
    }
}

fn compensated_sum(values: impl IntoIterator<Item = f64>) -> f64 {
    let mut sum = 0.0;
    let mut correction = 0.0;
    for value in values {
        let corrected = value - correction;
        let next = sum + corrected;
        correction = (next - sum) - corrected;
        sum = next;
    }
    sum
}

#[cfg(test)]
mod tests {

    #[test]
    fn lookup_is_indexed_and_stays_correct_at_scale() {
        // A linear scan is correct but not what the design allows, so this
        // exercises enough entries that a scan would be visible in a profile
        // and asserts the last one is reachable directly.
        let scenarios: Vec<_> = (0..20_000)
            .map(|index| Scenario::new(ScenarioId::new(format!("s{index}")).unwrap(), None, index))
            .collect();
        let set = ScenarioSet::new(scenarios).unwrap();
        assert_eq!(set.get("s19999"), Some(&19_999));
        assert_eq!(set.get("s0"), Some(&0));
        assert!(set.get("S0").is_none(), "IDs are case sensitive");
        assert!(set.get("missing").is_none());
        // Cloning shares the index rather than rebuilding it.
        assert_eq!(set.clone().get("s12345"), Some(&12_345),);
    }
    use super::*;

    fn scenario(id: &str, probability: Option<f64>) -> Scenario<u8> {
        Scenario::new(ScenarioId::new(id).unwrap(), probability, 1)
    }

    #[test]
    fn identities_are_exact_bounded_and_case_sensitive() {
        assert!(ScenarioId::new("").is_err());
        assert!(ScenarioId::new("x".repeat(65_537)).is_err());
        let set = ScenarioSet::new(vec![scenario("base", None), scenario("Base", None)]).unwrap();
        assert!(set.get("base").is_some());
        assert!(set.get("BASE").is_none());
        assert!(ScenarioSet::new(vec![scenario("same", None), scenario("same", None)]).is_err());
    }

    #[test]
    fn probabilities_are_all_or_none_and_sum_with_exact_tolerance() {
        assert!(ScenarioSet::new(vec![scenario("a", None), scenario("b", None)]).is_ok());
        assert!(ScenarioSet::new(vec![scenario("a", Some(1.0)), scenario("b", None)]).is_err());
        assert!(ScenarioSet::new(vec![scenario("a", Some(f64::NAN))]).is_err());
        assert!(
            ScenarioSet::new(vec![scenario("a", Some(-0.1)), scenario("b", Some(1.1))]).is_err()
        );
        assert!(ScenarioSet::new(vec![scenario("a", Some(0.4)), scenario("b", Some(0.6))]).is_ok());
        assert!(ScenarioSet::new(vec![scenario("a", Some(1.0 + 2e-12))]).is_err());
    }

    #[test]
    fn probability_accumulation_overflow_is_an_error() {
        let result = ScenarioSet::new(vec![
            scenario("a", Some(f64::MAX)),
            scenario("b", Some(f64::MAX)),
        ]);
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().diagnostics()[0].code(),
            "VALIDATE.SCENARIO.PROBABILITY_SUM"
        );
    }

    #[test]
    fn an_empty_set_is_valid() {
        let set = ScenarioSet::<u8>::new(Vec::new()).unwrap();
        assert!(set.is_empty());
    }

    #[test]
    fn entry_and_mutable_value_access_preserve_validated_metadata() {
        let mut edited = ScenarioSet::new(vec![
            Scenario::new(ScenarioId::new("base").unwrap(), Some(0.25), 1_u8),
            Scenario::new(ScenarioId::new("high").unwrap(), Some(0.75), 2_u8),
        ])
        .unwrap();
        let original = edited.clone();
        assert!(Arc::ptr_eq(&edited.scenarios, &original.scenarios));
        assert!(Arc::ptr_eq(&edited.index, &original.index));

        *edited.get_mut("high").unwrap() = 9;
        assert_eq!(edited.get("high"), Some(&9));
        assert_eq!(original.get("high"), Some(&2));
        assert_eq!(edited.entry("high").unwrap().probability(), Some(0.75));
        assert_eq!(edited.entry_at(0).unwrap().id().as_str(), "base");
        assert_eq!(edited.get_at(0), Some(&1));
        assert!(Arc::ptr_eq(&edited.index, &original.index));
        assert!(!Arc::ptr_eq(&edited.scenarios, &original.scenarios));

        *edited.entry_at_mut(0).unwrap().value_mut() = 7;
        for scenario in edited.iter_mut() {
            *scenario.value_mut() += 1;
        }
        assert_eq!(edited.get("base"), Some(&8));
        assert_eq!(edited.get("high"), Some(&10));
    }

    #[test]
    fn consuming_map_moves_unique_values_and_reuses_the_index() {
        use std::sync::atomic::{AtomicUsize, Ordering};

        static CLONES: AtomicUsize = AtomicUsize::new(0);

        #[derive(Debug)]
        struct Counted(u8);

        impl Clone for Counted {
            fn clone(&self) -> Self {
                CLONES.fetch_add(1, Ordering::Relaxed);
                Self(self.0)
            }
        }

        CLONES.store(0, Ordering::Relaxed);
        let set = ScenarioSet::new(vec![Scenario::new(
            ScenarioId::new("base").unwrap(),
            Some(1.0),
            Counted(3),
        )])
        .unwrap();
        let index = Arc::clone(&set.index);
        let mapped = set.map_values(|value| usize::from(value.0));
        assert_eq!(mapped.get("base"), Some(&3));
        assert_eq!(mapped.entry("base").unwrap().probability(), Some(1.0));
        assert!(Arc::ptr_eq(&mapped.index, &index));
        assert_eq!(CLONES.load(Ordering::Relaxed), 0);
    }
}