31
31
import java .util .Arrays ;
32
32
33
33
import com .oracle .graal .python .PythonLanguage ;
34
+ import com .oracle .graal .python .builtins .objects .ints .PInt ;
34
35
import com .oracle .graal .python .runtime .sequence .SequenceUtil ;
35
36
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
36
37
@@ -377,19 +378,36 @@ public int indexOfInt(int value) {
377
378
public void append (Object value ) throws SequenceStoreException {
378
379
if (value instanceof Integer ) {
379
380
appendInt ((int ) value );
381
+ } else if (value instanceof Long ) {
382
+ appendLong ((long ) value );
383
+ } else if (value instanceof PInt ) {
384
+ try {
385
+ appendInt (((PInt ) value ).intValueExact ());
386
+ } catch (ArithmeticException e ) {
387
+ throw SequenceStoreException .INSTANCE ;
388
+ }
380
389
} else if (value instanceof Byte ) {
381
390
appendByte ((byte ) value );
382
391
} else {
383
392
throw SequenceStoreException .INSTANCE ;
384
393
}
385
394
}
386
395
396
+ public void appendLong (long value ) {
397
+ if (value < 0 || value >= 256 ) {
398
+ throw SequenceStoreException .INSTANCE ;
399
+ }
400
+ ensureCapacity (length + 1 );
401
+ values [length ] = (byte ) value ;
402
+ length ++;
403
+ }
404
+
387
405
public void appendInt (int value ) {
388
406
if (value < 0 || value >= 256 ) {
389
407
throw SequenceStoreException .INSTANCE ;
390
408
}
391
409
ensureCapacity (length + 1 );
392
- values [length ] = (( Integer ) value ). byteValue () ;
410
+ values [length ] = (byte ) value ;
393
411
length ++;
394
412
}
395
413
@@ -405,6 +423,8 @@ public void extend(SequenceStorage other) throws SequenceStoreException {
405
423
extendWithByteStorage ((ByteSequenceStorage ) other );
406
424
} else if (other instanceof IntSequenceStorage ) {
407
425
extendWithIntStorage ((IntSequenceStorage ) other );
426
+ } else if (other instanceof ObjectSequenceStorage ) {
427
+ extendWithObjectStorage ((ObjectSequenceStorage ) other );
408
428
} else {
409
429
throw SequenceStoreException .INSTANCE ;
410
430
}
@@ -432,7 +452,35 @@ private void extendWithIntStorage(IntSequenceStorage other) {
432
452
if (otherValue < 0 || otherValue >= 256 ) {
433
453
throw SequenceStoreException .INSTANCE ;
434
454
}
435
- values [i ] = ((Integer ) otherValue ).byteValue ();
455
+ values [i ] = (byte ) otherValue ;
456
+ }
457
+
458
+ length = extendedLength ;
459
+ }
460
+
461
+ private void extendWithObjectStorage (ObjectSequenceStorage other ) {
462
+ int extendedLength = length + other .length ();
463
+ ensureCapacity (extendedLength );
464
+ Object [] otherValues = other .getInternalArray ();
465
+
466
+ for (int i = length , j = 0 ; i < extendedLength ; i ++, j ++) {
467
+ Object otherValue = otherValues [j ];
468
+ long value = 0 ;
469
+ if (otherValue instanceof Integer ) {
470
+ value = (int ) otherValue ;
471
+ } else if (otherValue instanceof Long ) {
472
+ value = (long ) otherValue ;
473
+ } else if (otherValue instanceof PInt ) {
474
+ try {
475
+ value = ((PInt ) otherValue ).intValueExact ();
476
+ } catch (ArithmeticException e ) {
477
+ throw SequenceStoreException .INSTANCE ;
478
+ }
479
+ }
480
+ if (value < 0 || value >= 256 ) {
481
+ throw SequenceStoreException .INSTANCE ;
482
+ }
483
+ values [i ] = (byte ) value ;
436
484
}
437
485
438
486
length = extendedLength ;
0 commit comments