Skip to content

Commit 2f4f8a9

Browse files
committed
add slice.indices() builtin
1 parent a6129c8 commit 2f4f8a9

File tree

1 file changed

+99
-0
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/slice

1 file changed

+99
-0
lines changed

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@
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.tuple.PTuple;
3738
import com.oracle.graal.python.nodes.SpecialMethodNames;
3839
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
3940
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
41+
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
4042
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
4143
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
44+
import com.oracle.truffle.api.dsl.Cached;
4245
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4346
import com.oracle.truffle.api.dsl.ImportStatic;
4447
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -85,6 +88,10 @@ protected int get(PSlice self) {
8588
protected Object getNone(@SuppressWarnings("unused") PSlice self) {
8689
return PNone.NONE;
8790
}
91+
92+
public static StartNode create() {
93+
return SliceBuiltinsFactory.StartNodeFactory.create();
94+
}
8895
}
8996

9097
@Builtin(name = "stop", fixedNumOfPositionalArgs = 1, isGetter = true)
@@ -101,6 +108,10 @@ protected int get(PSlice self) {
101108
protected Object getNone(@SuppressWarnings("unused") PSlice self) {
102109
return PNone.NONE;
103110
}
111+
112+
public static StopNode create() {
113+
return SliceBuiltinsFactory.StopNodeFactory.create();
114+
}
104115
}
105116

106117
@Builtin(name = "step", fixedNumOfPositionalArgs = 1, isGetter = true)
@@ -117,5 +128,93 @@ protected int get(PSlice self) {
117128
protected Object getNone(@SuppressWarnings("unused") PSlice self) {
118129
return PNone.NONE;
119130
}
131+
132+
public static StepNode create() {
133+
return SliceBuiltinsFactory.StepNodeFactory.create();
134+
}
135+
}
136+
137+
@Builtin(name = "indices", fixedNumOfPositionalArgs = 2)
138+
@GenerateNodeFactory
139+
@ImportStatic(SequenceUtil.class)
140+
abstract static class IndicesNode extends PythonBinaryBuiltinNode {
141+
142+
private Object[] adjustIndices(int length, int start, int stop, int step) {
143+
int _start = start;
144+
int _stop = stop;
145+
146+
if (start < 0) {
147+
_start += length;
148+
149+
if (_start < 0) {
150+
_start = (step < 0) ? -1 : 0;
151+
}
152+
} else if (start >= length) {
153+
_start = (step < 0) ? length - 1 : length;
154+
}
155+
156+
if (stop < 0) {
157+
_stop += length;
158+
159+
if (_stop < 0) {
160+
_stop = (step < 0) ? -1 : 0;
161+
}
162+
} else if (_stop >= length) {
163+
_stop = (step < 0) ? length - 1 : length;
164+
}
165+
166+
// if (step < 0) {
167+
// if (_stop < _start) {
168+
// return (_start - _stop - 1) / (-step) + 1;
169+
// }
170+
// }
171+
// else {
172+
// if (_start < _stop) {
173+
// return (_stop - _start - 1) / step + 1;
174+
// }
175+
// }
176+
177+
return new Object[]{_start, _stop, step};
178+
}
179+
180+
@Specialization()
181+
protected PTuple get(PSlice self, int length,
182+
@Cached("create()") StartNode startNode,
183+
@Cached("create()") StopNode stopNode,
184+
@Cached("create()") StepNode stepNode) {
185+
Object start = startNode.execute(self);
186+
Object stop = stopNode.execute(self);
187+
Object step = stepNode.execute(self);
188+
189+
int _start = -1;
190+
int _stop = -1;
191+
int _step = -1;
192+
193+
if (step == PNone.NONE) {
194+
_step = 1;
195+
} else if (step instanceof Integer) {
196+
_step = (int) step;
197+
}
198+
199+
if (start == PNone.NONE) {
200+
_start = _step < 0 ? length - 1 : 0;
201+
} else if (start instanceof Integer) {
202+
_start = (int) start;
203+
if (_start < 0) {
204+
_start += length;
205+
}
206+
}
207+
208+
if (stop == PNone.NONE) {
209+
_stop = _step < 0 ? -1 : length;
210+
} else if (stop instanceof Integer) {
211+
_stop = (int) stop;
212+
if (_stop < 0) {
213+
_stop += length;
214+
}
215+
}
216+
217+
return factory().createTuple(adjustIndices(length, _start, _stop, _step));
218+
}
120219
}
121220
}

0 commit comments

Comments
 (0)