ordereddict.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef Py_ORDEREDDICTOBJECT_H
  2. #define Py_ORDEREDDICTOBJECT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #if defined _MSC_VER || defined __CYGWIN__
  7. #undef PyAPI_FUNC
  8. #undef PyAPI_DATA
  9. #define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE
  10. #define PyAPI_DATA(RTYPE) __declspec(dllexport) RTYPE
  11. #endif
  12. /* Ordered Dictionary object implementation using a hash table and a vector of
  13. pointers to the items.
  14. */
  15. /*
  16. This file has been directly derived from dictobject.h in the Python 2.5.1
  17. source distribution. Its licensing therefore is governed by the license
  18. as distributed with Python 2.5.1 available in the
  19. file LICNESE in the source distribution of ordereddict
  20. Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software
  21. Foundation; All Rights Reserved"
  22. 2007-10-13: Anthon van der Neut
  23. */
  24. /* Dictionary object type -- mapping from hashable object to object */
  25. /* The distribution includes a separate file, Objects/dictnotes.txt,
  26. describing explorations into dictionary design and optimization.
  27. It covers typical dictionary use patterns, the parameters for
  28. tuning dictionaries, and several ideas for possible optimizations.
  29. */
  30. /*
  31. There are three kinds of slots in the table:
  32. 1. Unused. me_key == me_value == NULL
  33. Does not hold an active (key, value) pair now and never did. Unused can
  34. transition to Active upon key insertion. This is the only case in which
  35. me_key is NULL, and is each slot's initial state.
  36. 2. Active. me_key != NULL and me_key != dummy and me_value != NULL
  37. Holds an active (key, value) pair. Active can transition to Dummy upon
  38. key deletion. This is the only case in which me_value != NULL.
  39. 3. Dummy. me_key == dummy and me_value == NULL
  40. Previously held an active (key, value) pair, but that was deleted and an
  41. active pair has not yet overwritten the slot. Dummy can transition to
  42. Active upon key insertion. Dummy slots cannot be made Unused again
  43. (cannot have me_key set to NULL), else the probe sequence in case of
  44. collision would have no way to know they were once active.
  45. Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
  46. hold a search finger. The me_hash field of Unused or Dummy slots has no
  47. meaning otherwise.
  48. */
  49. #if PY_VERSION_HEX < 0x02050000
  50. #ifdef _MSC_VER
  51. typedef int Py_ssize_t;
  52. #else
  53. typedef ssize_t Py_ssize_t;
  54. #endif
  55. typedef Py_ssize_t (*lenfunc)(PyObject *);
  56. typedef intintargfunc ssizessizeargfunc;
  57. typedef intintobjargproc ssizessizeobjargproc;
  58. #define PyInt_FromSize_t(A) PyInt_FromLong((long) A)
  59. #endif
  60. /* PyOrderedDict_MINSIZE is the minimum size of a dictionary. This many slots are
  61. * allocated directly in the dict object (in the ma_smalltable member).
  62. * It must be a power of 2, and at least 4. 8 allows dicts with no more
  63. * than 5 active entries to live in ma_smalltable (and so avoid an
  64. * additional malloc); instrumentation suggested this suffices for the
  65. * majority of dicts (consisting mostly of usually-small instance dicts and
  66. * usually-small dicts created to pass keyword arguments).
  67. */
  68. #define PyOrderedDict_MINSIZE 8
  69. typedef struct {
  70. /* Cached hash code of me_key. Note that hash codes are C longs.
  71. * We have to use Py_ssize_t instead because dict_popitem() abuses
  72. * me_hash to hold a search finger.
  73. */
  74. Py_ssize_t me_hash;
  75. PyObject *me_key;
  76. PyObject *me_value;
  77. } PyOrderedDictEntry;
  78. /*
  79. To ensure the lookup algorithm terminates, there must be at least one Unused
  80. slot (NULL key) in the table.
  81. The value od_fill is the number of non-NULL keys (sum of Active and Dummy);
  82. ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL
  83. values == the number of Active items).
  84. To avoid slowing down lookups on a near-full table, we resize the table when
  85. it's two-thirds full.
  86. */
  87. typedef struct _ordereddictobject PyOrderedDictObject;
  88. struct _ordereddictobject {
  89. #if PY_MAJOR_VERSION < 3
  90. PyObject_HEAD
  91. #else
  92. PyObject_VAR_HEAD
  93. #endif
  94. Py_ssize_t od_fill; /* # Active + # Dummy */
  95. Py_ssize_t ma_used; /* # Active */
  96. /* The table contains ma_mask + 1 slots, and that's a power of 2.
  97. * We store the mask instead of the size because the mask is more
  98. * frequently needed.
  99. */
  100. Py_ssize_t ma_mask;
  101. /* ma_table points to ma_smalltable for small tables, else to
  102. * additional malloc'ed memory. ma_table is never NULL! This rule
  103. * saves repeated runtime null-tests in the workhorse getitem and
  104. * setitem calls.
  105. */
  106. PyOrderedDictEntry *ma_table;
  107. PyOrderedDictEntry *(*ma_lookup)(PyOrderedDictObject *mp, PyObject *key, long hash);
  108. PyOrderedDictEntry ma_smalltable[PyOrderedDict_MINSIZE];
  109. /* for small arrays, ordered table pointer points to small array of tables */
  110. PyOrderedDictEntry **od_otablep;
  111. PyOrderedDictEntry *ma_smallotablep[PyOrderedDict_MINSIZE];
  112. /* for storing kvio, relaxed bits */
  113. long od_state;
  114. };
  115. typedef struct _sorteddictobject PySortedDictObject;
  116. struct _sorteddictobject {
  117. struct _ordereddictobject od;
  118. PyObject *sd_cmp;
  119. PyObject *sd_key;
  120. PyObject *sd_value;
  121. };
  122. PyAPI_DATA(PyTypeObject) PyOrderedDict_Type;
  123. PyAPI_DATA(PyTypeObject) PySortedDict_Type;
  124. #if PY_VERSION_HEX >= 0x02070000
  125. PyAPI_DATA(PyTypeObject) PyOrderedDictIterKey_Type;
  126. PyAPI_DATA(PyTypeObject) PyOrderedDictIterValue_Type;
  127. PyAPI_DATA(PyTypeObject) PyOrderedDictIterItem_Type;
  128. #endif
  129. PyAPI_DATA(PyTypeObject) PyOrderedDictKeys_Type;
  130. PyAPI_DATA(PyTypeObject) PyOrderedDictItems_Type;
  131. PyAPI_DATA(PyTypeObject) PyOrderedDictValues_Type;
  132. #if PY_VERSION_HEX >= 0x02080000
  133. /* AvdN: this might need reviewing for > 2.7 */
  134. #define PyOrderedDict_Check(op) \
  135. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS)
  136. #define PySortedDict_Check(op) \
  137. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS)
  138. #define PyOrderedDict_CheckExact(op) (Py_TYPE(op) == &PyOrderedDict_Type)
  139. #define PySortedDict_CheckExact(op) (Py_TYPE(op) == &PySortedDict_Type)
  140. #else
  141. #define PyOrderedDict_Check(op) PyObject_TypeCheck(op, &PyOrderedDict_Type)
  142. #define PySortedDict_Check(op) PyObject_TypeCheck(op, &PySortedDict_Type)
  143. #define PyOrderedDict_CheckExact(op) ((op)->ob_type == &PyOrderedDict_Type)
  144. #define PySortedDict_CheckExact(op) ((op)->ob_type == &PySortedDict_Type)
  145. #endif
  146. PyAPI_FUNC(PyObject *) PyOrderedDict_New(void);
  147. PyAPI_FUNC(PyObject *) PyOrderedDict_GetItem(PyObject *mp, PyObject *key);
  148. PyAPI_FUNC(int) PyOrderedDict_SetItem(PyObject *mp, PyObject *key, PyObject *item);
  149. PyAPI_FUNC(int) PyOrderedDict_DelItem(PyObject *mp, PyObject *key);
  150. PyAPI_FUNC(void) PyOrderedDict_Clear(PyObject *mp);
  151. PyAPI_FUNC(int) PyOrderedDict_Next(
  152. PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value);
  153. PyAPI_FUNC(int) _PyOrderedDict_Next(
  154. PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, long *hash);
  155. PyAPI_FUNC(PyObject *) PyOrderedDict_Keys(PyObject *mp);
  156. PyAPI_FUNC(PyObject *) PyOrderedDict_Values(PyObject *mp);
  157. PyAPI_FUNC(PyObject *) PyOrderedDict_Items(PyObject *mp);
  158. PyAPI_FUNC(Py_ssize_t) PyOrderedDict_Size(PyObject *mp);
  159. PyAPI_FUNC(PyObject *) PyOrderedDict_Copy(PyObject *mp);
  160. PyAPI_FUNC(int) PyOrderedDict_Contains(PyObject *mp, PyObject *key);
  161. PyAPI_FUNC(int) _PyOrderedDict_Contains(PyObject *mp, PyObject *key, long hash);
  162. PyAPI_FUNC(PyObject *) _PyOrderedDict_NewPresized(Py_ssize_t minused);
  163. PyAPI_FUNC(void) _PyOrderedDict_MaybeUntrack(PyObject *mp);
  164. /* PyOrderedDict_Update(mp, other) is equivalent to PyOrderedDict_Merge(mp, other, 1). */
  165. PyAPI_FUNC(int) PyOrderedDict_Update(PyObject *mp, PyObject *other);
  166. /* PyOrderedDict_Merge updates/merges from a mapping object (an object that
  167. supports PyMapping_Keys() and PyObject_GetItem()). If override is true,
  168. the last occurrence of a key wins, else the first. The Python
  169. dict.update(other) is equivalent to PyOrderedDict_Merge(dict, other, 1).
  170. */
  171. PyAPI_FUNC(int) PyOrderedDict_Merge(PyObject *mp,
  172. PyObject *other,
  173. int override, int relaxed);
  174. /* PyOrderedDict_MergeFromSeq2 updates/merges from an iterable object producing
  175. iterable objects of length 2. If override is true, the last occurrence
  176. of a key wins, else the first. The Python dict constructor dict(seq2)
  177. is equivalent to dict={}; PyOrderedDict_MergeFromSeq(dict, seq2, 1).
  178. */
  179. PyAPI_FUNC(int) PyOrderedDict_MergeFromSeq2(PyObject *d,
  180. PyObject *seq2,
  181. int override);
  182. #ifdef __cplusplus
  183. }
  184. #endif
  185. #endif /* !Py_ORDEREDDICTOBJECT_H */