Skip to content

Commit 6418288

Browse files
committed
Refactor: reverse method
1 parent 88d9f85 commit 6418288

File tree

9 files changed

+105
-102
lines changed

9 files changed

+105
-102
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,10 +1421,111 @@ public abstract static class ReverseNode extends Node {
14211421
static void doEmpty(@SuppressWarnings("unused") EmptySequenceStorage storage) {
14221422
}
14231423

1424-
@Specialization(limit = "MAX_BASIC_STORAGES", guards = "storage.getClass() == cachedClass")
1425-
static void doBasic(BasicSequenceStorage storage,
1426-
@Cached("storage.getClass()") Class<? extends BasicSequenceStorage> cachedClass) {
1427-
cachedClass.cast(storage).reverse();
1424+
@Specialization
1425+
static void doIntStorage(IntSequenceStorage storage) {
1426+
final int length = storage.length();
1427+
final int[] values = storage.getInternalIntArray();
1428+
if (length > 0) {
1429+
int head = 0;
1430+
int tail = length - 1;
1431+
int middle = (length - 1) / 2;
1432+
1433+
for (; head <= middle; head++, tail--) {
1434+
int temp = values[head];
1435+
values[head] = values[tail];
1436+
values[tail] = temp;
1437+
}
1438+
}
1439+
}
1440+
1441+
@Specialization
1442+
static void doDoubleStorage(DoubleSequenceStorage storage) {
1443+
final int length = storage.length();
1444+
final double[] values = storage.getInternalDoubleArray();
1445+
if (length > 0) {
1446+
int head = 0;
1447+
int tail = length - 1;
1448+
int middle = (length - 1) / 2;
1449+
1450+
for (; head <= middle; head++, tail--) {
1451+
double temp = values[head];
1452+
values[head] = values[tail];
1453+
values[tail] = temp;
1454+
}
1455+
}
1456+
}
1457+
1458+
@Specialization
1459+
static void doLongStorage(LongSequenceStorage storage) {
1460+
final int length = storage.length();
1461+
final long[] values = storage.getInternalLongArray();
1462+
if (length > 0) {
1463+
int head = 0;
1464+
int tail = length - 1;
1465+
int middle = (length - 1) / 2;
1466+
1467+
for (; head <= middle; head++, tail--) {
1468+
long temp = values[head];
1469+
values[head] = values[tail];
1470+
values[tail] = temp;
1471+
}
1472+
}
1473+
}
1474+
1475+
@Specialization
1476+
static void doByteStorage(ByteSequenceStorage storage) {
1477+
final int length = storage.length();
1478+
final byte[] values = storage.getInternalByteArray();
1479+
if (length > 0) {
1480+
int head = 0;
1481+
int tail = length - 1;
1482+
int middle = (length - 1) / 2;
1483+
1484+
for (; head <= middle; head++, tail--) {
1485+
byte temp = values[head];
1486+
values[head] = values[tail];
1487+
values[tail] = temp;
1488+
}
1489+
}
1490+
}
1491+
1492+
@Specialization
1493+
static void doObjectStorage(ObjectSequenceStorage storage) {
1494+
final int length = storage.length();
1495+
final Object[] values = storage.getInternalArray();
1496+
if (length > 0) {
1497+
int head = 0;
1498+
int tail = length - 1;
1499+
int middle = (length - 1) / 2;
1500+
1501+
for (; head <= middle; head++, tail--) {
1502+
Object temp = values[head];
1503+
values[head] = values[tail];
1504+
values[tail] = temp;
1505+
}
1506+
}
1507+
}
1508+
1509+
@Specialization
1510+
static void doBoolStorage(BoolSequenceStorage storage) {
1511+
final int length = storage.length();
1512+
final boolean[] values = storage.getInternalBoolArray();
1513+
if (length > 0) {
1514+
int head = 0;
1515+
int tail = length - 1;
1516+
int middle = (length - 1) / 2;
1517+
1518+
for (; head <= middle; head++, tail--) {
1519+
boolean temp = values[head];
1520+
values[head] = values[tail];
1521+
values[tail] = temp;
1522+
}
1523+
}
1524+
}
1525+
1526+
@Specialization
1527+
static void doMroStorage(MroSequenceStorage storage) {
1528+
throw CompilerDirectives.shouldNotReachHere();
14281529
}
14291530

14301531
@Specialization

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/BasicSequenceStorage.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public abstract class BasicSequenceStorage extends SequenceStorage {
3636

3737
public abstract void insertItem(int idx, Object value) throws SequenceStoreException;
3838

39-
public abstract void reverse();
40-
4139
public abstract SequenceStorage copy();
4240

4341
public abstract SequenceStorage generalizeFor(Object value, SequenceStorage other);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/BoolSequenceStorage.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,6 @@ private void insertBoolItem(int idx, boolean value) {
130130
length++;
131131
}
132132

133-
@Override
134-
public void reverse() {
135-
if (length > 0) {
136-
int head = 0;
137-
int tail = length - 1;
138-
int middle = (length - 1) / 2;
139-
140-
for (; head <= middle; head++, tail--) {
141-
boolean temp = values[head];
142-
values[head] = values[tail];
143-
values[tail] = temp;
144-
}
145-
}
146-
}
147-
148133
@Override
149134
public Object getIndicativeValue() {
150135
return false;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/ByteSequenceStorage.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,6 @@ public int indexOfInt(int value) {
158158
return -1;
159159
}
160160

161-
@Override
162-
public void reverse() {
163-
if (length > 0) {
164-
int head = 0;
165-
int tail = length - 1;
166-
int middle = (length - 1) / 2;
167-
168-
for (; head <= middle; head++, tail--) {
169-
byte temp = values[head];
170-
values[head] = values[tail];
171-
values[tail] = temp;
172-
}
173-
}
174-
}
175-
176161
@Override
177162
public Object getIndicativeValue() {
178163
return 0;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/DoubleSequenceStorage.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,6 @@ public int indexOfDouble(double value) {
142142
return -1;
143143
}
144144

145-
@Override
146-
public void reverse() {
147-
if (length > 0) {
148-
int head = 0;
149-
int tail = length - 1;
150-
int middle = (length - 1) / 2;
151-
152-
for (; head <= middle; head++, tail--) {
153-
double temp = values[head];
154-
values[head] = values[tail];
155-
values[tail] = temp;
156-
}
157-
}
158-
}
159-
160145
@Override
161146
public Object getIndicativeValue() {
162147
return .0;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/IntSequenceStorage.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,6 @@ public int indexOfInt(int value) {
142142
return -1;
143143
}
144144

145-
@Override
146-
public void reverse() {
147-
if (length > 0) {
148-
int head = 0;
149-
int tail = length - 1;
150-
int middle = (length - 1) / 2;
151-
152-
for (; head <= middle; head++, tail--) {
153-
int temp = values[head];
154-
values[head] = values[tail];
155-
values[tail] = temp;
156-
}
157-
}
158-
}
159-
160145
@Override
161146
public Object getIndicativeValue() {
162147
return 0;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/LongSequenceStorage.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,6 @@ public int indexOfLong(long value) {
152152
return -1;
153153
}
154154

155-
@Override
156-
public void reverse() {
157-
if (length > 0) {
158-
int head = 0;
159-
int tail = length - 1;
160-
int middle = (length - 1) / 2;
161-
162-
for (; head <= middle; head++, tail--) {
163-
long temp = values[head];
164-
values[head] = values[tail];
165-
values[tail] = temp;
166-
}
167-
}
168-
}
169-
170155
@Override
171156
public Object getIndicativeValue() {
172157
return 0;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/MroSequenceStorage.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,6 @@ public void increaseCapacityExactWithCopy(int newCapacity) {
150150
throw CompilerDirectives.shouldNotReachHere();
151151
}
152152

153-
@SuppressWarnings("unused")
154-
@Override
155-
public void reverse() {
156-
throw CompilerDirectives.shouldNotReachHere();
157-
}
158-
159153
@Override
160154
public Object getIndicativeValue() {
161155
return null;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/ObjectSequenceStorage.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,6 @@ public void increaseCapacityExactWithCopy(int newCapacity) {
105105
capacity = values.length;
106106
}
107107

108-
@Override
109-
public void reverse() {
110-
if (length > 0) {
111-
int head = 0;
112-
int tail = length - 1;
113-
int middle = (length - 1) / 2;
114-
115-
for (; head <= middle; head++, tail--) {
116-
Object temp = values[head];
117-
values[head] = values[tail];
118-
values[tail] = temp;
119-
}
120-
}
121-
}
122-
123108
@Override
124109
public ObjectSequenceStorage generalizeFor(Object value, SequenceStorage other) {
125110
return this;

0 commit comments

Comments
 (0)