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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use HashMap;
/// Parallel data transformation and organization utilities.
///
/// This module provides high-level parallel operations for organizing and
/// transforming data collections. It includes partitioning, grouping, chunking,
/// and windowing operations that are commonly needed in data processing pipelines.
///
/// ## Features
///
/// - **Parallel Partitioning**: Split data into matching/non-matching groups
/// - **Parallel Grouping**: Group elements by computed keys
/// - **Parallel Chunking**: Divide data into fixed-size chunks
/// - **Parallel Windows**: Create sliding windows over data
/// - **Memory Efficient**: Operations designed to minimize memory overhead
/// - **Composable**: Results can be chained with other parallel operations
///
/// ## Examples
///
/// ### Partitioning Data
/// ```rust
/// use trash_utilities::parallel::parallel_partition;
///
/// let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// let (evens, odds) = parallel_partition(data, |&x| x % 2 == 0);
///
/// assert_eq!(evens, vec![2, 4, 6, 8, 10]);
/// assert_eq!(odds, vec![1, 3, 5, 7, 9]);
/// ```
///
/// ### Grouping by Keys
/// ```rust
/// use trash_utilities::parallel::parallel_group_by;
/// use std::collections::HashMap;
///
/// let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// let groups = parallel_group_by(data, |&x| x % 3);
///
/// // Elements grouped by their remainder when divided by 3
/// assert_eq!(groups.get(&0), Some(&vec![3, 6, 9]));
/// assert_eq!(groups.get(&1), Some(&vec![1, 4, 7, 10]));
/// assert_eq!(groups.get(&2), Some(&vec![2, 5, 8]));
/// ```
///
/// ### Chunking for Batch Processing
/// ```rust
/// use trash_utilities::parallel::parallel_chunks;
///
/// let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// let chunks = parallel_chunks(data, 3);
///
/// assert_eq!(chunks, vec![
/// vec![1, 2, 3],
/// vec![4, 5, 6],
/// vec![7, 8, 9],
/// vec![10] // Last chunk may be smaller
/// ]);
/// ```/// This function splits a vector into two vectors: one containing elements
/// that satisfy the predicate, and another containing elements that don't.
///
/// # 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 partition.
/// - `predicate`: Function that returns true for elements in the first partition.
///
/// # Returns
/// A tuple of two vectors: (`matching_elements`, `non_matching_elements`).
///
/// # Examples
/// ```rust
/// use trash_analyzer::parallel::parallel_partition;
///
/// let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// let (evens, odds) = parallel_partition(data, |&x| x % 2 == 0);
/// assert_eq!(evens, vec![2, 4, 6, 8, 10]);
/// assert_eq!(odds, vec![1, 3, 5, 7, 9]);
/// ```
/// Group elements by a key function in parallel.
///
/// This function groups elements of a vector by keys computed from each element,
/// similar to `itertools::group_by` but with parallel processing.
///
/// # Type Parameters
/// - `T`: The element type, must be `Send + Sync`.
/// - `K`: The key type, must be `Send + Sync + Eq + std::hash::Hash`.
/// - `F`: The key function type, must be `Fn(&T) -> K + Send + Sync`.
///
/// # Parameters
/// - `data`: The input vector to group.
/// - `key_fn`: Function that computes the key for each element.
///
/// # Returns
/// A `HashMap` where keys are the computed keys and values are vectors of elements.
///
/// # Examples
/// ```rust
/// use trash_analyzer::parallel::parallel_group_by;
/// use std::collections::HashMap;
///
/// let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// let groups = parallel_group_by(data, |&x| x % 3);
///
/// assert_eq!(groups.get(&0), Some(&vec![3, 6, 9]));
/// assert_eq!(groups.get(&1), Some(&vec![1, 4, 7, 10]));
/// assert_eq!(groups.get(&2), Some(&vec![2, 5, 8]));
/// ```
/// Split a vector into chunks of specified size in parallel.
///
/// This function divides a vector into chunks of equal size (except possibly
/// the last chunk), similar to `itertools::chunks` but with parallel processing.
///
/// # Type Parameters
/// - `T`: The element type, must be `Send + Sync`.
///
/// # Parameters
/// - `data`: The input vector to chunk.
/// - `chunk_size`: The size of each chunk.
///
/// # Returns
/// A vector of vectors, where each inner vector is a chunk.
///
/// # Examples
/// ```rust
/// use trash_analyzer::parallel::parallel_chunks;
///
/// let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// let chunks = parallel_chunks(data, 3);
/// assert_eq!(chunks, vec![
/// vec![1, 2, 3],
/// vec![4, 5, 6],
/// vec![7, 8, 9],
/// vec![10]
/// ]);
/// ```
/// Create sliding windows over a vector in parallel.
///
/// This function creates overlapping windows of elements from a vector,
/// similar to `itertools::windows` but with parallel processing support.
///
/// # Type Parameters
/// - `T`: The element type, must be `Send + Sync + Clone`.
///
/// # Parameters
/// - `data`: The input vector to create windows from.
/// - `window_size`: The size of each window.
///
/// # Returns
/// A vector of vectors, where each inner vector is a window.
///
/// # Examples
/// ```rust
/// use trash_analyzer::parallel::parallel_windows;
///
/// let data = vec![1, 2, 3, 4, 5];
/// let windows = parallel_windows(&data, 3);
/// assert_eq!(windows, vec![
/// vec![1, 2, 3],
/// vec![2, 3, 4],
/// vec![3, 4, 5]
/// ]);
/// ```