8787 self . count + self . overflow . as_ref ( ) . map_or ( 0 , Vec :: len)
8888 }
8989
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-
14390 /// Removes the element at a specific position (index) while preserving the order.
14491 ///
14592 /// This function performs the following operations:
@@ -541,15 +488,15 @@ mod tests {
541488 }
542489 }
543490 #[ test]
544- fn test_delete_by_value_from_inline ( ) {
491+ fn test_remove_at_inline ( ) {
545492 let mut collection = GrowableArray :: < i32 > :: new ( ) ;
546493 for i in 0 ..DEFAULT_MAX_INLINE_CAPACITY {
547494 collection. push ( i as i32 ) ;
548495 }
549496 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY ) ;
550497
551- // Delete a value from the inline array
552- let removed = collection. remove_first ( & 3 ) ;
498+ // Remove a value from the inline array using remove_at
499+ let removed = collection. remove_at ( 3 ) ;
553500 assert_eq ! ( removed, Some ( 3 ) ) ;
554501 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY - 1 ) ;
555502
@@ -561,13 +508,13 @@ mod tests {
561508 assert_eq ! ( collection. get( i) , Some ( & ( ( i + 1 ) as i32 ) ) ) ;
562509 }
563510
564- // Try to delete a value not in the array
565- let non_existent = collection. remove_first ( & 99 ) ;
511+ // Try to remove a value out of bounds
512+ let non_existent = collection. remove_at ( 99 ) ;
566513 assert_eq ! ( non_existent, None ) ;
567514 }
568515
569516 #[ test]
570- fn test_delete_by_value_from_overflow ( ) {
517+ fn test_remove_at_overflow ( ) {
571518 let mut collection = GrowableArray :: < i32 > :: new ( ) ;
572519 // Fill inline array
573520 for i in 0 ..DEFAULT_MAX_INLINE_CAPACITY {
@@ -579,8 +526,8 @@ mod tests {
579526 }
580527 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 5 ) ;
581528
582- // Delete a value from the overflow vector
583- let removed = collection. remove_first ( & 12 ) ;
529+ // Remove a value from the overflow vector using remove_at
530+ let removed = collection. remove_at ( DEFAULT_MAX_INLINE_CAPACITY + 2 ) ;
584531 assert_eq ! ( removed, Some ( 12 ) ) ;
585532 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 4 ) ;
586533
@@ -594,13 +541,13 @@ mod tests {
594541 }
595542
596543 #[ test]
597- fn test_delete_last_element ( ) {
544+ fn test_remove_at_last_element ( ) {
598545 let mut collection = GrowableArray :: < i32 > :: new ( ) ;
599546 collection. push ( 10 ) ;
600547 assert_eq ! ( collection. len( ) , 1 ) ;
601548
602- // Delete the only element in the collection
603- let removed = collection. remove_first ( & 10 ) ;
549+ // Remove the only element in the collection using remove_at
550+ let removed = collection. remove_at ( 0 ) ;
604551 assert_eq ! ( removed, Some ( 10 ) ) ;
605552 assert_eq ! ( collection. len( ) , 0 ) ;
606553
@@ -609,66 +556,25 @@ mod tests {
609556 }
610557
611558 #[ test]
612- fn test_delete_multiple_values ( ) {
613- let mut collection = GrowableArray :: < i32 > :: new ( ) ;
614- for i in 0 ..DEFAULT_MAX_INLINE_CAPACITY {
615- collection. push ( i as i32 ) ;
616- }
617-
618- // Delete multiple values
619- assert_eq ! ( collection. remove( & 2 ) , 1 ) ;
620- assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY - 1 ) ;
621- assert_eq ! ( collection. remove( & 4 ) , 1 ) ;
622- assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY - 2 ) ;
623-
624- // Ensure the elements are still correct
625- assert_eq ! ( collection. get( 2 ) , Some ( & 3 ) ) ;
626- assert_eq ! ( collection. get( 3 ) , Some ( & 5 ) ) ;
627- }
628-
629- #[ test]
630- fn test_delete_by_value_empty_array ( ) {
631- let mut collection = GrowableArray :: < i32 > :: new ( ) ;
632-
633- // Try to delete from an empty array
634- let removed = collection. remove_first ( & 5 ) ;
635- assert_eq ! ( removed, None ) ;
636- assert_eq ! ( collection. len( ) , 0 ) ;
637- }
638-
639- #[ test]
640- fn test_delete_by_value_not_in_array ( ) {
641- let mut collection = GrowableArray :: < i32 > :: new ( ) ;
642- collection. push ( 1 ) ;
643- collection. push ( 2 ) ;
644- collection. push ( 3 ) ;
645-
646- // Try to delete a value not present
647- let removed = collection. remove ( & 10 ) ;
648- assert_eq ! ( removed, 0 ) ;
649- assert_eq ! ( collection. len( ) , 3 ) ;
650- }
651-
652- #[ test]
653- fn test_delete_from_inline_and_replace_with_overflow ( ) {
559+ fn test_remove_at_from_inline_and_replace_with_overflow ( ) {
654560 let mut collection = GrowableArray :: < i32 > :: new ( ) ;
655561
656562 // Fill inline array
657563 for i in 0 ..DEFAULT_MAX_INLINE_CAPACITY {
658564 collection. push ( i as i32 ) ;
659- } // [0,1,2,3,4,5,6,7,8,9]
565+ }
660566
661567 // Add overflow elements
662568 for i in DEFAULT_MAX_INLINE_CAPACITY ..( DEFAULT_MAX_INLINE_CAPACITY + 3 ) {
663569 collection. push ( i as i32 ) ;
664- } // [0,1,2,3,4,5,6,7,8,9,10,11,12]
665- // Before delete, ensure that the count is correct
570+ }
571+
572+ // Before removing, ensure that the count is correct
666573 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 3 ) ;
667574
668- // Delete an inline value and ensure that an overflow value takes its place
669- let removed = collection. remove ( & 5 ) ; // Deleting from inline
670- assert_eq ! ( removed, 1 ) ;
671- // [0,1,2,3,4,6,7,8,9,10,11,12]
575+ // Remove an inline value and ensure that an overflow value takes its place using remove_at
576+ let removed = collection. remove_at ( 5 ) ;
577+ assert_eq ! ( removed, Some ( 5 ) ) ;
672578 assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 2 ) ;
673579
674580 // The last inline position should now be filled with the first overflow element
0 commit comments