Skip to content

Commit ff26d3f

Browse files
committed
Remove methods 'SequenceStorage.delItemInBound/popInBound'.
1 parent bf2c5ef commit ff26d3f

16 files changed

+21
-266
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_list.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@ class MyInt(int):
470470
a.remove(MyInt(2))
471471
self.assertEqual([1, 3], a)
472472

473+
a = ["1", "2"]
474+
a.remove("2");
475+
self.assertEqual(["1"], a)
476+
477+
a = [1.1, 2.2, 3.3]
478+
a.remove(2.2);
479+
self.assertEqual([1.1, 3.3], a)
480+
473481
def test_insert_spec(self):
474482
a = [1, 2]
475483
self.assertRaises(TypeError, a.insert, [1, 2, 3], 1)

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
@@ -2708,11 +2708,11 @@ public DeleteNode(NormalizeIndexNode normalizeIndexNode, String keyTypeErrorMess
27082708
this.keyTypeErrorMessage = keyTypeErrorMessage;
27092709
}
27102710

2711-
public abstract void execute(SequenceStorage s, Object key);
2711+
public abstract void execute(SequenceStorage s, Object indexOrSlice);
27122712

2713-
public abstract void execute(SequenceStorage s, int key);
2713+
public abstract void execute(SequenceStorage s, int index);
27142714

2715-
public abstract void execute(SequenceStorage s, long key);
2715+
public abstract void execute(SequenceStorage s, long index);
27162716

27172717
@Specialization
27182718
protected void doScalarInt(SequenceStorage storage, int idx) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/ListBuiltins.java

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import com.oracle.graal.python.nodes.control.GetIteratorNode;
7878
import com.oracle.graal.python.nodes.control.GetNextNode;
7979
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
80+
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
8081
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
8182
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
8283
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -400,78 +401,28 @@ protected ListInsertNode createListInsertNode() {
400401
// list.remove(x)
401402
@Builtin(name = "remove", fixedNumOfPositionalArgs = 2)
402403
@GenerateNodeFactory
403-
public abstract static class ListRemoveNode extends PythonBuiltinNode {
404+
public abstract static class ListRemoveNode extends PythonBinaryBuiltinNode {
404405

405-
private static String NOT_IN_LIST_MESSAGE = "list.index(x): x not in list";
406+
private static final String NOT_IN_LIST_MESSAGE = "list.index(x): x not in list";
406407

407-
@Specialization(guards = "isIntStorage(list)")
408-
public PNone removeInt(PList list, int value) {
409-
IntSequenceStorage store = (IntSequenceStorage) list.getSequenceStorage();
410-
for (int index = 0; index < store.length(); index++) {
411-
if (value == store.getIntItemNormalized(index)) {
412-
store.delItemInBound(index);
413-
return PNone.NONE;
414-
}
415-
}
416-
throw raise(PythonErrorType.ValueError, NOT_IN_LIST_MESSAGE);
417-
}
418-
419-
@Specialization(guards = "isLongStorage(list)")
420-
public PNone removeLong(PList list, int value) {
421-
LongSequenceStorage store = (LongSequenceStorage) list.getSequenceStorage();
422-
for (int index = 0; index < store.length(); index++) {
423-
if (value == store.getLongItemNormalized(index)) {
424-
store.delItemInBound(index);
425-
return PNone.NONE;
426-
}
427-
}
428-
throw raise(PythonErrorType.ValueError, NOT_IN_LIST_MESSAGE);
429-
}
430-
431-
@Specialization(guards = "isLongStorage(list)")
432-
public PNone removeLong(PList list, long value) {
433-
LongSequenceStorage store = (LongSequenceStorage) list.getSequenceStorage();
434-
for (int index = 0; index < store.length(); index++) {
435-
if (value == store.getLongItemNormalized(index)) {
436-
store.delItemInBound(index);
437-
return PNone.NONE;
438-
}
439-
}
440-
throw raise(PythonErrorType.ValueError, NOT_IN_LIST_MESSAGE);
441-
}
442-
443-
@Specialization(guards = "isDoubleStorage(list)")
444-
public PNone removeDouble(PList list, double value) {
445-
DoubleSequenceStorage store = (DoubleSequenceStorage) list.getSequenceStorage();
446-
for (int index = 0; index < store.length(); index++) {
447-
if (value == store.getDoubleItemNormalized(index)) {
448-
store.delItemInBound(index);
449-
return PNone.NONE;
450-
}
451-
}
452-
throw raise(PythonErrorType.ValueError, NOT_IN_LIST_MESSAGE);
453-
}
454-
455-
@Specialization(guards = "isNotSpecialCase(list, value)")
408+
@Specialization
456409
public PNone remove(PList list, Object value,
457410
@Cached("createNotNormalized()") SequenceStorageNodes.GetItemNode getItemNode,
411+
@Cached("create()") SequenceStorageNodes.DeleteNode deleteNode,
412+
@Cached("create()") SequenceStorageNodes.LenNode lenNode,
413+
@Cached("createIfTrueNode()") CastToBooleanNode castToBooleanNode,
458414
@Cached("create(__EQ__, __EQ__, __EQ__)") BinaryComparisonNode eqNode) {
459-
int len = list.len();
460415
SequenceStorage listStore = list.getSequenceStorage();
416+
int len = lenNode.execute(listStore);
461417
for (int i = 0; i < len; i++) {
462418
Object object = getItemNode.execute(listStore, i);
463-
if (eqNode.executeBool(object, value)) {
464-
listStore.delItemInBound(i);
419+
if (castToBooleanNode.executeWith(eqNode.executeWith(object, value))) {
420+
deleteNode.execute(listStore, i);
465421
return PNone.NONE;
466422
}
467423
}
468424
throw raise(PythonErrorType.ValueError, NOT_IN_LIST_MESSAGE);
469425
}
470-
471-
protected boolean isNotSpecialCase(PList list, Object value) {
472-
return !((PGuards.isIntStorage(list) && value instanceof Integer) || (PGuards.isLongStorage(list) && (value instanceof Integer || value instanceof Long)) ||
473-
PGuards.isDoubleStorage(list) && value instanceof Double);
474-
}
475426
}
476427

477428
// list.pop([i])

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,6 @@ public void setBoolSliceInBound(int start, int stop, int step, BoolSequenceStora
171171
length = length > stop ? length : stop;
172172
}
173173

174-
@Override
175-
public void delItemInBound(int idx) {
176-
if (values.length - 1 == idx) {
177-
popBool();
178-
} else {
179-
popInBound(idx);
180-
}
181-
}
182-
183-
@Override
184-
public Object popInBound(int idx) {
185-
boolean pop = values[idx];
186-
187-
for (int i = idx; i < values.length - 1; i++) {
188-
values[i] = values[i + 1];
189-
}
190-
191-
length--;
192-
return pop;
193-
}
194-
195174
public boolean popBool() {
196175
boolean pop = values[capacity - 1];
197176
length--;

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -253,27 +253,6 @@ public void setByteSliceInBound(int start, int stop, int step, ByteSequenceStora
253253
length = newLength;
254254
}
255255

256-
@Override
257-
public void delItemInBound(int idx) {
258-
if (values.length - 1 == idx) {
259-
popInt();
260-
} else {
261-
popInBound(idx);
262-
}
263-
}
264-
265-
@Override
266-
public Object popInBound(int idx) {
267-
int pop = values[idx] & 0xFF;
268-
269-
for (int i = idx; i < values.length - 1; i++) {
270-
values[i] = values[i + 1];
271-
}
272-
273-
length--;
274-
return pop;
275-
}
276-
277256
public int popInt() {
278257
int pop = values[capacity - 1] & 0xFF;
279258
length--;

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,6 @@ public CharSequenceStorage getSliceInBound(int start, int stop, int step, int sl
114114
throw new UnsupportedOperationException();
115115
}
116116

117-
@Override
118-
public void delItemInBound(int idx) {
119-
throw new UnsupportedOperationException();
120-
}
121-
122-
@Override
123-
public Object popInBound(int idx) {
124-
throw new UnsupportedOperationException();
125-
}
126-
127117
@Override
128118
public int index(Object value) {
129119
throw new UnsupportedOperationException();

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,6 @@ public void setDoubleSliceInBound(int start, int stop, int step, DoubleSequenceS
179179
length = length > stop ? length : stop;
180180
}
181181

182-
@Override
183-
public void delItemInBound(int idx) {
184-
popInBound(idx);
185-
}
186-
187-
@Override
188-
public Object popInBound(int idx) {
189-
double pop = values[idx];
190-
191-
for (int i = idx; i < values.length - 1; i++) {
192-
values[i] = values[i + 1];
193-
}
194-
195-
length--;
196-
return pop;
197-
}
198-
199182
public double popDouble() {
200183
double pop = values[length - 1];
201184
length--;

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,6 @@ public SequenceStorage getSliceInBound(int start, int stop, int step, int length
136136
return this;
137137
}
138138

139-
@Override
140-
public void delItemInBound(int idx) {
141-
throw new UnsupportedOperationException("Cannot delete from empty storage");
142-
}
143-
144-
@Override
145-
public Object popInBound(int idx) {
146-
return new UnsupportedOperationException();
147-
}
148-
149139
@Override
150140
public void reverse() {
151141
}

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,27 +179,6 @@ public void setIntSliceInBound(int start, int stop, int step, IntSequenceStorage
179179
length = length > stop ? length : stop;
180180
}
181181

182-
@Override
183-
public void delItemInBound(int idx) {
184-
if (values.length - 1 == idx) {
185-
popInt();
186-
} else {
187-
popInBound(idx);
188-
}
189-
}
190-
191-
@Override
192-
public Object popInBound(int idx) {
193-
int pop = values[idx];
194-
195-
for (int i = idx; i < values.length - 1; i++) {
196-
values[i] = values[i + 1];
197-
}
198-
199-
length--;
200-
return pop;
201-
}
202-
203182
public int popInt() {
204183
int pop = values[length - 1];
205184
length--;

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,27 +205,6 @@ public void setListSliceInBound(int start, int stop, int step, ListSequenceStora
205205
length = length > stop ? length : stop;
206206
}
207207

208-
@Override
209-
public void delItemInBound(int idx) {
210-
if (values.length - 1 == idx) {
211-
popList();
212-
} else {
213-
popInBound(idx);
214-
}
215-
}
216-
217-
@Override
218-
public Object popInBound(int idx) {
219-
PList pop = values[idx];
220-
221-
for (int i = idx; i < values.length - 1; i++) {
222-
values[i] = values[i + 1];
223-
}
224-
225-
length--;
226-
return pop;
227-
}
228-
229208
public PList popList() {
230209
PList pop = values[capacity - 1];
231210
length--;

0 commit comments

Comments
 (0)