annotate Objects/setobject.c @ 51271:87fbbf22adf3 3.0

Merged revisions 69214 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r69214 | mark.dickinson | 2009-02-02 20:36:42 +0000 (Mon, 02 Feb 2009) | 4 lines Issue #1717: rename tp_compare to tp_reserved. I'll change the type of tp_compare in a separate commit, for ease of reversion should things go wrong. ........
author Mark Dickinson <dickinsm@gmail.com>
date Mon, 02 Feb 2009 20:41:29 +0000
parents e9675d72e3e3
children b34cae786cb0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1
35311
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
2 /* set object implementation
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
4 Derived from Lib/sets.py and Objects/dictobject.c.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
5
49909
6e891353ce96 Update copyright for recent changes.
Raymond Hettinger <python@rcn.com>
parents: 49730
diff changeset
6 Copyright (c) 2003-2008 Python Software Foundation.
35311
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
7 All rights reserved.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
8 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
9
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10 #include "Python.h"
35311
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
11 #include "structmember.h"
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
12 #include "stringlib/eq.h"
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
13
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
14 /* Set a key error with the specified argument, wrapping it in a
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
15 * tuple automatically so that tuple keys are not unpacked as the
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
16 * exception arguments. */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
17 static void
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
18 set_key_error(PyObject *arg)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
19 {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
20 PyObject *tup;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
21 tup = PyTuple_Pack(1, arg);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
22 if (!tup)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
23 return; /* caller will expect error to be set anyway */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
24 PyErr_SetObject(PyExc_KeyError, tup);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
25 Py_DECREF(tup);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
26 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
27
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
28 /* This must be >= 1. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
29 #define PERTURB_SHIFT 5
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
30
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
31 /* Object used as dummy key to fill deleted entries */
35311
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
32 static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
33
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
34 #ifdef Py_REF_DEBUG
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
35 PyObject *
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
36 _PySet_Dummy(void)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
37 {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
38 return dummy;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
39 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
40 #endif
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
41
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
42 #define INIT_NONZERO_SET_SLOTS(so) do { \
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
43 (so)->table = (so)->smalltable; \
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
44 (so)->mask = PySet_MINSIZE - 1; \
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
45 (so)->hash = -1; \
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
46 } while(0)
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
47
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
48 #define EMPTY_TO_MINSIZE(so) do { \
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
50 (so)->used = (so)->fill = 0; \
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
51 INIT_NONZERO_SET_SLOTS(so); \
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
52 } while(0)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
53
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
54 /* Reuse scheme to save calls to malloc, free, and memset */
45192
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
55 #ifndef PySet_MAXFREELIST
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
56 #define PySet_MAXFREELIST 80
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
57 #endif
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
58 static PySetObject *free_list[PySet_MAXFREELIST];
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
59 static int numfree = 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
60
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
61
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
62 /*
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
63 The basic lookup function used by all operations.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
64 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
65 Open addressing is preferred over chaining since the link overhead for
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
66 chaining would be substantial (100% with typical malloc overhead).
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
67
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
68 The initial probe index is computed as hash mod the table size. Subsequent
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
69 probe indices are computed as explained in Objects/dictobject.c.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
70
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
71 All arithmetic on hash should ignore overflow.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
72
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
73 Unlike the dictionary implementation, the lookkey functions can return
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
74 NULL if the rich comparison returns an error.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
75 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
76
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
77 static setentry *
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
78 set_lookkey(PySetObject *so, PyObject *key, register long hash)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
79 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
80 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
81 register size_t perturb;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
82 register setentry *freeslot;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
83 register size_t mask = so->mask;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
84 setentry *table = so->table;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
85 register setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
86 register int cmp;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
87 PyObject *startkey;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
88
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
89 i = hash & mask;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
90 entry = &table[i];
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
91 if (entry->key == NULL || entry->key == key)
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
92 return entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
93
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
94 if (entry->key == dummy)
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
95 freeslot = entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
96 else {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
97 if (entry->hash == hash) {
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
98 startkey = entry->key;
47557
98827a21e352 Merged revisions 63724,63726,63732,63744,63754-63755,63757-63758,63760,63775,63781-63782,63787,63805-63808,63818-63819,63823-63824 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46742
diff changeset
99 Py_INCREF(startkey);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
100 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
47557
98827a21e352 Merged revisions 63724,63726,63732,63744,63754-63755,63757-63758,63760,63775,63781-63782,63787,63805-63808,63818-63819,63823-63824 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46742
diff changeset
101 Py_DECREF(startkey);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
102 if (cmp < 0)
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
103 return NULL;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
104 if (table == so->table && entry->key == startkey) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
105 if (cmp > 0)
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
106 return entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
107 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
108 else {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
109 /* The compare did major nasty stuff to the
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
110 * set: start over.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
111 */
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
112 return set_lookkey(so, key, hash);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
113 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
114 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
115 freeslot = NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
116 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
117
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
118 /* In the loop, key == dummy is by far (factor of 100s) the
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
119 least likely outcome, so test for that last. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
120 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
121 i = (i << 2) + i + perturb + 1;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
122 entry = &table[i & mask];
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
123 if (entry->key == NULL) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
124 if (freeslot != NULL)
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
125 entry = freeslot;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
126 break;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
127 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
128 if (entry->key == key)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
129 break;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
130 if (entry->hash == hash && entry->key != dummy) {
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
131 startkey = entry->key;
47557
98827a21e352 Merged revisions 63724,63726,63732,63744,63754-63755,63757-63758,63760,63775,63781-63782,63787,63805-63808,63818-63819,63823-63824 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46742
diff changeset
132 Py_INCREF(startkey);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
47557
98827a21e352 Merged revisions 63724,63726,63732,63744,63754-63755,63757-63758,63760,63775,63781-63782,63787,63805-63808,63818-63819,63823-63824 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46742
diff changeset
134 Py_DECREF(startkey);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
135 if (cmp < 0)
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
136 return NULL;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
137 if (table == so->table && entry->key == startkey) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
138 if (cmp > 0)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
139 break;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
140 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
141 else {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
142 /* The compare did major nasty stuff to the
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
143 * set: start over.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
144 */
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
145 return set_lookkey(so, key, hash);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
146 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
147 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
148 else if (entry->key == dummy && freeslot == NULL)
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
149 freeslot = entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
150 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
151 return entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
152 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
153
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
154 /*
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
155 * Hacked up version of set_lookkey which can assume keys are always unicode;
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
156 * This means we can always use unicode_eq directly and not have to check to
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
157 * see if the comparison altered the table.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
158 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
159 static setentry *
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
160 set_lookkey_unicode(PySetObject *so, PyObject *key, register long hash)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
161 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
162 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
163 register size_t perturb;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
164 register setentry *freeslot;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
165 register size_t mask = so->mask;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
166 setentry *table = so->table;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
167 register setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
168
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
169 /* Make sure this function doesn't have to handle non-unicode keys,
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
170 including subclasses of str; e.g., one reason to subclass
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
171 strings is to override __eq__, and for speed we don't cater to
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
172 that here. */
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
173 if (!PyUnicode_CheckExact(key)) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
174 so->lookup = set_lookkey;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
175 return set_lookkey(so, key, hash);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
176 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
177 i = hash & mask;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
178 entry = &table[i];
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
179 if (entry->key == NULL || entry->key == key)
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
180 return entry;
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
181 if (entry->key == dummy)
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
182 freeslot = entry;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
183 else {
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
184 if (entry->hash == hash && unicode_eq(entry->key, key))
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
185 return entry;
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
186 freeslot = NULL;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
187 }
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
188
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
189 /* In the loop, key == dummy is by far (factor of 100s) the
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
190 least likely outcome, so test for that last. */
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
191 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
192 i = (i << 2) + i + perturb + 1;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
193 entry = &table[i & mask];
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
194 if (entry->key == NULL)
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
195 return freeslot == NULL ? entry : freeslot;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
196 if (entry->key == key
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
197 || (entry->hash == hash
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
198 && entry->key != dummy
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
199 && unicode_eq(entry->key, key)))
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
200 return entry;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
201 if (entry->key == dummy && freeslot == NULL)
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
202 freeslot = entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
203 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
204 assert(0); /* NOT REACHED */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
205 return 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
206 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
207
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
208 /*
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
209 Internal routine to insert a new key into the table.
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
210 Used by the public insert routine.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
211 Eats a reference to key.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
212 */
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
213 static int
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
214 set_insert_key(register PySetObject *so, PyObject *key, long hash)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
215 {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
216 register setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
218
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
219 assert(so->lookup != NULL);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
220 entry = so->lookup(so, key, hash);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
221 if (entry == NULL)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
222 return -1;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
223 if (entry->key == NULL) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
224 /* UNUSED */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
225 so->fill++;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
226 entry->key = key;
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
227 entry->hash = hash;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
228 so->used++;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
229 } else if (entry->key == dummy) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
230 /* DUMMY */
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
231 entry->key = key;
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
232 entry->hash = hash;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
233 so->used++;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
234 Py_DECREF(dummy);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
235 } else {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
236 /* ACTIVE */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
237 Py_DECREF(key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
238 }
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
239 return 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
240 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
241
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
242 /*
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
243 Internal routine used by set_table_resize() to insert an item which is
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
244 known to be absent from the set. This routine also assumes that
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
245 the set contains no deleted entries. Besides the performance benefit,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
246 using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
247 Note that no refcounts are changed by this routine; if needed, the caller
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
248 is responsible for incref'ing `key`.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
249 */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
250 static void
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
251 set_insert_clean(register PySetObject *so, PyObject *key, long hash)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
252 {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
253 register size_t i;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
254 register size_t perturb;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
255 register size_t mask = (size_t)so->mask;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
256 setentry *table = so->table;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
257 register setentry *entry;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
258
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
259 i = hash & mask;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
260 entry = &table[i];
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
261 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
262 i = (i << 2) + i + perturb + 1;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
263 entry = &table[i & mask];
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
264 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
265 so->fill++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
266 entry->key = key;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
267 entry->hash = hash;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
268 so->used++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
269 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
270
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
271 /*
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
272 Restructure the table by allocating a new table and reinserting all
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
273 keys again. When entries have been deleted, the new table may
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
274 actually be smaller than the old one.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
275 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
276 static int
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
277 set_table_resize(PySetObject *so, Py_ssize_t minused)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
278 {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
279 Py_ssize_t newsize;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
280 setentry *oldtable, *newtable, *entry;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
281 Py_ssize_t i;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
282 int is_oldtable_malloced;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
283 setentry small_copy[PySet_MINSIZE];
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
284
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
285 assert(minused >= 0);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
286
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
287 /* Find the smallest table size > minused. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
288 for (newsize = PySet_MINSIZE;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
289 newsize <= minused && newsize > 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
290 newsize <<= 1)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
291 ;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
292 if (newsize <= 0) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
293 PyErr_NoMemory();
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
294 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
295 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
296
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
297 /* Get space for a new table. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
298 oldtable = so->table;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
299 assert(oldtable != NULL);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
300 is_oldtable_malloced = oldtable != so->smalltable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
301
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
302 if (newsize == PySet_MINSIZE) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
303 /* A large table is shrinking, or we can't get any smaller. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
304 newtable = so->smalltable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
305 if (newtable == oldtable) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
306 if (so->fill == so->used) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
307 /* No dummies, so no point doing anything. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
308 return 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
309 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
310 /* We're not going to resize it, but rebuild the
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
311 table anyway to purge old dummy entries.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
312 Subtle: This is *necessary* if fill==size,
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
313 as set_lookkey needs at least one virgin slot to
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
314 terminate failing searches. If fill < size, it's
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
315 merely desirable, as dummies slow searches. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
316 assert(so->fill > so->used);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
317 memcpy(small_copy, oldtable, sizeof(small_copy));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
318 oldtable = small_copy;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
319 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
320 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
321 else {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
322 newtable = PyMem_NEW(setentry, newsize);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
323 if (newtable == NULL) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
324 PyErr_NoMemory();
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
325 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
326 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
327 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
328
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
329 /* Make the set empty, using the new table. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
330 assert(newtable != oldtable);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
331 so->table = newtable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
332 so->mask = newsize - 1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
333 memset(newtable, 0, sizeof(setentry) * newsize);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
334 so->used = 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
335 i = so->fill;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
336 so->fill = 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
337
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
338 /* Copy the data over; this is refcount-neutral for active entries;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
339 dummy entries aren't copied over, of course */
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
340 for (entry = oldtable; i > 0; entry++) {
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
341 if (entry->key == NULL) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
342 /* UNUSED */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
343 ;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
344 } else if (entry->key == dummy) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
345 /* DUMMY */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
346 --i;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
347 assert(entry->key == dummy);
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
348 Py_DECREF(entry->key);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
349 } else {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
350 /* ACTIVE */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
351 --i;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
352 set_insert_clean(so, entry->key, entry->hash);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
353 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
354 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
355
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
356 if (is_oldtable_malloced)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
357 PyMem_DEL(oldtable);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
358 return 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
359 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
360
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
361 /* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
362
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
363 static int
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
364 set_add_entry(register PySetObject *so, setentry *entry)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
365 {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
366 register Py_ssize_t n_used;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
367
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
368 assert(so->fill <= so->mask); /* at least one empty slot */
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
369 n_used = so->used;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
370 Py_INCREF(entry->key);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
371 if (set_insert_key(so, entry->key, entry->hash) == -1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
372 Py_DECREF(entry->key);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
373 return -1;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
374 }
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
375 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
376 return 0;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
377 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
378 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
379
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
380 static int
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
381 set_add_key(register PySetObject *so, PyObject *key)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
382 {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
383 register long hash;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
384 register Py_ssize_t n_used;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
385
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
386 if (!PyUnicode_CheckExact(key) ||
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
387 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
388 hash = PyObject_Hash(key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
389 if (hash == -1)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
390 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
391 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
392 assert(so->fill <= so->mask); /* at least one empty slot */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
393 n_used = so->used;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
394 Py_INCREF(key);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
395 if (set_insert_key(so, key, hash) == -1) {
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
396 Py_DECREF(key);
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
397 return -1;
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
398 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
399 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
400 return 0;
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
401 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
402 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
403
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
404 #define DISCARD_NOTFOUND 0
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
405 #define DISCARD_FOUND 1
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
406
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
407 static int
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
408 set_discard_entry(PySetObject *so, setentry *oldentry)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
409 { register setentry *entry;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
410 PyObject *old_key;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
411
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
412 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
413 if (entry == NULL)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
414 return -1;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
415 if (entry->key == NULL || entry->key == dummy)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
416 return DISCARD_NOTFOUND;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
417 old_key = entry->key;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
418 Py_INCREF(dummy);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
419 entry->key = dummy;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
420 so->used--;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
421 Py_DECREF(old_key);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
422 return DISCARD_FOUND;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
423 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
424
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
425 static int
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
426 set_discard_key(PySetObject *so, PyObject *key)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
427 {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
428 register long hash;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
429 register setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
430 PyObject *old_key;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
431
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
432 assert (PyAnySet_Check(so));
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
433
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
434 if (!PyUnicode_CheckExact(key) ||
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
435 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
436 hash = PyObject_Hash(key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
437 if (hash == -1)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
438 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
439 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
440 entry = (so->lookup)(so, key, hash);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
441 if (entry == NULL)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
442 return -1;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
443 if (entry->key == NULL || entry->key == dummy)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
444 return DISCARD_NOTFOUND;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
445 old_key = entry->key;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
446 Py_INCREF(dummy);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
447 entry->key = dummy;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
448 so->used--;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
449 Py_DECREF(old_key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
450 return DISCARD_FOUND;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
451 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
452
35315
a6cbfcd0e691 Factor away a redundant clear() function.
Raymond Hettinger <python@rcn.com>
parents: 35312
diff changeset
453 static int
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
454 set_clear_internal(PySetObject *so)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
455 {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
456 setentry *entry, *table;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
457 int table_is_malloced;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
458 Py_ssize_t fill;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
459 setentry small_copy[PySet_MINSIZE];
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
460 #ifdef Py_DEBUG
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
461 Py_ssize_t i, n;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
462 assert (PyAnySet_Check(so));
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
463
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
464 n = so->mask + 1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
465 i = 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
466 #endif
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
467
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
468 table = so->table;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
469 assert(table != NULL);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
470 table_is_malloced = table != so->smalltable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
471
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
472 /* This is delicate. During the process of clearing the set,
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
473 * decrefs can cause the set to mutate. To avoid fatal confusion
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
474 * (voice of experience), we have to make the set empty before
35315
a6cbfcd0e691 Factor away a redundant clear() function.
Raymond Hettinger <python@rcn.com>
parents: 35312
diff changeset
475 * clearing the slots, and never refer to anything via so->ref while
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
476 * clearing.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
477 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
478 fill = so->fill;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
479 if (table_is_malloced)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
480 EMPTY_TO_MINSIZE(so);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
481
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
482 else if (fill > 0) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
483 /* It's a small table with something that needs to be cleared.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
484 * Afraid the only safe way is to copy the set entries into
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
485 * another small table first.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
486 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
487 memcpy(small_copy, table, sizeof(small_copy));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
488 table = small_copy;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
489 EMPTY_TO_MINSIZE(so);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
490 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
491 /* else it's a small table that's already empty */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
492
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
493 /* Now we can finally clear things. If C had refcounts, we could
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
494 * assert that the refcount on table is 1 now, i.e. that this function
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
495 * has unique access to it, so decref side-effects can't alter it.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
496 */
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
497 for (entry = table; fill > 0; ++entry) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
498 #ifdef Py_DEBUG
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
499 assert(i < n);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
500 ++i;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
501 #endif
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
502 if (entry->key) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
503 --fill;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
504 Py_DECREF(entry->key);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
505 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
506 #ifdef Py_DEBUG
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
507 else
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
508 assert(entry->key == NULL);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
509 #endif
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
510 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
511
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
512 if (table_is_malloced)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
513 PyMem_DEL(table);
35315
a6cbfcd0e691 Factor away a redundant clear() function.
Raymond Hettinger <python@rcn.com>
parents: 35312
diff changeset
514 return 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
515 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
516
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
517 /*
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
518 * Iterate over a set table. Use like so:
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
519 *
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
520 * Py_ssize_t pos;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
521 * setentry *entry;
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
522 * pos = 0; # important! pos should not otherwise be changed by you
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
523 * while (set_next(yourset, &pos, &entry)) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
524 * Refer to borrowed reference in entry->key.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
525 * }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
526 *
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
527 * CAUTION: In general, it isn't safe to use set_next in a loop that
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
528 * mutates the table.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
529 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
530 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
531 set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
532 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
533 Py_ssize_t i;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
534 Py_ssize_t mask;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
535 register setentry *table;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
536
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
537 assert (PyAnySet_Check(so));
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
538 i = *pos_ptr;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
539 assert(i >= 0);
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
540 table = so->table;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
541 mask = so->mask;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
542 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
543 i++;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
544 *pos_ptr = i+1;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
545 if (i > mask)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
546 return 0;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
547 assert(table[i].key != NULL);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
548 *entry_ptr = &table[i];
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
549 return 1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
550 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
551
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
552 static void
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
553 set_dealloc(PySetObject *so)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
554 {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
555 register setentry *entry;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
556 Py_ssize_t fill = so->fill;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
557 PyObject_GC_UnTrack(so);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
558 Py_TRASHCAN_SAFE_BEGIN(so)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
559 if (so->weakreflist != NULL)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
560 PyObject_ClearWeakRefs((PyObject *) so);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
561
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
562 for (entry = so->table; fill > 0; entry++) {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
563 if (entry->key) {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
564 --fill;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
565 Py_DECREF(entry->key);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
566 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
567 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
568 if (so->table != so->smalltable)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
569 PyMem_DEL(so->table);
45192
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
570 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
571 free_list[numfree++] = so;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
572 else
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
573 Py_TYPE(so)->tp_free(so);
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
574 Py_TRASHCAN_SAFE_END(so)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
575 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
576
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
577 static PyObject *
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
578 set_repr(PySetObject *so)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
579 {
41782
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
580 PyObject *keys, *result=NULL;
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41683
diff changeset
581 Py_UNICODE *u;
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
582 int status = Py_ReprEnter((PyObject*)so);
42482
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
583 PyObject *listrepr;
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
584 Py_ssize_t newsize;
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
585
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
586 if (status != 0) {
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
587 if (status < 0)
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
588 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
589 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
590 }
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
591
39696
d14f10a4d9d4 Fix str() and repr() of empty sets.
Georg Brandl <georg@python.org>
parents: 39693
diff changeset
592 /* shortcut for the empty set */
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
593 if (!so->used) {
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
594 Py_ReprLeave((PyObject*)so);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
595 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
596 }
39696
d14f10a4d9d4 Fix str() and repr() of empty sets.
Georg Brandl <georg@python.org>
parents: 39693
diff changeset
597
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
598 keys = PySequence_List((PyObject *)so);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
599 if (keys == NULL)
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
600 goto done;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
601
42482
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
602 listrepr = PyObject_Repr(keys);
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
603 Py_DECREF(keys);
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
604 if (listrepr == NULL) {
41782
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
605 Py_DECREF(keys);
42482
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
606 goto done;
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
607 }
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
608 newsize = PyUnicode_GET_SIZE(listrepr);
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
609 result = PyUnicode_FromUnicode(NULL, newsize);
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
610 if (result) {
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
611 u = PyUnicode_AS_UNICODE(result);
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
612 *u++ = '{';
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
613 /* Omit the brackets from the listrepr */
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
614 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
615 PyUnicode_GET_SIZE(listrepr)-2);
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
616 u += newsize-2;
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
617 *u++ = '}';
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
618 }
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
619 Py_DECREF(listrepr);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
620 if (Py_TYPE(so) != &PySet_Type) {
42482
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
621 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
622 Py_TYPE(so)->tp_name,
42482
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
623 result);
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
624 Py_DECREF(result);
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
625 result = tmp;
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
626 }
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
627 done:
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
628 Py_ReprLeave((PyObject*)so);
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
629 return result;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
630 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
631
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
632 static Py_ssize_t
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
633 set_len(PyObject *so)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
634 {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
635 return ((PySetObject *)so)->used;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
636 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
637
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
638 static int
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
639 set_merge(PySetObject *so, PyObject *otherset)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
640 {
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
641 PySetObject *other;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
642 register Py_ssize_t i;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
643 register setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
644
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
645 assert (PyAnySet_Check(so));
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
646 assert (PyAnySet_Check(otherset));
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
647
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
648 other = (PySetObject*)otherset;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
649 if (other == so || other->used == 0)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
650 /* a.update(a) or a.update({}); nothing to do */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
651 return 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
652 /* Do one big resize at the start, rather than
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
653 * incrementally resizing as we insert new keys. Expect
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
654 * that there will be no (or few) overlapping keys.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
655 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
656 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
657 if (set_table_resize(so, (so->used + other->used)*2) != 0)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
658 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
659 }
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
660 for (i = 0; i <= other->mask; i++) {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
661 entry = &other->table[i];
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
662 if (entry->key != NULL &&
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
663 entry->key != dummy) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
664 Py_INCREF(entry->key);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
665 if (set_insert_key(so, entry->key, entry->hash) == -1) {
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
666 Py_DECREF(entry->key);
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
667 return -1;
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
668 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
669 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
670 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
671 return 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
672 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
673
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
674 static int
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
675 set_contains_key(PySetObject *so, PyObject *key)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
676 {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
677 long hash;
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
678 setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
679
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
680 if (!PyUnicode_CheckExact(key) ||
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
681 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
682 hash = PyObject_Hash(key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
683 if (hash == -1)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
684 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
685 }
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
686 entry = (so->lookup)(so, key, hash);
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
687 if (entry == NULL)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
688 return -1;
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
689 key = entry->key;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
690 return key != NULL && key != dummy;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
691 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
692
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
693 static int
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
694 set_contains_entry(PySetObject *so, setentry *entry)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
695 {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
696 PyObject *key;
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
697 setentry *lu_entry;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
698
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
699 lu_entry = (so->lookup)(so, entry->key, entry->hash);
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
700 if (lu_entry == NULL)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
701 return -1;
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
702 key = lu_entry->key;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
703 return key != NULL && key != dummy;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
704 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
705
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
706 static PyObject *
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
707 set_pop(PySetObject *so)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
708 {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
709 register Py_ssize_t i = 0;
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
710 register setentry *entry;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
711 PyObject *key;
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
712
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
713 assert (PyAnySet_Check(so));
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
714 if (so->used == 0) {
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
715 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
716 return NULL;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
717 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
718
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
719 /* Set entry to "the first" unused or dummy set entry. We abuse
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
720 * the hash field of slot 0 to hold a search finger:
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
721 * If slot 0 has a value, use slot 0.
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
722 * Else slot 0 is being used to hold a search finger,
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
723 * and we use its hash value as the first index to look.
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
724 */
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
725 entry = &so->table[0];
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
726 if (entry->key == NULL || entry->key == dummy) {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
727 i = entry->hash;
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
728 /* The hash field may be a real hash value, or it may be a
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
729 * legit search finger, or it may be a once-legit search
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
730 * finger that's out of bounds now because it wrapped around
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
731 * or the table shrunk -- simply make sure it's in bounds now.
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
732 */
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
733 if (i > so->mask || i < 1)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
734 i = 1; /* skip slot 0 */
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
735 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
736 i++;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
737 if (i > so->mask)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
738 i = 1;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
739 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
740 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
741 key = entry->key;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
742 Py_INCREF(dummy);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
743 entry->key = dummy;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
744 so->used--;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
745 so->table[0].hash = i + 1; /* next place to start */
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
746 return key;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
747 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
748
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
749 PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
750 Raises KeyError if the set is empty.");
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
751
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
752 static int
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
753 set_traverse(PySetObject *so, visitproc visit, void *arg)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
754 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
755 Py_ssize_t pos = 0;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
756 setentry *entry;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
757
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
758 while (set_next(so, &pos, &entry))
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
759 Py_VISIT(entry->key);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
760 return 0;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
761 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
762
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
763 static long
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
764 frozenset_hash(PyObject *self)
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
765 {
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
766 PySetObject *so = (PySetObject *)self;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
767 long h, hash = 1927868237L;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
768 setentry *entry;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
769 Py_ssize_t pos = 0;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
770
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
771 if (so->hash != -1)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
772 return so->hash;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
773
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
774 hash *= PySet_GET_SIZE(self) + 1;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
775 while (set_next(so, &pos, &entry)) {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
776 /* Work to increase the bit dispersion for closely spaced hash
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
777 values. The is important because some use cases have many
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
778 combinations of a small number of elements with nearby
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
779 hashes so that many distinct combinations collapse to only
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
780 a handful of distinct hash values. */
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
781 h = entry->hash;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
782 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
783 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
784 hash = hash * 69069L + 907133923L;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
785 if (hash == -1)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
786 hash = 590923713L;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
787 so->hash = hash;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
788 return hash;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
789 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
790
35311
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
791 /***** Set iterator type ***********************************************/
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
792
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
793 typedef struct {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
794 PyObject_HEAD
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
795 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
796 Py_ssize_t si_used;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
797 Py_ssize_t si_pos;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
798 Py_ssize_t len;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
799 } setiterobject;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
800
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
801 static void
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
802 setiter_dealloc(setiterobject *si)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
803 {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
804 Py_XDECREF(si->si_set);
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
805 PyObject_GC_Del(si);
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
806 }
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
807
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
808 static int
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
809 setiter_traverse(setiterobject *si, visitproc visit, void *arg)
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
810 {
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
811 Py_VISIT(si->si_set);
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
812 return 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
813 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
814
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
815 static PyObject *
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
816 setiter_len(setiterobject *si)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
817 {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
818 Py_ssize_t len = 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
819 if (si->si_set != NULL && si->si_used == si->si_set->used)
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
820 len = si->len;
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44061
diff changeset
821 return PyLong_FromLong(len);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
822 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
823
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36244
diff changeset
824 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
825
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
826 static PyMethodDef setiter_methods[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36244
diff changeset
827 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
828 {NULL, NULL} /* sentinel */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
829 };
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
830
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
831 static PyObject *setiter_iternext(setiterobject *si)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
832 {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
833 PyObject *key;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
834 register Py_ssize_t i, mask;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
835 register setentry *entry;
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
836 PySetObject *so = si->si_set;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
837
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
838 if (so == NULL)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
839 return NULL;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
840 assert (PyAnySet_Check(so));
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
841
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
842 if (si->si_used != so->used) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
843 PyErr_SetString(PyExc_RuntimeError,
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
844 "Set changed size during iteration");
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
845 si->si_used = -1; /* Make this state sticky */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
846 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
847 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
848
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
849 i = si->si_pos;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
850 assert(i>=0);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
851 entry = so->table;
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
852 mask = so->mask;
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
853 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
854 i++;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
855 si->si_pos = i+1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
856 if (i > mask)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
857 goto fail;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
858 si->len--;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
859 key = entry[i].key;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
860 Py_INCREF(key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
861 return key;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
862
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
863 fail:
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
864 Py_DECREF(so);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
865 si->si_set = NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
866 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
867 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
868
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
869 PyTypeObject PySetIter_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41782
diff changeset
870 PyVarObject_HEAD_INIT(&PyType_Type, 0)
44043
1f024a95e9d9 Added view and iterator types to collections / _abcoll
Christian Heimes <christian@cheimes.de>
parents: 43961
diff changeset
871 "set_iterator", /* tp_name */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
872 sizeof(setiterobject), /* tp_basicsize */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
873 0, /* tp_itemsize */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
874 /* methods */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
875 (destructor)setiter_dealloc, /* tp_dealloc */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
876 0, /* tp_print */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
877 0, /* tp_getattr */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
878 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51244
diff changeset
879 0, /* tp_reserved */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
880 0, /* tp_repr */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
881 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
882 0, /* tp_as_sequence */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
883 0, /* tp_as_mapping */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
884 0, /* tp_hash */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
885 0, /* tp_call */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
886 0, /* tp_str */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
887 PyObject_GenericGetAttr, /* tp_getattro */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
888 0, /* tp_setattro */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
889 0, /* tp_as_buffer */
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
890 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
891 0, /* tp_doc */
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
892 (traverseproc)setiter_traverse, /* tp_traverse */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
893 0, /* tp_clear */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
894 0, /* tp_richcompare */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
895 0, /* tp_weaklistoffset */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
896 PyObject_SelfIter, /* tp_iter */
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
897 (iternextfunc)setiter_iternext, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
898 setiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
899 0,
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
900 };
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
901
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
902 static PyObject *
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
903 set_iter(PySetObject *so)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
904 {
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
905 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
906 if (si == NULL)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
907 return NULL;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
908 Py_INCREF(so);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
909 si->si_set = so;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
910 si->si_used = so->used;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
911 si->si_pos = 0;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
912 si->len = so->used;
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49909
diff changeset
913 _PyObject_GC_TRACK(si);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
914 return (PyObject *)si;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
915 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
916
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
917 static int
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
918 set_update_internal(PySetObject *so, PyObject *other)
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
919 {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
920 PyObject *key, *it;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
921
44999
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
922 if (PyAnySet_Check(other))
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
923 return set_merge(so, other);
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
924
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40643
diff changeset
925 if (PyDict_CheckExact(other)) {
36244
e7880f4f53a4 Remove some shadowed variables
Neal Norwitz <nnorwitz@gmail.com>
parents: 35862
diff changeset
926 PyObject *value;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
927 Py_ssize_t pos = 0;
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
928 long hash;
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
929 Py_ssize_t dictsize = PyDict_Size(other);
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
930
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
931 /* Do one big resize at the start, rather than
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
932 * incrementally resizing as we insert new keys. Expect
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
933 * that there will be no (or few) overlapping keys.
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
934 */
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
935 if (dictsize == -1)
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
936 return -1;
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
937 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
938 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
939 return -1;
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
940 }
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
941 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
942 setentry an_entry;
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
943
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
944 an_entry.hash = hash;
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
945 an_entry.key = key;
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
946 if (set_add_entry(so, &an_entry) == -1)
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
947 return -1;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
948 }
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
949 return 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
950 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
951
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
952 it = PyObject_GetIter(other);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
953 if (it == NULL)
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
954 return -1;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
955
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
956 while ((key = PyIter_Next(it)) != NULL) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
957 if (set_add_key(so, key) == -1) {
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
958 Py_DECREF(it);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
959 Py_DECREF(key);
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
960 return -1;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
961 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
962 Py_DECREF(key);
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
963 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
964 Py_DECREF(it);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
965 if (PyErr_Occurred())
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
966 return -1;
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
967 return 0;
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
968 }
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
969
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
970 static PyObject *
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
971 set_update(PySetObject *so, PyObject *args)
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
972 {
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
973 Py_ssize_t i;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
974
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
975 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
976 PyObject *other = PyTuple_GET_ITEM(args, i);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
977 if (set_update_internal(so, other) == -1)
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
978 return NULL;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
979 }
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
980 Py_RETURN_NONE;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
981 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
982
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
983 PyDoc_STRVAR(update_doc,
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
984 "Update a set with the union of itself and others.");
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
985
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
986 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
987 make_new_set(PyTypeObject *type, PyObject *iterable)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
988 {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
989 register PySetObject *so = NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
990
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
991 if (dummy == NULL) { /* Auto-initialize dummy */
42750
ce52cd71a8d9 Convert a bunch of constant strings in C to unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42482
diff changeset
992 dummy = PyUnicode_FromString("<dummy key>");
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
993 if (dummy == NULL)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
994 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
995 }
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
996
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997 /* create PySetObject structure */
45192
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
998 if (numfree &&
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
999 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
45192
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1000 so = free_list[--numfree];
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1001 assert (so != NULL && PyAnySet_CheckExact(so));
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1002 Py_TYPE(so) = type;
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1003 _Py_NewReference((PyObject *)so);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1004 EMPTY_TO_MINSIZE(so);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1005 PyObject_GC_Track(so);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1006 } else {
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1007 so = (PySetObject *)type->tp_alloc(type, 0);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1008 if (so == NULL)
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1009 return NULL;
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1010 /* tp_alloc has already zeroed the structure */
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1011 assert(so->table == NULL && so->fill == 0 && so->used == 0);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1012 INIT_NONZERO_SET_SLOTS(so);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1013 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1014
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1015 so->lookup = set_lookkey_unicode;
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
1016 so->weakreflist = NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1017
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
1018 if (iterable != NULL) {
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1019 if (set_update_internal(so, iterable) == -1) {
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
1020 Py_DECREF(so);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
1021 return NULL;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
1022 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
1023 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
1024
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1028 static PyObject *
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1029 make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1030 {
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1031 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1032 if (PyType_IsSubtype(type, &PySet_Type))
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1033 type = &PySet_Type;
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1034 else
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1035 type = &PyFrozenSet_Type;
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1036 }
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1037 return make_new_set(type, iterable);
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1038 }
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1039
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1040 /* The empty frozenset is a singleton */
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1041 static PyObject *emptyfrozenset = NULL;
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1042
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1043 static PyObject *
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1044 frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045 {
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1046 PyObject *iterable = NULL, *result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1047
40643
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
1048 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 35398
diff changeset
1049 return NULL;
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 35398
diff changeset
1050
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1052 return NULL;
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1053
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1054 if (type != &PyFrozenSet_Type)
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1055 return make_new_set(type, iterable);
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1056
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1057 if (iterable != NULL) {
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1058 /* frozenset(f) is idempotent */
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1059 if (PyFrozenSet_CheckExact(iterable)) {
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1060 Py_INCREF(iterable);
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1061 return iterable;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1062 }
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1063 result = make_new_set(type, iterable);
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1064 if (result == NULL || PySet_GET_SIZE(result))
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1065 return result;
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1066 Py_DECREF(result);
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1067 }
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1068 /* The empty frozenset is a singleton */
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1069 if (emptyfrozenset == NULL)
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1070 emptyfrozenset = make_new_set(type, NULL);
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1071 Py_XINCREF(emptyfrozenset);
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1072 return emptyfrozenset;
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1073 }
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1074
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1075 void
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1076 PySet_Fini(void)
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1077 {
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1078 PySetObject *so;
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1079
45192
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1080 while (numfree) {
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1081 numfree--;
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45145
diff changeset
1082 so = free_list[numfree];
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1083 PyObject_GC_Del(so);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1084 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1085 Py_CLEAR(dummy);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1086 Py_CLEAR(emptyfrozenset);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1087 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1088
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1089 static PyObject *
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1090 set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1091 {
40643
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40582
diff changeset
1092 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 35398
diff changeset
1093 return NULL;
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 35398
diff changeset
1094
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1095 return make_new_set(type, NULL);
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1096 }
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1097
35293
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1098 /* set_swap_bodies() switches the contents of any two sets by moving their
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1099 internal data pointers and, if needed, copying the internal smalltables.
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1100 Semantically equivalent to:
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1101
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1102 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1103
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1104 The function always succeeds and it leaves both objects in a stable state.
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1105 Useful for creating temporary frozensets from sets for membership testing
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1106 in __contains__(), discard(), and remove(). Also useful for operations
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1107 that update in-place (by allowing an intermediate result to be swapped
35294
3f1b48f9ff0d Fix frozenset() ref count and a comment typo.
Raymond Hettinger <python@rcn.com>
parents: 35293
diff changeset
1108 into one of the original inputs).
35293
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1109 */
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1110
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1111 static void
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1112 set_swap_bodies(PySetObject *a, PySetObject *b)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1113 {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
1114 Py_ssize_t t;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1115 setentry *u;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1116 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1117 setentry tab[PySet_MINSIZE];
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1118 long h;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1119
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1120 t = a->fill; a->fill = b->fill; b->fill = t;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1121 t = a->used; a->used = b->used; b->used = t;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1122 t = a->mask; a->mask = b->mask; b->mask = t;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1123
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1124 u = a->table;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1125 if (a->table == a->smalltable)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1126 u = b->smalltable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1127 a->table = b->table;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1128 if (b->table == b->smalltable)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1129 a->table = a->smalltable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1130 b->table = u;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1131
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1132 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1133
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1134 if (a->table == a->smalltable || b->table == b->smalltable) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1135 memcpy(tab, a->smalltable, sizeof(tab));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1136 memcpy(a->smalltable, b->smalltable, sizeof(tab));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1137 memcpy(b->smalltable, tab, sizeof(tab));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1138 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1139
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1140 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1141 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1142 h = a->hash; a->hash = b->hash; b->hash = h;
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1143 } else {
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1144 a->hash = -1;
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1145 b->hash = -1;
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1146 }
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1147 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1148
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
1149 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1150 set_copy(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1151 {
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1152 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1153 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1154
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1155 static PyObject *
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1156 frozenset_copy(PySetObject *so)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1157 {
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1158 if (PyFrozenSet_CheckExact(so)) {
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1159 Py_INCREF(so);
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1160 return (PyObject *)so;
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1161 }
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1162 return set_copy(so);
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1163 }
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1164
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1165 PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1166
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1167 static PyObject *
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1168 set_clear(PySetObject *so)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1169 {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1170 set_clear_internal(so);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1171 Py_RETURN_NONE;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1172 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1173
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1174 PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1175
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1176 static PyObject *
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1177 set_union(PySetObject *so, PyObject *args)
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1178 {
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1179 PySetObject *result;
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1180 PyObject *other;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1181 Py_ssize_t i;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1182
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1183 result = (PySetObject *)set_copy(so);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1184 if (result == NULL)
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1185 return NULL;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1186
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1187 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1188 other = PyTuple_GET_ITEM(args, i);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1189 if ((PyObject *)so == other)
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1190 return (PyObject *)result;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1191 if (set_update_internal(result, other) == -1) {
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1192 Py_DECREF(result);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1193 return NULL;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1194 }
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1195 }
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1196 return (PyObject *)result;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1197 }
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1198
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1199 PyDoc_STRVAR(union_doc,
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1200 "Return the union of sets as a new set.\n\
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1201 \n\
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1202 (i.e. all elements that are in either set.)");
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1203
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1204 static PyObject *
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1205 set_or(PySetObject *so, PyObject *other)
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1206 {
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1207 PySetObject *result;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1208
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1209 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1210 Py_INCREF(Py_NotImplemented);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1211 return Py_NotImplemented;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1212 }
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1213
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1214 result = (PySetObject *)set_copy(so);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1215 if (result == NULL)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1216 return NULL;
35353
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
1217 if ((PyObject *)so == other)
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
1218 return (PyObject *)result;
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1219 if (set_update_internal(result, other) == -1) {
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1220 Py_DECREF(result);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1221 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1222 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1223 return (PyObject *)result;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1224 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1225
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1226 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1227 set_ior(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1228 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1229 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1230 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1231 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1232 }
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1233 if (set_update_internal(so, other) == -1)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1234 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1235 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1236 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1237 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1238
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1239 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1240 set_intersection(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1241 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1242 PySetObject *result;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1243 PyObject *key, *it, *tmp;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1244
35353
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
1245 if ((PyObject *)so == other)
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
1246 return set_copy(so);
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1247
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1248 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1249 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1250 return NULL;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1251
44999
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
1252 if (PyAnySet_Check(other)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1253 Py_ssize_t pos = 0;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1254 setentry *entry;
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1255
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1256 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1257 tmp = (PyObject *)so;
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1258 so = (PySetObject *)other;
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1259 other = tmp;
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1260 }
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1261
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1262 while (set_next((PySetObject *)other, &pos, &entry)) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1263 int rv = set_contains_entry(so, entry);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1264 if (rv == -1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1265 Py_DECREF(result);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1266 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1267 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1268 if (rv) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1269 if (set_add_entry(result, entry) == -1) {
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1270 Py_DECREF(result);
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1271 return NULL;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1272 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1273 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1274 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1275 return (PyObject *)result;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1276 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1277
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1278 it = PyObject_GetIter(other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1279 if (it == NULL) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1280 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1281 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1282 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1283
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1284 while ((key = PyIter_Next(it)) != NULL) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1285 int rv;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1286 setentry entry;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1287 long hash = PyObject_Hash(key);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1288
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1289 if (hash == -1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1290 Py_DECREF(it);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1291 Py_DECREF(result);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1292 Py_DECREF(key);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1293 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1294 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1295 entry.hash = hash;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1296 entry.key = key;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1297 rv = set_contains_entry(so, &entry);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1298 if (rv == -1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1299 Py_DECREF(it);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1300 Py_DECREF(result);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1301 Py_DECREF(key);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1302 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1303 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1304 if (rv) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1305 if (set_add_entry(result, &entry) == -1) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1306 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1307 Py_DECREF(result);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1308 Py_DECREF(key);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1309 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1310 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1311 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1312 Py_DECREF(key);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1313 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1314 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1315 if (PyErr_Occurred()) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1316 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1317 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1318 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1319 return (PyObject *)result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1320 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1321
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1322 static PyObject *
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1323 set_intersection_multi(PySetObject *so, PyObject *args)
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1324 {
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1325 Py_ssize_t i;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1326 PyObject *result = (PyObject *)so;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1327
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1328 if (PyTuple_GET_SIZE(args) == 0)
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1329 return set_copy(so);
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1330
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1331 Py_INCREF(so);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1332 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1333 PyObject *other = PyTuple_GET_ITEM(args, i);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1334 PyObject *newresult = set_intersection((PySetObject *)result, other);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1335 if (newresult == NULL) {
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1336 Py_DECREF(result);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1337 return NULL;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1338 }
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1339 Py_DECREF(result);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1340 result = newresult;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1341 }
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1342 return result;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1343 }
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1344
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1345 PyDoc_STRVAR(intersection_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1346 "Return the intersection of two sets as a new set.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1347 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1348 (i.e. all elements that are in both sets.)");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1349
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1350 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1351 set_intersection_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1352 {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1353 PyObject *tmp;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1354
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1355 tmp = set_intersection(so, other);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1356 if (tmp == NULL)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1357 return NULL;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1358 set_swap_bodies(so, (PySetObject *)tmp);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1359 Py_DECREF(tmp);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1360 Py_RETURN_NONE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1361 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1362
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1363 static PyObject *
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1364 set_intersection_update_multi(PySetObject *so, PyObject *args)
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1365 {
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1366 PyObject *tmp;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1367
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1368 tmp = set_intersection_multi(so, args);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1369 if (tmp == NULL)
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1370 return NULL;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1371 set_swap_bodies(so, (PySetObject *)tmp);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1372 Py_DECREF(tmp);
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1373 Py_RETURN_NONE;
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1374 }
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
1375
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1376 PyDoc_STRVAR(intersection_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1377 "Update a set with the intersection of itself and another.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1378
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1379 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1380 set_and(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1381 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1382 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1383 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1384 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1385 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1386 return set_intersection(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1387 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1388
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1389 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1390 set_iand(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1391 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1392 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1393
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1394 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1395 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1396 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1397 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1398 result = set_intersection_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1399 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1400 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1401 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1402 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1403 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1404 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1405
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1406 static PyObject *
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1407 set_isdisjoint(PySetObject *so, PyObject *other)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1408 {
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1409 PyObject *key, *it, *tmp;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1410
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1411 if ((PyObject *)so == other) {
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1412 if (PySet_GET_SIZE(so) == 0)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1413 Py_RETURN_TRUE;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1414 else
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1415 Py_RETURN_FALSE;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1416 }
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1417
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1418 if (PyAnySet_CheckExact(other)) {
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1419 Py_ssize_t pos = 0;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1420 setentry *entry;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1421
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1422 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1423 tmp = (PyObject *)so;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1424 so = (PySetObject *)other;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1425 other = tmp;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1426 }
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1427 while (set_next((PySetObject *)other, &pos, &entry)) {
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1428 int rv = set_contains_entry(so, entry);
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1429 if (rv == -1)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1430 return NULL;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1431 if (rv)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1432 Py_RETURN_FALSE;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1433 }
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1434 Py_RETURN_TRUE;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1435 }
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1436
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1437 it = PyObject_GetIter(other);
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1438 if (it == NULL)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1439 return NULL;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1440
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1441 while ((key = PyIter_Next(it)) != NULL) {
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1442 int rv;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1443 setentry entry;
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1444 long hash = PyObject_Hash(key);;
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1445
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1446 if (hash == -1) {
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1447 Py_DECREF(key);
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1448 Py_DECREF(it);
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1449 return NULL;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1450 }
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1451 entry.hash = hash;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1452 entry.key = key;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1453 rv = set_contains_entry(so, &entry);
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1454 Py_DECREF(key);
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1455 if (rv == -1) {
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1456 Py_DECREF(it);
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1457 return NULL;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1458 }
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1459 if (rv) {
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1460 Py_DECREF(it);
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1461 Py_RETURN_FALSE;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1462 }
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1463 }
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1464 Py_DECREF(it);
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1465 if (PyErr_Occurred())
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1466 return NULL;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1467 Py_RETURN_TRUE;
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1468 }
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1469
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1470 PyDoc_STRVAR(isdisjoint_doc,
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1471 "Return True if two sets have a null intersection.");
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
1472
35862
6867995a18a7 Prevent name pollution by making lots of internal functions static.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35636
diff changeset
1473 static int
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1474 set_difference_update_internal(PySetObject *so, PyObject *other)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1475 {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1476 if ((PyObject *)so == other)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1477 return set_clear_internal(so);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1478
44999
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
1479 if (PyAnySet_Check(other)) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1480 setentry *entry;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1481 Py_ssize_t pos = 0;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1482
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1483 while (set_next((PySetObject *)other, &pos, &entry))
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1484 if (set_discard_entry(so, entry) == -1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1485 return -1;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1486 } else {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1487 PyObject *key, *it;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1488 it = PyObject_GetIter(other);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1489 if (it == NULL)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1490 return -1;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1491
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1492 while ((key = PyIter_Next(it)) != NULL) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1493 if (set_discard_key(so, key) == -1) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1494 Py_DECREF(it);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1495 Py_DECREF(key);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1496 return -1;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1497 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1498 Py_DECREF(key);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1499 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1500 Py_DECREF(it);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1501 if (PyErr_Occurred())
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1502 return -1;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1503 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1504 /* If more than 1/5 are dummies, then resize them away. */
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1505 if ((so->fill - so->used) * 5 < so->mask)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1506 return 0;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1507 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1508 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1509
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1510 static PyObject *
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1511 set_difference_update(PySetObject *so, PyObject *args)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1512 {
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1513 Py_ssize_t i;
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1514
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1515 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1516 PyObject *other = PyTuple_GET_ITEM(args, i);
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1517 if (set_difference_update_internal(so, other) == -1)
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1518 return NULL;
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1519 }
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1520 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1521 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1522
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1523 PyDoc_STRVAR(difference_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1524 "Remove all elements of another set from this set.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1525
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1526 static PyObject *
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1527 set_difference(PySetObject *so, PyObject *other)
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1528 {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1529 PyObject *result;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1530 setentry *entry;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1531 Py_ssize_t pos = 0;
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1532
44999
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
1533 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1534 result = set_copy(so);
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1535 if (result == NULL)
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1536 return NULL;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1537 if (set_difference_update_internal((PySetObject *)result, other) != -1)
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1538 return result;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1539 Py_DECREF(result);
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1540 return NULL;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1541 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1542
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1543 result = make_new_set_basetype(Py_TYPE(so), NULL);
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1544 if (result == NULL)
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1545 return NULL;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1546
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40643
diff changeset
1547 if (PyDict_CheckExact(other)) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1548 while (set_next(so, &pos, &entry)) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1549 setentry entrycopy;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1550 entrycopy.hash = entry->hash;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1551 entrycopy.key = entry->key;
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
1552 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1553 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1554 Py_DECREF(result);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1555 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1556 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1557 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1558 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1559 return result;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1560 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1561
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1562 while (set_next(so, &pos, &entry)) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1563 int rv = set_contains_entry((PySetObject *)other, entry);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1564 if (rv == -1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1565 Py_DECREF(result);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1566 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1567 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1568 if (!rv) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1569 if (set_add_entry((PySetObject *)result, entry) == -1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1570 Py_DECREF(result);
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1571 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1572 }
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1573 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1574 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1575 return result;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1576 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1577
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1578 static PyObject *
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1579 set_difference_multi(PySetObject *so, PyObject *args)
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1580 {
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1581 Py_ssize_t i;
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1582 PyObject *result, *other;
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1583
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1584 if (PyTuple_GET_SIZE(args) == 0)
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1585 return set_copy(so);
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1586
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1587 other = PyTuple_GET_ITEM(args, 0);
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1588 result = set_difference(so, other);
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1589 if (result == NULL)
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1590 return NULL;
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1591
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1592 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1593 other = PyTuple_GET_ITEM(args, i);
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1594 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1595 Py_DECREF(result);
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1596 return NULL;
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1597 }
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1598 }
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1599 return result;
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1600 }
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1601
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1602 PyDoc_STRVAR(difference_doc,
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1603 "Return the difference of two or more sets as a new set.\n\
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1604 \n\
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1605 (i.e. all elements that are in this set but not the others.)");
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1606 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1607 set_sub(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1608 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1609 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1610 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1611 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1612 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1613 return set_difference(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1614 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1615
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1616 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1617 set_isub(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1618 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1619 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1620 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1621 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1622 }
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
1623 if (set_difference_update_internal(so, other) == -1)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1624 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1625 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1626 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1627 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1628
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1629 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1630 set_symmetric_difference_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1631 {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1632 PySetObject *otherset;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1633 PyObject *key;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1634 Py_ssize_t pos = 0;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1635 setentry *entry;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1636
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1637 if ((PyObject *)so == other)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1638 return set_clear(so);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1639
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40643
diff changeset
1640 if (PyDict_CheckExact(other)) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1641 PyObject *value;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1642 int rv;
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
1643 long hash;
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40742
diff changeset
1644 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1645 setentry an_entry;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1646
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1647 an_entry.hash = hash;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1648 an_entry.key = key;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1649 rv = set_discard_entry(so, &an_entry);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1650 if (rv == -1)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1651 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1652 if (rv == DISCARD_NOTFOUND) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1653 if (set_add_entry(so, &an_entry) == -1)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1654 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1655 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1656 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1657 Py_RETURN_NONE;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1658 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1659
44999
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
1660 if (PyAnySet_Check(other)) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1661 Py_INCREF(other);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1662 otherset = (PySetObject *)other;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1663 } else {
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1664 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1665 if (otherset == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1666 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1667 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1668
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1669 while (set_next(otherset, &pos, &entry)) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1670 int rv = set_discard_entry(so, entry);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1671 if (rv == -1) {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
1672 Py_DECREF(otherset);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1673 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1674 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1675 if (rv == DISCARD_NOTFOUND) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1676 if (set_add_entry(so, entry) == -1) {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
1677 Py_DECREF(otherset);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1678 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1679 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1680 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1681 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1682 Py_DECREF(otherset);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1683 Py_RETURN_NONE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1684 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1685
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1686 PyDoc_STRVAR(symmetric_difference_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1687 "Update a set with the symmetric difference of itself and another.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1688
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1689 static PyObject *
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1690 set_symmetric_difference(PySetObject *so, PyObject *other)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1691 {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1692 PyObject *rv;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1693 PySetObject *otherset;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1694
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49493
diff changeset
1695 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1696 if (otherset == NULL)
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1697 return NULL;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1698 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1699 if (rv == NULL)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1700 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1701 Py_DECREF(rv);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1702 return (PyObject *)otherset;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1703 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1704
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1705 PyDoc_STRVAR(symmetric_difference_doc,
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1706 "Return the symmetric difference of two sets as a new set.\n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1707 \n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1708 (i.e. all elements that are in exactly one of the sets.)");
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1709
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1710 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1711 set_xor(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1712 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1713 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1714 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1715 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1716 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1717 return set_symmetric_difference(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1718 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1719
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1720 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1721 set_ixor(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1722 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1723 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1724
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1725 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1726 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1727 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1728 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1729 result = set_symmetric_difference_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1730 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1731 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1732 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1733 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1734 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1735 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1736
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1737 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1738 set_issubset(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1739 {
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1740 setentry *entry;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1741 Py_ssize_t pos = 0;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1742
44999
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
1743 if (!PyAnySet_Check(other)) {
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1744 PyObject *tmp, *result;
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1745 tmp = make_new_set(&PySet_Type, other);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1746 if (tmp == NULL)
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1747 return NULL;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1748 result = set_issubset(so, tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1749 Py_DECREF(tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1750 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1751 }
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1752 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1753 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1754
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1755 while (set_next(so, &pos, &entry)) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1756 int rv = set_contains_entry((PySetObject *)other, entry);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1757 if (rv == -1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1758 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1759 if (!rv)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1760 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1761 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1762 Py_RETURN_TRUE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1763 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1764
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1765 PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1766
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1767 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1768 set_issuperset(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1769 {
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1770 PyObject *tmp, *result;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1771
44999
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
1772 if (!PyAnySet_Check(other)) {
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1773 tmp = make_new_set(&PySet_Type, other);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1774 if (tmp == NULL)
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1775 return NULL;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1776 result = set_issuperset(so, tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1777 Py_DECREF(tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1778 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1779 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1780 return set_issubset((PySetObject *)other, (PyObject *)so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1781 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1782
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1783 PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1784
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1785 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1786 set_richcompare(PySetObject *v, PyObject *w, int op)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1787 {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1788 PyObject *r1, *r2;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1789
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1790 if(!PyAnySet_Check(w)) {
42756
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
1791 Py_INCREF(Py_NotImplemented);
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
1792 return Py_NotImplemented;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1793 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1794 switch (op) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1795 case Py_EQ:
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1796 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1797 Py_RETURN_FALSE;
35398
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1798 if (v->hash != -1 &&
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1799 ((PySetObject *)w)->hash != -1 &&
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1800 v->hash != ((PySetObject *)w)->hash)
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1801 Py_RETURN_FALSE;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1802 return set_issubset(v, w);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1803 case Py_NE:
35398
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1804 r1 = set_richcompare(v, w, Py_EQ);
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1805 if (r1 == NULL)
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1806 return NULL;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1807 r2 = PyBool_FromLong(PyObject_Not(r1));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1808 Py_DECREF(r1);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1809 return r2;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1810 case Py_LE:
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1811 return set_issubset(v, w);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1812 case Py_GE:
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1813 return set_issuperset(v, w);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1814 case Py_LT:
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1815 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1816 Py_RETURN_FALSE;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1817 return set_issubset(v, w);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1818 case Py_GT:
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1819 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1820 Py_RETURN_FALSE;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1821 return set_issuperset(v, w);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1822 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1823 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1824 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1825 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1826
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1827 static PyObject *
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1828 set_add(PySetObject *so, PyObject *key)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1829 {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1830 if (set_add_key(so, key) == -1)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1831 return NULL;
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
1832 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1833 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1834
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1835 PyDoc_STRVAR(add_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1836 "Add an element to a set.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1837 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1838 This has no effect if the element is already present.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1839
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1840 static int
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1841 set_contains(PySetObject *so, PyObject *key)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1842 {
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1843 PyObject *tmpkey;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1844 int rv;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1845
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1846 rv = set_contains_key(so, key);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1847 if (rv == -1) {
46742
981caf24dd8a Forward port r62848.
Raymond Hettinger <python@rcn.com>
parents: 45192
diff changeset
1848 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1849 return -1;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1850 PyErr_Clear();
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1851 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1852 if (tmpkey == NULL)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1853 return -1;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1854 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1855 rv = set_contains(so, tmpkey);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1856 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1857 Py_DECREF(tmpkey);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1858 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1859 return rv;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1860 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1861
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1862 static PyObject *
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1863 set_direct_contains(PySetObject *so, PyObject *key)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1864 {
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1865 long result;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1866
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1867 result = set_contains(so, key);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1868 if (result == -1)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1869 return NULL;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1870 return PyBool_FromLong(result);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1871 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1872
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1873 PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1874
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1875 static PyObject *
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1876 set_remove(PySetObject *so, PyObject *key)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1877 {
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
1878 PyObject *tmpkey;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1879 int rv;
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
1880
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1881 rv = set_discard_key(so, key);
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1882 if (rv == -1) {
46742
981caf24dd8a Forward port r62848.
Raymond Hettinger <python@rcn.com>
parents: 45192
diff changeset
1883 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1884 return NULL;
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1885 PyErr_Clear();
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1886 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1887 if (tmpkey == NULL)
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
1888 return NULL;
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1889 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
1890 rv = set_discard_key(so, tmpkey);
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1891 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1892 Py_DECREF(tmpkey);
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
1893 if (rv == -1)
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
1894 return NULL;
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
1895 }
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
1896
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
1897 if (rv == DISCARD_NOTFOUND) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1898 set_key_error(key);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1899 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1900 }
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
1901 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1902 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1903
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1904 PyDoc_STRVAR(remove_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1905 "Remove an element from a set; it must be a member.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1906 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1907 If the element is not a member, raise a KeyError.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1908
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1909 static PyObject *
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1910 set_discard(PySetObject *so, PyObject *key)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1911 {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1912 PyObject *tmpkey, *result;
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1913 int rv;
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1914
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1915 rv = set_discard_key(so, key);
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1916 if (rv == -1) {
46742
981caf24dd8a Forward port r62848.
Raymond Hettinger <python@rcn.com>
parents: 45192
diff changeset
1917 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1918 return NULL;
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1919 PyErr_Clear();
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1920 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1921 if (tmpkey == NULL)
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1922 return NULL;
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1923 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1924 result = set_discard(so, tmpkey);
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1925 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1926 Py_DECREF(tmpkey);
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1927 return result;
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1928 }
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
1929 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1930 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1931
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1932 PyDoc_STRVAR(discard_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1933 "Remove an element from a set if it is a member.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1934 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1935 If the element is not a member, do nothing.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1936
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1937 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1938 set_reduce(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1939 {
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1940 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1941
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1942 keys = PySequence_List((PyObject *)so);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1943 if (keys == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1944 goto done;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1945 args = PyTuple_Pack(1, keys);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1946 if (args == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1947 goto done;
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1948 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1949 if (dict == NULL) {
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1950 PyErr_Clear();
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1951 dict = Py_None;
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1952 Py_INCREF(dict);
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1953 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1954 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1955 done:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1956 Py_XDECREF(args);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1957 Py_XDECREF(keys);
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1958 Py_XDECREF(dict);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1959 return result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1960 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1961
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1962 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1963
48467
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1964 static PyObject *
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1965 set_sizeof(PySetObject *so)
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1966 {
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1967 Py_ssize_t res;
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1968
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1969 res = sizeof(PySetObject);
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1970 if (so->table != so->smalltable)
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1971 res = res + (so->mask + 1) * sizeof(setentry);
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1972 return PyLong_FromSsize_t(res);
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1973 }
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1974
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
1975 PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1976 static int
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1977 set_init(PySetObject *self, PyObject *args, PyObject *kwds)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1978 {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1979 PyObject *iterable = NULL;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1980
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1981 if (!PyAnySet_Check(self))
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1982 return -1;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1983 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1984 return -1;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1985 set_clear_internal(self);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1986 self->hash = -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1987 if (iterable == NULL)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1988 return 0;
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1989 return set_update_internal(self, iterable);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1990 }
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1991
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1992 static PySequenceMethods set_as_sequence = {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1993 set_len, /* sq_length */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1994 0, /* sq_concat */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1995 0, /* sq_repeat */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1996 0, /* sq_item */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1997 0, /* sq_slice */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1998 0, /* sq_ass_item */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1999 0, /* sq_ass_slice */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2000 (objobjproc)set_contains, /* sq_contains */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2001 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2002
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2003 /* set object ********************************************************/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2004
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2005 #ifdef Py_DEBUG
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2006 static PyObject *test_c_api(PySetObject *so);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2007
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2008 PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2009 All is well if assertions don't fail.");
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2010 #endif
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2011
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2012 static PyMethodDef set_methods[] = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2013 {"add", (PyCFunction)set_add, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2014 add_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2015 {"clear", (PyCFunction)set_clear, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2016 clear_doc},
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
2017 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2018 contains_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2019 {"copy", (PyCFunction)set_copy, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2020 copy_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2021 {"discard", (PyCFunction)set_discard, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2022 discard_doc},
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
2023 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2024 difference_doc},
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
2025 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2026 difference_update_doc},
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
2027 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2028 intersection_doc},
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
2029 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2030 intersection_update_doc},
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
2031 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
2032 isdisjoint_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2033 {"issubset", (PyCFunction)set_issubset, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2034 issubset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2035 {"issuperset", (PyCFunction)set_issuperset, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2036 issuperset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2037 {"pop", (PyCFunction)set_pop, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2038 pop_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2039 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2040 reduce_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2041 {"remove", (PyCFunction)set_remove, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2042 remove_doc},
48467
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
2043 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
2044 sizeof_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2045 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2046 symmetric_difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2047 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2048 symmetric_difference_update_doc},
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2049 #ifdef Py_DEBUG
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2050 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2051 test_c_api_doc},
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2052 #endif
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
2053 {"union", (PyCFunction)set_union, METH_VARARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2054 union_doc},
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
2055 {"update", (PyCFunction)set_update, METH_VARARGS,
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
2056 update_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2057 {NULL, NULL} /* sentinel */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2058 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2059
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2060 static PyNumberMethods set_as_number = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2061 0, /*nb_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2062 (binaryfunc)set_sub, /*nb_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2063 0, /*nb_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2064 0, /*nb_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2065 0, /*nb_divmod*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2066 0, /*nb_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2067 0, /*nb_negative*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2068 0, /*nb_positive*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2069 0, /*nb_absolute*/
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 39696
diff changeset
2070 0, /*nb_bool*/
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2071 0, /*nb_invert*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2072 0, /*nb_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2073 0, /*nb_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2074 (binaryfunc)set_and, /*nb_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2075 (binaryfunc)set_xor, /*nb_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2076 (binaryfunc)set_or, /*nb_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2077 0, /*nb_int*/
50829
af02b6e42142 Merged revisions 68650-68653 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50553
diff changeset
2078 0, /*nb_reserved*/
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2079 0, /*nb_float*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2080 0, /*nb_inplace_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2081 (binaryfunc)set_isub, /*nb_inplace_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2082 0, /*nb_inplace_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2083 0, /*nb_inplace_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2084 0, /*nb_inplace_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2085 0, /*nb_inplace_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2086 0, /*nb_inplace_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2087 (binaryfunc)set_iand, /*nb_inplace_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2088 (binaryfunc)set_ixor, /*nb_inplace_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2089 (binaryfunc)set_ior, /*nb_inplace_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2090 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2091
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2092 PyDoc_STRVAR(set_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2093 "set(iterable) --> set object\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2094 \n\
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
2095 Build an unordered collection of unique elements.");
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2096
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2097 PyTypeObject PySet_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41782
diff changeset
2098 PyVarObject_HEAD_INIT(&PyType_Type, 0)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2099 "set", /* tp_name */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2100 sizeof(PySetObject), /* tp_basicsize */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2101 0, /* tp_itemsize */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2102 /* methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2103 (destructor)set_dealloc, /* tp_dealloc */
42477
e7fd327e0c19 Kill all uses and definitions of tp_print under Objects/. (Others will follow.)
Guido van Rossum <guido@python.org>
parents: 42335
diff changeset
2104 0, /* tp_print */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2105 0, /* tp_getattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2106 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51244
diff changeset
2107 0, /* tp_reserved */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2108 (reprfunc)set_repr, /* tp_repr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2109 &set_as_number, /* tp_as_number */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2110 &set_as_sequence, /* tp_as_sequence */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2111 0, /* tp_as_mapping */
48475
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 48467
diff changeset
2112 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2113 0, /* tp_call */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2114 0, /* tp_str */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2115 PyObject_GenericGetAttr, /* tp_getattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2116 0, /* tp_setattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2117 0, /* tp_as_buffer */
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
2118 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
2119 Py_TPFLAGS_BASETYPE, /* tp_flags */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2120 set_doc, /* tp_doc */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
2121 (traverseproc)set_traverse, /* tp_traverse */
35315
a6cbfcd0e691 Factor away a redundant clear() function.
Raymond Hettinger <python@rcn.com>
parents: 35312
diff changeset
2122 (inquiry)set_clear_internal, /* tp_clear */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2123 (richcmpfunc)set_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
2124 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
2125 (getiterfunc)set_iter, /* tp_iter */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2126 0, /* tp_iternext */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2127 set_methods, /* tp_methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2128 0, /* tp_members */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2129 0, /* tp_getset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2130 0, /* tp_base */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2131 0, /* tp_dict */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2132 0, /* tp_descr_get */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2133 0, /* tp_descr_set */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2134 0, /* tp_dictoffset */
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
2135 (initproc)set_init, /* tp_init */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2136 PyType_GenericAlloc, /* tp_alloc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2137 set_new, /* tp_new */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
2138 PyObject_GC_Del, /* tp_free */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2139 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2140
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2141 /* frozenset object ********************************************************/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2142
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2143
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2144 static PyMethodDef frozenset_methods[] = {
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
2145 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2146 contains_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
2147 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2148 copy_doc},
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
2149 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2150 difference_doc},
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
2151 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2152 intersection_doc},
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
2153 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
2154 isdisjoint_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
2155 {"issubset", (PyCFunction)set_issubset, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2156 issubset_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
2157 {"issuperset", (PyCFunction)set_issuperset, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2158 issuperset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2159 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2160 reduce_doc},
48467
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
2161 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
c0c06ffaea83 Merged revisions 64842,64853,64856,64945 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47750
diff changeset
2162 sizeof_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2163 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2164 symmetric_difference_doc},
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
2165 {"union", (PyCFunction)set_union, METH_VARARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2166 union_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2167 {NULL, NULL} /* sentinel */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2168 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2169
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2170 static PyNumberMethods frozenset_as_number = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2171 0, /*nb_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2172 (binaryfunc)set_sub, /*nb_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2173 0, /*nb_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2174 0, /*nb_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2175 0, /*nb_divmod*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2176 0, /*nb_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2177 0, /*nb_negative*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2178 0, /*nb_positive*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2179 0, /*nb_absolute*/
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 39696
diff changeset
2180 0, /*nb_bool*/
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2181 0, /*nb_invert*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2182 0, /*nb_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2183 0, /*nb_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2184 (binaryfunc)set_and, /*nb_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2185 (binaryfunc)set_xor, /*nb_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2186 (binaryfunc)set_or, /*nb_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2187 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2188
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2189 PyDoc_STRVAR(frozenset_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2190 "frozenset(iterable) --> frozenset object\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2191 \n\
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
2192 Build an immutable unordered collection of unique elements.");
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2193
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2194 PyTypeObject PyFrozenSet_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41782
diff changeset
2195 PyVarObject_HEAD_INIT(&PyType_Type, 0)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2196 "frozenset", /* tp_name */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2197 sizeof(PySetObject), /* tp_basicsize */
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
2198 0, /* tp_itemsize */
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
2199 /* methods */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2200 (destructor)set_dealloc, /* tp_dealloc */
42477
e7fd327e0c19 Kill all uses and definitions of tp_print under Objects/. (Others will follow.)
Guido van Rossum <guido@python.org>
parents: 42335
diff changeset
2201 0, /* tp_print */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2202 0, /* tp_getattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2203 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51244
diff changeset
2204 0, /* tp_reserved */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2205 (reprfunc)set_repr, /* tp_repr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2206 &frozenset_as_number, /* tp_as_number */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2207 &set_as_sequence, /* tp_as_sequence */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2208 0, /* tp_as_mapping */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2209 frozenset_hash, /* tp_hash */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2210 0, /* tp_call */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2211 0, /* tp_str */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2212 PyObject_GenericGetAttr, /* tp_getattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2213 0, /* tp_setattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2214 0, /* tp_as_buffer */
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
2215 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
2216 Py_TPFLAGS_BASETYPE, /* tp_flags */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2217 frozenset_doc, /* tp_doc */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
2218 (traverseproc)set_traverse, /* tp_traverse */
35315
a6cbfcd0e691 Factor away a redundant clear() function.
Raymond Hettinger <python@rcn.com>
parents: 35312
diff changeset
2219 (inquiry)set_clear_internal, /* tp_clear */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2220 (richcmpfunc)set_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
2221 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2222 (getiterfunc)set_iter, /* tp_iter */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2223 0, /* tp_iternext */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2224 frozenset_methods, /* tp_methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2225 0, /* tp_members */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2226 0, /* tp_getset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2227 0, /* tp_base */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2228 0, /* tp_dict */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2229 0, /* tp_descr_get */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2230 0, /* tp_descr_set */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2231 0, /* tp_dictoffset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2232 0, /* tp_init */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2233 PyType_GenericAlloc, /* tp_alloc */
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
2234 frozenset_new, /* tp_new */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
2235 PyObject_GC_Del, /* tp_free */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2236 };
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2237
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2238
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2239 /***** C API functions *************************************************/
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2240
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2241 PyObject *
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2242 PySet_New(PyObject *iterable)
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2243 {
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2244 return make_new_set(&PySet_Type, iterable);
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2245 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2246
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2247 PyObject *
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2248 PyFrozenSet_New(PyObject *iterable)
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2249 {
45044
c3b38a85989f Merged revisions 60383-60407 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
2250 return make_new_set(&PyFrozenSet_Type, iterable);
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2251 }
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2252
36833
9e81788b0d4b Use Py_ssize_t for PySet_Size() like all the other Py*_Size() functions.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36501
diff changeset
2253 Py_ssize_t
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2254 PySet_Size(PyObject *anyset)
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2255 {
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2256 if (!PyAnySet_Check(anyset)) {
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2257 PyErr_BadInternalCall();
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2258 return -1;
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2259 }
35398
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
2260 return PySet_GET_SIZE(anyset);
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2261 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2262
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2263 int
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2264 PySet_Clear(PyObject *set)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2265 {
45044
c3b38a85989f Merged revisions 60383-60407 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
2266 if (!PySet_Check(set)) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2267 PyErr_BadInternalCall();
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2268 return -1;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2269 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2270 return set_clear_internal((PySetObject *)set);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2271 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2272
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2273 int
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2274 PySet_Contains(PyObject *anyset, PyObject *key)
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2275 {
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2276 if (!PyAnySet_Check(anyset)) {
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2277 PyErr_BadInternalCall();
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2278 return -1;
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2279 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2280 return set_contains_key((PySetObject *)anyset, key);
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2281 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2282
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2283 int
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2284 PySet_Discard(PyObject *set, PyObject *key)
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2285 {
45044
c3b38a85989f Merged revisions 60383-60407 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
2286 if (!PySet_Check(set)) {
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2287 PyErr_BadInternalCall();
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2288 return -1;
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2289 }
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2290 return set_discard_key((PySetObject *)set, key);
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2291 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2292
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2293 int
45044
c3b38a85989f Merged revisions 60383-60407 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
2294 PySet_Add(PyObject *anyset, PyObject *key)
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2295 {
45145
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45066
diff changeset
2296 if (!PySet_Check(anyset) &&
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45066
diff changeset
2297 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2298 PyErr_BadInternalCall();
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2299 return -1;
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2300 }
45044
c3b38a85989f Merged revisions 60383-60407 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
2301 return set_add_key((PySetObject *)anyset, key);
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2302 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2303
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2304 int
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2305 _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2306 {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2307 setentry *entry;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2308
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2309 if (!PyAnySet_Check(set)) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2310 PyErr_BadInternalCall();
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2311 return -1;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2312 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2313 if (set_next((PySetObject *)set, pos, &entry) == 0)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2314 return 0;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2315 *key = entry->key;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40893
diff changeset
2316 *hash = entry->hash;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2317 return 1;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2318 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2319
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2320 PyObject *
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2321 PySet_Pop(PyObject *set)
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2322 {
45044
c3b38a85989f Merged revisions 60383-60407 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
2323 if (!PySet_Check(set)) {
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2324 PyErr_BadInternalCall();
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2325 return NULL;
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2326 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2327 return set_pop((PySetObject *)set);
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2328 }
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2329
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2330 int
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2331 _PySet_Update(PyObject *set, PyObject *iterable)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2332 {
45044
c3b38a85989f Merged revisions 60383-60407 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
2333 if (!PySet_Check(set)) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2334 PyErr_BadInternalCall();
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2335 return -1;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2336 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2337 return set_update_internal((PySetObject *)set, iterable);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2338 }
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2339
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2340 #ifdef Py_DEBUG
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2341
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2342 /* Test code to be called with any three element set.
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2343 Returns True and original set is restored. */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2344
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2345 #define assertRaises(call_return_value, exception) \
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2346 do { \
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2347 assert(call_return_value); \
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2348 assert(PyErr_ExceptionMatches(exception)); \
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2349 PyErr_Clear(); \
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2350 } while(0)
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2351
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2352 static PyObject *
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2353 test_c_api(PySetObject *so)
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2354 {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
2355 Py_ssize_t count;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2356 char *s;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2357 Py_ssize_t i;
41683
c5ebf9c219be Merged revisions 55228 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
2358 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2359 PyObject *ob = (PyObject *)so;
45066
3f2496dc91db Fixed r60466
Christian Heimes <christian@cheimes.de>
parents: 45065
diff changeset
2360 long hash;
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2361
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2362 /* Verify preconditions and exercise type/size checks */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2363 assert(PyAnySet_Check(ob));
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2364 assert(PyAnySet_CheckExact(ob));
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2365 assert(!PyFrozenSet_CheckExact(ob));
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2366 assert(PySet_Size(ob) == 3);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2367 assert(PySet_GET_SIZE(ob) == 3);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2368
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2369 /* Raise TypeError for non-iterable constructor arguments */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2370 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2371 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2372
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2373 /* Raise TypeError for unhashable key */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2374 dup = PySet_New(ob);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2375 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2376 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2377 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2378
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2379 /* Exercise successful pop, contains, add, and discard */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2380 elem = PySet_Pop(ob);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2381 assert(PySet_Contains(ob, elem) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2382 assert(PySet_GET_SIZE(ob) == 2);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2383 assert(PySet_Add(ob, elem) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2384 assert(PySet_Contains(ob, elem) == 1);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2385 assert(PySet_GET_SIZE(ob) == 3);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2386 assert(PySet_Discard(ob, elem) == 1);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2387 assert(PySet_GET_SIZE(ob) == 2);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2388 assert(PySet_Discard(ob, elem) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2389 assert(PySet_GET_SIZE(ob) == 2);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2390
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2391 /* Exercise clear */
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2392 dup2 = PySet_New(dup);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2393 assert(PySet_Clear(dup2) == 0);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2394 assert(PySet_Size(dup2) == 0);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2395 Py_DECREF(dup2);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2396
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2397 /* Raise SystemError on clear or update of frozen set */
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2398 f = PyFrozenSet_New(dup);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2399 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2400 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
45145
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45066
diff changeset
2401 assert(PySet_Add(f, elem) == 0);
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45066
diff changeset
2402 Py_INCREF(f);
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45066
diff changeset
2403 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
3d7b84daa34a Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45066
diff changeset
2404 Py_DECREF(f);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2405 Py_DECREF(f);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2406
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2407 /* Exercise direct iteration */
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2408 i = 0, count = 0;
45066
3f2496dc91db Fixed r60466
Christian Heimes <christian@cheimes.de>
parents: 45065
diff changeset
2409 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48475
diff changeset
2410 s = _PyUnicode_AsString(x);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2411 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2412 count++;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2413 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2414 assert(count == 3);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2415
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2416 /* Exercise updates */
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2417 dup2 = PySet_New(NULL);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2418 assert(_PySet_Update(dup2, dup) == 0);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2419 assert(PySet_Size(dup2) == 3);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2420 assert(_PySet_Update(dup2, dup) == 0);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2421 assert(PySet_Size(dup2) == 3);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2422 Py_DECREF(dup2);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2423
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2424 /* Raise SystemError when self argument is not a set or frozenset. */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2425 t = PyTuple_New(0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2426 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2427 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2428 Py_DECREF(t);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2429
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2430 /* Raise SystemError when self argument is not a set. */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2431 f = PyFrozenSet_New(dup);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2432 assert(PySet_Size(f) == 3);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2433 assert(PyFrozenSet_CheckExact(f));
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2434 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2435 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2436 Py_DECREF(f);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2437
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2438 /* Raise KeyError when popping from an empty set */
35353
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
2439 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
2440 Py_DECREF(ob);
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2441 assert(PySet_GET_SIZE(ob) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2442 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2443
35353
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
2444 /* Restore the set from the copy using the PyNumber API */
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
2445 assert(PyNumber_InPlaceOr(ob, dup) == ob);
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
2446 Py_DECREF(ob);
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2447
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2448 /* Verify constructors accept NULL arguments */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2449 f = PySet_New(NULL);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2450 assert(f != NULL);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2451 assert(PySet_GET_SIZE(f) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2452 Py_DECREF(f);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2453 f = PyFrozenSet_New(NULL);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2454 assert(f != NULL);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2455 assert(PyFrozenSet_CheckExact(f));
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2456 assert(PySet_GET_SIZE(f) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2457 Py_DECREF(f);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2458
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2459 Py_DECREF(elem);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2460 Py_DECREF(dup);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2461 Py_RETURN_TRUE;
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2462 }
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2463
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
2464 #undef assertRaises
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
2465
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2466 #endif