1120 lines
25 KiB
C
1120 lines
25 KiB
C
#include "Python.h"
|
|
#include "structmember.h"
|
|
|
|
/* set object implementation
|
|
written and maintained by Raymond D. Hettinger <python@rcn.com>
|
|
derived from sets.py written by Greg V. Wilson, Alex Martelli,
|
|
Guido van Rossum, Raymond Hettinger, and Tim Peters.
|
|
|
|
Copyright (c) 2003 Python Software Foundation.
|
|
All rights reserved.
|
|
*/
|
|
|
|
static PyObject *
|
|
set_update(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *item, *data, *it;
|
|
|
|
if (PyAnySet_Check(other)) {
|
|
if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 1) == -1)
|
|
return NULL;
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
it = PyObject_GetIter(other);
|
|
if (it == NULL)
|
|
return NULL;
|
|
data = so->data;
|
|
|
|
while ((item = PyIter_Next(it)) != NULL) {
|
|
if (PyDict_SetItem(data, item, Py_True) == -1) {
|
|
Py_DECREF(it);
|
|
Py_DECREF(item);
|
|
return NULL;
|
|
}
|
|
Py_DECREF(item);
|
|
}
|
|
Py_DECREF(it);
|
|
if (PyErr_Occurred())
|
|
return NULL;
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(update_doc,
|
|
"Update a set with the union of itself and another.");
|
|
|
|
static PyObject *
|
|
make_new_set(PyTypeObject *type, PyObject *iterable)
|
|
{
|
|
PyObject *data = NULL;
|
|
PyObject *tmp;
|
|
PySetObject *so = NULL;
|
|
|
|
data = PyDict_New();
|
|
if (data == NULL)
|
|
return NULL;
|
|
|
|
/* create PySetObject structure */
|
|
so = (PySetObject *)type->tp_alloc(type, 0);
|
|
if (so == NULL) {
|
|
Py_DECREF(data);
|
|
return NULL;
|
|
}
|
|
so->data = data;
|
|
so->hash = -1;
|
|
so->weakreflist = NULL;
|
|
|
|
if (iterable != NULL) {
|
|
tmp = set_update(so, iterable);
|
|
if (tmp == NULL) {
|
|
Py_DECREF(so);
|
|
return NULL;
|
|
}
|
|
Py_DECREF(tmp);
|
|
}
|
|
|
|
return (PyObject *)so;
|
|
}
|
|
|
|
static PyObject *
|
|
frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
{
|
|
PyObject *iterable = NULL;
|
|
|
|
if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
|
|
return NULL;
|
|
if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
|
|
Py_INCREF(iterable);
|
|
return iterable;
|
|
}
|
|
return make_new_set(type, iterable);
|
|
}
|
|
|
|
static PyObject *
|
|
set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
{
|
|
return make_new_set(type, NULL);
|
|
}
|
|
|
|
static PyObject *
|
|
frozenset_dict_wrapper(PyObject *d)
|
|
{
|
|
PySetObject *w;
|
|
|
|
assert(PyDict_Check(d));
|
|
w = (PySetObject *)make_new_set(&PyFrozenSet_Type, NULL);
|
|
if (w == NULL)
|
|
return NULL;
|
|
Py_CLEAR(w->data);
|
|
Py_INCREF(d);
|
|
w->data = d;
|
|
return (PyObject *)w;
|
|
}
|
|
|
|
static void
|
|
set_dealloc(PySetObject *so)
|
|
{
|
|
if (so->weakreflist != NULL)
|
|
PyObject_ClearWeakRefs((PyObject *) so);
|
|
Py_XDECREF(so->data);
|
|
so->ob_type->tp_free(so);
|
|
}
|
|
|
|
static PyObject *
|
|
set_iter(PySetObject *so)
|
|
{
|
|
return PyObject_GetIter(so->data);
|
|
}
|
|
|
|
static int
|
|
set_len(PySetObject *so)
|
|
{
|
|
return PyDict_Size(so->data);
|
|
}
|
|
|
|
static int
|
|
set_contains(PySetObject *so, PyObject *key)
|
|
{
|
|
PyObject *tmp;
|
|
int result;
|
|
|
|
result = PyDict_Contains(so->data, key);
|
|
if (result == -1 && PyAnySet_Check(key)) {
|
|
PyErr_Clear();
|
|
tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
|
|
if (tmp == NULL)
|
|
return -1;
|
|
result = PyDict_Contains(so->data, tmp);
|
|
Py_DECREF(tmp);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
set_direct_contains(PySetObject *so, PyObject *key)
|
|
{
|
|
long result;
|
|
|
|
result = set_contains(so, key);
|
|
if (result == -1)
|
|
return NULL;
|
|
return PyBool_FromLong(result);
|
|
}
|
|
|
|
PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
|
|
|
|
static PyObject *
|
|
set_copy(PySetObject *so)
|
|
{
|
|
return make_new_set(so->ob_type, (PyObject *)so);
|
|
}
|
|
|
|
static PyObject *
|
|
frozenset_copy(PySetObject *so)
|
|
{
|
|
if (PyFrozenSet_CheckExact(so)) {
|
|
Py_INCREF(so);
|
|
return (PyObject *)so;
|
|
}
|
|
return set_copy(so);
|
|
}
|
|
|
|
PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
|
|
|
|
static PyObject *
|
|
set_union(PySetObject *so, PyObject *other)
|
|
{
|
|
PySetObject *result;
|
|
PyObject *rv;
|
|
|
|
result = (PySetObject *)set_copy(so);
|
|
if (result == NULL)
|
|
return NULL;
|
|
rv = set_update(result, other);
|
|
if (rv == NULL) {
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
Py_DECREF(rv);
|
|
return (PyObject *)result;
|
|
}
|
|
|
|
PyDoc_STRVAR(union_doc,
|
|
"Return the union of two sets as a new set.\n\
|
|
\n\
|
|
(i.e. all elements that are in either set.)");
|
|
|
|
static PyObject *
|
|
set_or(PySetObject *so, PyObject *other)
|
|
{
|
|
if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
return set_union(so, other);
|
|
}
|
|
|
|
static PyObject *
|
|
set_ior(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *result;
|
|
|
|
if (!PyAnySet_Check(other)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
result = set_update(so, other);
|
|
if (result == NULL)
|
|
return NULL;
|
|
Py_DECREF(result);
|
|
Py_INCREF(so);
|
|
return (PyObject *)so;
|
|
}
|
|
|
|
static PyObject *
|
|
set_intersection(PySetObject *so, PyObject *other)
|
|
{
|
|
PySetObject *result;
|
|
PyObject *item, *selfdata, *tgtdata, *it, *tmp;
|
|
|
|
result = (PySetObject *)make_new_set(so->ob_type, NULL);
|
|
if (result == NULL)
|
|
return NULL;
|
|
tgtdata = result->data;
|
|
selfdata = so->data;
|
|
|
|
if (PyAnySet_Check(other))
|
|
other = ((PySetObject *)other)->data;
|
|
|
|
if (PyDict_Check(other) && PyDict_Size(other) > PyDict_Size(selfdata)) {
|
|
tmp = selfdata;
|
|
selfdata = other;
|
|
other = tmp;
|
|
}
|
|
|
|
if (PyDict_CheckExact(other)) {
|
|
PyObject *value;
|
|
int pos = 0;
|
|
while (PyDict_Next(other, &pos, &item, &value)) {
|
|
if (PyDict_Contains(selfdata, item)) {
|
|
if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
return (PyObject *)result;
|
|
}
|
|
|
|
it = PyObject_GetIter(other);
|
|
if (it == NULL) {
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
|
|
while ((item = PyIter_Next(it)) != NULL) {
|
|
if (PyDict_Contains(selfdata, item)) {
|
|
if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
|
|
Py_DECREF(it);
|
|
Py_DECREF(result);
|
|
Py_DECREF(item);
|
|
return NULL;
|
|
}
|
|
}
|
|
Py_DECREF(item);
|
|
}
|
|
Py_DECREF(it);
|
|
if (PyErr_Occurred()) {
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
return (PyObject *)result;
|
|
}
|
|
|
|
PyDoc_STRVAR(intersection_doc,
|
|
"Return the intersection of two sets as a new set.\n\
|
|
\n\
|
|
(i.e. all elements that are in both sets.)");
|
|
|
|
static PyObject *
|
|
set_intersection_update(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *item, *selfdata, *it, *newdict, *tmp;
|
|
|
|
newdict = PyDict_New();
|
|
if (newdict == NULL)
|
|
return newdict;
|
|
|
|
it = PyObject_GetIter(other);
|
|
if (it == NULL) {
|
|
Py_DECREF(newdict);
|
|
return NULL;
|
|
}
|
|
|
|
selfdata = so->data;
|
|
while ((item = PyIter_Next(it)) != NULL) {
|
|
if (PyDict_Contains(selfdata, item)) {
|
|
if (PyDict_SetItem(newdict, item, Py_True) == -1) {
|
|
Py_DECREF(newdict);
|
|
Py_DECREF(it);
|
|
Py_DECREF(item);
|
|
return NULL;
|
|
}
|
|
}
|
|
Py_DECREF(item);
|
|
}
|
|
Py_DECREF(it);
|
|
if (PyErr_Occurred()) {
|
|
Py_DECREF(newdict);
|
|
return NULL;
|
|
}
|
|
tmp = so->data;
|
|
so->data = newdict;
|
|
Py_DECREF(tmp);
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(intersection_update_doc,
|
|
"Update a set with the intersection of itself and another.");
|
|
|
|
static PyObject *
|
|
set_and(PySetObject *so, PyObject *other)
|
|
{
|
|
if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
return set_intersection(so, other);
|
|
}
|
|
|
|
static PyObject *
|
|
set_iand(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *result;
|
|
|
|
if (!PyAnySet_Check(other)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
result = set_intersection_update(so, other);
|
|
if (result == NULL)
|
|
return NULL;
|
|
Py_DECREF(result);
|
|
Py_INCREF(so);
|
|
return (PyObject *)so;
|
|
}
|
|
|
|
static PyObject *
|
|
set_difference_update(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *item, *tgtdata, *it;
|
|
|
|
it = PyObject_GetIter(other);
|
|
if (it == NULL)
|
|
return NULL;
|
|
|
|
tgtdata = so->data;
|
|
while ((item = PyIter_Next(it)) != NULL) {
|
|
if (PyDict_DelItem(tgtdata, item) == -1) {
|
|
if (PyErr_ExceptionMatches(PyExc_KeyError))
|
|
PyErr_Clear();
|
|
else {
|
|
Py_DECREF(it);
|
|
Py_DECREF(item);
|
|
return NULL;
|
|
}
|
|
}
|
|
Py_DECREF(item);
|
|
}
|
|
Py_DECREF(it);
|
|
if (PyErr_Occurred())
|
|
return NULL;
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(difference_update_doc,
|
|
"Remove all elements of another set from this set.");
|
|
|
|
static PyObject *
|
|
set_difference(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *result, *tmp;
|
|
PyObject *otherdata, *tgtdata;
|
|
PyObject *key, *value;
|
|
int pos = 0;
|
|
|
|
if (PyDict_Check(other))
|
|
otherdata = other;
|
|
else if (PyAnySet_Check(other))
|
|
otherdata = ((PySetObject *)other)->data;
|
|
else {
|
|
result = set_copy(so);
|
|
if (result == NULL)
|
|
return result;
|
|
tmp = set_difference_update((PySetObject *)result, other);
|
|
if (tmp != NULL) {
|
|
Py_DECREF(tmp);
|
|
return result;
|
|
}
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
|
|
result = make_new_set(so->ob_type, NULL);
|
|
if (result == NULL)
|
|
return NULL;
|
|
tgtdata = ((PySetObject *)result)->data;
|
|
|
|
while (PyDict_Next(so->data, &pos, &key, &value)) {
|
|
if (!PyDict_Contains(otherdata, key)) {
|
|
if (PyDict_SetItem(tgtdata, key, Py_True) == -1)
|
|
return NULL;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
PyDoc_STRVAR(difference_doc,
|
|
"Return the difference of two sets as a new set.\n\
|
|
\n\
|
|
(i.e. all elements that are in this set but not the other.)");
|
|
static PyObject *
|
|
set_sub(PySetObject *so, PyObject *other)
|
|
{
|
|
if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
return set_difference(so, other);
|
|
}
|
|
|
|
static PyObject *
|
|
set_isub(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *result;
|
|
|
|
if (!PyAnySet_Check(other)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
result = set_difference_update(so, other);
|
|
if (result == NULL)
|
|
return NULL;
|
|
Py_DECREF(result);
|
|
Py_INCREF(so);
|
|
return (PyObject *)so;
|
|
}
|
|
|
|
static PyObject *
|
|
set_symmetric_difference_update(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *selfdata, *otherdata;
|
|
PySetObject *otherset = NULL;
|
|
PyObject *key, *value;
|
|
int pos = 0;
|
|
|
|
selfdata = so->data;
|
|
if (PyDict_Check(other))
|
|
otherdata = other;
|
|
else if (PyAnySet_Check(other))
|
|
otherdata = ((PySetObject *)other)->data;
|
|
else {
|
|
otherset = (PySetObject *)make_new_set(so->ob_type, other);
|
|
if (otherset == NULL)
|
|
return NULL;
|
|
otherdata = otherset->data;
|
|
}
|
|
|
|
while (PyDict_Next(otherdata, &pos, &key, &value)) {
|
|
if (PyDict_Contains(selfdata, key)) {
|
|
if (PyDict_DelItem(selfdata, key) == -1) {
|
|
Py_XDECREF(otherset);
|
|
return NULL;
|
|
}
|
|
} else {
|
|
if (PyDict_SetItem(selfdata, key, Py_True) == -1) {
|
|
Py_XDECREF(otherset);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
Py_XDECREF(otherset);
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(symmetric_difference_update_doc,
|
|
"Update a set with the symmetric difference of itself and another.");
|
|
|
|
static PyObject *
|
|
set_symmetric_difference(PySetObject *so, PyObject *other)
|
|
{
|
|
PySetObject *result;
|
|
PyObject *selfdata, *otherdata, *tgtdata, *rv, *otherset;
|
|
PyObject *key, *value;
|
|
int pos = 0;
|
|
|
|
if (PyDict_Check(other))
|
|
otherdata = other;
|
|
else if (PyAnySet_Check(other))
|
|
otherdata = ((PySetObject *)other)->data;
|
|
else {
|
|
otherset = make_new_set(so->ob_type, other);
|
|
if (otherset == NULL)
|
|
return NULL;
|
|
rv = set_symmetric_difference_update((PySetObject *)otherset, (PyObject *)so);
|
|
if (rv == NULL)
|
|
return NULL;
|
|
Py_DECREF(rv);
|
|
return otherset;
|
|
}
|
|
|
|
result = (PySetObject *)make_new_set(so->ob_type, NULL);
|
|
if (result == NULL)
|
|
return NULL;
|
|
tgtdata = result->data;
|
|
selfdata = so->data;
|
|
|
|
while (PyDict_Next(otherdata, &pos, &key, &value)) {
|
|
if (!PyDict_Contains(selfdata, key)) {
|
|
if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
pos = 0;
|
|
while (PyDict_Next(selfdata, &pos, &key, &value)) {
|
|
if (!PyDict_Contains(otherdata, key)) {
|
|
if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
return (PyObject *)result;
|
|
}
|
|
|
|
PyDoc_STRVAR(symmetric_difference_doc,
|
|
"Return the symmetric difference of two sets as a new set.\n\
|
|
\n\
|
|
(i.e. all elements that are in exactly one of the sets.)");
|
|
|
|
static PyObject *
|
|
set_xor(PySetObject *so, PyObject *other)
|
|
{
|
|
if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
return set_symmetric_difference(so, other);
|
|
}
|
|
|
|
static PyObject *
|
|
set_ixor(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *result;
|
|
|
|
if (!PyAnySet_Check(other)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
result = set_symmetric_difference_update(so, other);
|
|
if (result == NULL)
|
|
return NULL;
|
|
Py_DECREF(result);
|
|
Py_INCREF(so);
|
|
return (PyObject *)so;
|
|
}
|
|
|
|
static PyObject *
|
|
set_issubset(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *otherdata, *tmp, *result;
|
|
PyObject *key, *value;
|
|
int pos = 0;
|
|
|
|
if (!PyAnySet_Check(other)) {
|
|
tmp = make_new_set(&PySet_Type, other);
|
|
if (tmp == NULL)
|
|
return NULL;
|
|
result = set_issubset(so, tmp);
|
|
Py_DECREF(tmp);
|
|
return result;
|
|
}
|
|
if (set_len(so) > set_len((PySetObject *)other))
|
|
Py_RETURN_FALSE;
|
|
|
|
otherdata = ((PySetObject *)other)->data;
|
|
while (PyDict_Next(((PySetObject *)so)->data, &pos, &key, &value)) {
|
|
if (!PyDict_Contains(otherdata, key))
|
|
Py_RETURN_FALSE;
|
|
}
|
|
Py_RETURN_TRUE;
|
|
}
|
|
|
|
PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
|
|
|
|
static PyObject *
|
|
set_issuperset(PySetObject *so, PyObject *other)
|
|
{
|
|
PyObject *tmp, *result;
|
|
|
|
if (!PyAnySet_Check(other)) {
|
|
tmp = make_new_set(&PySet_Type, other);
|
|
if (tmp == NULL)
|
|
return NULL;
|
|
result = set_issuperset(so, tmp);
|
|
Py_DECREF(tmp);
|
|
return result;
|
|
}
|
|
return set_issubset((PySetObject *)other, (PyObject *)so);
|
|
}
|
|
|
|
PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
|
|
|
|
static long
|
|
set_nohash(PyObject *self)
|
|
{
|
|
PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
|
|
return -1;
|
|
}
|
|
|
|
static int
|
|
set_nocmp(PyObject *self)
|
|
{
|
|
PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
|
|
return -1;
|
|
}
|
|
|
|
static long
|
|
frozenset_hash(PyObject *self)
|
|
{
|
|
PySetObject *so = (PySetObject *)self;
|
|
PyObject *key, *value;
|
|
int pos = 0;
|
|
long hash = 1927868237L;
|
|
|
|
if (so->hash != -1)
|
|
return so->hash;
|
|
|
|
hash *= (PyDict_Size(so->data) + 1);
|
|
while (PyDict_Next(so->data, &pos, &key, &value)) {
|
|
/* Work to increase the bit dispersion for closely spaced hash
|
|
values. The is important because some use cases have many
|
|
combinations of a small number of elements with nearby
|
|
hashes so that many distinct combinations collapse to only
|
|
a handful of distinct hash values. */
|
|
long h = PyObject_Hash(key);
|
|
hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
|
|
}
|
|
hash = hash * 69069L + 907133923L;
|
|
if (hash == -1)
|
|
hash = 590923713L;
|
|
so->hash = hash;
|
|
return hash;
|
|
}
|
|
|
|
static PyObject *
|
|
set_richcompare(PySetObject *v, PyObject *w, int op)
|
|
{
|
|
if(!PyAnySet_Check(w)) {
|
|
if (op == Py_EQ)
|
|
Py_RETURN_FALSE;
|
|
if (op == Py_NE)
|
|
Py_RETURN_TRUE;
|
|
PyErr_SetString(PyExc_TypeError, "can only compare to a set");
|
|
return NULL;
|
|
}
|
|
switch (op) {
|
|
case Py_EQ:
|
|
case Py_NE:
|
|
return PyObject_RichCompare(((PySetObject *)v)->data,
|
|
((PySetObject *)w)->data, op);
|
|
case Py_LE:
|
|
return set_issubset((PySetObject *)v, w);
|
|
case Py_GE:
|
|
return set_issuperset((PySetObject *)v, w);
|
|
case Py_LT:
|
|
if (set_len(v) >= set_len((PySetObject *)w))
|
|
Py_RETURN_FALSE;
|
|
return set_issubset((PySetObject *)v, w);
|
|
case Py_GT:
|
|
if (set_len(v) <= set_len((PySetObject *)w))
|
|
Py_RETURN_FALSE;
|
|
return set_issuperset((PySetObject *)v, w);
|
|
}
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
|
|
static PyObject *
|
|
set_repr(PySetObject *so)
|
|
{
|
|
PyObject *keys, *result, *listrepr;
|
|
|
|
keys = PyDict_Keys(so->data);
|
|
if (keys == NULL)
|
|
return NULL;
|
|
listrepr = PyObject_Repr(keys);
|
|
Py_DECREF(keys);
|
|
if (listrepr == NULL)
|
|
return NULL;
|
|
|
|
result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
|
|
PyString_AS_STRING(listrepr));
|
|
Py_DECREF(listrepr);
|
|
return result;
|
|
}
|
|
|
|
static int
|
|
set_tp_print(PySetObject *so, FILE *fp, int flags)
|
|
{
|
|
PyObject *key, *value;
|
|
int pos=0;
|
|
char *emit = ""; /* No separator emitted on first pass */
|
|
char *separator = ", ";
|
|
|
|
fprintf(fp, "%s([", so->ob_type->tp_name);
|
|
while (PyDict_Next(so->data, &pos, &key, &value)) {
|
|
fputs(emit, fp);
|
|
emit = separator;
|
|
if (PyObject_Print(key, fp, 0) != 0)
|
|
return -1;
|
|
}
|
|
fputs("])", fp);
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
set_clear(PySetObject *so)
|
|
{
|
|
PyDict_Clear(so->data);
|
|
so->hash = -1;
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
|
|
|
|
static PyObject *
|
|
set_add(PySetObject *so, PyObject *item)
|
|
{
|
|
if (PyDict_SetItem(so->data, item, Py_True) == -1)
|
|
return NULL;
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(add_doc,
|
|
"Add an element to a set.\n\
|
|
\n\
|
|
This has no effect if the element is already present.");
|
|
|
|
static PyObject *
|
|
set_remove(PySetObject *so, PyObject *item)
|
|
{
|
|
PyObject *tmp, *result;
|
|
|
|
if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
|
|
tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
|
|
if (tmp == NULL)
|
|
return NULL;
|
|
result = set_remove(so, tmp);
|
|
Py_DECREF(tmp);
|
|
return result;
|
|
}
|
|
|
|
if (PyDict_DelItem(so->data, item) == -1)
|
|
return NULL;
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(remove_doc,
|
|
"Remove an element from a set; it must be a member.\n\
|
|
\n\
|
|
If the element is not a member, raise a KeyError.");
|
|
|
|
static PyObject *
|
|
set_discard(PySetObject *so, PyObject *item)
|
|
{
|
|
PyObject *tmp, *result;
|
|
|
|
if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
|
|
tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
|
|
if (tmp == NULL)
|
|
return NULL;
|
|
result = set_discard(so, tmp);
|
|
Py_DECREF(tmp);
|
|
return result;
|
|
}
|
|
|
|
if (PyDict_DelItem(so->data, item) == -1) {
|
|
if (!PyErr_ExceptionMatches(PyExc_KeyError))
|
|
return NULL;
|
|
PyErr_Clear();
|
|
}
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(discard_doc,
|
|
"Remove an element from a set if it is a member.\n\
|
|
\n\
|
|
If the element is not a member, do nothing.");
|
|
|
|
static PyObject *
|
|
set_pop(PySetObject *so)
|
|
{
|
|
PyObject *key, *value;
|
|
int pos = 0;
|
|
|
|
if (!PyDict_Next(so->data, &pos, &key, &value)) {
|
|
PyErr_SetString(PyExc_KeyError, "pop from an empty set");
|
|
return NULL;
|
|
}
|
|
Py_INCREF(key);
|
|
if (PyDict_DelItem(so->data, key) == -1) {
|
|
Py_DECREF(key);
|
|
return NULL;
|
|
}
|
|
return key;
|
|
}
|
|
|
|
PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
|
|
|
|
static PyObject *
|
|
set_reduce(PySetObject *so)
|
|
{
|
|
PyObject *keys=NULL, *args=NULL, *result=NULL;
|
|
|
|
keys = PyDict_Keys(so->data);
|
|
if (keys == NULL)
|
|
goto done;
|
|
args = PyTuple_Pack(1, keys);
|
|
if (args == NULL)
|
|
goto done;
|
|
result = PyTuple_Pack(2, so->ob_type, args);
|
|
done:
|
|
Py_XDECREF(args);
|
|
Py_XDECREF(keys);
|
|
return result;
|
|
}
|
|
|
|
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
|
|
|
|
static int
|
|
set_init(PySetObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
PyObject *iterable = NULL;
|
|
PyObject *result;
|
|
|
|
if (!PyAnySet_Check(self))
|
|
return -1;
|
|
if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
|
|
return -1;
|
|
PyDict_Clear(self->data);
|
|
self->hash = -1;
|
|
if (iterable == NULL)
|
|
return 0;
|
|
result = set_update(self, iterable);
|
|
if (result != NULL) {
|
|
Py_DECREF(result);
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static PySequenceMethods set_as_sequence = {
|
|
(inquiry)set_len, /* sq_length */
|
|
0, /* sq_concat */
|
|
0, /* sq_repeat */
|
|
0, /* sq_item */
|
|
0, /* sq_slice */
|
|
0, /* sq_ass_item */
|
|
0, /* sq_ass_slice */
|
|
(objobjproc)set_contains, /* sq_contains */
|
|
};
|
|
|
|
/* set object ********************************************************/
|
|
|
|
static PyMethodDef set_methods[] = {
|
|
{"add", (PyCFunction)set_add, METH_O,
|
|
add_doc},
|
|
{"clear", (PyCFunction)set_clear, METH_NOARGS,
|
|
clear_doc},
|
|
{"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
|
|
contains_doc},
|
|
{"copy", (PyCFunction)set_copy, METH_NOARGS,
|
|
copy_doc},
|
|
{"discard", (PyCFunction)set_discard, METH_O,
|
|
discard_doc},
|
|
{"difference", (PyCFunction)set_difference, METH_O,
|
|
difference_doc},
|
|
{"difference_update", (PyCFunction)set_difference_update, METH_O,
|
|
difference_update_doc},
|
|
{"intersection",(PyCFunction)set_intersection, METH_O,
|
|
intersection_doc},
|
|
{"intersection_update",(PyCFunction)set_intersection_update, METH_O,
|
|
intersection_update_doc},
|
|
{"issubset", (PyCFunction)set_issubset, METH_O,
|
|
issubset_doc},
|
|
{"issuperset", (PyCFunction)set_issuperset, METH_O,
|
|
issuperset_doc},
|
|
{"pop", (PyCFunction)set_pop, METH_NOARGS,
|
|
pop_doc},
|
|
{"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
|
|
reduce_doc},
|
|
{"remove", (PyCFunction)set_remove, METH_O,
|
|
remove_doc},
|
|
{"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
|
|
symmetric_difference_doc},
|
|
{"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
|
|
symmetric_difference_update_doc},
|
|
{"union", (PyCFunction)set_union, METH_O,
|
|
union_doc},
|
|
{"update", (PyCFunction)set_update, METH_O,
|
|
update_doc},
|
|
{NULL, NULL} /* sentinel */
|
|
};
|
|
|
|
static PyNumberMethods set_as_number = {
|
|
0, /*nb_add*/
|
|
(binaryfunc)set_sub, /*nb_subtract*/
|
|
0, /*nb_multiply*/
|
|
0, /*nb_divide*/
|
|
0, /*nb_remainder*/
|
|
0, /*nb_divmod*/
|
|
0, /*nb_power*/
|
|
0, /*nb_negative*/
|
|
0, /*nb_positive*/
|
|
0, /*nb_absolute*/
|
|
0, /*nb_nonzero*/
|
|
0, /*nb_invert*/
|
|
0, /*nb_lshift*/
|
|
0, /*nb_rshift*/
|
|
(binaryfunc)set_and, /*nb_and*/
|
|
(binaryfunc)set_xor, /*nb_xor*/
|
|
(binaryfunc)set_or, /*nb_or*/
|
|
0, /*nb_coerce*/
|
|
0, /*nb_int*/
|
|
0, /*nb_long*/
|
|
0, /*nb_float*/
|
|
0, /*nb_oct*/
|
|
0, /*nb_hex*/
|
|
0, /*nb_inplace_add*/
|
|
(binaryfunc)set_isub, /*nb_inplace_subtract*/
|
|
0, /*nb_inplace_multiply*/
|
|
0, /*nb_inplace_divide*/
|
|
0, /*nb_inplace_remainder*/
|
|
0, /*nb_inplace_power*/
|
|
0, /*nb_inplace_lshift*/
|
|
0, /*nb_inplace_rshift*/
|
|
(binaryfunc)set_iand, /*nb_inplace_and*/
|
|
(binaryfunc)set_ixor, /*nb_inplace_xor*/
|
|
(binaryfunc)set_ior, /*nb_inplace_or*/
|
|
};
|
|
|
|
PyDoc_STRVAR(set_doc,
|
|
"set(iterable) --> set object\n\
|
|
\n\
|
|
Build an unordered collection.");
|
|
|
|
PyTypeObject PySet_Type = {
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
0, /* ob_size */
|
|
"set", /* tp_name */
|
|
sizeof(PySetObject), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
/* methods */
|
|
(destructor)set_dealloc, /* tp_dealloc */
|
|
(printfunc)set_tp_print, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
(cmpfunc)set_nocmp, /* tp_compare */
|
|
(reprfunc)set_repr, /* tp_repr */
|
|
&set_as_number, /* tp_as_number */
|
|
&set_as_sequence, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
set_nohash, /* tp_hash */
|
|
0, /* tp_call */
|
|
0, /* tp_str */
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
0, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
|
|
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
|
|
set_doc, /* tp_doc */
|
|
0, /* tp_traverse */
|
|
0, /* tp_clear */
|
|
(richcmpfunc)set_richcompare, /* tp_richcompare */
|
|
offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
|
|
(getiterfunc)set_iter, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
set_methods, /* tp_methods */
|
|
0, /* tp_members */
|
|
0, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
0, /* tp_dictoffset */
|
|
(initproc)set_init, /* tp_init */
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
set_new, /* tp_new */
|
|
PyObject_Del, /* tp_free */
|
|
};
|
|
|
|
/* frozenset object ********************************************************/
|
|
|
|
|
|
static PyMethodDef frozenset_methods[] = {
|
|
{"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
|
|
contains_doc},
|
|
{"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
|
|
copy_doc},
|
|
{"difference", (PyCFunction)set_difference, METH_O,
|
|
difference_doc},
|
|
{"intersection",(PyCFunction)set_intersection, METH_O,
|
|
intersection_doc},
|
|
{"issubset", (PyCFunction)set_issubset, METH_O,
|
|
issubset_doc},
|
|
{"issuperset", (PyCFunction)set_issuperset, METH_O,
|
|
issuperset_doc},
|
|
{"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
|
|
reduce_doc},
|
|
{"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
|
|
symmetric_difference_doc},
|
|
{"union", (PyCFunction)set_union, METH_O,
|
|
union_doc},
|
|
{NULL, NULL} /* sentinel */
|
|
};
|
|
|
|
static PyNumberMethods frozenset_as_number = {
|
|
0, /*nb_add*/
|
|
(binaryfunc)set_sub, /*nb_subtract*/
|
|
0, /*nb_multiply*/
|
|
0, /*nb_divide*/
|
|
0, /*nb_remainder*/
|
|
0, /*nb_divmod*/
|
|
0, /*nb_power*/
|
|
0, /*nb_negative*/
|
|
0, /*nb_positive*/
|
|
0, /*nb_absolute*/
|
|
0, /*nb_nonzero*/
|
|
0, /*nb_invert*/
|
|
0, /*nb_lshift*/
|
|
0, /*nb_rshift*/
|
|
(binaryfunc)set_and, /*nb_and*/
|
|
(binaryfunc)set_xor, /*nb_xor*/
|
|
(binaryfunc)set_or, /*nb_or*/
|
|
};
|
|
|
|
PyDoc_STRVAR(frozenset_doc,
|
|
"frozenset(iterable) --> frozenset object\n\
|
|
\n\
|
|
Build an immutable unordered collection.");
|
|
|
|
PyTypeObject PyFrozenSet_Type = {
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
0, /* ob_size */
|
|
"frozenset", /* tp_name */
|
|
sizeof(PySetObject), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
/* methods */
|
|
(destructor)set_dealloc, /* tp_dealloc */
|
|
(printfunc)set_tp_print, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
(cmpfunc)set_nocmp, /* tp_compare */
|
|
(reprfunc)set_repr, /* tp_repr */
|
|
&frozenset_as_number, /* tp_as_number */
|
|
&set_as_sequence, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
frozenset_hash, /* tp_hash */
|
|
0, /* tp_call */
|
|
0, /* tp_str */
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
0, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
|
|
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
|
|
frozenset_doc, /* tp_doc */
|
|
0, /* tp_traverse */
|
|
0, /* tp_clear */
|
|
(richcmpfunc)set_richcompare, /* tp_richcompare */
|
|
offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
|
|
(getiterfunc)set_iter, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
frozenset_methods, /* tp_methods */
|
|
0, /* tp_members */
|
|
0, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
0, /* tp_dictoffset */
|
|
0, /* tp_init */
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
frozenset_new, /* tp_new */
|
|
PyObject_Del, /* tp_free */
|
|
};
|