@@ -87,40 +87,91 @@ impl<
87
87
self . count + self . overflow . as_ref ( ) . map_or ( 0 , Vec :: len)
88
88
}
89
89
90
- /// Deletes the element matching the given value from the array while preserving the order.
90
+ /// Removes the first element matching the given value from the array.
91
+ ///
92
+ /// This function searches both the internal array (`inline`) and the heap-allocated
93
+ /// vector (`overflow`) for the specified value. If found, it calls `delete_at` to
94
+ /// perform the deletion and preserve the order of the remaining elements.
95
+ ///
96
+ /// # Arguments
97
+ ///
98
+ /// - `item`: A reference to the value to be deleted.
99
+ ///
100
+ /// # Returns
101
+ ///
102
+ /// - `Some(T)`: The deleted value, if found.
103
+ /// - `None`: If the value was not found in either the array or the vector.
104
+ ///
105
+ #[ allow( dead_code) ]
106
+ pub ( crate ) fn remove_first ( & mut self , item : & T ) -> Option < T > {
107
+ // Search for the item using `iter()` and get its index
108
+ let index = self . iter ( ) . position ( |v| v == item) ?;
109
+
110
+ // Use `delete_at()` to remove the item by index
111
+ self . remove_at ( index)
112
+ }
113
+
114
+ /// Remove all elements matching the given value from the array.
115
+ ///
116
+ /// This function searches both the internal array (`inline`) and the heap-allocated
117
+ /// vector (`overflow`) for the specified value. If found, it removes all occurrences.
118
+ ///
119
+ /// # Arguments
120
+ ///
121
+ /// - `item`: A reference to the value to be deleted.
122
+ ///
123
+ /// # Returns
124
+ ///
125
+ /// - The number of deleted occurrences of the value.
126
+ #[ allow( dead_code) ]
127
+ /// Remove all elements matching the given value from the array.
128
+ pub ( crate ) fn remove ( & mut self , item : & T ) -> usize {
129
+ let mut deleted_count = 0 ;
130
+
131
+ // Loop to find and delete all occurrences
132
+ while let Some ( index) = {
133
+ let position = self . iter ( ) . position ( |v| v == item) ;
134
+ position
135
+ } {
136
+ self . remove_at ( index) ;
137
+ deleted_count += 1 ;
138
+ }
139
+
140
+ deleted_count
141
+ }
142
+
143
+ /// Removes the element at a specific position (index) while preserving the order.
91
144
///
92
145
/// This function performs the following operations:
93
146
///
94
- /// - Searches the internal array (`inline`) for the specified value.
95
- /// - If the value is found in the internal array:
96
- /// - Removes the value.
97
- /// - Shifts the remaining elements in the array to the left to fill the gap, preserving the order.
147
+ /// - If the index points to an element in the internal array (`inline`):
148
+ /// - Removes the element at the specified index.
149
+ /// - Shifts the remaining elements in the internal array to the left to fill the gap, preserving the order.
98
150
/// - If an overflow vector exists:
99
- /// - Moves the first element from the overflow vector into the last position of the internal array.
100
- /// - If the value is not found in the internal array, searches the heap-allocated vector (`overflow`).
101
- /// - If the value is found in the overflow vector, it is removed, and the remaining elements in the vector are shifted left to maintain order .
151
+ /// - Moves the first element from the overflow vector into the last position of the internal array, if available .
152
+ /// - If the index points to an element in the overflow vector:
153
+ /// - The element is removed directly from the overflow vector .
102
154
///
103
155
/// # Arguments
104
156
///
105
- /// - `value `: A reference to the value to be deleted.
157
+ /// - `index `: The index of the element to be deleted.
106
158
///
107
159
/// # Returns
108
160
///
109
- /// - `Some(T)`: The deleted value , if found.
110
- /// - `None`: If the value was found in neither the array nor the vector
161
+ /// - `Some(T)`: The deleted element , if found.
162
+ /// - `None`: If the index is out of bounds for both the internal array and the overflow vector.
111
163
///
112
164
#[ allow( dead_code) ]
113
- pub ( crate ) fn delete_item ( & mut self , item : & T ) -> Option < T > {
114
- // Search and remove from inline array
115
- if let Some ( index) = self . inline [ ..self . count ] . iter ( ) . position ( |v| v == item) {
165
+ pub ( crate ) fn remove_at ( & mut self , index : usize ) -> Option < T > {
166
+ if index < self . count {
116
167
let removed_value = self . inline [ index] . clone ( ) ;
117
168
118
- // Shift elements to the left to fill the gap
169
+ // Shift elements in inline array to the left
119
170
for i in index..self . count - 1 {
120
171
self . inline [ i] = self . inline [ i + 1 ] . clone ( ) ;
121
172
}
122
173
123
- // Check if we can move an element from the overflow into the inline array
174
+ // Handle moving an overflow element to inline, if available
124
175
let moved_from_overflow = if let Some ( ref mut overflow) = self . overflow {
125
176
if let Some ( first_from_overflow) = overflow. first ( ) . cloned ( ) {
126
177
self . inline [ self . count - 1 ] = first_from_overflow;
@@ -135,21 +186,22 @@ impl<
135
186
false
136
187
} ;
137
188
138
- // Only decrement count if no item was moved from the overflow
189
+ // Only decrement count if no item was moved from overflow
139
190
if !moved_from_overflow {
140
191
self . count -= 1 ;
141
192
}
142
193
return Some ( removed_value) ;
143
194
}
144
195
145
- // Search and remove from overflow vector
196
+ // Handle removing from the overflow vector
146
197
if let Some ( ref mut overflow) = self . overflow {
147
- if let Some ( index) = overflow. iter ( ) . position ( |v| v == item) {
148
- return Some ( overflow. remove ( index) ) ;
198
+ let overflow_index = index - MAX_INLINE_CAPACITY ;
199
+ if overflow_index < overflow. len ( ) {
200
+ return Some ( overflow. remove ( overflow_index) ) ;
149
201
}
150
202
}
151
203
152
- // Value not found
204
+ // Index out of bounds
153
205
None
154
206
}
155
207
@@ -497,7 +549,7 @@ mod tests {
497
549
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY ) ;
498
550
499
551
// Delete a value from the inline array
500
- let removed = collection. delete_item ( & 3 ) ;
552
+ let removed = collection. remove_first ( & 3 ) ;
501
553
assert_eq ! ( removed, Some ( 3 ) ) ;
502
554
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY - 1 ) ;
503
555
@@ -510,7 +562,7 @@ mod tests {
510
562
}
511
563
512
564
// Try to delete a value not in the array
513
- let non_existent = collection. delete_item ( & 99 ) ;
565
+ let non_existent = collection. remove_first ( & 99 ) ;
514
566
assert_eq ! ( non_existent, None ) ;
515
567
}
516
568
@@ -528,7 +580,7 @@ mod tests {
528
580
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 5 ) ;
529
581
530
582
// Delete a value from the overflow vector
531
- let removed = collection. delete_item ( & 12 ) ;
583
+ let removed = collection. remove_first ( & 12 ) ;
532
584
assert_eq ! ( removed, Some ( 12 ) ) ;
533
585
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 4 ) ;
534
586
@@ -548,7 +600,7 @@ mod tests {
548
600
assert_eq ! ( collection. len( ) , 1 ) ;
549
601
550
602
// Delete the only element in the collection
551
- let removed = collection. delete_item ( & 10 ) ;
603
+ let removed = collection. remove_first ( & 10 ) ;
552
604
assert_eq ! ( removed, Some ( 10 ) ) ;
553
605
assert_eq ! ( collection. len( ) , 0 ) ;
554
606
@@ -564,9 +616,9 @@ mod tests {
564
616
}
565
617
566
618
// Delete multiple values
567
- assert_eq ! ( collection. delete_item ( & 2 ) , Some ( 2 ) ) ;
619
+ assert_eq ! ( collection. remove ( & 2 ) , 1 ) ;
568
620
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY - 1 ) ;
569
- assert_eq ! ( collection. delete_item ( & 4 ) , Some ( 4 ) ) ;
621
+ assert_eq ! ( collection. remove ( & 4 ) , 1 ) ;
570
622
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY - 2 ) ;
571
623
572
624
// Ensure the elements are still correct
@@ -579,7 +631,7 @@ mod tests {
579
631
let mut collection = GrowableArray :: < i32 > :: new ( ) ;
580
632
581
633
// Try to delete from an empty array
582
- let removed = collection. delete_item ( & 5 ) ;
634
+ let removed = collection. remove_first ( & 5 ) ;
583
635
assert_eq ! ( removed, None ) ;
584
636
assert_eq ! ( collection. len( ) , 0 ) ;
585
637
}
@@ -592,8 +644,8 @@ mod tests {
592
644
collection. push ( 3 ) ;
593
645
594
646
// Try to delete a value not present
595
- let removed = collection. delete_item ( & 10 ) ;
596
- assert_eq ! ( removed, None ) ;
647
+ let removed = collection. remove ( & 10 ) ;
648
+ assert_eq ! ( removed, 0 ) ;
597
649
assert_eq ! ( collection. len( ) , 3 ) ;
598
650
}
599
651
@@ -614,8 +666,8 @@ mod tests {
614
666
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 3 ) ;
615
667
616
668
// Delete an inline value and ensure that an overflow value takes its place
617
- let removed = collection. delete_item ( & 5 ) ; // Deleting from inline
618
- assert_eq ! ( removed, Some ( 5 ) ) ;
669
+ let removed = collection. remove ( & 5 ) ; // Deleting from inline
670
+ assert_eq ! ( removed, 1 ) ;
619
671
// [0,1,2,3,4,6,7,8,9,10,11,12]
620
672
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 2 ) ;
621
673
0 commit comments