Issue 16398: Use memcpy() in deque.rotate().
This commit is contained in:
parent
2a051fa117
commit
426453282c
|
@ -413,9 +413,8 @@ deque_inplace_concat(dequeobject *deque, PyObject *other)
|
||||||
static int
|
static int
|
||||||
_deque_rotate(dequeobject *deque, Py_ssize_t n)
|
_deque_rotate(dequeobject *deque, Py_ssize_t n)
|
||||||
{
|
{
|
||||||
Py_ssize_t i, len=deque->len, halflen=(len+1)>>1;
|
Py_ssize_t i, m, len=deque->len, halflen=(len+1)>>1;
|
||||||
PyObject *item;
|
block *prevblock;
|
||||||
block *prevblock, *leftblock, *rightblock;
|
|
||||||
|
|
||||||
if (len <= 1)
|
if (len <= 1)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -429,64 +428,75 @@ _deque_rotate(dequeobject *deque, Py_ssize_t n)
|
||||||
|
|
||||||
assert(deque->len > 1);
|
assert(deque->len > 1);
|
||||||
deque->state++;
|
deque->state++;
|
||||||
leftblock = deque->leftblock;
|
for (i=0 ; i<n ; ) {
|
||||||
rightblock = deque->rightblock;
|
|
||||||
for (i=0 ; i<n ; i++) {
|
|
||||||
item = rightblock->data[deque->rightindex];
|
|
||||||
assert (item != NULL);
|
|
||||||
deque->rightindex--;
|
|
||||||
if (deque->rightindex == -1) {
|
|
||||||
assert(rightblock != NULL);
|
|
||||||
prevblock = rightblock->leftlink;
|
|
||||||
assert(leftblock != rightblock);
|
|
||||||
freeblock(rightblock);
|
|
||||||
prevblock->rightlink = NULL;
|
|
||||||
deque->rightblock = rightblock = prevblock;
|
|
||||||
deque->rightindex = BLOCKLEN - 1;
|
|
||||||
}
|
|
||||||
if (deque->leftindex == 0) {
|
if (deque->leftindex == 0) {
|
||||||
block *b = newblock(NULL, leftblock, deque->len);
|
block *b = newblock(NULL, deque->leftblock, deque->len);
|
||||||
if (b == NULL) {
|
if (b == NULL)
|
||||||
deque->len--;
|
|
||||||
Py_DECREF(item);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
assert(deque->leftblock->leftlink == NULL);
|
||||||
assert(leftblock->leftlink == NULL);
|
deque->leftblock->leftlink = b;
|
||||||
leftblock->leftlink = b;
|
deque->leftblock = b;
|
||||||
deque->leftblock = leftblock = b;
|
|
||||||
deque->leftindex = BLOCKLEN;
|
deque->leftindex = BLOCKLEN;
|
||||||
}
|
}
|
||||||
deque->leftindex--;
|
assert(deque->leftindex > 0);
|
||||||
leftblock->data[deque->leftindex] = item;
|
|
||||||
|
m = n - i;
|
||||||
|
if (m > deque->rightindex + 1)
|
||||||
|
m = deque->rightindex + 1;
|
||||||
|
if (m > deque->leftindex)
|
||||||
|
m = deque->leftindex;
|
||||||
|
assert (m > 0);
|
||||||
|
memcpy(&deque->leftblock->data[deque->leftindex - m],
|
||||||
|
&deque->rightblock->data[deque->rightindex - m + 1],
|
||||||
|
m * sizeof(PyObject *));
|
||||||
|
deque->rightindex -= m;
|
||||||
|
deque->leftindex -= m;
|
||||||
|
i += m;
|
||||||
|
|
||||||
|
if (deque->rightindex == -1) {
|
||||||
|
assert(deque->rightblock != NULL);
|
||||||
|
prevblock = deque->rightblock->leftlink;
|
||||||
|
assert(deque->leftblock != deque->rightblock);
|
||||||
|
freeblock(deque->rightblock);
|
||||||
|
prevblock->rightlink = NULL;
|
||||||
|
deque->rightblock = prevblock;
|
||||||
|
deque->rightindex = BLOCKLEN - 1;
|
||||||
}
|
}
|
||||||
for (i=0 ; i>n ; i--) {
|
|
||||||
assert(leftblock != NULL);
|
|
||||||
item = leftblock->data[deque->leftindex];
|
|
||||||
assert (item != NULL);
|
|
||||||
deque->leftindex++;
|
|
||||||
if (deque->leftindex == BLOCKLEN) {
|
|
||||||
assert(leftblock != rightblock);
|
|
||||||
prevblock = leftblock->rightlink;
|
|
||||||
freeblock(leftblock);
|
|
||||||
assert(prevblock != NULL);
|
|
||||||
prevblock->leftlink = NULL;
|
|
||||||
deque->leftblock = leftblock = prevblock;
|
|
||||||
deque->leftindex = 0;
|
|
||||||
}
|
}
|
||||||
|
for (i=0 ; i>n ; ) {
|
||||||
if (deque->rightindex == BLOCKLEN - 1) {
|
if (deque->rightindex == BLOCKLEN - 1) {
|
||||||
block *b = newblock(rightblock, NULL, deque->len);
|
block *b = newblock(deque->rightblock, NULL, deque->len);
|
||||||
if (b == NULL) {
|
if (b == NULL)
|
||||||
deque->len--;
|
|
||||||
Py_DECREF(item);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
assert(deque->rightblock->rightlink == NULL);
|
||||||
assert(rightblock->rightlink == NULL);
|
deque->rightblock->rightlink = b;
|
||||||
rightblock->rightlink = b;
|
deque->rightblock = b;
|
||||||
deque->rightblock = rightblock = b;
|
|
||||||
deque->rightindex = -1;
|
deque->rightindex = -1;
|
||||||
}
|
}
|
||||||
deque->rightindex++;
|
assert (deque->rightindex < BLOCKLEN - 1);
|
||||||
rightblock->data[deque->rightindex] = item;
|
|
||||||
|
m = i - n;
|
||||||
|
if (m > BLOCKLEN - deque->leftindex)
|
||||||
|
m = BLOCKLEN - deque->leftindex;
|
||||||
|
if (m > BLOCKLEN - 1 - deque->rightindex)
|
||||||
|
m = BLOCKLEN - 1 - deque->rightindex;
|
||||||
|
assert (m > 0);
|
||||||
|
memcpy(&deque->rightblock->data[deque->rightindex + 1],
|
||||||
|
&deque->leftblock->data[deque->leftindex],
|
||||||
|
m * sizeof(PyObject *));
|
||||||
|
deque->leftindex += m;
|
||||||
|
deque->rightindex += m;
|
||||||
|
i -= m;
|
||||||
|
|
||||||
|
if (deque->leftindex == BLOCKLEN) {
|
||||||
|
assert(deque->leftblock != deque->rightblock);
|
||||||
|
prevblock = deque->leftblock->rightlink;
|
||||||
|
freeblock(deque->leftblock);
|
||||||
|
assert(prevblock != NULL);
|
||||||
|
prevblock->leftlink = NULL;
|
||||||
|
deque->leftblock = prevblock;
|
||||||
|
deque->leftindex = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue