@@ -566,6 +566,57 @@ set_contains_entry(PySetObject *so, setentry *entry)
566566 return key != NULL && key != dummy ;
567567}
568568
569+ static PyObject *
570+ set_pop (PySetObject * so )
571+ {
572+ PyObject * key ;
573+ register setentry * entry ;
574+ register int i = 0 ;
575+
576+ assert (PyAnySet_Check (so ));
577+ if (so -> used == 0 ) {
578+ PyErr_SetString (PyExc_KeyError , "pop from an empty set" );
579+ return NULL ;
580+ }
581+
582+ /* Set entry to "the first" unused or dummy set entry. We abuse
583+ * the hash field of slot 0 to hold a search finger:
584+ * If slot 0 has a value, use slot 0.
585+ * Else slot 0 is being used to hold a search finger,
586+ * and we use its hash value as the first index to look.
587+ */
588+ entry = & so -> table [0 ];
589+ if (entry -> key == NULL || entry -> key == dummy ) {
590+ i = (int )entry -> hash ;
591+ /* The hash field may be a real hash value, or it may be a
592+ * legit search finger, or it may be a once-legit search
593+ * finger that's out of bounds now because it wrapped around
594+ * or the table shrunk -- simply make sure it's in bounds now.
595+ */
596+ if (i > so -> mask || i < 1 )
597+ i = 1 ; /* skip slot 0 */
598+ while ((entry = & so -> table [i ])-> key == NULL || entry -> key == dummy ) {
599+ i ++ ;
600+ if (i > so -> mask )
601+ i = 1 ;
602+ }
603+ }
604+ key = entry -> key ;
605+ Py_INCREF (dummy );
606+ entry -> key = dummy ;
607+ so -> used -- ;
608+ so -> table [0 ].hash = i + 1 ; /* next place to start */
609+ return key ;
610+ }
611+
612+ PyDoc_STRVAR (pop_doc , "Remove and return an arbitrary set element." );
613+
614+ static int
615+ set_len (PyObject * so )
616+ {
617+ return ((PySetObject * )so )-> used ;
618+ }
619+
569620/***** Set iterator type ***********************************************/
570621
571622static PyTypeObject PySetIter_Type ; /* Forward */
@@ -682,12 +733,6 @@ static PyTypeObject PySetIter_Type = {
682733 (iternextfunc )setiter_iternext , /* tp_iternext */
683734};
684735
685- static int
686- set_len (PyObject * so )
687- {
688- return ((PySetObject * )so )-> used ;
689- }
690-
691736static int
692737set_update_internal (PySetObject * so , PyObject * other )
693738{
@@ -918,41 +963,6 @@ set_swap_bodies(PySetObject *a, PySetObject *b)
918963 }
919964}
920965
921- static int
922- set_contains (PySetObject * so , PyObject * key )
923- {
924- PyObject * tmpkey ;
925- int rv ;
926-
927- rv = set_contains_key (so , key );
928- if (rv == -1 ) {
929- if (!PyAnySet_Check (key ) || !PyErr_ExceptionMatches (PyExc_TypeError ))
930- return -1 ;
931- PyErr_Clear ();
932- tmpkey = make_new_set (& PyFrozenSet_Type , NULL );
933- if (tmpkey == NULL )
934- return -1 ;
935- set_swap_bodies ((PySetObject * )tmpkey , (PySetObject * )key );
936- rv = set_contains (so , tmpkey );
937- set_swap_bodies ((PySetObject * )tmpkey , (PySetObject * )key );
938- Py_DECREF (tmpkey );
939- }
940- return rv ;
941- }
942-
943- static PyObject *
944- set_direct_contains (PySetObject * so , PyObject * key )
945- {
946- long result ;
947-
948- result = set_contains (so , key );
949- if (result == -1 )
950- return NULL ;
951- return PyBool_FromLong (result );
952- }
953-
954- PyDoc_STRVAR (contains_doc , "x.__contains__(y) <==> y in x." );
955-
956966static PyObject *
957967set_copy (PySetObject * so )
958968{
@@ -1537,6 +1547,41 @@ PyDoc_STRVAR(add_doc,
15371547\n\
15381548This has no effect if the element is already present." );
15391549
1550+ static int
1551+ set_contains (PySetObject * so , PyObject * key )
1552+ {
1553+ PyObject * tmpkey ;
1554+ int rv ;
1555+
1556+ rv = set_contains_key (so , key );
1557+ if (rv == -1 ) {
1558+ if (!PyAnySet_Check (key ) || !PyErr_ExceptionMatches (PyExc_TypeError ))
1559+ return -1 ;
1560+ PyErr_Clear ();
1561+ tmpkey = make_new_set (& PyFrozenSet_Type , NULL );
1562+ if (tmpkey == NULL )
1563+ return -1 ;
1564+ set_swap_bodies ((PySetObject * )tmpkey , (PySetObject * )key );
1565+ rv = set_contains (so , tmpkey );
1566+ set_swap_bodies ((PySetObject * )tmpkey , (PySetObject * )key );
1567+ Py_DECREF (tmpkey );
1568+ }
1569+ return rv ;
1570+ }
1571+
1572+ static PyObject *
1573+ set_direct_contains (PySetObject * so , PyObject * key )
1574+ {
1575+ long result ;
1576+
1577+ result = set_contains (so , key );
1578+ if (result == -1 )
1579+ return NULL ;
1580+ return PyBool_FromLong (result );
1581+ }
1582+
1583+ PyDoc_STRVAR (contains_doc , "x.__contains__(y) <==> y in x." );
1584+
15401585static PyObject *
15411586set_remove (PySetObject * so , PyObject * key )
15421587{
@@ -1596,51 +1641,6 @@ PyDoc_STRVAR(discard_doc,
15961641\n\
15971642If the element is not a member, do nothing." );
15981643
1599- static PyObject *
1600- set_pop (PySetObject * so )
1601- {
1602- PyObject * key ;
1603- register setentry * entry ;
1604- register int i = 0 ;
1605-
1606- assert (PyAnySet_Check (so ));
1607- if (so -> used == 0 ) {
1608- PyErr_SetString (PyExc_KeyError , "pop from an empty set" );
1609- return NULL ;
1610- }
1611-
1612- /* Set entry to "the first" unused or dummy set entry. We abuse
1613- * the hash field of slot 0 to hold a search finger:
1614- * If slot 0 has a value, use slot 0.
1615- * Else slot 0 is being used to hold a search finger,
1616- * and we use its hash value as the first index to look.
1617- */
1618- entry = & so -> table [0 ];
1619- if (entry -> key == NULL || entry -> key == dummy ) {
1620- i = (int )entry -> hash ;
1621- /* The hash field may be a real hash value, or it may be a
1622- * legit search finger, or it may be a once-legit search
1623- * finger that's out of bounds now because it wrapped around
1624- * or the table shrunk -- simply make sure it's in bounds now.
1625- */
1626- if (i > so -> mask || i < 1 )
1627- i = 1 ; /* skip slot 0 */
1628- while ((entry = & so -> table [i ])-> key == NULL || entry -> key == dummy ) {
1629- i ++ ;
1630- if (i > so -> mask )
1631- i = 1 ;
1632- }
1633- }
1634- key = entry -> key ;
1635- Py_INCREF (dummy );
1636- entry -> key = dummy ;
1637- so -> used -- ;
1638- so -> table [0 ].hash = i + 1 ; /* next place to start */
1639- return key ;
1640- }
1641-
1642- PyDoc_STRVAR (pop_doc , "Remove and return an arbitrary set element." );
1643-
16441644static PyObject *
16451645set_reduce (PySetObject * so )
16461646{
0 commit comments