Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.5 KiB

  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. /*
  13. There are three kinds of slots in the table:
  14. 1. Unused. me_key == me_value == NULL
  15. Does not hold an active (key, value) pair now and never did. Unused can
  16. transition to Active upon key insertion. This is the only case in which
  17. me_key is NULL, and is each slot's initial state.
  18. 2. Active. me_key != NULL and me_key != dummy and me_value != NULL
  19. Holds an active (key, value) pair. Active can transition to Dummy upon
  20. key deletion. This is the only case in which me_value != NULL.
  21. 3. Dummy. me_key == dummy and me_value == NULL
  22. Previously held an active (key, value) pair, but that was deleted and an
  23. active pair has not yet overwritten the slot. Dummy can transition to
  24. Active upon key insertion. Dummy slots cannot be made Unused again
  25. (cannot have me_key set to NULL), else the probe sequence in case of
  26. collision would have no way to know they were once active.
  27. Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
  28. hold a search finger. The me_hash field of Unused or Dummy slots has no
  29. meaning otherwise.
  30. */
  31. /* PyDict_MINSIZE is the minimum size of a dictionary. This many slots are
  32. * allocated directly in the dict object (in the ma_smalltable member).
  33. * It must be a power of 2, and at least 4. 8 allows dicts with no more
  34. * than 5 active entries to live in ma_smalltable (and so avoid an
  35. * additional malloc); instrumentation suggested this suffices for the
  36. * majority of dicts (consisting mostly of usually-small instance dicts and
  37. * usually-small dicts created to pass keyword arguments).
  38. */
  39. #define PyDict_MINSIZE 8
  40. typedef struct {
  41. /* Cached hash code of me_key. Note that hash codes are C longs.
  42. * We have to use Py_ssize_t instead because dict_popitem() abuses
  43. * me_hash to hold a search finger.
  44. */
  45. Py_ssize_t me_hash;
  46. PyObject *me_key;
  47. PyObject *me_value;
  48. } PyDictEntry;
  49. /*
  50. To ensure the lookup algorithm terminates, there must be at least one Unused
  51. slot (NULL key) in the table.
  52. The value ma_fill is the number of non-NULL keys (sum of Active and Dummy);
  53. ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL
  54. values == the number of Active items).
  55. To avoid slowing down lookups on a near-full table, we resize the table when
  56. it's two-thirds full.
  57. */
  58. typedef struct _dictobject PyDictObject;
  59. struct _dictobject {
  60. PyObject_HEAD
  61. Py_ssize_t ma_fill; /* # Active + # Dummy */
  62. Py_ssize_t ma_used; /* # Active */
  63. /* The table contains ma_mask + 1 slots, and that's a power of 2.
  64. * We store the mask instead of the size because the mask is more
  65. * frequently needed.
  66. */
  67. Py_ssize_t ma_mask;
  68. /* ma_table points to ma_smalltable for small tables, else to
  69. * additional malloc'ed memory. ma_table is never NULL! This rule
  70. * saves repeated runtime null-tests in the workhorse getitem and
  71. * setitem calls.
  72. */
  73. PyDictEntry *ma_table;
  74. PyDictEntry *(*ma_lookup)(PyDictObject *mp, PyObject *key, long hash);
  75. PyDictEntry ma_smalltable[PyDict_MINSIZE];
  76. };
  77. PyAPI_DATA(PyTypeObject) PyDict_Type;
  78. #define PyDict_Check(op) PyObject_TypeCheck(op, &PyDict_Type)
  79. #define PyDict_CheckExact(op) ((op)->ob_type == &PyDict_Type)
  80. PyAPI_FUNC(PyObject *) PyDict_New(void);
  81. PyAPI_FUNC(PyObject *) PyDict_GetItem(PyObject *mp, PyObject *key);
  82. PyAPI_FUNC(int) PyDict_SetItem(PyObject *mp, PyObject *key, PyObject *item);
  83. PyAPI_FUNC(int) PyDict_DelItem(PyObject *mp, PyObject *key);
  84. PyAPI_FUNC(void) PyDict_Clear(PyObject *mp);
  85. PyAPI_FUNC(int) PyDict_Next(
  86. PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value);
  87. PyAPI_FUNC(int) _PyDict_Next(
  88. PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, long *hash);
  89. PyAPI_FUNC(PyObject *) PyDict_Keys(PyObject *mp);
  90. PyAPI_FUNC(PyObject *) PyDict_Values(PyObject *mp);
  91. PyAPI_FUNC(PyObject *) PyDict_Items(PyObject *mp);
  92. PyAPI_FUNC(Py_ssize_t) PyDict_Size(PyObject *mp);
  93. PyAPI_FUNC(PyObject *) PyDict_Copy(PyObject *mp);
  94. PyAPI_FUNC(int) PyDict_Contains(PyObject *mp, PyObject *key);
  95. PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, long hash);
  96. /* PyDict_Update(mp, other) is equivalent to PyDict_Merge(mp, other, 1). */
  97. PyAPI_FUNC(int) PyDict_Update(PyObject *mp, PyObject *other);
  98. /* PyDict_Merge updates/merges from a mapping object (an object that
  99. supports PyMapping_Keys() and PyObject_GetItem()). If override is true,
  100. the last occurrence of a key wins, else the first. The Python
  101. dict.update(other) is equivalent to PyDict_Merge(dict, other, 1).
  102. */
  103. PyAPI_FUNC(int) PyDict_Merge(PyObject *mp,
  104. PyObject *other,
  105. int override);
  106. /* PyDict_MergeFromSeq2 updates/merges from an iterable object producing
  107. iterable objects of length 2. If override is true, the last occurrence
  108. of a key wins, else the first. The Python dict constructor dict(seq2)
  109. is equivalent to dict={}; PyDict_MergeFromSeq(dict, seq2, 1).
  110. */
  111. PyAPI_FUNC(int) PyDict_MergeFromSeq2(PyObject *d,
  112. PyObject *seq2,
  113. int override);
  114. PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key);
  115. PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
  116. PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif /* !Py_DICTOBJECT_H */