Skip to content

Commit dbed99d

Browse files
committed
Introduce 'RangeSequenceStorage'.
1 parent 7427dba commit dbed99d

File tree

2 files changed

+158
-10
lines changed

2 files changed

+158
-10
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.range;
2727

28-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.IndexError;
2928
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
3029
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
3130

3231
import com.oracle.graal.python.PythonLanguage;
3332
import com.oracle.graal.python.builtins.objects.type.PythonClass;
3433
import com.oracle.graal.python.runtime.sequence.PImmutableSequence;
34+
import com.oracle.graal.python.runtime.sequence.storage.RangeSequenceStorage;
3535
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
3636
import com.oracle.truffle.api.CompilerAsserts;
3737
import com.oracle.truffle.api.CompilerDirectives;
@@ -100,12 +100,8 @@ public int getStop() {
100100
return stop;
101101
}
102102

103-
public Object getItemNormalized(int index) {
104-
if (index >= length) {
105-
CompilerDirectives.transferToInterpreter();
106-
throw PythonLanguage.getCore().raise(IndexError, "range object index out of range");
107-
}
108-
103+
public int getItemNormalized(int index) {
104+
assert index < length;
109105
return index * step + start;
110106
}
111107

@@ -116,8 +112,7 @@ public int len() {
116112

117113
@Override
118114
public SequenceStorage getSequenceStorage() {
119-
CompilerDirectives.transferToInterpreter();
120-
throw new UnsupportedOperationException();
115+
return new RangeSequenceStorage(this);
121116
}
122117

123118
@Override
@@ -145,5 +140,4 @@ public boolean equals(Object other) {
145140
return false;
146141
}
147142
}
148-
149143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.oracle.graal.python.runtime.sequence.storage;
2+
3+
import com.oracle.graal.python.builtins.objects.range.PRange;
4+
5+
public class RangeSequenceStorage extends SequenceStorage {
6+
7+
private final PRange range;
8+
9+
public RangeSequenceStorage(PRange range) {
10+
this.range = range;
11+
}
12+
13+
public PRange getRange() {
14+
return range;
15+
}
16+
17+
@Override
18+
public int length() {
19+
return range.len();
20+
}
21+
22+
@Override
23+
public void setNewLength(int length) {
24+
throw new UnsupportedOperationException();
25+
}
26+
27+
@Override
28+
public SequenceStorage copy() {
29+
return this;
30+
}
31+
32+
@Override
33+
public SequenceStorage createEmpty(int newCapacity) {
34+
throw new UnsupportedOperationException();
35+
}
36+
37+
@Override
38+
public Object getInternalArrayObject() {
39+
throw new UnsupportedOperationException();
40+
}
41+
42+
@Override
43+
public ListStorageType getElementType() {
44+
return ListStorageType.Int;
45+
}
46+
47+
@Override
48+
public Object[] getInternalArray() {
49+
throw new UnsupportedOperationException();
50+
}
51+
52+
@Override
53+
public Object[] getCopyOfInternalArray() {
54+
throw new UnsupportedOperationException();
55+
}
56+
57+
@Override
58+
public Object getItemNormalized(int idx) {
59+
return range.getItemNormalized(idx);
60+
}
61+
62+
public int getIntItemNormalized(int idx) {
63+
return range.getItemNormalized(idx);
64+
}
65+
66+
@Override
67+
public void setItemNormalized(int idx, Object value) throws SequenceStoreException {
68+
throw new UnsupportedOperationException();
69+
}
70+
71+
@Override
72+
public void insertItem(int idx, Object value) throws SequenceStoreException {
73+
throw new UnsupportedOperationException();
74+
}
75+
76+
@Override
77+
public SequenceStorage getSliceInBound(int start, int stop, int step, int length) {
78+
throw new UnsupportedOperationException();
79+
}
80+
81+
@Override
82+
public void setSliceInBound(int start, int stop, int step, SequenceStorage sequence) throws SequenceStoreException {
83+
throw new UnsupportedOperationException();
84+
}
85+
86+
@Override
87+
public void delSlice(int start, int stop, int step) {
88+
throw new UnsupportedOperationException();
89+
}
90+
91+
@Override
92+
public void delItemInBound(int idx) {
93+
throw new UnsupportedOperationException();
94+
}
95+
96+
@Override
97+
public Object popInBound(int idx) {
98+
throw new UnsupportedOperationException();
99+
}
100+
101+
@Override
102+
public int index(Object value) {
103+
throw new UnsupportedOperationException();
104+
}
105+
106+
@Override
107+
public void append(Object value) throws SequenceStoreException {
108+
throw new UnsupportedOperationException();
109+
}
110+
111+
@Override
112+
public void extend(SequenceStorage other) throws SequenceStoreException, ArithmeticException {
113+
throw new UnsupportedOperationException();
114+
}
115+
116+
@Override
117+
public void reverse() {
118+
throw new UnsupportedOperationException();
119+
}
120+
121+
@Override
122+
public void sort() {
123+
throw new UnsupportedOperationException();
124+
}
125+
126+
@Override
127+
public boolean equals(SequenceStorage other) {
128+
if (other instanceof RangeSequenceStorage) {
129+
return range.equals(((RangeSequenceStorage) other).range);
130+
}
131+
return false;
132+
}
133+
134+
@Override
135+
public SequenceStorage generalizeFor(Object value, SequenceStorage other) {
136+
throw new UnsupportedOperationException();
137+
}
138+
139+
@Override
140+
public Object getIndicativeValue() {
141+
return range.getStart();
142+
}
143+
144+
@Override
145+
public void ensureCapacity(int newCapacity) {
146+
throw new UnsupportedOperationException();
147+
}
148+
149+
@Override
150+
public void copyItem(int idxTo, int idxFrom) {
151+
throw new UnsupportedOperationException();
152+
}
153+
154+
}

0 commit comments

Comments
 (0)