[Python-checkins] cpython: Minor edits: Tighten-up the halflen logic and touch-up the assertions and

raymond.hettinger python-checkins at python.org
Mon Feb 4 06:08:29 CET 2013


http://hg.python.org/cpython/rev/1791304d05c4
changeset:   81985:1791304d05c4
user:        Raymond Hettinger <python at rcn.com>
date:        Mon Feb 04 00:08:12 2013 -0500
summary:
  Minor edits:  Tighten-up the halflen logic and touch-up the assertions and comments.

files:
  Modules/_collectionsmodule.c |  16 ++++++----------
  1 files changed, 6 insertions(+), 10 deletions(-)


diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -3,13 +3,13 @@
 
 /* collections module implementation of a deque() datatype
    Written and maintained by Raymond D. Hettinger <python at rcn.com>
-   Copyright (c) 2004 Python Software Foundation.
+   Copyright (c) 2004-2013 Python Software Foundation.
    All rights reserved.
 */
 
 /* The block length may be set to any number over 1.  Larger numbers
  * reduce the number of calls to the memory allocator but take more
- * memory.  Ideally, BLOCKLEN should be set with an eye to the
+ * memory.  Ideally, BLOCKLEN should be set to a multiple of the
  * length of a cache line.
  */
 
@@ -413,7 +413,7 @@
 static int
 _deque_rotate(dequeobject *deque, Py_ssize_t n)
 {
-    Py_ssize_t i, m, len=deque->len, halflen=(len+1)>>1;
+    Py_ssize_t i, m, len=deque->len, halflen=len>>1;
     block *prevblock;
 
     if (len <= 1)
@@ -426,7 +426,7 @@
             n += len;
     }
     assert(deque->len > 1);
-    assert((n < len / 2) || (n > len / -2));
+    assert(-halflen <= n && n <= halflen);
 
     deque->state++;
     for (i=0 ; i<n ; ) {
@@ -446,9 +446,7 @@
             m = deque->rightindex + 1;
         if (m > deque->leftindex)
             m = deque->leftindex;
-        assert (m > 0);
-        assert (deque->leftblock != deque->rightblock ||
-                deque->leftindex < deque->rightindex - m + 1);
+        assert (m > 0 && m <= deque->len);
         memcpy(&deque->leftblock->data[deque->leftindex - m],
                &deque->rightblock->data[deque->rightindex - m + 1],
                m * sizeof(PyObject *));
@@ -483,9 +481,7 @@
             m = BLOCKLEN - deque->leftindex;
         if (m > BLOCKLEN - 1 - deque->rightindex)
             m = BLOCKLEN - 1 - deque->rightindex;
-        assert (m > 0);
-        assert (deque->leftblock != deque->rightblock ||
-                deque->leftindex + m < deque->rightindex + 1);
+        assert (m > 0 && m <= deque->len);
         memcpy(&deque->rightblock->data[deque->rightindex + 1],
                &deque->leftblock->data[deque->leftindex],
                m * sizeof(PyObject *));

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list