single_utilities/traits/
mod.rs

1use num_traits::float::FloatCore;
2use num_traits::{Bounded, FromPrimitive, NumCast, One, ToPrimitive, Unsigned, Zero};
3#[cfg(feature = "simd")]
4use simba::{scalar::RealField, simd::SimdRealField};
5use std::fmt::Debug;
6use std::hash::Hash;
7use std::iter::Sum;
8use std::ops::{Add, AddAssign, MulAssign, SubAssign};
9
10/// A trait defining fundamental numeric operations and constraints.
11/// 
12/// This trait bundles common numeric operations required for mathematical computations,
13/// including zero/one elements, numeric casting, assignment operations, ordering, 
14/// bounds checking, and basic arithmetic. Types implementing this trait can be used
15/// in generic numeric algorithms.
16pub trait NumericOps:
17    Zero
18    + One
19    + NumCast
20    + Copy
21    + AddAssign
22    + MulAssign
23    + SubAssign
24    + PartialOrd
25    + Bounded
26    + Add<Output = Self>
27    + Sum
28    + Debug
29    + Default
30{
31}
32impl<
33    T: Zero
34        + One
35        + NumCast
36        + Copy
37        + AddAssign
38        + MulAssign
39        + SubAssign
40        + PartialOrd
41        + Bounded
42        + Add<Output = Self>
43        + Sum
44        + Debug
45        + Default,
46> NumericOps for T
47{
48}
49
50/// A thread-safe version of `NumericOps`.
51/// 
52/// This trait extends `NumericOps` with `Send + Sync` bounds, making it suitable
53/// for use in concurrent and parallel computations where data needs to be 
54/// safely shared across threads.
55pub trait NumericOpsTS: NumericOps + Send + Sync {}
56
57impl<T: NumericOps + Send + Sync> NumericOpsTS for T {}
58
59/// A trait for floating-point numeric operations.
60/// 
61/// Extends `NumericOps` with floating-point specific operations from the `num_traits`
62/// crate, including floating-point arithmetic, primitive conversions, and core 
63/// floating-point functionality. This trait is designed for types that represent
64/// real numbers with decimal precision.
65pub trait FloatOps:
66    NumericOps + num_traits::Float + FromPrimitive + ToPrimitive + FloatCore
67{
68}
69
70impl<T: NumericOps + num_traits::Float + FromPrimitive + ToPrimitive + FloatCore> FloatOps for T {}
71
72/// A thread-safe version of `FloatOps`.
73/// 
74/// This trait extends `FloatOps` with `Send + Sync` bounds for safe use in
75/// concurrent floating-point computations across multiple threads.
76pub trait FloatOpsTS: FloatOps + Sync + Send {}
77
78impl<T: FloatOps + Send + Sync> FloatOpsTS for T {}
79
80#[cfg(feature = "simd")]
81/// A SIMD-enabled floating-point operations trait.
82/// 
83/// Extends `FloatOpsTS` with SIMD (Single Instruction, Multiple Data) capabilities
84/// from the `simba` crate. This enables vectorized floating-point operations for
85/// improved performance in mathematical computations.
86/// 
87/// Only available when the "simd" feature is enabled.
88pub trait FloatOpsTSSimba: FloatOpsTS + SimdRealField + RealField {}
89
90#[cfg(feature = "simd")]
91impl<T: FloatOpsTS + SimdRealField + RealField> FloatOpsTSSimba for T {}
92
93/// A trait for numeric types suitable for normalization operations.
94/// 
95/// This trait combines floating-point arithmetic with assignment operations,
96/// summation capabilities, and numeric casting. It's specifically designed
97/// for types that need to participate in normalization algorithms where
98/// values are scaled or adjusted relative to some total or maximum.
99pub trait NumericNormalize:
100    num_traits::Float + std::ops::AddAssign + std::iter::Sum + num_traits::NumCast
101{
102}
103
104// Blanket implementation for any type that satisfies the bounds
105impl<T> NumericNormalize for T where
106    T: num_traits::Float + std::ops::AddAssign + std::iter::Sum + num_traits::NumCast
107{
108}
109
110/// A trait for vectors that can be resized and filled with default values.
111/// 
112/// Provides functionality to clear a vector and resize it to a specific length,
113/// filling all positions with the default value of the element type. This is
114/// useful for initializing or resetting vectors to a known state.
115pub trait ZeroVec {
116    /// Clears the vector and resizes it to the specified length, filling with default values.
117    /// 
118    /// # Arguments
119    /// * `len` - The desired length of the vector
120    fn zero_len(&mut self, len: usize);
121}
122
123impl<T: Default + Clone> ZeroVec for Vec<T> {
124    fn zero_len(&mut self, len: usize) {
125        self.clear();
126        self.reserve(len);
127        self.extend(std::iter::repeat_n(T::default(), len));
128    }
129}
130
131/// A trait for unsigned integer types suitable for indexing operations.
132/// 
133/// This trait combines unsigned integer properties with zero/one elements,
134/// ordering capabilities, and conversion to/from `usize`. It's designed for
135/// types that can be safely used as array or vector indices while providing
136/// mathematical operations and bounds checking.
137pub trait UIndex:
138    Unsigned + Zero + One + Copy + Eq + Ord + PartialOrd + From<usize> + Into<usize> + Bounded + Hash
139{
140}
141
142impl<I: Unsigned + Zero + One + Copy + Eq + Ord + PartialOrd + From<usize> + Into<usize> + Bounded + Hash>
143    UIndex for I
144{
145}
146
147/// A trait for scalar values used in mathematical computations.
148/// 
149/// This trait defines the minimal requirements for types that can be used as
150/// scalar values in mathematical operations. It requires static lifetime,
151/// cloning capability, equality comparison, and debug formatting. This is
152/// typically used as a constraint for generic mathematical algorithms.
153pub trait Scalar: 'static + Clone + PartialEq + Debug {}
154
155impl<T: 'static + Clone + PartialEq + Debug> Scalar for T {}