0
0
mirror of https://github.com/python/cpython.git synced 2024-11-24 17:47:13 +01:00

Simply deque repeat by reusing code in in-line repeat. Avoid unnecessary division.

This commit is contained in:
Raymond Hettinger 2015-09-09 22:39:44 -04:00
parent 87e8fe6cb3
commit f5d72f35e8

View File

@ -538,32 +538,6 @@ deque_concat(dequeobject *deque, PyObject *other)
static void deque_clear(dequeobject *deque);
static PyObject *
deque_repeat(dequeobject *deque, Py_ssize_t n)
{
dequeobject *new_deque;
PyObject *result;
/* XXX add a special case for when maxlen is defined */
if (n < 0)
n = 0;
else if (n > 0 && Py_SIZE(deque) > MAX_DEQUE_LEN / n)
return PyErr_NoMemory();
new_deque = (dequeobject *)deque_new(&deque_type, (PyObject *)NULL, (PyObject *)NULL);
new_deque->maxlen = deque->maxlen;
for ( ; n ; n--) {
result = deque_extend(new_deque, (PyObject *)deque);
if (result == NULL) {
Py_DECREF(new_deque);
return NULL;
}
Py_DECREF(result);
}
return (PyObject *)new_deque;
}
static PyObject *
deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
{
@ -583,10 +557,6 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
return (PyObject *)deque;
}
if (size > MAX_DEQUE_LEN / n) {
return PyErr_NoMemory();
}
if (size == 1) {
/* common case, repeating a single element */
PyObject *item = deque->leftblock->data[deque->leftindex];
@ -594,6 +564,9 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
if (deque->maxlen != -1 && n > deque->maxlen)
n = deque->maxlen;
if (n > MAX_DEQUE_LEN)
return PyErr_NoMemory();
for (i = 0 ; i < n-1 ; i++) {
rv = deque_append(deque, item);
if (rv == NULL)
@ -604,6 +577,10 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
return (PyObject *)deque;
}
if ((size_t)size > MAX_DEQUE_LEN / (size_t)n) {
return PyErr_NoMemory();
}
seq = PySequence_List((PyObject *)deque);
if (seq == NULL)
return seq;
@ -621,6 +598,17 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
return (PyObject *)deque;
}
static PyObject *
deque_repeat(dequeobject *deque, Py_ssize_t n)
{
dequeobject *new_deque;
new_deque = (dequeobject *)deque_copy((PyObject *) deque);
if (new_deque == NULL)
return NULL;
return deque_inplace_repeat(new_deque, n);
}
/* The rotate() method is part of the public API and is used internally
as a primitive for other methods.