Skip to content

Commit e346194

Browse files
committed
SliceBuiltins: reuse existing computeIndices method from PSlice
1 parent be3019d commit e346194

File tree

7 files changed

+22
-109
lines changed

7 files changed

+22
-109
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/TruffleCextBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,22 +1248,22 @@ abstract static class PyTruffleSlice_GetIndicesEx extends NativeBuiltin {
12481248
@Specialization
12491249
Object doUnpack(int start, int stop, int step, int length) {
12501250
PSlice tmpSlice = factory().createSlice(start, stop, step);
1251-
SliceInfo actualIndices = tmpSlice.computeActualIndices(length);
1251+
SliceInfo actualIndices = tmpSlice.computeIndices(length);
12521252
return factory().createTuple(new Object[]{actualIndices.start, actualIndices.stop, actualIndices.step, actualIndices.length});
12531253
}
12541254

12551255
@Specialization(rewriteOn = ArithmeticException.class)
12561256
Object doUnpackLong(long start, long stop, long step, long length) {
12571257
PSlice tmpSlice = factory().createSlice(PInt.intValueExact(start), PInt.intValueExact(stop), PInt.intValueExact(step));
1258-
SliceInfo actualIndices = tmpSlice.computeActualIndices(PInt.intValueExact(length));
1258+
SliceInfo actualIndices = tmpSlice.computeIndices(PInt.intValueExact(length));
12591259
return factory().createTuple(new Object[]{actualIndices.start, actualIndices.stop, actualIndices.step, actualIndices.length});
12601260
}
12611261

12621262
@Specialization(replaces = {"doUnpackLong", "doUnpack"})
12631263
Object doUnpackLongOvf(long start, long stop, long step, long length) {
12641264
try {
12651265
PSlice tmpSlice = factory().createSlice(PInt.intValueExact(start), PInt.intValueExact(stop), PInt.intValueExact(step));
1266-
SliceInfo actualIndices = tmpSlice.computeActualIndices(length > Integer.MAX_VALUE ? Integer.MAX_VALUE : PInt.intValueExact(length));
1266+
SliceInfo actualIndices = tmpSlice.computeIndices(length > Integer.MAX_VALUE ? Integer.MAX_VALUE : PInt.intValueExact(length));
12671267
return factory().createTuple(new Object[]{actualIndices.start, actualIndices.stop, actualIndices.step, actualIndices.length});
12681268
} catch (ArithmeticException e) {
12691269
throw raiseIndexError();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ protected Object doScalarGeneric(SequenceStorage storage, Object idx) {
396396

397397
@Specialization
398398
protected Object doSlice(SequenceStorage storage, PSlice slice) {
399-
SliceInfo info = slice.computeActualIndices(storage.length());
399+
SliceInfo info = slice.computeIndices(storage.length());
400400
if (factoryMethod != null) {
401401
return factoryMethod.apply(getGetItemSliceNode().execute(storage, info.start, info.stop, info.step, info.length), factory());
402402
}
@@ -749,7 +749,7 @@ protected SequenceStorage doScalarGeneric(SequenceStorage storage, Object idx, O
749749

750750
@Specialization
751751
protected SequenceStorage doSlice(SequenceStorage storage, PSlice slice, Object iterable) {
752-
SliceInfo info = slice.computeActualIndices(storage.length());
752+
SliceInfo info = slice.computeIndices(storage.length());
753753
try {
754754
getSetItemSliceNode().execute(storage, info, iterable);
755755
return storage;
@@ -2727,7 +2727,7 @@ protected void doScalarGeneric(SequenceStorage storage, Object idx) {
27272727

27282728
@Specialization
27292729
protected void doSlice(SequenceStorage storage, PSlice slice) {
2730-
SliceInfo info = slice.computeActualIndices(storage.length());
2730+
SliceInfo info = slice.computeIndices(storage.length());
27312731
try {
27322732
getGetItemSliceNode().execute(storage, info);
27332733
} catch (SequenceStoreException e) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/foreign/AccessForeignItemNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected SliceInfo materializeSlice(PSlice idxSlice, TruffleObject object, Node
115115
}
116116

117117
// determine length (foreignSize is only required if start or stop is missing)
118-
return idxSlice.computeActualIndices(foreignSize);
118+
return idxSlice.computeIndices(foreignSize);
119119
}
120120
}
121121

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/range/RangeBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ Object doPRange(PRange primary, PInt idx) {
207207

208208
@Specialization
209209
Object doPRange(PRange range, PSlice slice) {
210-
SliceInfo info = slice.computeActualIndices(range.len());
210+
SliceInfo info = slice.computeIndices(range.len());
211211
int newStep = range.getStep() * info.step;
212212
int newStart = info.start == PSlice.MISSING_INDEX ? range.getStart() : range.getStart() + info.start * range.getStep();
213213
int newStop = info.stop == PSlice.MISSING_INDEX ? range.getStop() : Math.min(range.getStop(), newStart + info.length * newStep);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/slice/PSlice.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public SliceInfo(int start, int stop, int step, int length) {
101101
}
102102
}
103103

104-
public SliceInfo computeActualIndices(int len) {
104+
public SliceInfo computeIndices(int length) {
105105
int newStart = this.start;
106106
int newStop = this.stop;
107107
int newStep = this.step;
@@ -116,33 +116,33 @@ public SliceInfo computeActualIndices(int len) {
116116
throw PythonLanguage.getCore().raise(ValueError, "slice step cannot be zero");
117117
}
118118
}
119-
tmpStart = newStep < 0 ? len - 1 : 0;
120-
tmpStop = newStep < 0 ? -1 : len;
119+
tmpStart = newStep < 0 ? length - 1 : 0;
120+
tmpStop = newStep < 0 ? -1 : length;
121121

122122
if (newStart == MISSING_INDEX) {
123123
newStart = tmpStart;
124124
} else {
125125
if (newStart < 0) {
126-
newStart += len;
126+
newStart += length;
127127
}
128128
if (newStart < 0) {
129129
newStart = newStep < 0 ? -1 : 0;
130130
}
131-
if (newStart >= len) {
132-
newStart = newStep < 0 ? len - 1 : len;
131+
if (newStart >= length) {
132+
newStart = newStep < 0 ? length - 1 : length;
133133
}
134134
}
135135
if (newStop == MISSING_INDEX) {
136136
newStop = tmpStop;
137137
} else {
138138
if (newStop < 0) {
139-
newStop += len;
139+
newStop += length;
140140
}
141141
if (newStop < 0) {
142142
newStop = newStep < 0 ? -1 : 0;
143143
}
144-
if (newStop >= len) {
145-
newStop = newStep < 0 ? len - 1 : len;
144+
if (newStop >= length) {
145+
newStop = newStep < 0 ? length - 1 : length;
146146
}
147147
}
148148
if ((newStep < 0 && newStop >= newStart) || (newStep > 0 && newStart >= newStop)) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/slice/SliceBuiltins.java

Lines changed: 4 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,13 @@
3434
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3535
import com.oracle.graal.python.builtins.PythonBuiltins;
3636
import com.oracle.graal.python.builtins.objects.PNone;
37-
import com.oracle.graal.python.builtins.objects.slice.SliceBuiltinsFactory.StartNodeFactory;
38-
import com.oracle.graal.python.builtins.objects.slice.SliceBuiltinsFactory.StepNodeFactory;
39-
import com.oracle.graal.python.builtins.objects.slice.SliceBuiltinsFactory.StopNodeFactory;
4037
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
4138
import com.oracle.graal.python.nodes.SpecialMethodNames;
4239
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
4340
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
4441
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
4542
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
4643
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
47-
import com.oracle.truffle.api.dsl.Cached;
4844
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4945
import com.oracle.truffle.api.dsl.ImportStatic;
5046
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -91,10 +87,6 @@ protected int get(PSlice self) {
9187
protected Object getNone(@SuppressWarnings("unused") PSlice self) {
9288
return PNone.NONE;
9389
}
94-
95-
public static StartNode create() {
96-
return StartNodeFactory.create();
97-
}
9890
}
9991

10092
@Builtin(name = "stop", fixedNumOfPositionalArgs = 1, isGetter = true)
@@ -111,10 +103,6 @@ protected int get(PSlice self) {
111103
protected Object getNone(@SuppressWarnings("unused") PSlice self) {
112104
return PNone.NONE;
113105
}
114-
115-
public static StopNode create() {
116-
return StopNodeFactory.create();
117-
}
118106
}
119107

120108
@Builtin(name = "step", fixedNumOfPositionalArgs = 1, isGetter = true)
@@ -131,93 +119,18 @@ protected int get(PSlice self) {
131119
protected Object getNone(@SuppressWarnings("unused") PSlice self) {
132120
return PNone.NONE;
133121
}
134-
135-
public static StepNode create() {
136-
return StepNodeFactory.create();
137-
}
138122
}
139123

140124
@Builtin(name = "indices", fixedNumOfPositionalArgs = 2)
141125
@GenerateNodeFactory
142126
@ImportStatic(PSlice.class)
143127
abstract static class IndicesNode extends PythonBinaryBuiltinNode {
144128

145-
private static Object[] adjustIndices(int length, int start, int stop, int step) {
146-
int _start = start;
147-
int _stop = stop;
148-
149-
if (start < 0) {
150-
_start += length;
151-
152-
if (_start < 0) {
153-
_start = (step < 0) ? -1 : 0;
154-
}
155-
} else if (start >= length) {
156-
_start = (step < 0) ? length - 1 : length;
157-
}
158-
159-
if (stop < 0) {
160-
_stop += length;
161-
162-
if (_stop < 0) {
163-
_stop = (step < 0) ? -1 : 0;
164-
}
165-
} else if (_stop >= length) {
166-
_stop = (step < 0) ? length - 1 : length;
167-
}
168-
169-
// if (step < 0) {
170-
// if (_stop < _start) {
171-
// return (_start - _stop - 1) / (-step) + 1;
172-
// }
173-
// }
174-
// else {
175-
// if (_start < _stop) {
176-
// return (_stop - _start - 1) / step + 1;
177-
// }
178-
// }
179-
180-
return new Object[]{_start, _stop, step};
181-
}
182-
183129
@Specialization()
184-
protected PTuple get(PSlice self, int length,
185-
@Cached("create()") StartNode startNode,
186-
@Cached("create()") StopNode stopNode,
187-
@Cached("create()") StepNode stepNode) {
188-
Object start = startNode.execute(self);
189-
Object stop = stopNode.execute(self);
190-
Object step = stepNode.execute(self);
191-
192-
int _start = -1;
193-
int _stop = -1;
194-
int _step = -1;
195-
196-
if (step == PNone.NONE) {
197-
_step = 1;
198-
} else if (step instanceof Integer) {
199-
_step = (int) step;
200-
}
201-
202-
if (start == PNone.NONE) {
203-
_start = _step < 0 ? length - 1 : 0;
204-
} else if (start instanceof Integer) {
205-
_start = (int) start;
206-
if (_start < 0) {
207-
_start += length;
208-
}
209-
}
210-
211-
if (stop == PNone.NONE) {
212-
_stop = _step < 0 ? -1 : length;
213-
} else if (stop instanceof Integer) {
214-
_stop = (int) stop;
215-
if (_stop < 0) {
216-
_stop += length;
217-
}
218-
}
219-
220-
return factory().createTuple(adjustIndices(length, _start, _stop, _step));
130+
protected PTuple get(PSlice self, int length) {
131+
PSlice.SliceInfo sliceInfo = self.computeIndices(length);
132+
133+
return factory().createTuple(new Object[]{sliceInfo.start, sliceInfo.stop, sliceInfo.step});
221134
}
222135
}
223136
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ public abstract static class StrGetItemNode extends PythonBinaryBuiltinNode {
15461546

15471547
@Specialization
15481548
public String doString(String primary, PSlice slice) {
1549-
SliceInfo info = slice.computeActualIndices(primary.length());
1549+
SliceInfo info = slice.computeIndices(primary.length());
15501550
final int start = info.start;
15511551
int stop = info.stop;
15521552
int step = info.step;

0 commit comments

Comments
 (0)