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
/// Core parallel processing primitives.
///
/// This module provides the fundamental building blocks for parallel computation
/// in Rust. It offers a clean, safe interface for common parallel operations
/// like mapping, filtering, and folding over collections.
///
/// ## Features
///
/// - **Parallel Map**: Apply functions to each element concurrently
/// - **Parallel Filter**: Filter elements based on predicates in parallel
/// - **Parallel Fold**: Combine elements using associative operations
/// - **Parallel For-Each**: Execute side effects on each element concurrently
/// - **Type Safety**: Full compile-time type checking and safety guarantees
/// - **Performance**: Optimized for both CPU-bound and I/O-bound workloads
///
/// ## Design Principles
///
/// - **Zero-Cost Abstractions**: No runtime overhead for unused features
/// - **Composable**: Operations can be chained and combined
/// - **Memory Safe**: All operations respect Rust's ownership and borrowing rules
/// - **Thread Safe**: Designed for concurrent execution across multiple threads
///
/// ## Examples
///
/// ### Basic Parallel Mapping
/// ```rust
/// use trash_utilities::parallel::parallel_map;
///
/// let data = vec![1, 2, 3, 4, 5];
/// let doubled = parallel_map(data, |x| x * 2);
/// assert_eq!(doubled, vec![2, 4, 6, 8, 10]);
/// ```
///
/// ### Parallel Filtering with Side Effects
/// ```rust
/// use trash_utilities::parallel::{parallel_filter, parallel_for_each};
/// use std::sync::Mutex;
///
/// let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// let evens = parallel_filter(data, |&x| x % 2 == 0);
///
/// let processed_count = Mutex::new(0);
/// parallel_for_each(evens.clone(), |_| {
/// *processed_count.lock().unwrap() += 1;
/// });
///
/// assert_eq!(*processed_count.lock().unwrap(), 5);
/// ```
///
/// ### Parallel Folding
/// ```rust
/// use trash_utilities::parallel::parallel_fold;
///
/// let data = vec![1, 2, 3, 4, 5];
/// let sum = parallel_fold(data, 0, |acc, x| acc + x);
/// let product = parallel_fold(vec![2, 3, 4], 1, |acc, x| acc * x);
///
/// assert_eq!(sum, 15);
/// assert_eq!(product, 24);
/// ```
/// Apply a function to each element of a vector in parallel.
///
/// This function provides a parallel-ready interface for mapping operations.
/// Currently uses sequential processing but is designed to be easily extended
/// with actual parallel execution backends.
///
/// # Type Parameters
/// - `T`: The input element type, must be `Send`.
/// - `U`: The output element type, must be `Send`.
/// - `F`: The mapping function type.
///
/// # Parameters
/// - `data`: The input vector to map over.
/// - `f`: The function to apply to each element.
///
/// # Returns
/// A new vector containing the results of applying `f` to each element.
///
/// # Examples
/// ```rust
/// use trash_analyzer::parallel::parallel_map;
///
/// let data = vec![1, 2, 3, 4, 5];
/// let result = parallel_map(data, |x| x * 2);
/// assert_eq!(result, vec![2, 4, 6, 8, 10]);
/// ```
/// Apply a function to each element of a vector in parallel without collecting results.
///
/// This function provides a parallel-ready interface for executing a function on each
/// element of the input vector in parallel, discarding the results. Useful for
/// side-effect operations like I/O or logging. Currently uses sequential processing
/// but is designed to be easily extended with actual parallel execution backends.
///
/// # Type Parameters
/// - `T`: The element type, must be `Send`.
/// - `F`: The function type.
///
/// # Parameters
/// - `data`: The input vector to iterate over.
/// - `f`: The function to apply to each element.
///
/// # Examples
/// ```rust
/// use trash_analyzer::parallel::parallel_for_each;
/// use std::sync::Mutex;
///
/// let data = vec![1, 2, 3, 4, 5];
/// let sum = Mutex::new(0);
/// parallel_for_each(data, |x| {
/// let mut s = sum.lock().unwrap();
/// *s += x;
/// });
/// assert_eq!(*sum.lock().unwrap(), 15);
/// ```
/// Combine all elements of a vector using a folding function in parallel.
///
/// This function performs a parallel fold operation, first folding chunks of the
/// data in parallel, then combining the results using the same folding function.
/// Currently uses sequential processing but is designed to be easily extended
/// with actual parallel execution backends.
///
/// # Type Parameters
/// - `T`: The element type, must be `Send + Sync`.
/// - `F`: The folding function type.
///
/// # Parameters
/// - `data`: The input vector to fold.
/// - `init`: The initial value for the fold.
/// - `f`: The folding function that takes two values and combines them.
///
/// # Returns
/// The final folded value.
///
/// # Examples
/// ```rust
/// use trash_analyzer::parallel::parallel_fold;
///
/// let data = vec![1, 2, 3, 4, 5];
/// let sum = parallel_fold(data, 0, |acc, x| acc + x);
/// assert_eq!(sum, 15);
/// ```
/// Apply a predicate to filter elements in parallel.
///
/// This function uses parallel processing to filter elements from a vector
/// based on a predicate function, returning only elements that satisfy the condition.
///
/// # Type Parameters
/// - `T`: The element type, must be `Send + Sync`.
/// - `F`: The predicate function type, must be `Fn(&T) -> bool + Send + Sync`.
///
/// # Parameters
/// - `data`: The input vector to filter.
/// - `predicate`: Function that returns true for elements to keep.
///
/// # Returns
/// A new vector containing only elements that satisfy the predicate.
///
/// # Examples
/// ```rust
/// use trash_analyzer::parallel::parallel_filter;
///
/// let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// let evens = parallel_filter(data, |&x| x % 2 == 0);
/// assert_eq!(evens, vec![2, 4, 6, 8, 10]);
/// ```