setobject.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Set object interface */
  2. #ifndef Py_SETOBJECT_H
  3. #define Py_SETOBJECT_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /*
  8. There are three kinds of slots in the table:
  9. 1. Unused: key == NULL
  10. 2. Active: key != NULL and key != dummy
  11. 3. Dummy: key == dummy
  12. Note: .pop() abuses the hash field of an Unused or Dummy slot to
  13. hold a search finger. The hash field of Unused or Dummy slots has
  14. no meaning otherwise.
  15. */
  16. #define PySet_MINSIZE 8
  17. typedef struct {
  18. long hash; /* cached hash code for the entry key */
  19. PyObject *key;
  20. } setentry;
  21. /*
  22. This data structure is shared by set and frozenset objects.
  23. */
  24. typedef struct _setobject PySetObject;
  25. struct _setobject {
  26. PyObject_HEAD
  27. Py_ssize_t fill; /* # Active + # Dummy */
  28. Py_ssize_t used; /* # Active */
  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. /* table points to smalltable for small tables, else to
  35. * additional malloc'ed memory. table is never NULL! This rule
  36. * saves repeated runtime null-tests.
  37. */
  38. setentry *table;
  39. setentry *(*lookup)(PySetObject *so, PyObject *key, long hash);
  40. setentry smalltable[PySet_MINSIZE];
  41. long hash; /* only used by frozenset objects */
  42. PyObject *weakreflist; /* List of weak references */
  43. };
  44. PyAPI_DATA(PyTypeObject) PySet_Type;
  45. PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
  46. /* Invariants for frozensets:
  47. * data is immutable.
  48. * hash is the hash of the frozenset or -1 if not computed yet.
  49. * Invariants for sets:
  50. * hash is -1
  51. */
  52. #define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
  53. #define PyAnySet_CheckExact(ob) \
  54. (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
  55. #define PyAnySet_Check(ob) \
  56. (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
  57. PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
  58. PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
  59. #define PySet_Check(ob) \
  60. (Py_TYPE(ob) == &PySet_Type || \
  61. PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
  62. #define PyFrozenSet_Check(ob) \
  63. (Py_TYPE(ob) == &PyFrozenSet_Type || \
  64. PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
  65. PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
  66. PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
  67. PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
  68. #define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
  69. PyAPI_FUNC(int) PySet_Clear(PyObject *set);
  70. PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key);
  71. PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key);
  72. PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key);
  73. PyAPI_FUNC(int) _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key);
  74. PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash);
  75. PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
  76. PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
  77. #ifdef __cplusplus
  78. }
  79. #endif
  80. #endif /* !Py_SETOBJECT_H */