cinap_lenrekadd hg and python4bb9e2215 years agoHistory
2312 lines · 54.2 KB · C
RawDownload
	} 	so->lookup = set_lookkey_string;	so->weakreflist = NULL; 	if (iterable != NULL) {		if (set_update_internal(so, iterable) == -1) {			Py_DECREF(so);			return NULL;		}	} 	return (PyObject *)so;} /* The empty frozenset is a singleton */static PyObject *emptyfrozenset = NULL; static PyObject *frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds){	PyObject *iterable = NULL, *result; 	if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))		return NULL; 	if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))		return NULL; 	if (type != &PyFrozenSet_Type)		return make_new_set(type, iterable); 	if (iterable != NULL) {		/* frozenset(f) is idempotent */		if (PyFrozenSet_CheckExact(iterable)) {			Py_INCREF(iterable);			return iterable;		}		result = make_new_set(type, iterable);		if (result == NULL || PySet_GET_SIZE(result))			return result;		Py_DECREF(result);	}	/* The empty frozenset is a singleton */	if (emptyfrozenset == NULL)		emptyfrozenset = make_new_set(type, NULL);	Py_XINCREF(emptyfrozenset);	return emptyfrozenset;} voidPySet_Fini(void){	PySetObject *so; 	while (num_free_sets) {		num_free_sets--;		so = free_sets[num_free_sets];		PyObject_GC_Del(so);	}	Py_CLEAR(dummy);	Py_CLEAR(emptyfrozenset);} static PyObject *set_new(PyTypeObject *type, PyObject *args, PyObject *kwds){	if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))		return NULL;		return make_new_set(type, NULL);} /* set_swap_bodies() switches the contents of any two sets by moving their   internal data pointers and, if needed, copying the internal smalltables.   Semantically equivalent to:      t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t    The function always succeeds and it leaves both objects in a stable state.   Useful for creating temporary frozensets from sets for membership testing    in __contains__(), discard(), and remove().  Also useful for operations   that update in-place (by allowing an intermediate result to be swapped    into one of the original inputs).*/ static voidset_swap_bodies(PySetObject *a, PySetObject *b){	Py_ssize_t t;	setentry *u;	setentry *(*f)(PySetObject *so, PyObject *key, long hash);	setentry tab[PySet_MINSIZE];	long h; 	t = a->fill;     a->fill   = b->fill;        b->fill  = t;	t = a->used;     a->used   = b->used;        b->used  = t;	t = a->mask;     a->mask   = b->mask;        b->mask  = t; 	u = a->table;	if (a->table == a->smalltable)		u = b->smalltable;	a->table  = b->table;	if (b->table == b->smalltable)		a->table = a->smalltable;	b->table = u; 	f = a->lookup;   a->lookup = b->lookup;      b->lookup = f; 	if (a->table == a->smalltable || b->table == b->smalltable) {		memcpy(tab, a->smalltable, sizeof(tab));		memcpy(a->smalltable, b->smalltable, sizeof(tab));		memcpy(b->smalltable, tab, sizeof(tab));	} 	if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type)  &&	    PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {		h = a->hash;     a->hash = b->hash;  b->hash = h;	} else {		a->hash = -1;		b->hash = -1;	}} 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_clear(PySetObject *so){	set_clear_internal(so);	Py_RETURN_NONE;} PyDoc_STRVAR(clear_doc, "Remove all elements from this set."); static PyObject *set_union(PySetObject *so, PyObject *other){	PySetObject *result; 	result = (PySetObject *)set_copy(so);	if (result == NULL)		return NULL;	if ((PyObject *)so == other)		return (PyObject *)result;	if (set_update_internal(result, other) == -1) {		Py_DECREF(result);		return NULL;	}	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){	if (!PyAnySet_Check(other)) {		Py_INCREF(Py_NotImplemented);		return Py_NotImplemented;	}	if (set_update_internal(so, other) == -1)		return NULL;	Py_INCREF(so);	return (PyObject *)so;} static PyObject *set_intersection(PySetObject *so, PyObject *other){	PySetObject *result;	PyObject *key, *it, *tmp; 	if ((PyObject *)so == other)		return set_copy(so); 	result = (PySetObject *)make_new_set(so->ob_type, NULL);	if (result == NULL)		return NULL; 	if (PyAnySet_Check(other)) {				Py_ssize_t pos = 0;		setentry *entry; 		if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {			tmp = (PyObject *)so;			so = (PySetObject *)other;			other = tmp;		} 		while (set_next((PySetObject *)other, &pos, &entry)) {			int rv = set_contains_entry(so, entry);			if (rv == -1) {				Py_DECREF(result);				return NULL;			}			if (rv) {				if (set_add_entry(result, entry) == -1) {					Py_DECREF(result);					return NULL;				}			}		}		return (PyObject *)result;	} 	it = PyObject_GetIter(other);	if (it == NULL) {		Py_DECREF(result);		return NULL;	} 	while ((key = PyIter_Next(it)) != NULL) {		int rv;		setentry entry;		long hash = PyObject_Hash(key); 		if (hash == -1) {			Py_DECREF(it);			Py_DECREF(result);			Py_DECREF(key);			return NULL;		}		entry.hash = hash;		entry.key = key;		rv = set_contains_entry(so, &entry);		if (rv == -1) {			Py_DECREF(it);			Py_DECREF(result);			Py_DECREF(key);			return NULL;		}		if (rv) {			if (set_add_entry(result, &entry) == -1) {				Py_DECREF(it);				Py_DECREF(result);				Py_DECREF(key);				return NULL;			}		}		Py_DECREF(key);	}	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 *tmp; 	tmp = set_intersection(so, other);	if (tmp == NULL)		return NULL;	set_swap_bodies(so, (PySetObject *)tmp);	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 intset_difference_update_internal(PySetObject *so, PyObject *other){	if ((PyObject *)so == other)		return set_clear_internal(so);		if (PyAnySet_Check(other)) {		setentry *entry;		Py_ssize_t pos = 0; 		while (set_next((PySetObject *)other, &pos, &entry))			if (set_discard_entry(so, entry) == -1)				return -1;	} else {		PyObject *key, *it;		it = PyObject_GetIter(other);		if (it == NULL)			return -1; 		while ((key = PyIter_Next(it)) != NULL) {			if (set_discard_key(so, key) == -1) {				Py_DECREF(it);				Py_DECREF(key);				return -1;			}			Py_DECREF(key);		}		Py_DECREF(it);		if (PyErr_Occurred())			return -1;	}	/* If more than 1/5 are dummies, then resize them away. */	if ((so->fill - so->used) * 5 < so->mask)		return 0;	return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);} static PyObject *set_difference_update(PySetObject *so, PyObject *other){	if (set_difference_update_internal(so, other) != -1)		Py_RETURN_NONE;	return NULL;} PyDoc_STRVAR(difference_update_doc,"Remove all elements of another set from this set."); static PyObject *set_difference(PySetObject *so, PyObject *other){	PyObject *result;	setentry *entry;	Py_ssize_t pos = 0; 	if (!PyAnySet_Check(other)  && !PyDict_CheckExact(other)) {		result = set_copy(so);		if (result == NULL)			return NULL;		if (set_difference_update_internal((PySetObject *)result, other) != -1)			return result;		Py_DECREF(result);		return NULL;	}		result = make_new_set(so->ob_type, NULL);	if (result == NULL)		return NULL; 	if (PyDict_CheckExact(other)) {		while (set_next(so, &pos, &entry)) {			setentry entrycopy;			entrycopy.hash = entry->hash;			entrycopy.key = entry->key;			if (!_PyDict_Contains(other, entry->key, entry->hash)) {				if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {					Py_DECREF(result);					return NULL;				}			}		}		return result;	} 	while (set_next(so, &pos, &entry)) {		int rv = set_contains_entry((PySetObject *)other, entry);		if (rv == -1) {			Py_DECREF(result);			return NULL;		}		if (!rv) {			if (set_add_entry((PySetObject *)result, entry) == -1) {				Py_DECREF(result);				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){	PySetObject *otherset;	PyObject *key;	Py_ssize_t pos = 0;	setentry *entry; 	if ((PyObject *)so == other)		return set_clear(so); 	if (PyDict_CheckExact(other)) {		PyObject *value;		int rv;		long hash;		while (_PyDict_Next(other, &pos, &key, &value, &hash)) {			setentry an_entry; 			an_entry.hash = hash;			an_entry.key = key;			rv = set_discard_entry(so, &an_entry);			if (rv == -1)				return NULL;			if (rv == DISCARD_NOTFOUND) {				if (set_add_entry(so, &an_entry) == -1)					return NULL;			}		}		Py_RETURN_NONE;	} 	if (PyAnySet_Check(other)) {		Py_INCREF(other);		otherset = (PySetObject *)other;	} else {		otherset = (PySetObject *)make_new_set(so->ob_type, other);		if (otherset == NULL)			return NULL;	} 	while (set_next(otherset, &pos, &entry)) {		int rv = set_discard_entry(so, entry);

Lines 1001–1500 of 2312. Previous lines