[Python-checkins] python/dist/src/Modules collectionsmodule.c, 1.5, 1.6

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sat Feb 7 23:05:28 EST 2004


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15559/Modules

Modified Files:
	collectionsmodule.c 
Log Message:
Make deque.rotate() smarter.  Beef-up related tests.

Index: collectionsmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/collectionsmodule.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** collectionsmodule.c	7 Feb 2004 21:13:00 -0000	1.5
--- collectionsmodule.c	8 Feb 2004 04:05:26 -0000	1.6
***************
*** 248,259 ****
  deque_rotate(dequeobject *deque, PyObject *args)
  {
! 	int i, n;
  	PyObject *item, *rv;
  
! 	if (!PyArg_ParseTuple(args, "i:rotate", &n))
  		return NULL;
  
! 	if (n == 0  ||  deque->len == 0)
  		Py_RETURN_NONE;
  
  	for (i=0 ; i<n ; i++) {
--- 248,266 ----
  deque_rotate(dequeobject *deque, PyObject *args)
  {
! 	int i, n=1, len=deque->len, halflen=(len+1)>>1;
  	PyObject *item, *rv;
  
! 	if (!PyArg_ParseTuple(args, "|i:rotate", &n))
  		return NULL;
  
! 	if (len == 0)
  		Py_RETURN_NONE;
+ 	if (n > halflen || n < -halflen) {
+ 		n %= len;
+ 		if (n > halflen)
+ 			n -= len;
+ 		else if (n < -halflen)
+ 			n += len;
+ 	}
  
  	for (i=0 ; i<n ; i++) {
***************
*** 281,285 ****
  
  PyDoc_STRVAR(rotate_doc, 
! "Rotate the deque n steps to the right.  If n is negative, rotates left.");
  
  static int
--- 288,292 ----
  
  PyDoc_STRVAR(rotate_doc, 
! "Rotate the deque n steps to the right (default n=1).  If n is negative, rotates left.");
  
  static int




More information about the Python-checkins mailing list