Skip to content

Commit e8626bf

Browse files
author
Ian
committed
Upgraded Documentation of the Library
1 parent b77c108 commit e8626bf

5 files changed

Lines changed: 139 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "single-utilities"
3-
version = "0.8.4"
3+
version = "0.8.5"
44
edition = "2024"
55
description = "This crate provdes types, traits and utility functions to the single-rust ecosystem that can be universally used. You can also use it within your own ecosystem 👀"
66
homepage = "https://singlerust.com"

src/lib.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,62 @@
1+
//! # Single Utilities
2+
//!
3+
//! A comprehensive utilities library designed for the SingleRust ecosystem and beyond.
4+
//! This crate provides fundamental building blocks for mathematical computations,
5+
//! data processing, and algorithmic operations commonly needed in scientific computing,
6+
//! machine learning, and data analysis applications.
7+
//!
8+
//! ## Overview
9+
//!
10+
//! Single Utilities offers a collection of traits and types that abstract common
11+
//! patterns in numerical computing while maintaining flexibility and performance.
12+
//! While primarily developed for the SingleRust ecosystem, the library is designed
13+
//! with modularity and interoperability in mind, making it suitable for use with
14+
//! other Rust libraries and frameworks.
15+
//!
16+
//! ## Features
17+
//!
18+
//! ### Traits Module
19+
//! - **Numeric Operations**: Comprehensive traits for basic and advanced numeric operations
20+
//! - **Thread Safety**: Thread-safe variants for concurrent and parallel computations
21+
//! - **SIMD Support**: Optional SIMD-accelerated operations when the "simd" feature is enabled
22+
//! - **Type Constraints**: Flexible trait bounds for generic mathematical algorithms
23+
//!
24+
//! ### Types Module
25+
//! - **Direction Handling**: Utilities for row/column-oriented operations
26+
//! - **Distance Metrics**: Common distance functions for similarity calculations
27+
//! - **Batch Processing**: Identifiers and utilities for batch operations
28+
//!
29+
//! ## Usage
30+
//!
31+
//! ```rust
32+
//! use single_utilities::traits::NumericOps;
33+
//! use single_utilities::types::{Direction, DistanceMetric};
34+
//!
35+
//! // Use traits to constrain generic functions
36+
//! fn process_data<T: NumericOps>(data: &[T]) -> T {
37+
//! // Your numeric processing logic here
38+
//! T::zero()
39+
//! }
40+
//!
41+
//! // Use direction for matrix operations
42+
//! let direction = Direction::ROW;
43+
//! if direction.is_row() {
44+
//! // Process row-wise
45+
//! }
46+
//! ```
47+
//!
48+
//! ## Feature Flags
49+
//!
50+
//! - `simd`: Enables SIMD-accelerated operations using the `simba` crate
51+
//!
52+
//! ## Compatibility
53+
//!
54+
//! This library is designed to work seamlessly with:
55+
//! - The broader SingleRust ecosystem
56+
//! - Standard numeric libraries like `num-traits`
57+
//! - SIMD libraries like `simba` (when feature is enabled)
58+
//! - Custom mathematical and scientific computing libraries
59+
160
pub mod traits;
261

362
pub mod types;

src/traits/mod.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ use std::fmt::Debug;
66
use std::iter::Sum;
77
use std::ops::{Add, AddAssign, MulAssign, SubAssign};
88

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.
915
pub trait NumericOps:
1016
Zero
1117
+ One
@@ -40,28 +46,55 @@ impl<
4046
{
4147
}
4248

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.
4354
pub trait NumericOpsTS: NumericOps + Send + Sync {}
4455

4556
impl<T: NumericOps + Send + Sync> NumericOpsTS for T {}
4657

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.
4764
pub trait FloatOps:
4865
NumericOps + num_traits::Float + FromPrimitive + ToPrimitive + FloatCore
4966
{
5067
}
5168

5269
impl<T: NumericOps + num_traits::Float + FromPrimitive + ToPrimitive + FloatCore> FloatOps for T {}
5370

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.
5475
pub trait FloatOpsTS: FloatOps + Sync + Send {}
5576

5677
impl<T: FloatOps + Send + Sync> FloatOpsTS for T {}
5778

5879
#[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.
5987
pub trait FloatOpsTSSimba: FloatOpsTS + SimdRealField + RealField {}
6088

6189
#[cfg(feature = "simd")]
6290
impl<T: FloatOpsTS + SimdRealField + RealField> FloatOpsTSSimba for T {}
6391

64-
// Define a type alias for our numeric constraints
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.
6598
pub trait NumericNormalize:
6699
num_traits::Float + std::ops::AddAssign + std::iter::Sum + num_traits::NumCast
67100
{
@@ -73,7 +106,16 @@ impl<T> NumericNormalize for T where
73106
{
74107
}
75108

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.
76114
pub 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
77119
fn zero_len(&mut self, len: usize);
78120
}
79121

@@ -85,6 +127,12 @@ impl<T: Default + Clone> ZeroVec for Vec<T> {
85127
}
86128
}
87129

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.
88136
pub trait UIndex:
89137
Unsigned + Zero + One + Copy + Eq + Ord + PartialOrd + From<usize> + Into<usize> + Bounded
90138
{
@@ -95,6 +143,12 @@ impl<I: Unsigned + Zero + One + Copy + Eq + Ord + PartialOrd + From<usize> + Int
95143
{
96144
}
97145

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.
98152
pub trait Scalar: 'static + Clone + PartialEq + Debug {}
99153

100154
impl<T: 'static + Clone + PartialEq + Debug> Scalar for T {}

src/types/mod.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
use std::hash::Hash;
22

3+
/// Represents the direction of operations in matrix or array computations.
4+
///
5+
/// This enum is used to specify whether operations should be performed
6+
/// along rows or columns of a data structure.
37
pub enum Direction {
8+
/// Operations performed along columns (vertical direction)
49
COLUMN,
10+
/// Operations performed along rows (horizontal direction)
511
ROW,
612
}
713

@@ -15,6 +21,10 @@ impl Clone for Direction {
1521
}
1622

1723
impl Direction {
24+
/// Checks if the direction is row-wise.
25+
///
26+
/// # Returns
27+
/// `true` if the direction is `ROW`, `false` if it's `COLUMN`
1828
pub fn is_row(&self) -> bool {
1929
match self {
2030
Self::ROW => true,
@@ -23,6 +33,11 @@ impl Direction {
2333
}
2434
}
2535

36+
/// A trait for types that can serve as batch identifiers.
37+
///
38+
/// This trait is used to identify and group data in batch processing operations.
39+
/// Types implementing this trait must be cloneable, comparable for equality,
40+
/// and hashable for efficient lookup operations.
2641
pub trait BatchIdentifier: Clone + Eq + Hash {}
2742

2843
// Implement BatchIdentifier for common types
@@ -32,12 +47,17 @@ impl BatchIdentifier for i32 {}
3247
impl BatchIdentifier for u32 {}
3348
impl BatchIdentifier for usize {}
3449

50+
/// Enumeration of distance metrics for mathematical computations.
51+
///
52+
/// This enum defines common distance metrics used in machine learning,
53+
/// clustering, and similarity calculations. Each variant represents
54+
/// a different approach to measuring the distance between points or vectors.
3555
#[derive(Debug, Clone, Copy)]
3656
pub enum DistanceMetric {
37-
/// Euclidean distance
57+
/// Euclidean distance (L2 norm) - straight-line distance between points
3858
Euclidean,
39-
/// Manhattan distance
59+
/// Manhattan distance (L1 norm) - sum of absolute differences along each dimension
4060
Manhattan,
41-
/// Cosine distance
61+
/// Cosine distance - measures the cosine of the angle between vectors
4262
Cosine,
4363
}

0 commit comments

Comments
 (0)