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