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
use HashSet;
use Hash;
/// Removes duplicate elements from a vector in-place.
///
/// This function efficiently removes duplicate elements from the input vector
/// while preserving the order of first occurrence for each unique element.
/// It uses a HashSet for fast lookup and the `retain` method for in-place filtering.
///
/// # Type Parameters
///
/// * `T`: The type of elements in the vector. It must implement `Eq`, `Hash`, and `Copy` traits.
///
/// # Arguments
///
/// * `v` - A mutable reference to the vector to be deduplicated.
///
/// # Example
///
/// ```rust
/// let mut numbers = vec![1, 2, 3, 2, 4, 1, 5];
/// byteutils::vec::dedup(&mut numbers);
/// assert_eq!(numbers, vec![1, 2, 3, 4, 5]);
/// ```
///
/// # Note
///
/// This function requires the `Copy` trait because it needs to copy elements
/// into the HashSet. For types that don't implement `Copy`, consider using
/// references or implementing a different deduplication strategy.
/// Retains only the elements specified by the predicate.
///
/// In-place variant of `Vec::retain()`. This function will remove all elements
/// for which the predicate returns `false`, while keeping all elements for which
/// the predicate returns `true`.
///
/// # Arguments
///
/// * `v` - A mutable reference to the vector to be filtered
/// * `predicate` - A closure that takes a reference to an element and returns a boolean
///
/// # Examples
///
/// ```
/// let mut numbers = vec![1, 2, 3, 4, 5, 6];
/// byteutils::vec::retain_if(&mut numbers, |&x| x % 2 == 0);
/// assert_eq!(numbers, vec![2, 4, 6]);
/// ```
/// Reverses the order of elements in the vector in place.
///
/// This function modifies the original vector, reversing the order of its elements
/// without allocating a new vector.
///
/// # Arguments
///
/// * `v` - A mutable reference to the vector to be reversed
///
/// # Examples
///
/// ```
/// let mut vec = vec![1, 2, 3, 4, 5];
/// byteutils::vec::reverse_in_place(&mut vec);
/// assert_eq!(vec, vec![5, 4, 3, 2, 1]);
/// ```
///
/// # Note
///
/// This function has a time complexity of O(n/2) where n is the length of the vector.
/// It performs in-place swapping, which is memory-efficient for large vectors.
/// Splits a vector into two at the given index, creating two new vectors.
///
/// This function takes a mutable reference to a vector and an index, and returns
/// two new vectors. The first vector contains cloned elements from the original vector
/// up to (but not including) the given index, and the second vector contains
/// cloned elements of the remaining items.
///
/// # Type Parameters
///
/// * `T` - The type of elements in the vector, which must implement the `Clone` trait
///
/// # Arguments
///
/// * `v` - A mutable reference to the vector to be split
/// * `at` - The index at which to split the vector
///
/// # Returns
///
/// A tuple containing two new vectors: (left, right)
///
/// # Panics
///
/// This function will panic if `at` is greater than the length of the vector.
///
/// # Examples
///
/// ```
/// let mut vec = vec![1, 2, 3, 4, 5];
/// let (left, right) = byteutils::vec::split_at_vec(&mut vec, 3);
/// assert_eq!(left, vec![1, 2, 3]);
/// assert_eq!(right, vec![4, 5]);
/// ```
///
/// # Note
///
/// - This function creates new vectors, which means it allocates new memory and clones elements.
/// - If you only need to work with the split parts without creating new vectors or cloning elements,
/// consider using the standard library's `split_at` or `split_at_mut` methods instead.
/// - The original vector `v` is not modified by this operation.
/// Returns a new vector containing only unique elements from the input slice,
/// preserving the order of their first occurrence.
///
/// This function takes a slice of elements and returns a new vector containing
/// only the unique elements, maintaining the order in which they first appeared
/// in the original slice.
///
/// # Type Parameters
///
/// * `T` - The type of elements in the slice, which must implement `Clone`, `Eq`, and `Hash` traits.
///
/// # Arguments
///
/// * `input` - A slice of elements to be processed.
///
/// # Returns
///
/// A new `Vec<T>` containing only the unique elements from the input slice.
///
/// # Performance
///
/// - Time complexity: O(n), where n is the length of the input slice.
/// - Space complexity: O(n) for storing both the result and the hash set.
///
/// # Examples
///
/// Basic usage with integers:
/// ```
/// let numbers = vec![1, 2, 2, 3, 1, 4];
/// let unique = byteutils::vec::get_unique(&numbers);
/// assert_eq!(unique, vec![1, 2, 3, 4]);
/// ```
///
/// Using with strings:
/// ```
/// let words = vec!["apple", "banana", "apple", "cherry"];
/// let unique = byteutils::vec::get_unique(&words);
/// assert_eq!(unique, vec!["apple", "banana", "cherry"]);
/// ```
///
/// Empty input:
/// ```
/// let empty: Vec<i32> = vec![];
/// let unique = byteutils::vec::get_unique(&empty);
/// assert_eq!(unique, Vec::<i32>::new());
/// ```
///
/// # Note
///
/// - This function creates a new vector and clones elements, which means it allocates new memory.
/// - The implementation uses a HashSet internally for O(1) lookups while preserving order.
/// - The original slice is not modified by this operation.
/// - Compared to the O(n²) version using Vec::contains, this implementation is much more efficient
/// for large inputs, though it requires elements to implement the Hash trait.