@@ -87,40 +87,91 @@ impl<
8787 self . count + self . overflow . as_ref ( ) . map_or ( 0 , Vec :: len)
8888 }
8989
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.
91144 ///
92145 /// This function performs the following operations:
93146 ///
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.
98150 /// - 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 .
102154 ///
103155 /// # Arguments
104156 ///
105- /// - `value `: A reference to the value to be deleted.
157+ /// - `index `: The index of the element to be deleted.
106158 ///
107159 /// # Returns
108160 ///
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.
111163 ///
112164 #[ 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 {
116167 let removed_value = self . inline [ index] . clone ( ) ;
117168
118- // Shift elements to the left to fill the gap
169+ // Shift elements in inline array to the left
119170 for i in index..self . count - 1 {
120171 self . inline [ i] = self . inline [ i + 1 ] . clone ( ) ;
121172 }
122173
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
124175 let moved_from_overflow = if let Some ( ref mut overflow) = self . overflow {
125176 if let Some ( first_from_overflow) = overflow. first ( ) . cloned ( ) {
126177 self . inline [ self . count - 1 ] = first_from_overflow;
@@ -135,21 +186,22 @@ impl<
135186 false
136187 } ;
137188
138- // Only decrement count if no item was moved from the overflow
189+ // Only decrement count if no item was moved from overflow
139190 if !moved_from_overflow {
140191 self . count -= 1 ;
141192 }
142193 return Some ( removed_value) ;
143194 }
144195
145- // Search and remove from overflow vector
196+ // Handle removing from the overflow vector
146197 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) ) ;
149201 }
150202 }
151203
152- // Value not found
204+ // Index out of bounds
153205 None
154206 }
155207
@@ -497,7 +549,7 @@ mod tests {
497549 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY ) ;
498550
499551 // Delete a value from the inline array
500- let removed = collection. delete_item ( & 3 ) ;
552+ let removed = collection. remove_first ( & 3 ) ;
501553 assert_eq ! ( removed, Some ( 3 ) ) ;
502554 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY - 1 ) ;
503555
@@ -510,7 +562,7 @@ mod tests {
510562 }
511563
512564 // 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 ) ;
514566 assert_eq ! ( non_existent, None ) ;
515567 }
516568
@@ -528,7 +580,7 @@ mod tests {
528580 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 5 ) ;
529581
530582 // Delete a value from the overflow vector
531- let removed = collection. delete_item ( & 12 ) ;
583+ let removed = collection. remove_first ( & 12 ) ;
532584 assert_eq ! ( removed, Some ( 12 ) ) ;
533585 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 4 ) ;
534586
@@ -548,7 +600,7 @@ mod tests {
548600 assert_eq ! ( collection. len( ) , 1 ) ;
549601
550602 // Delete the only element in the collection
551- let removed = collection. delete_item ( & 10 ) ;
603+ let removed = collection. remove_first ( & 10 ) ;
552604 assert_eq ! ( removed, Some ( 10 ) ) ;
553605 assert_eq ! ( collection. len( ) , 0 ) ;
554606
@@ -564,9 +616,9 @@ mod tests {
564616 }
565617
566618 // Delete multiple values
567- assert_eq ! ( collection. delete_item ( & 2 ) , Some ( 2 ) ) ;
619+ assert_eq ! ( collection. remove ( & 2 ) , 1 ) ;
568620 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 ) ;
570622 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY - 2 ) ;
571623
572624 // Ensure the elements are still correct
@@ -579,7 +631,7 @@ mod tests {
579631 let mut collection = GrowableArray :: < i32 > :: new ( ) ;
580632
581633 // Try to delete from an empty array
582- let removed = collection. delete_item ( & 5 ) ;
634+ let removed = collection. remove_first ( & 5 ) ;
583635 assert_eq ! ( removed, None ) ;
584636 assert_eq ! ( collection. len( ) , 0 ) ;
585637 }
@@ -592,8 +644,8 @@ mod tests {
592644 collection. push ( 3 ) ;
593645
594646 // 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 ) ;
597649 assert_eq ! ( collection. len( ) , 3 ) ;
598650 }
599651
@@ -614,8 +666,8 @@ mod tests {
614666 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 3 ) ;
615667
616668 // 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 ) ;
619671 // [0,1,2,3,4,6,7,8,9,10,11,12]
620672 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 2 ) ;
621673
0 commit comments