Skip to content

Commit d520096

Browse files
committed
libstdc++: Fix std::deque::operator[] Xmethod [PR112491]
The Xmethod for std::deque::operator[] has the same bug that I recently fixed for the std::deque::size() Xmethod. The first node might have unused capacity at the start, which needs to be accounted for when indexing into the deque. libstdc++-v3/ChangeLog: PR libstdc++/112491 * python/libstdcxx/v6/xmethods.py (DequeWorkerBase.index): Correctly handle unused capacity at the start of the first node. * testsuite/libstdc++-xmethods/deque.cc: Check index operator when elements have been removed from the front. (cherry picked from commit 452476d)
1 parent c665769 commit d520096

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

libstdc++-v3/python/libstdcxx/v6/xmethods.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,21 @@ def __init__(self, val_type):
195195
def size(self, obj):
196196
start = obj['_M_impl']['_M_start']
197197
finish = obj['_M_impl']['_M_finish']
198-
if not start['_M_node']:
198+
if start['_M_cur'] == finish['_M_cur']:
199199
return 0
200200
return (self._bufsize
201201
* (finish['_M_node'] - start['_M_node'] - 1)
202202
+ (finish['_M_cur'] - finish['_M_first'])
203203
+ (start['_M_last'] - start['_M_cur']))
204204

205205
def index(self, obj, idx):
206-
first_node = obj['_M_impl']['_M_start']['_M_node']
207-
index_node = first_node + int(idx) // self._bufsize
208-
return index_node[0][idx % self._bufsize]
206+
start = obj['_M_impl']['_M_start']
207+
first_node_size = start['_M_last'] - start['_M_cur']
208+
if idx < first_node_size:
209+
return start['_M_cur'][idx]
210+
idx = idx - first_node_size
211+
index_node = start['_M_node'][1 + int(idx) // self._bufsize]
212+
return index_node[idx % self._bufsize]
209213

210214

211215
class DequeEmptyWorker(DequeWorkerBase):

libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,24 @@ main ()
6969

7070
// PR libstdc++/112491
7171
std::deque<int> q5;
72-
q5.push_front(0);
72+
q5.push_front(5);
7373
// { dg-final { note-test q5.size() 1 } }
74+
// { dg-final { note-test q5\[0\] 5 } }
7475
std::deque<int> q6 = q1;
7576
q6.pop_front();
7677
// { dg-final { note-test {q6.size() == (q1_size-1)} true } }
78+
// { dg-final { note-test q6\[1\] 102 } }
7779
std::deque<int> q7 = q2;
7880
q7.pop_front();
7981
q7.pop_front();
8082
// { dg-final { note-test {q7.size() == (q2_size-2)} true } }
83+
// { dg-final { note-test q7\[1\] 203 } }
8184
std::deque<int> q8 = q3;
8285
q8.pop_front();
8386
q8.pop_front();
8487
q8.pop_front();
8588
// { dg-final { note-test {q8.size() == (q3_size-3)} true } }
89+
// { dg-final { note-test q8\[1\] 304 } }
8690
std::deque<int> q9 = q8;
8791
q9.clear();
8892
// { dg-final { note-test q9.size() 0 } }

0 commit comments

Comments
 (0)