blob: ba90aaf676c55cfb47786642f84424096b8b9f88 [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_DICTOBJECT_H
2#define Py_DICTOBJECT_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Guido van Rossumf70e43a1991-02-19 12:39:46 +00007
Guido van Rossum2ec90311997-05-13 21:23:32 +00008/* Dictionary object type -- mapping from hashable object to object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009
Raymond Hettinger930427b2003-05-03 06:51:59 +000010/* The distribution includes a separate file, Objects/dictnotes.txt,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000011 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +000012 It covers typical dictionary use patterns, the parameters for
13 tuning dictionaries, and several ideas for possible optimizations.
14*/
15
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000016#ifndef Py_LIMITED_API
Tim Peters6d6c1a32001-08-02 04:15:00 +000017
Benjamin Peterson7d95e402012-04-23 11:24:50 -040018typedef struct _dictkeysobject PyDictKeysObject;
19
20/* The ma_values pointer is NULL for a combined table
21 * or points to an array of PyObject* for a split table
22 */
Tim Peters6d6c1a32001-08-02 04:15:00 +000023typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 PyObject_HEAD
Benjamin Peterson7d95e402012-04-23 11:24:50 -040025 Py_ssize_t ma_used;
26 PyDictKeysObject *ma_keys;
27 PyObject **ma_values;
28} PyDictObject;
Tim Peters6d6c1a32001-08-02 04:15:00 +000029
Eric Snow96c6af92015-05-29 22:21:39 -060030typedef struct {
31 PyObject_HEAD
32 PyDictObject *dv_dict;
33} _PyDictViewObject;
34
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000035#endif /* Py_LIMITED_API */
Tim Peters6d6c1a32001-08-02 04:15:00 +000036
Mark Hammond91a681d2002-08-12 07:21:58 +000037PyAPI_DATA(PyTypeObject) PyDict_Type;
Christian Heimesa22e8bd2007-11-29 22:35:39 +000038PyAPI_DATA(PyTypeObject) PyDictIterKey_Type;
39PyAPI_DATA(PyTypeObject) PyDictIterValue_Type;
40PyAPI_DATA(PyTypeObject) PyDictIterItem_Type;
41PyAPI_DATA(PyTypeObject) PyDictKeys_Type;
42PyAPI_DATA(PyTypeObject) PyDictItems_Type;
43PyAPI_DATA(PyTypeObject) PyDictValues_Type;
Guido van Rossum2ec90311997-05-13 21:23:32 +000044
Thomas Wouters27d517b2007-02-25 20:39:11 +000045#define PyDict_Check(op) \
Christian Heimes90aa7642007-12-19 02:45:37 +000046 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS)
47#define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)
Eric Snow8c7ed012015-05-30 09:29:53 -060048#define PyDictKeys_Check(op) PyObject_TypeCheck(op, &PyDictKeys_Type)
49#define PyDictItems_Check(op) PyObject_TypeCheck(op, &PyDictItems_Type)
50#define PyDictValues_Check(op) PyObject_TypeCheck(op, &PyDictValues_Type)
Christian Heimesa22e8bd2007-11-29 22:35:39 +000051/* This excludes Values, since they are not sets. */
52# define PyDictViewSet_Check(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 (PyDictKeys_Check(op) || PyDictItems_Check(op))
Christian Heimesa22e8bd2007-11-29 22:35:39 +000054
Guido van Rossum2ec90311997-05-13 21:23:32 +000055
Mark Hammond91a681d2002-08-12 07:21:58 +000056PyAPI_FUNC(PyObject *) PyDict_New(void);
57PyAPI_FUNC(PyObject *) PyDict_GetItem(PyObject *mp, PyObject *key);
Benjamin Peterson39992d32014-05-03 19:39:15 -040058#ifndef Py_LIMITED_API
Raymond Hettinger4b74fba2014-05-03 16:32:11 -070059PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
60 Py_hash_t hash);
Benjamin Peterson39992d32014-05-03 19:39:15 -040061#endif
Alexandre Vassalotticb314332008-06-01 03:53:03 +000062PyAPI_FUNC(PyObject *) PyDict_GetItemWithError(PyObject *mp, PyObject *key);
Brett Cannonfd074152012-04-14 14:10:13 -040063PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
64 struct _Py_Identifier *key);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010065#ifndef Py_LIMITED_API
Benjamin Peterson00e98862013-03-07 22:16:29 -050066PyAPI_FUNC(PyObject *) PyDict_SetDefault(
67 PyObject *mp, PyObject *key, PyObject *defaultobj);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010068#endif
Mark Hammond91a681d2002-08-12 07:21:58 +000069PyAPI_FUNC(int) PyDict_SetItem(PyObject *mp, PyObject *key, PyObject *item);
Benjamin Peterson39992d32014-05-03 19:39:15 -040070#ifndef Py_LIMITED_API
Raymond Hettinger4b74fba2014-05-03 16:32:11 -070071PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,
72 PyObject *item, Py_hash_t hash);
Benjamin Peterson39992d32014-05-03 19:39:15 -040073#endif
Mark Hammond91a681d2002-08-12 07:21:58 +000074PyAPI_FUNC(int) PyDict_DelItem(PyObject *mp, PyObject *key);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +030075#ifndef Py_LIMITED_API
76PyAPI_FUNC(int) _PyDict_DelItem_KnownHash(PyObject *mp, PyObject *key,
77 Py_hash_t hash);
78#endif
Mark Hammond91a681d2002-08-12 07:21:58 +000079PyAPI_FUNC(void) PyDict_Clear(PyObject *mp);
80PyAPI_FUNC(int) PyDict_Next(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000082#ifndef Py_LIMITED_API
Benjamin Peterson7d95e402012-04-23 11:24:50 -040083PyDictKeysObject *_PyDict_NewKeysForClass(void);
84PyAPI_FUNC(PyObject *) PyObject_GenericGetDict(PyObject *, void *);
Thomas Wouterscf297e42007-02-23 15:07:44 +000085PyAPI_FUNC(int) _PyDict_Next(
Benjamin Peterson8f67d082010-10-17 20:54:53 +000086 PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, Py_hash_t *hash);
Eric Snow96c6af92015-05-29 22:21:39 -060087PyObject *_PyDictView_New(PyObject *, PyTypeObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000088#endif
Mark Hammond91a681d2002-08-12 07:21:58 +000089PyAPI_FUNC(PyObject *) PyDict_Keys(PyObject *mp);
90PyAPI_FUNC(PyObject *) PyDict_Values(PyObject *mp);
91PyAPI_FUNC(PyObject *) PyDict_Items(PyObject *mp);
Martin v. Löwis18e16552006-02-15 17:27:45 +000092PyAPI_FUNC(Py_ssize_t) PyDict_Size(PyObject *mp);
Mark Hammond91a681d2002-08-12 07:21:58 +000093PyAPI_FUNC(PyObject *) PyDict_Copy(PyObject *mp);
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +000094PyAPI_FUNC(int) PyDict_Contains(PyObject *mp, PyObject *key);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000095#ifndef Py_LIMITED_API
Benjamin Peterson8f67d082010-10-17 20:54:53 +000096PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, Py_hash_t hash);
Christian Heimes99170a52007-12-19 02:07:34 +000097PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
Antoine Pitrou3a652b12009-03-23 18:52:06 +000098PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);
Benjamin Petersonfb886362010-04-24 18:21:17 +000099PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +0200100Py_ssize_t _PyDict_KeysSize(PyDictKeysObject *keys);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +0200101Py_ssize_t _PyDict_SizeOf(PyDictObject *);
Eric Snow96c6af92015-05-29 22:21:39 -0600102PyObject *_PyDict_Pop(PyDictObject *, PyObject *, PyObject *);
103PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400104#define _PyDict_HasSplitTable(d) ((d)->ma_values != NULL)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100105
106PyAPI_FUNC(int) PyDict_ClearFreeList(void);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000107#endif
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000108
Tim Petersf582b822001-12-11 18:51:08 +0000109/* PyDict_Update(mp, other) is equivalent to PyDict_Merge(mp, other, 1). */
Mark Hammond91a681d2002-08-12 07:21:58 +0000110PyAPI_FUNC(int) PyDict_Update(PyObject *mp, PyObject *other);
Tim Petersf582b822001-12-11 18:51:08 +0000111
112/* PyDict_Merge updates/merges from a mapping object (an object that
113 supports PyMapping_Keys() and PyObject_GetItem()). If override is true,
114 the last occurrence of a key wins, else the first. The Python
115 dict.update(other) is equivalent to PyDict_Merge(dict, other, 1).
116*/
Mark Hammond91a681d2002-08-12 07:21:58 +0000117PyAPI_FUNC(int) PyDict_Merge(PyObject *mp,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 PyObject *other,
119 int override);
Tim Petersf582b822001-12-11 18:51:08 +0000120
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400121#ifndef Py_LIMITED_API
122PyAPI_FUNC(PyObject *) _PyDictView_Intersect(PyObject* self, PyObject *other);
123#endif
124
Tim Petersf582b822001-12-11 18:51:08 +0000125/* PyDict_MergeFromSeq2 updates/merges from an iterable object producing
126 iterable objects of length 2. If override is true, the last occurrence
127 of a key wins, else the first. The Python dict constructor dict(seq2)
128 is equivalent to dict={}; PyDict_MergeFromSeq(dict, seq2, 1).
129*/
Mark Hammond91a681d2002-08-12 07:21:58 +0000130PyAPI_FUNC(int) PyDict_MergeFromSeq2(PyObject *d,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 PyObject *seq2,
132 int override);
Guido van Rossum2ec90311997-05-13 21:23:32 +0000133
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +0000134PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key);
Victor Stinner3c1e4812012-03-26 22:10:51 +0200135PyAPI_FUNC(PyObject *) _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key);
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +0000136PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
Victor Stinner3c1e4812012-03-26 22:10:51 +0200137PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, struct _Py_Identifier *key, PyObject *item);
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +0000138PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400140#ifndef Py_LIMITED_API
Victor Stinner8ea86502013-11-08 14:07:11 +0100141PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, struct _Py_Identifier *key);
142PyAPI_FUNC(void) _PyDict_DebugMallocStats(FILE *out);
143
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400144int _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, PyObject *name, PyObject *value);
145PyObject *_PyDict_LoadGlobal(PyDictObject *, PyDictObject *, PyObject *);
146#endif
147
Guido van Rossuma3309961993-07-28 09:05:47 +0000148#ifdef __cplusplus
149}
150#endif
151#endif /* !Py_DICTOBJECT_H */