87
87
self . count + self . overflow . as_ref ( ) . map_or ( 0 , Vec :: len)
88
88
}
89
89
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
90
/// Removes the element at a specific position (index) while preserving the order.
144
91
///
145
92
/// This function performs the following operations:
@@ -541,15 +488,15 @@ mod tests {
541
488
}
542
489
}
543
490
#[ test]
544
- fn test_delete_by_value_from_inline ( ) {
491
+ fn test_remove_at_inline ( ) {
545
492
let mut collection = GrowableArray :: < i32 > :: new ( ) ;
546
493
for i in 0 ..DEFAULT_MAX_INLINE_CAPACITY {
547
494
collection. push ( i as i32 ) ;
548
495
}
549
496
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY ) ;
550
497
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 ) ;
553
500
assert_eq ! ( removed, Some ( 3 ) ) ;
554
501
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY - 1 ) ;
555
502
@@ -561,13 +508,13 @@ mod tests {
561
508
assert_eq ! ( collection. get( i) , Some ( & ( ( i + 1 ) as i32 ) ) ) ;
562
509
}
563
510
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 ) ;
566
513
assert_eq ! ( non_existent, None ) ;
567
514
}
568
515
569
516
#[ test]
570
- fn test_delete_by_value_from_overflow ( ) {
517
+ fn test_remove_at_overflow ( ) {
571
518
let mut collection = GrowableArray :: < i32 > :: new ( ) ;
572
519
// Fill inline array
573
520
for i in 0 ..DEFAULT_MAX_INLINE_CAPACITY {
@@ -579,8 +526,8 @@ mod tests {
579
526
}
580
527
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 5 ) ;
581
528
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 ) ;
584
531
assert_eq ! ( removed, Some ( 12 ) ) ;
585
532
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 4 ) ;
586
533
@@ -594,13 +541,13 @@ mod tests {
594
541
}
595
542
596
543
#[ test]
597
- fn test_delete_last_element ( ) {
544
+ fn test_remove_at_last_element ( ) {
598
545
let mut collection = GrowableArray :: < i32 > :: new ( ) ;
599
546
collection. push ( 10 ) ;
600
547
assert_eq ! ( collection. len( ) , 1 ) ;
601
548
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 ) ;
604
551
assert_eq ! ( removed, Some ( 10 ) ) ;
605
552
assert_eq ! ( collection. len( ) , 0 ) ;
606
553
@@ -609,66 +556,25 @@ mod tests {
609
556
}
610
557
611
558
#[ 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 ( ) {
654
560
let mut collection = GrowableArray :: < i32 > :: new ( ) ;
655
561
656
562
// Fill inline array
657
563
for i in 0 ..DEFAULT_MAX_INLINE_CAPACITY {
658
564
collection. push ( i as i32 ) ;
659
- } // [0,1,2,3,4,5,6,7,8,9]
565
+ }
660
566
661
567
// Add overflow elements
662
568
for i in DEFAULT_MAX_INLINE_CAPACITY ..( DEFAULT_MAX_INLINE_CAPACITY + 3 ) {
663
569
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
666
573
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 3 ) ;
667
574
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 ) ) ;
672
578
assert_eq ! ( collection. len( ) , DEFAULT_MAX_INLINE_CAPACITY + 2 ) ;
673
579
674
580
// The last inline position should now be filled with the first overflow element
0 commit comments