@@ -613,15 +613,73 @@ protected ListInsertNode createListInsertNode() {
613
613
@ GenerateNodeFactory
614
614
public abstract static class ListRemoveNode extends PythonBuiltinNode {
615
615
616
- @ Specialization
617
- public PList remove (PList list , Object arg ) {
618
- int index = list .index (arg );
619
- if (index >= 0 ) {
620
- list .delItem (index );
621
- return list ;
622
- } else {
623
- throw raise (PythonErrorType .ValueError , "list.remove(x): x not in list" );
616
+ private static String NOT_IN_LIST_MESSAGE = "list.index(x): x not in list" ;
617
+
618
+ @ Specialization (guards = "isIntStorage(list)" )
619
+ public PNone removeInt (PList list , int value ) {
620
+ IntSequenceStorage store = (IntSequenceStorage ) list .getSequenceStorage ();
621
+ for (int index = 0 ; index < store .length (); index ++) {
622
+ if (value == store .getIntItemNormalized (index )) {
623
+ store .delItemInBound (index );
624
+ return PNone .NONE ;
625
+ }
626
+ }
627
+ throw raise (PythonErrorType .ValueError , NOT_IN_LIST_MESSAGE );
628
+ }
629
+
630
+ @ Specialization (guards = "isLongStorage(list)" )
631
+ public PNone removeLong (PList list , int value ) {
632
+ LongSequenceStorage store = (LongSequenceStorage ) list .getSequenceStorage ();
633
+ for (int index = 0 ; index < store .length (); index ++) {
634
+ if (value == store .getLongItemNormalized (index )) {
635
+ store .delItemInBound (index );
636
+ return PNone .NONE ;
637
+ }
638
+ }
639
+ throw raise (PythonErrorType .ValueError , NOT_IN_LIST_MESSAGE );
640
+ }
641
+
642
+ @ Specialization (guards = "isLongStorage(list)" )
643
+ public PNone removeLong (PList list , long value ) {
644
+ LongSequenceStorage store = (LongSequenceStorage ) list .getSequenceStorage ();
645
+ for (int index = 0 ; index < store .length (); index ++) {
646
+ if (value == store .getLongItemNormalized (index )) {
647
+ store .delItemInBound (index );
648
+ return PNone .NONE ;
649
+ }
624
650
}
651
+ throw raise (PythonErrorType .ValueError , NOT_IN_LIST_MESSAGE );
652
+ }
653
+
654
+ @ Specialization (guards = "isDoubleStorage(list)" )
655
+ public PNone removeDouble (PList list , double value ) {
656
+ DoubleSequenceStorage store = (DoubleSequenceStorage ) list .getSequenceStorage ();
657
+ for (int index = 0 ; index < store .length (); index ++) {
658
+ if (value == store .getDoubleItemNormalized (index )) {
659
+ store .delItemInBound (index );
660
+ return PNone .NONE ;
661
+ }
662
+ }
663
+ throw raise (PythonErrorType .ValueError , NOT_IN_LIST_MESSAGE );
664
+ }
665
+
666
+ @ Specialization (guards = "isNotSpecialCase(list, value)" )
667
+ public PNone remove (PList list , Object value ,
668
+ @ Cached ("create(__EQ__, __EQ__, __EQ__)" ) BinaryComparisonNode eqNode ) {
669
+ int len = list .len ();
670
+ for (int i = 0 ; i < len ; i ++) {
671
+ Object object = list .getItem (i );
672
+ if (eqNode .executeBool (object , value )) {
673
+ list .delItem (i );
674
+ return PNone .NONE ;
675
+ }
676
+ }
677
+ throw raise (PythonErrorType .ValueError , NOT_IN_LIST_MESSAGE );
678
+ }
679
+
680
+ protected boolean isNotSpecialCase (PList list , Object value ) {
681
+ return !((PGuards .isIntStorage (list ) && value instanceof Integer ) || (PGuards .isLongStorage (list ) && (value instanceof Integer || value instanceof Long )) ||
682
+ PGuards .isDoubleStorage (list ) && value instanceof Double );
625
683
}
626
684
}
627
685
0 commit comments