[Python-checkins] python/dist/src/Objects setobject.c,1.9,1.10

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sat Nov 22 21:49:07 EST 2003


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv14916/Objects

Modified Files:
	setobject.c 
Log Message:
* Simplify hash function and add test to show effectiveness of the hash 
  function.

* Add a better test for deepcopying.

* Add tests to show the __init__() function works like it does for list
  and tuple.  Add related test.

* Have shallow copies of frozensets return self.  Add related test.

* Have frozenset(f) return f if f is already a frozenset. Add related test.

* Beefed-up some existing tests.



Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/setobject.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** setobject.c	22 Nov 2003 03:55:23 -0000	1.9
--- setobject.c	23 Nov 2003 02:49:05 -0000	1.10
***************
*** 65,68 ****
--- 65,72 ----
  	if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
  		return NULL;
+ 	if (iterable != NULL && iterable->ob_type == &PyFrozenSet_Type) {
+ 		Py_INCREF(iterable);
+ 		return iterable;
+ 	}
  	return make_new_set(type, iterable);
  }
***************
*** 155,158 ****
--- 159,172 ----
  }
  
+ static PyObject *
+ frozenset_copy(PySetObject *so)
+ {
+ 	if (so->ob_type == &PyFrozenSet_Type) {
+ 		Py_INCREF(so);
+ 		return (PyObject *)so;
+ 	}
+ 	return set_copy(so);
+ }
+ 
  PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
  
***************
*** 687,691 ****
  	PyObject *it, *item;
  	PySetObject *so = (PySetObject *)self;
! 	long hash = 0, x;
  
  	if (so->hash != -1)
--- 701,705 ----
  	PyObject *it, *item;
  	PySetObject *so = (PySetObject *)self;
! 	long hash = 0;
  
  	if (so->hash != -1)
***************
*** 697,708 ****
  
  	while ((item = PyIter_Next(it)) != NULL) {
! 		x = PyObject_Hash(item);
! 		/* Applying  x*(x+1) breaks-up linear relationships so that
! 		   h(1) ^ h(2) will be less likely to coincide with hash(3).
! 		   Multiplying by a large prime increases the dispersion 
! 		   between consecutive hashes.  Adding one bit from the 
! 		   original restores the one bit lost during the multiply 
! 		   (all the products are even numbers).  */
! 		hash ^= (x * (x+1) * 3644798167) | (x&1);
  		Py_DECREF(item);
  	}
--- 711,720 ----
  
  	while ((item = PyIter_Next(it)) != NULL) {
! 		/* Multiplying by a large prime increases the bit dispersion for
! 		   closely spaced hash values.  The is important because some
! 		   use cases have many combinations of a small number of 
! 		   elements with nearby hashes so that many distinct combinations
! 		   collapse to only a handful of distinct hash values. */
! 		hash ^= PyObject_Hash(item) * 3644798167;
  		Py_DECREF(item);
  	}
***************
*** 1097,1111 ****
  
  static PyMethodDef frozenset_methods[] = {
! 	{"copy",	(PyCFunction)set_copy,		METH_NOARGS,
  	 copy_doc},
! 	{"__copy__",	(PyCFunction)set_copy,		METH_NOARGS,
  	 copy_doc},
! 	{"difference",(PyCFunction)set_difference,	METH_O,
  	 difference_doc},
  	{"intersection",(PyCFunction)set_intersection,	METH_O,
  	 intersection_doc},
! 	{"issubset",(PyCFunction)set_issubset,		METH_O,
  	 issubset_doc},
! 	{"issuperset",(PyCFunction)set_issuperset,	METH_O,
  	 issuperset_doc},
  	{"__reduce__",	(PyCFunction)set_reduce,	METH_NOARGS,
--- 1109,1123 ----
  
  static PyMethodDef frozenset_methods[] = {
! 	{"copy",	(PyCFunction)frozenset_copy,	METH_NOARGS,
  	 copy_doc},
! 	{"__copy__",	(PyCFunction)frozenset_copy,	METH_NOARGS,
  	 copy_doc},
! 	{"difference",	(PyCFunction)set_difference,	METH_O,
  	 difference_doc},
  	{"intersection",(PyCFunction)set_intersection,	METH_O,
  	 intersection_doc},
! 	{"issubset",	(PyCFunction)set_issubset,	METH_O,
  	 issubset_doc},
! 	{"issuperset",	(PyCFunction)set_issuperset,	METH_O,
  	 issuperset_doc},
  	{"__reduce__",	(PyCFunction)set_reduce,	METH_NOARGS,





More information about the Python-checkins mailing list