Skip to main content

rumtk_core/
base.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20use crate::strings::rumtk_format;
21use std::collections::VecDeque;
22
23pub use rumtk_arena::base::*;
24pub type RUMVecDeque<T> = VecDeque<T>;
25
26pub fn is_unique<T>(data: &RUMVec<T>) -> bool
27where
28    T: std::cmp::Eq + std::hash::Hash,
29{
30    let mut keys = ahash::AHashSet::with_capacity(data.len());
31    for itm in data {
32        if !keys.insert(itm) {
33            return false;
34        }
35    }
36    true
37}
38
39///
40/// Take a requested index and the maximum size of the item container.
41/// Check if the index is valid and return an error if it is.
42/// The purpose of this function is to enable handling of out of bounds without triggering a panic.
43/// Also, add negative indices like Python does when doing a reverse search!
44///
45/// * If the index is 0, return Error
46/// * If the index is below 0, return the max - index iff max - index > 0
47/// * If the index is bigger than the defined max, return Error.
48/// * Otherwise, return the given index.
49///
50/// # Examples
51///
52/// ## Min
53/// ```
54/// use ::rumtk_core::base::clamp_index;
55/// use ::rumtk_core::strings::rumtk_format;
56/// let max: isize = 5;
57/// let i: isize = 1;
58/// let result = clamp_index(&i, &max).unwrap();
59/// assert_eq!(&1, &result, "{}", rumtk_format!("Expected to receive 0 but got {}", &result))
60/// ```
61///
62/// ## Max
63/// ```
64/// use ::rumtk_core::base::clamp_index;
65/// use ::rumtk_core::strings::rumtk_format;
66/// let max: isize = 5;
67/// let i: isize = 5;
68/// let result = clamp_index(&i, &max).unwrap();
69/// assert_eq!(&5, &result, "{}", rumtk_format!("Expected to receive 0 but got {}", &result))
70/// ```
71///
72/// ## Valid
73/// ```
74/// use ::rumtk_core::base::clamp_index;
75/// use ::rumtk_core::strings::rumtk_format;
76/// let max: isize = 5;
77/// let i: isize = 5;
78/// let result = clamp_index(&i, &max).unwrap();
79/// assert_eq!(&5, &result, "{}", rumtk_format!("Expected to receive 0 but got {}", &result))
80/// ```
81///
82/// ## Valid Negative Index (reverse lookup)
83/// ```
84/// use ::rumtk_core::base::clamp_index;
85/// use ::rumtk_core::strings::rumtk_format;
86/// let max: isize = 5;
87/// let i: isize = -1;
88/// let result = clamp_index(&i, &max).unwrap();
89/// assert_eq!(&5, &result, "{}", rumtk_format!("Expected to receive 0 but got {}", &result))
90/// ```
91#[inline(always)]
92pub fn clamp_index(given_indx: &isize, max_size: &isize) -> RUMResult<usize> {
93    let neg_max_indx = *max_size * -1;
94    if *given_indx == 0 {
95        return Err(rumtk_format!(
96            "Index {} is invalid! Use 1-indexed values if using positive indices.",
97            given_indx
98        ));
99    }
100
101    if *given_indx >= neg_max_indx && *given_indx < 0 {
102        return Ok((max_size + given_indx + 1) as usize);
103    }
104
105    if *given_indx > 0 && given_indx <= max_size {
106        return Ok(*given_indx as usize);
107    }
108
109    Err(rumtk_format!(
110        "Index {} is outside {} < x < {} boundary!",
111        given_indx,
112        neg_max_indx,
113        max_size
114    ))
115}