Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_DICTOBJECT_H |
| 2 | #define Py_DICTOBJECT_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 2ec9031 | 1997-05-13 21:23:32 +0000 | [diff] [blame] | 8 | /* Dictionary object type -- mapping from hashable object to object */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | |
Raymond Hettinger | 930427b | 2003-05-03 06:51:59 +0000 | [diff] [blame] | 10 | /* The distribution includes a separate file, Objects/dictnotes.txt, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 11 | describing explorations into dictionary design and optimization. |
Raymond Hettinger | 930427b | 2003-05-03 06:51:59 +0000 | [diff] [blame] | 12 | It covers typical dictionary use patterns, the parameters for |
| 13 | tuning dictionaries, and several ideas for possible optimizations. |
| 14 | */ |
| 15 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 16 | /* |
| 17 | There are three kinds of slots in the table: |
| 18 | |
| 19 | 1. Unused. me_key == me_value == NULL |
| 20 | Does not hold an active (key, value) pair now and never did. Unused can |
| 21 | transition to Active upon key insertion. This is the only case in which |
| 22 | me_key is NULL, and is each slot's initial state. |
| 23 | |
| 24 | 2. Active. me_key != NULL and me_key != dummy and me_value != NULL |
| 25 | Holds an active (key, value) pair. Active can transition to Dummy upon |
| 26 | key deletion. This is the only case in which me_value != NULL. |
| 27 | |
| 28 | 3. Dummy. me_key == dummy and me_value == NULL |
| 29 | Previously held an active (key, value) pair, but that was deleted and an |
| 30 | active pair has not yet overwritten the slot. Dummy can transition to |
| 31 | Active upon key insertion. Dummy slots cannot be made Unused again |
| 32 | (cannot have me_key set to NULL), else the probe sequence in case of |
| 33 | collision would have no way to know they were once active. |
| 34 | |
| 35 | Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to |
| 36 | hold a search finger. The me_hash field of Unused or Dummy slots has no |
| 37 | meaning otherwise. |
| 38 | */ |
| 39 | |
| 40 | /* PyDict_MINSIZE is the minimum size of a dictionary. This many slots are |
| 41 | * allocated directly in the dict object (in the ma_smalltable member). |
| 42 | * It must be a power of 2, and at least 4. 8 allows dicts with no more |
| 43 | * than 5 active entries to live in ma_smalltable (and so avoid an |
| 44 | * additional malloc); instrumentation suggested this suffices for the |
| 45 | * majority of dicts (consisting mostly of usually-small instance dicts and |
| 46 | * usually-small dicts created to pass keyword arguments). |
| 47 | */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 48 | #ifndef Py_LIMITED_API |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 49 | #define PyDict_MINSIZE 8 |
| 50 | |
| 51 | typedef struct { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 52 | /* Cached hash code of me_key. */ |
| 53 | Py_hash_t me_hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | PyObject *me_key; |
| 55 | PyObject *me_value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 56 | } PyDictEntry; |
| 57 | |
| 58 | /* |
| 59 | To ensure the lookup algorithm terminates, there must be at least one Unused |
| 60 | slot (NULL key) in the table. |
| 61 | The value ma_fill is the number of non-NULL keys (sum of Active and Dummy); |
| 62 | ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL |
| 63 | values == the number of Active items). |
| 64 | To avoid slowing down lookups on a near-full table, we resize the table when |
| 65 | it's two-thirds full. |
| 66 | */ |
| 67 | typedef struct _dictobject PyDictObject; |
| 68 | struct _dictobject { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | PyObject_HEAD |
| 70 | Py_ssize_t ma_fill; /* # Active + # Dummy */ |
| 71 | Py_ssize_t ma_used; /* # Active */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 72 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | /* The table contains ma_mask + 1 slots, and that's a power of 2. |
| 74 | * We store the mask instead of the size because the mask is more |
| 75 | * frequently needed. |
| 76 | */ |
| 77 | Py_ssize_t ma_mask; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 78 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | /* ma_table points to ma_smalltable for small tables, else to |
| 80 | * additional malloc'ed memory. ma_table is never NULL! This rule |
| 81 | * saves repeated runtime null-tests in the workhorse getitem and |
| 82 | * setitem calls. |
| 83 | */ |
| 84 | PyDictEntry *ma_table; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 85 | PyDictEntry *(*ma_lookup)(PyDictObject *mp, PyObject *key, Py_hash_t hash); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | PyDictEntry ma_smalltable[PyDict_MINSIZE]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 87 | }; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 88 | #endif /* Py_LIMITED_API */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 89 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 90 | PyAPI_DATA(PyTypeObject) PyDict_Type; |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 91 | PyAPI_DATA(PyTypeObject) PyDictIterKey_Type; |
| 92 | PyAPI_DATA(PyTypeObject) PyDictIterValue_Type; |
| 93 | PyAPI_DATA(PyTypeObject) PyDictIterItem_Type; |
| 94 | PyAPI_DATA(PyTypeObject) PyDictKeys_Type; |
| 95 | PyAPI_DATA(PyTypeObject) PyDictItems_Type; |
| 96 | PyAPI_DATA(PyTypeObject) PyDictValues_Type; |
Guido van Rossum | 2ec9031 | 1997-05-13 21:23:32 +0000 | [diff] [blame] | 97 | |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 98 | #define PyDict_Check(op) \ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 99 | PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS) |
| 100 | #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type) |
| 101 | #define PyDictKeys_Check(op) (Py_TYPE(op) == &PyDictKeys_Type) |
| 102 | #define PyDictItems_Check(op) (Py_TYPE(op) == &PyDictItems_Type) |
| 103 | #define PyDictValues_Check(op) (Py_TYPE(op) == &PyDictValues_Type) |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 104 | /* This excludes Values, since they are not sets. */ |
| 105 | # define PyDictViewSet_Check(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | (PyDictKeys_Check(op) || PyDictItems_Check(op)) |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | 2ec9031 | 1997-05-13 21:23:32 +0000 | [diff] [blame] | 108 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 109 | PyAPI_FUNC(PyObject *) PyDict_New(void); |
| 110 | PyAPI_FUNC(PyObject *) PyDict_GetItem(PyObject *mp, PyObject *key); |
Alexandre Vassalotti | cb31433 | 2008-06-01 03:53:03 +0000 | [diff] [blame] | 111 | PyAPI_FUNC(PyObject *) PyDict_GetItemWithError(PyObject *mp, PyObject *key); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 112 | PyAPI_FUNC(int) PyDict_SetItem(PyObject *mp, PyObject *key, PyObject *item); |
| 113 | PyAPI_FUNC(int) PyDict_DelItem(PyObject *mp, PyObject *key); |
| 114 | PyAPI_FUNC(void) PyDict_Clear(PyObject *mp); |
| 115 | PyAPI_FUNC(int) PyDict_Next( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 117 | #ifndef Py_LIMITED_API |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 118 | PyAPI_FUNC(int) _PyDict_Next( |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 119 | PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, Py_hash_t *hash); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 120 | #endif |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 121 | PyAPI_FUNC(PyObject *) PyDict_Keys(PyObject *mp); |
| 122 | PyAPI_FUNC(PyObject *) PyDict_Values(PyObject *mp); |
| 123 | PyAPI_FUNC(PyObject *) PyDict_Items(PyObject *mp); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 124 | PyAPI_FUNC(Py_ssize_t) PyDict_Size(PyObject *mp); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 125 | PyAPI_FUNC(PyObject *) PyDict_Copy(PyObject *mp); |
Raymond Hettinger | bc0f2ab | 2003-11-25 21:12:14 +0000 | [diff] [blame] | 126 | PyAPI_FUNC(int) PyDict_Contains(PyObject *mp, PyObject *key); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 127 | #ifndef Py_LIMITED_API |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 128 | PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, Py_hash_t hash); |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 129 | PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused); |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 130 | PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp); |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 131 | PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp); |
Antoine Pitrou | 9a812cb | 2011-11-15 00:00:12 +0100 | [diff] [blame] | 132 | |
| 133 | PyAPI_FUNC(int) PyDict_ClearFreeList(void); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 134 | #endif |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 135 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 136 | /* PyDict_Update(mp, other) is equivalent to PyDict_Merge(mp, other, 1). */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 137 | PyAPI_FUNC(int) PyDict_Update(PyObject *mp, PyObject *other); |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 138 | |
| 139 | /* PyDict_Merge updates/merges from a mapping object (an object that |
| 140 | supports PyMapping_Keys() and PyObject_GetItem()). If override is true, |
| 141 | the last occurrence of a key wins, else the first. The Python |
| 142 | dict.update(other) is equivalent to PyDict_Merge(dict, other, 1). |
| 143 | */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 144 | PyAPI_FUNC(int) PyDict_Merge(PyObject *mp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | PyObject *other, |
| 146 | int override); |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 147 | |
| 148 | /* PyDict_MergeFromSeq2 updates/merges from an iterable object producing |
| 149 | iterable objects of length 2. If override is true, the last occurrence |
| 150 | of a key wins, else the first. The Python dict constructor dict(seq2) |
| 151 | is equivalent to dict={}; PyDict_MergeFromSeq(dict, seq2, 1). |
| 152 | */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 153 | PyAPI_FUNC(int) PyDict_MergeFromSeq2(PyObject *d, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | PyObject *seq2, |
| 155 | int override); |
Guido van Rossum | 2ec9031 | 1997-05-13 21:23:32 +0000 | [diff] [blame] | 156 | |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 157 | PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key); |
| 158 | PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item); |
| 159 | PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 161 | #ifdef __cplusplus |
| 162 | } |
| 163 | #endif |
| 164 | #endif /* !Py_DICTOBJECT_H */ |