@@ -502,17 +502,92 @@ public PList extend(PList list, Object source,
502
502
@ GenerateNodeFactory
503
503
public abstract static class ListInsertNode extends PythonBuiltinNode {
504
504
505
- @ Specialization
506
- public PList insert (PList list , int index , Object value ) {
505
+ public abstract PNone execute (PList list , Object index , Object value );
506
+
507
+ @ Specialization (guards = "isIntStorage(list)" )
508
+ public PNone insertIntInt (PList list , int index , int value ) {
509
+ IntSequenceStorage target = (IntSequenceStorage ) list .getSequenceStorage ();
510
+ index = normalizeIndex (index , list .len ());
511
+ target .insertIntItem (index , value );
512
+ return PNone .NONE ;
513
+ }
514
+
515
+ @ Specialization (guards = "isLongStorage(list)" )
516
+ public PNone insertLongLong (PList list , int index , int value ) {
517
+ LongSequenceStorage target = (LongSequenceStorage ) list .getSequenceStorage ();
518
+ index = normalizeIndex (index , list .len ());
519
+ target .insertLongItem (index , value );
520
+ return PNone .NONE ;
521
+ }
522
+
523
+ @ Specialization (guards = "isLongStorage(list)" )
524
+ public PNone insertLongLong (PList list , int index , long value ) {
525
+ LongSequenceStorage target = (LongSequenceStorage ) list .getSequenceStorage ();
526
+ index = normalizeIndex (index , list .len ());
527
+ target .insertLongItem (index , value );
528
+ return PNone .NONE ;
529
+ }
530
+
531
+ @ Specialization (guards = "isDoubleStorage(list)" )
532
+ public PNone insertDoubleDouble (PList list , int index , double value ) {
533
+ DoubleSequenceStorage target = (DoubleSequenceStorage ) list .getSequenceStorage ();
534
+ index = normalizeIndex (index , list .len ());
535
+ target .insertDoubleItem (index , value );
536
+ return PNone .NONE ;
537
+ }
538
+
539
+ @ Specialization (guards = "isNotSpecialCase(list, value)" )
540
+ public PNone insert (PList list , int index , Object value ) {
541
+ index = normalizeIndex (index , list .len ());
507
542
list .insert (index , value );
508
- return list ;
543
+ return PNone . NONE ;
509
544
}
510
545
511
546
@ Specialization
547
+ public PNone insert (PList list , PInt index , Object value ) {
548
+ int where = normalizeIndex (index .intValue (), list .len ());
549
+ list .insert (where , value );
550
+ return PNone .NONE ;
551
+ }
552
+
553
+ @ Specialization (guards = {"!isIntegerOrPInt(i)" })
512
554
@ SuppressWarnings ("unused" )
513
- public PList insert (PList list , Object i , Object arg1 ) {
514
- throw new RuntimeException ("invalid arguments for insert()" );
555
+ public PNone insert (PList list , Object i , Object value ,
556
+ @ Cached ("create(__INDEX__)" ) LookupAndCallUnaryNode indexNode ,
557
+ @ Cached ("createListInsertNode()" ) ListInsertNode insertNode ) {
558
+ Object indexValue = indexNode .executeObject (i );
559
+ if (PNone .NO_VALUE == indexValue ) {
560
+ throw raise (TypeError , "'%p' object cannot be interpreted as an integer" , i );
561
+ }
562
+ return insertNode .execute (list , indexValue , value );
563
+ }
564
+
565
+ private int normalizeIndex (int index , int len ) {
566
+ if (index < 0 ) {
567
+ index += len ;
568
+ if (index < 0 ) {
569
+ index = 0 ;
570
+ }
571
+ }
572
+ if (index > len ) {
573
+ index = len ;
574
+ }
575
+ return index ;
515
576
}
577
+
578
+ protected boolean isNotSpecialCase (PList list , Object value ) {
579
+ return !((PGuards .isIntStorage (list ) && value instanceof Integer ) || (PGuards .isLongStorage (list ) && PGuards .isInteger (value )) ||
580
+ (PGuards .isDoubleStorage (list ) && value instanceof Double ));
581
+ }
582
+
583
+ protected boolean isIntegerOrPInt (Object index ) {
584
+ return index instanceof Integer || index instanceof PInt ;
585
+ }
586
+
587
+ protected ListInsertNode createListInsertNode () {
588
+ return ListBuiltinsFactory .ListInsertNodeFactory .create (new PNode [0 ]);
589
+ }
590
+
516
591
}
517
592
518
593
// list.remove(x)
0 commit comments