dictobject.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef Py_DICTOBJECT_H
  2. #define Py_DICTOBJECT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /* Dictionary object type -- mapping from hashable object to object */
  7. /* The distribution includes a separate file, Objects/dictnotes.txt,
  8. describing explorations into dictionary design and optimization.
  9. It covers typical dictionary use patterns, the parameters for
  10. tuning dictionaries, and several ideas for possible optimizations.
  11. */
  12. #ifndef Py_LIMITED_API
  13. typedef struct _dictkeysobject PyDictKeysObject;
  14. /* The ma_values pointer is NULL for a combined table
  15. * or points to an array of PyObject* for a split table
  16. */
  17. typedef struct {
  18. PyObject_HEAD
  19. /* Number of items in the dictionary */
  20. Py_ssize_t ma_used;
  21. /* Dictionary version: globally unique, value change each time
  22. the dictionary is modified */
  23. uint64_t ma_version_tag;
  24. PyDictKeysObject *ma_keys;
  25. /* If ma_values is NULL, the table is "combined": keys and values
  26. are stored in ma_keys.
  27. If ma_values is not NULL, the table is splitted:
  28. keys are stored in ma_keys and values are stored in ma_values */
  29. PyObject **ma_values;
  30. } PyDictObject;
  31. typedef struct {
  32. PyObject_HEAD
  33. PyDictObject *dv_dict;
  34. } _PyDictViewObject;
  35. #endif /* Py_LIMITED_API */
  36. PyAPI_DATA(PyTypeObject) PyDict_Type;
  37. PyAPI_DATA(PyTypeObject) PyDictIterKey_Type;
  38. PyAPI_DATA(PyTypeObject) PyDictIterValue_Type;
  39. PyAPI_DATA(PyTypeObject) PyDictIterItem_Type;
  40. PyAPI_DATA(PyTypeObject) PyDictKeys_Type;
  41. PyAPI_DATA(PyTypeObject) PyDictItems_Type;
  42. PyAPI_DATA(PyTypeObject) PyDictValues_Type;
  43. #define PyDict_Check(op) \
  44. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS)
  45. #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)
  46. #define PyDictKeys_Check(op) PyObject_TypeCheck(op, &PyDictKeys_Type)
  47. #define PyDictItems_Check(op) PyObject_TypeCheck(op, &PyDictItems_Type)
  48. #define PyDictValues_Check(op) PyObject_TypeCheck(op, &PyDictValues_Type)
  49. /* This excludes Values, since they are not sets. */
  50. # define PyDictViewSet_Check(op) \
  51. (PyDictKeys_Check(op) || PyDictItems_Check(op))
  52. PyAPI_FUNC(PyObject *) PyDict_New(void);
  53. PyAPI_FUNC(PyObject *) PyDict_GetItem(PyObject *mp, PyObject *key);
  54. #ifndef Py_LIMITED_API
  55. PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
  56. Py_hash_t hash);
  57. #endif
  58. PyAPI_FUNC(PyObject *) PyDict_GetItemWithError(PyObject *mp, PyObject *key);
  59. #ifndef Py_LIMITED_API
  60. PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
  61. struct _Py_Identifier *key);
  62. PyAPI_FUNC(PyObject *) PyDict_SetDefault(
  63. PyObject *mp, PyObject *key, PyObject *defaultobj);
  64. #endif
  65. PyAPI_FUNC(int) PyDict_SetItem(PyObject *mp, PyObject *key, PyObject *item);
  66. #ifndef Py_LIMITED_API
  67. PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,
  68. PyObject *item, Py_hash_t hash);
  69. #endif
  70. PyAPI_FUNC(int) PyDict_DelItem(PyObject *mp, PyObject *key);
  71. #ifndef Py_LIMITED_API
  72. PyAPI_FUNC(int) _PyDict_DelItem_KnownHash(PyObject *mp, PyObject *key,
  73. Py_hash_t hash);
  74. PyAPI_FUNC(int) _PyDict_DelItemIf(PyObject *mp, PyObject *key,
  75. int (*predicate)(PyObject *value));
  76. #endif
  77. PyAPI_FUNC(void) PyDict_Clear(PyObject *mp);
  78. PyAPI_FUNC(int) PyDict_Next(
  79. PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value);
  80. #ifndef Py_LIMITED_API
  81. PyDictKeysObject *_PyDict_NewKeysForClass(void);
  82. PyAPI_FUNC(PyObject *) PyObject_GenericGetDict(PyObject *, void *);
  83. PyAPI_FUNC(int) _PyDict_Next(
  84. PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, Py_hash_t *hash);
  85. PyObject *_PyDictView_New(PyObject *, PyTypeObject *);
  86. #endif
  87. PyAPI_FUNC(PyObject *) PyDict_Keys(PyObject *mp);
  88. PyAPI_FUNC(PyObject *) PyDict_Values(PyObject *mp);
  89. PyAPI_FUNC(PyObject *) PyDict_Items(PyObject *mp);
  90. PyAPI_FUNC(Py_ssize_t) PyDict_Size(PyObject *mp);
  91. PyAPI_FUNC(PyObject *) PyDict_Copy(PyObject *mp);
  92. PyAPI_FUNC(int) PyDict_Contains(PyObject *mp, PyObject *key);
  93. #ifndef Py_LIMITED_API
  94. /* Get the number of items of a dictionary. */
  95. #define PyDict_GET_SIZE(mp) (assert(PyDict_Check(mp)),((PyDictObject *)mp)->ma_used)
  96. PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, Py_hash_t hash);
  97. PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
  98. PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);
  99. PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp);
  100. Py_ssize_t _PyDict_KeysSize(PyDictKeysObject *keys);
  101. PyAPI_FUNC(Py_ssize_t) _PyDict_SizeOf(PyDictObject *);
  102. PyAPI_FUNC(PyObject *) _PyDict_Pop(PyObject *, PyObject *, PyObject *);
  103. PyObject *_PyDict_Pop_KnownHash(PyObject *, PyObject *, Py_hash_t, PyObject *);
  104. PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
  105. #define _PyDict_HasSplitTable(d) ((d)->ma_values != NULL)
  106. PyAPI_FUNC(int) PyDict_ClearFreeList(void);
  107. #endif
  108. /* PyDict_Update(mp, other) is equivalent to PyDict_Merge(mp, other, 1). */
  109. PyAPI_FUNC(int) PyDict_Update(PyObject *mp, PyObject *other);
  110. /* PyDict_Merge updates/merges from a mapping object (an object that
  111. supports PyMapping_Keys() and PyObject_GetItem()). If override is true,
  112. the last occurrence of a key wins, else the first. The Python
  113. dict.update(other) is equivalent to PyDict_Merge(dict, other, 1).
  114. */
  115. PyAPI_FUNC(int) PyDict_Merge(PyObject *mp,
  116. PyObject *other,
  117. int override);
  118. #ifndef Py_LIMITED_API
  119. /* Like PyDict_Merge, but override can be 0, 1 or 2. If override is 0,
  120. the first occurrence of a key wins, if override is 1, the last occurrence
  121. of a key wins, if override is 2, a KeyError with conflicting key as
  122. argument is raised.
  123. */
  124. PyAPI_FUNC(int) _PyDict_MergeEx(PyObject *mp, PyObject *other, int override);
  125. PyAPI_FUNC(PyObject *) _PyDictView_Intersect(PyObject* self, PyObject *other);
  126. #endif
  127. /* PyDict_MergeFromSeq2 updates/merges from an iterable object producing
  128. iterable objects of length 2. If override is true, the last occurrence
  129. of a key wins, else the first. The Python dict constructor dict(seq2)
  130. is equivalent to dict={}; PyDict_MergeFromSeq(dict, seq2, 1).
  131. */
  132. PyAPI_FUNC(int) PyDict_MergeFromSeq2(PyObject *d,
  133. PyObject *seq2,
  134. int override);
  135. PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key);
  136. #ifndef Py_LIMITED_API
  137. PyAPI_FUNC(PyObject *) _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key);
  138. #endif /* !Py_LIMITED_API */
  139. PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
  140. #ifndef Py_LIMITED_API
  141. PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, struct _Py_Identifier *key, PyObject *item);
  142. #endif /* !Py_LIMITED_API */
  143. PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
  144. #ifndef Py_LIMITED_API
  145. PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, struct _Py_Identifier *key);
  146. PyAPI_FUNC(void) _PyDict_DebugMallocStats(FILE *out);
  147. int _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, PyObject *name, PyObject *value);
  148. PyObject *_PyDict_LoadGlobal(PyDictObject *, PyDictObject *, PyObject *);
  149. #endif
  150. #ifdef __cplusplus
  151. }
  152. #endif
  153. #endif /* !Py_DICTOBJECT_H */