setobject.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef Py_CPYTHON_SETOBJECT_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. /* There are three kinds of entries in the table:
  5. 1. Unused: key == NULL and hash == 0
  6. 2. Dummy: key == dummy and hash == -1
  7. 3. Active: key != NULL and key != dummy and hash != -1
  8. The hash field of Unused slots is always zero.
  9. The hash field of Dummy slots are set to -1
  10. meaning that dummy entries can be detected by
  11. either entry->key==dummy or by entry->hash==-1.
  12. */
  13. #define PySet_MINSIZE 8
  14. typedef struct {
  15. PyObject *key;
  16. Py_hash_t hash; /* Cached hash code of the key */
  17. } setentry;
  18. /* The SetObject data structure is shared by set and frozenset objects.
  19. Invariant for sets:
  20. - hash is -1
  21. Invariants for frozensets:
  22. - data is immutable.
  23. - hash is the hash of the frozenset or -1 if not computed yet.
  24. */
  25. typedef struct {
  26. PyObject_HEAD
  27. Py_ssize_t fill; /* Number active and dummy entries*/
  28. Py_ssize_t used; /* Number active entries */
  29. /* The table contains mask + 1 slots, and that's a power of 2.
  30. * We store the mask instead of the size because the mask is more
  31. * frequently needed.
  32. */
  33. Py_ssize_t mask;
  34. /* The table points to a fixed-size smalltable for small tables
  35. * or to additional malloc'ed memory for bigger tables.
  36. * The table pointer is never NULL which saves us from repeated
  37. * runtime null-tests.
  38. */
  39. setentry *table;
  40. Py_hash_t hash; /* Only used by frozenset objects */
  41. Py_ssize_t finger; /* Search finger for pop() */
  42. setentry smalltable[PySet_MINSIZE];
  43. PyObject *weakreflist; /* List of weak references */
  44. } PySetObject;
  45. #define _PySet_CAST(so) \
  46. (assert(PyAnySet_Check(so)), _Py_CAST(PySetObject*, so))
  47. static inline Py_ssize_t PySet_GET_SIZE(PyObject *so) {
  48. return _PySet_CAST(so)->used;
  49. }
  50. #define PySet_GET_SIZE(so) PySet_GET_SIZE(_PyObject_CAST(so))
  51. PyAPI_DATA(PyObject *) _PySet_Dummy;
  52. PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash);
  53. PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);