annotate DEPENDENCIES/mingw32/Python27/include/dictobject.h @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2a2c65a20a8b
children
rev   line source
Chris@87 1 #ifndef Py_DICTOBJECT_H
Chris@87 2 #define Py_DICTOBJECT_H
Chris@87 3 #ifdef __cplusplus
Chris@87 4 extern "C" {
Chris@87 5 #endif
Chris@87 6
Chris@87 7
Chris@87 8 /* Dictionary object type -- mapping from hashable object to object */
Chris@87 9
Chris@87 10 /* The distribution includes a separate file, Objects/dictnotes.txt,
Chris@87 11 describing explorations into dictionary design and optimization.
Chris@87 12 It covers typical dictionary use patterns, the parameters for
Chris@87 13 tuning dictionaries, and several ideas for possible optimizations.
Chris@87 14 */
Chris@87 15
Chris@87 16 /*
Chris@87 17 There are three kinds of slots in the table:
Chris@87 18
Chris@87 19 1. Unused. me_key == me_value == NULL
Chris@87 20 Does not hold an active (key, value) pair now and never did. Unused can
Chris@87 21 transition to Active upon key insertion. This is the only case in which
Chris@87 22 me_key is NULL, and is each slot's initial state.
Chris@87 23
Chris@87 24 2. Active. me_key != NULL and me_key != dummy and me_value != NULL
Chris@87 25 Holds an active (key, value) pair. Active can transition to Dummy upon
Chris@87 26 key deletion. This is the only case in which me_value != NULL.
Chris@87 27
Chris@87 28 3. Dummy. me_key == dummy and me_value == NULL
Chris@87 29 Previously held an active (key, value) pair, but that was deleted and an
Chris@87 30 active pair has not yet overwritten the slot. Dummy can transition to
Chris@87 31 Active upon key insertion. Dummy slots cannot be made Unused again
Chris@87 32 (cannot have me_key set to NULL), else the probe sequence in case of
Chris@87 33 collision would have no way to know they were once active.
Chris@87 34
Chris@87 35 Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
Chris@87 36 hold a search finger. The me_hash field of Unused or Dummy slots has no
Chris@87 37 meaning otherwise.
Chris@87 38 */
Chris@87 39
Chris@87 40 /* PyDict_MINSIZE is the minimum size of a dictionary. This many slots are
Chris@87 41 * allocated directly in the dict object (in the ma_smalltable member).
Chris@87 42 * It must be a power of 2, and at least 4. 8 allows dicts with no more
Chris@87 43 * than 5 active entries to live in ma_smalltable (and so avoid an
Chris@87 44 * additional malloc); instrumentation suggested this suffices for the
Chris@87 45 * majority of dicts (consisting mostly of usually-small instance dicts and
Chris@87 46 * usually-small dicts created to pass keyword arguments).
Chris@87 47 */
Chris@87 48 #define PyDict_MINSIZE 8
Chris@87 49
Chris@87 50 typedef struct {
Chris@87 51 /* Cached hash code of me_key. Note that hash codes are C longs.
Chris@87 52 * We have to use Py_ssize_t instead because dict_popitem() abuses
Chris@87 53 * me_hash to hold a search finger.
Chris@87 54 */
Chris@87 55 Py_ssize_t me_hash;
Chris@87 56 PyObject *me_key;
Chris@87 57 PyObject *me_value;
Chris@87 58 } PyDictEntry;
Chris@87 59
Chris@87 60 /*
Chris@87 61 To ensure the lookup algorithm terminates, there must be at least one Unused
Chris@87 62 slot (NULL key) in the table.
Chris@87 63 The value ma_fill is the number of non-NULL keys (sum of Active and Dummy);
Chris@87 64 ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL
Chris@87 65 values == the number of Active items).
Chris@87 66 To avoid slowing down lookups on a near-full table, we resize the table when
Chris@87 67 it's two-thirds full.
Chris@87 68 */
Chris@87 69 typedef struct _dictobject PyDictObject;
Chris@87 70 struct _dictobject {
Chris@87 71 PyObject_HEAD
Chris@87 72 Py_ssize_t ma_fill; /* # Active + # Dummy */
Chris@87 73 Py_ssize_t ma_used; /* # Active */
Chris@87 74
Chris@87 75 /* The table contains ma_mask + 1 slots, and that's a power of 2.
Chris@87 76 * We store the mask instead of the size because the mask is more
Chris@87 77 * frequently needed.
Chris@87 78 */
Chris@87 79 Py_ssize_t ma_mask;
Chris@87 80
Chris@87 81 /* ma_table points to ma_smalltable for small tables, else to
Chris@87 82 * additional malloc'ed memory. ma_table is never NULL! This rule
Chris@87 83 * saves repeated runtime null-tests in the workhorse getitem and
Chris@87 84 * setitem calls.
Chris@87 85 */
Chris@87 86 PyDictEntry *ma_table;
Chris@87 87 PyDictEntry *(*ma_lookup)(PyDictObject *mp, PyObject *key, long hash);
Chris@87 88 PyDictEntry ma_smalltable[PyDict_MINSIZE];
Chris@87 89 };
Chris@87 90
Chris@87 91 PyAPI_DATA(PyTypeObject) PyDict_Type;
Chris@87 92 PyAPI_DATA(PyTypeObject) PyDictIterKey_Type;
Chris@87 93 PyAPI_DATA(PyTypeObject) PyDictIterValue_Type;
Chris@87 94 PyAPI_DATA(PyTypeObject) PyDictIterItem_Type;
Chris@87 95 PyAPI_DATA(PyTypeObject) PyDictKeys_Type;
Chris@87 96 PyAPI_DATA(PyTypeObject) PyDictItems_Type;
Chris@87 97 PyAPI_DATA(PyTypeObject) PyDictValues_Type;
Chris@87 98
Chris@87 99 #define PyDict_Check(op) \
Chris@87 100 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS)
Chris@87 101 #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)
Chris@87 102 #define PyDictKeys_Check(op) (Py_TYPE(op) == &PyDictKeys_Type)
Chris@87 103 #define PyDictItems_Check(op) (Py_TYPE(op) == &PyDictItems_Type)
Chris@87 104 #define PyDictValues_Check(op) (Py_TYPE(op) == &PyDictValues_Type)
Chris@87 105 /* This excludes Values, since they are not sets. */
Chris@87 106 # define PyDictViewSet_Check(op) \
Chris@87 107 (PyDictKeys_Check(op) || PyDictItems_Check(op))
Chris@87 108
Chris@87 109 PyAPI_FUNC(PyObject *) PyDict_New(void);
Chris@87 110 PyAPI_FUNC(PyObject *) PyDict_GetItem(PyObject *mp, PyObject *key);
Chris@87 111 PyAPI_FUNC(int) PyDict_SetItem(PyObject *mp, PyObject *key, PyObject *item);
Chris@87 112 PyAPI_FUNC(int) PyDict_DelItem(PyObject *mp, PyObject *key);
Chris@87 113 PyAPI_FUNC(void) PyDict_Clear(PyObject *mp);
Chris@87 114 PyAPI_FUNC(int) PyDict_Next(
Chris@87 115 PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value);
Chris@87 116 PyAPI_FUNC(int) _PyDict_Next(
Chris@87 117 PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, long *hash);
Chris@87 118 PyAPI_FUNC(PyObject *) PyDict_Keys(PyObject *mp);
Chris@87 119 PyAPI_FUNC(PyObject *) PyDict_Values(PyObject *mp);
Chris@87 120 PyAPI_FUNC(PyObject *) PyDict_Items(PyObject *mp);
Chris@87 121 PyAPI_FUNC(Py_ssize_t) PyDict_Size(PyObject *mp);
Chris@87 122 PyAPI_FUNC(PyObject *) PyDict_Copy(PyObject *mp);
Chris@87 123 PyAPI_FUNC(int) PyDict_Contains(PyObject *mp, PyObject *key);
Chris@87 124 PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, long hash);
Chris@87 125 PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
Chris@87 126 PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);
Chris@87 127
Chris@87 128 /* PyDict_Update(mp, other) is equivalent to PyDict_Merge(mp, other, 1). */
Chris@87 129 PyAPI_FUNC(int) PyDict_Update(PyObject *mp, PyObject *other);
Chris@87 130
Chris@87 131 /* PyDict_Merge updates/merges from a mapping object (an object that
Chris@87 132 supports PyMapping_Keys() and PyObject_GetItem()). If override is true,
Chris@87 133 the last occurrence of a key wins, else the first. The Python
Chris@87 134 dict.update(other) is equivalent to PyDict_Merge(dict, other, 1).
Chris@87 135 */
Chris@87 136 PyAPI_FUNC(int) PyDict_Merge(PyObject *mp,
Chris@87 137 PyObject *other,
Chris@87 138 int override);
Chris@87 139
Chris@87 140 /* PyDict_MergeFromSeq2 updates/merges from an iterable object producing
Chris@87 141 iterable objects of length 2. If override is true, the last occurrence
Chris@87 142 of a key wins, else the first. The Python dict constructor dict(seq2)
Chris@87 143 is equivalent to dict={}; PyDict_MergeFromSeq(dict, seq2, 1).
Chris@87 144 */
Chris@87 145 PyAPI_FUNC(int) PyDict_MergeFromSeq2(PyObject *d,
Chris@87 146 PyObject *seq2,
Chris@87 147 int override);
Chris@87 148
Chris@87 149 PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key);
Chris@87 150 PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
Chris@87 151 PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
Chris@87 152
Chris@87 153 #ifdef __cplusplus
Chris@87 154 }
Chris@87 155 #endif
Chris@87 156 #endif /* !Py_DICTOBJECT_H */