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