@@ -519,16 +519,76 @@ public PList insert(PList list, Object i, Object arg1) {
519
519
@ Builtin (name = "remove" , fixedNumOfArguments = 2 )
520
520
@ GenerateNodeFactory
521
521
public abstract static class ListRemoveNode extends PythonBuiltinNode {
522
-
523
- @ Specialization
524
- public PList remove (PList list , Object arg ) {
525
- int index = list .index (arg );
526
- if (index >= 0 ) {
527
- list .delItem (index );
528
- return list ;
529
- } else {
530
- throw raise (PythonErrorType .ValueError , "list.remove(x): x not in list" );
522
+
523
+ private static String NOT_IN_LIST_MESSAGE = "list.index(x): x not in list" ;
524
+
525
+ @ Specialization (guards = "isIntStorage(list)" )
526
+ public PNone removeInt (PList list , int value ) {
527
+ IntSequenceStorage store = (IntSequenceStorage )list .getSequenceStorage ();
528
+ for (int index = 0 ; index < store .length (); index ++) {
529
+ if (value == store .getIntItemNormalized (index )) {
530
+ store .popInBound (index );
531
+ return PNone .NONE ;
532
+ }
533
+ }
534
+ throw raise (PythonErrorType .ValueError , NOT_IN_LIST_MESSAGE );
535
+ }
536
+
537
+ @ Specialization (guards = "isLongStorage(list)" )
538
+ public PNone removeLong (PList list , int value ) {
539
+ LongSequenceStorage store = (LongSequenceStorage )list .getSequenceStorage ();
540
+ for (int index = 0 ; index < store .length (); index ++) {
541
+ if (value == store .getLongItemNormalized (index )) {
542
+ store .popInBound (index );
543
+ return PNone .NONE ;
544
+ }
545
+ }
546
+ throw raise (PythonErrorType .ValueError , NOT_IN_LIST_MESSAGE );
547
+ }
548
+
549
+ @ Specialization (guards = "isLongStorage(list)" )
550
+ public PNone removeLong (PList list , long value ) {
551
+ LongSequenceStorage store = (LongSequenceStorage )list .getSequenceStorage ();
552
+ for (int index = 0 ; index < store .length (); index ++) {
553
+ if (value == store .getLongItemNormalized (index )) {
554
+ store .popInBound (index );
555
+ return PNone .NONE ;
556
+ }
557
+ }
558
+ throw raise (PythonErrorType .ValueError , NOT_IN_LIST_MESSAGE );
559
+ }
560
+
561
+ @ Specialization (guards = "isDoubleStorage(list)" )
562
+ public PNone removeLong (PList list , double value ) {
563
+ DoubleSequenceStorage store = (DoubleSequenceStorage )list .getSequenceStorage ();
564
+ for (int index = 0 ; index < store .length (); index ++) {
565
+ if (value == store .getDoubleItemNormalized (index )) {
566
+ store .popInBound (index );
567
+ return PNone .NONE ;
568
+ }
569
+ }
570
+ throw raise (PythonErrorType .ValueError , NOT_IN_LIST_MESSAGE );
571
+ }
572
+
573
+ @ Specialization (guards = "isNotSpecialCase(list, value)" )
574
+ public PNone remove (PList list , Object value ,
575
+ @ Cached ("createBinaryProfile()" ) ConditionProfile errorProfile ,
576
+ @ Cached ("create(__EQ__, __EQ__, __EQ__)" ) BinaryComparisonNode eqNode ) {
577
+ int len = list .len ();
578
+ for (int i = 0 ; i < len ; i ++) {
579
+ Object object = list .getItem (i );
580
+ if (eqNode .executeBool (object , value )) {
581
+ list .delItem (i );
582
+ return PNone .NONE ;
583
+ }
531
584
}
585
+ throw raise (PythonErrorType .ValueError , NOT_IN_LIST_MESSAGE );
586
+ }
587
+
588
+ protected boolean isNotSpecialCase (PList list , Object value ) {
589
+ return !((PGuards .isIntStorage (list ) && value instanceof Integer )
590
+ || (PGuards .isLongStorage (list ) && (value instanceof Integer || value instanceof Long ))
591
+ || PGuards .isDoubleStorage (list ) && value instanceof Double );
532
592
}
533
593
}
534
594
0 commit comments