annotate Objects/dictobject.c @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents c043901fa1fe
children 77fb880eca49
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
2059
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2056
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2056
diff changeset
3 The Netherlands.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
8 documentation for any purpose and without fee is hereby granted,
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
10 both that copyright notice and this permission notice appear in
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
15 permission.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ******************************************************************/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
32 /* Dictionaru object implementation using a hash table */
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
33
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
34 #include "Python.h"
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 /*
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
38 * MINSIZE is the minimum size of a dictionary.
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
39 */
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
40
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
41 #define MINSIZE 4
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
42
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
43 /*
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
44 Table of irreducible polynomials to efficiently cycle through
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
45 GF(2^n)-{0}, 2<=n<=30.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 */
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
47 static long polys[] = {
5056
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
48 4 + 3,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
49 8 + 3,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
50 16 + 3,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
51 32 + 5,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
52 64 + 3,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
53 128 + 3,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
54 256 + 29,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
55 512 + 17,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
56 1024 + 9,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
57 2048 + 5,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
58 4096 + 83,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
59 8192 + 27,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
60 16384 + 43,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
61 32768 + 3,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
62 65536 + 45,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
63 131072 + 9,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
64 262144 + 39,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
65 524288 + 39,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
66 1048576 + 9,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
67 2097152 + 5,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
68 4194304 + 3,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
69 8388608 + 33,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
70 16777216 + 27,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
71 33554432 + 9,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
72 67108864 + 71,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
73 134217728 + 39,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
74 268435456 + 9,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
75 536870912 + 5,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
76 1073741824 + 83,
e629c88f3dd8 Final three poly table entries corrected by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5053
diff changeset
77 0
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 /* Object used as dummy key to fill deleted entries */
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
81 static PyObject *dummy; /* Initialized by first call to newdictobject() */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 Invariant for entries: when in use, de_value is not NULL and de_key is
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 not NULL and not dummy; when not in use, de_value is NULL and de_key
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 is either NULL or dummy. A dummy key value cannot be replaced by
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 NULL, since otherwise other keys may be lost.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 typedef struct {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 long me_hash;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
91 PyObject *me_key;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
92 PyObject *me_value;
5310
192e2e0cbce5 (Jack:) Align mapping entries to 4-words if USE_CACHE_ALIGNED is defined.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
93 #ifdef USE_CACHE_ALIGNED
192e2e0cbce5 (Jack:) Align mapping entries to 4-words if USE_CACHE_ALIGNED is defined.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
94 long aligner;
192e2e0cbce5 (Jack:) Align mapping entries to 4-words if USE_CACHE_ALIGNED is defined.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
95 #endif
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
96 } dictentry;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 To ensure the lookup algorithm terminates, the table size must be a
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 prime number and there must be at least one NULL key in the table.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 The value ma_fill is the number of non-NULL keys; ma_used is the number
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 of non-NULL, non-dummy keys.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 To avoid slowing down lookups on a near-full table, we resize the table
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 when it is more than half filled.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 typedef struct {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
107 PyObject_HEAD
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 int ma_fill;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 int ma_used;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 int ma_size;
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
111 int ma_poly;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
112 dictentry *ma_table;
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
113 } dictobject;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
115 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
116 PyDict_New()
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 {
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
118 register dictobject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 if (dummy == NULL) { /* Auto-initialize dummy */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
120 dummy = PyString_FromString("<dummy key>");
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 if (dummy == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
124 mp = PyObject_NEW(dictobject, &PyDict_Type);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 if (mp == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 return NULL;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
127 mp->ma_size = 0;
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
128 mp->ma_poly = 0;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
129 mp->ma_table = NULL;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 mp->ma_fill = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 mp->ma_used = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
132 return (PyObject *)mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 The basic lookup function used by all operations.
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
137 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 Open addressing is preferred over chaining since the link overhead for
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 chaining would be substantial (100% with typical malloc overhead).
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
140 However, instead of going through the table at constant steps, we cycle
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
141 through the values of GF(2^n)-{0}. This avoids modulo computations, being
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
142 much cheaper on RISC machines, without leading to clustering.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 First a 32-bit hash value, 'sum', is computed from the key string.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 The first character is added an extra time shifted by 8 to avoid hashing
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 single-character keys (often heavily used variables) too close together.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 All arithmetic on sum should ignore overflow.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 The initial probe index is then computed as sum mod the table size.
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
150 Subsequent probe indices use the values of x^i in GF(2^n) as an offset,
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
151 where x is a root. The initial value is derived from sum, too.
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
152
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
153 (This version is due to Reimer Behrends, some ideas are also due to
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
154 Jyrki Alakuijala.)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 */
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
156 static dictentry *lookdict Py_PROTO((dictobject *, PyObject *, long));
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
157 static dictentry *
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
158 lookdict(mp, key, hash)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
159 dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
160 PyObject *key;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 long hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 {
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
163 register int i;
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
164 register unsigned incr;
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
165 register unsigned long sum = (unsigned long) hash;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
166 register dictentry *freeslot = NULL;
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5209
diff changeset
167 register unsigned int mask = mp->ma_size-1;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
168 dictentry *ep0 = mp->ma_table;
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
169 register dictentry *ep;
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
170 /* We must come up with (i, incr) such that 0 <= i < ma_size
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
171 and 0 < incr < ma_size and both are a function of hash */
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
172 i = (~sum) & mask;
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
173 /* We use ~sum instead if sum, as degenerate hash functions, such
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
174 as for ints <sigh>, can have lots of leading zeros. It's not
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
175 really a performance risk, but better safe than sorry. */
5059
192d571cc461 Small lookmapping nits:
Guido van Rossum <guido@python.org>
parents: 5056
diff changeset
176 ep = &ep0[i];
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
177 if (ep->me_key == NULL)
4976
2e2945d8163c Rewrote lookmapping() according to suggestions by Jyrki Alakuijala.
Guido van Rossum <guido@python.org>
parents: 4586
diff changeset
178 return ep;
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
179 if (ep->me_key == dummy)
4976
2e2945d8163c Rewrote lookmapping() according to suggestions by Jyrki Alakuijala.
Guido van Rossum <guido@python.org>
parents: 4586
diff changeset
180 freeslot = ep;
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
181 else if (ep->me_key == key ||
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
182 (ep->me_hash == hash &&
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
183 PyObject_Compare(ep->me_key, key) == 0))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
184 {
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
185 return ep;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
186 }
5609
f28eea02cf94 PyObject_Compare can now return an error. Unfortunately, there are a
Guido van Rossum <guido@python.org>
parents: 5561
diff changeset
187 /* XXX What if PyObject_Compare returned an exception? */
5059
192d571cc461 Small lookmapping nits:
Guido van Rossum <guido@python.org>
parents: 5056
diff changeset
188 /* Derive incr from sum, just to make it more arbitrary. Note that
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
189 incr must not be 0, or we will get into an infinite loop.*/
5059
192d571cc461 Small lookmapping nits:
Guido van Rossum <guido@python.org>
parents: 5056
diff changeset
190 incr = (sum ^ (sum >> 3)) & mask;
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
191 if (!incr)
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
192 incr = mask;
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
193 if (incr > mask) /* Cycle through GF(2^n)-{0} */
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
194 incr ^= mp->ma_poly; /* This will implicitly clear the
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
195 highest bit */
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
196 for (;;) {
5059
192d571cc461 Small lookmapping nits:
Guido van Rossum <guido@python.org>
parents: 5056
diff changeset
197 ep = &ep0[(i+incr)&mask];
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
198 if (ep->me_key == NULL) {
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
199 if (freeslot != NULL)
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
200 return freeslot;
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
201 else
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 return ep;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 }
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
204 if (ep->me_key == dummy) {
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
205 if (freeslot == NULL)
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
206 freeslot = ep;
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
207 }
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
208 else if (ep->me_key == key ||
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
209 (ep->me_hash == hash &&
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
210 PyObject_Compare(ep->me_key, key) == 0)) {
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 return ep;
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
212 }
5609
f28eea02cf94 PyObject_Compare can now return an error. Unfortunately, there are a
Guido van Rossum <guido@python.org>
parents: 5561
diff changeset
213 /* XXX What if PyObject_Compare returned an exception? */
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
214 /* Cycle through GF(2^n)-{0} */
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
215 incr = incr << 1;
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
216 if (incr > mask)
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
217 incr ^= mp->ma_poly;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 Internal routine to insert a new item into the table.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 Used both by the internal resize routine and by the public insert routine.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 Eats a reference to key and one to value.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 */
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
226 static void insertdict
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
227 Py_PROTO((dictobject *, PyObject *, long, PyObject *));
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 static void
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
229 insertdict(mp, key, hash, value)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
230 register dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
231 PyObject *key;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 long hash;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
233 PyObject *value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
235 PyObject *old_value;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
236 register dictentry *ep;
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
237 ep = lookdict(mp, key, hash);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 if (ep->me_value != NULL) {
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
239 old_value = ep->me_value;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
240 ep->me_value = value;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
241 Py_DECREF(old_value); /* which **CAN** re-enter */
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
242 Py_DECREF(key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 else {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 if (ep->me_key == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 mp->ma_fill++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
248 Py_DECREF(ep->me_key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 ep->me_key = key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 ep->me_hash = hash;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
251 ep->me_value = value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 mp->ma_used++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 Restructure the table by allocating a new table and reinserting all
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 items again. When entries have been deleted, the new table may
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 actually be smaller than the old one.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 */
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
261 static int dictresize Py_PROTO((dictobject *, int));
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 static int
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
263 dictresize(mp, minused)
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
264 dictobject *mp;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
265 int minused;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 register int oldsize = mp->ma_size;
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
268 register int newsize, newpoly;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
269 register dictentry *oldtable = mp->ma_table;
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
270 register dictentry *newtable;
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
271 register dictentry *ep;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 register int i;
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
273 for (i = 0, newsize = MINSIZE; ; i++, newsize <<= 1) {
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
274 if (i > sizeof(polys)/sizeof(polys[0])) {
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
275 /* Ran out of polynomials */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
276 PyErr_NoMemory();
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1414
diff changeset
277 return -1;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1414
diff changeset
278 }
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
279 if (newsize > minused) {
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
280 newpoly = polys[i];
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 break;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
284 newtable = (dictentry *) calloc(sizeof(dictentry), newsize);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 if (newtable == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
286 PyErr_NoMemory();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 mp->ma_size = newsize;
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
290 mp->ma_poly = newpoly;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 mp->ma_table = newtable;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 mp->ma_fill = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 mp->ma_used = 0;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
294
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
295 /* Make two passes, so we can avoid decrefs
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
296 (and possible side effects) till the table is copied */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 if (ep->me_value != NULL)
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
299 insertdict(mp,ep->me_key,ep->me_hash,ep->me_value);
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
300 }
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
301 for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
302 if (ep->me_value == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
303 Py_XDECREF(ep->me_key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 }
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
305
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
306 PyMem_XDEL(oldtable);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
310 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
311 PyDict_GetItem(op, key)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
312 PyObject *op;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
313 PyObject *key;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 long hash;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
316 if (!PyDict_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
317 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
320 if (((dictobject *)op)->ma_table == NULL)
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
321 return NULL;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
322 #ifdef CACHE_HASH
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
323 if (!PyString_Check(key) ||
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
324 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
325 #endif
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
326 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
327 hash = PyObject_Hash(key);
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
328 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
329 return NULL;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
330 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
331 return lookdict((dictobject *)op, key, hash) -> me_value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
335 PyDict_SetItem(op, key, value)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
336 register PyObject *op;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
337 PyObject *key;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
338 PyObject *value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 {
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
340 register dictobject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 register long hash;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
342 if (!PyDict_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
343 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
346 mp = (dictobject *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
347 #ifdef CACHE_HASH
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
348 if (PyString_Check(key)) {
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
349 #ifdef INTERN_STRINGS
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
350 if (((PyStringObject *)key)->ob_sinterned != NULL) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
351 key = ((PyStringObject *)key)->ob_sinterned;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
352 hash = ((PyStringObject *)key)->ob_shash;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
353 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
354 else
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
355 #endif
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
356 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
357 hash = ((PyStringObject *)key)->ob_shash;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
358 if (hash == -1)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
359 hash = PyObject_Hash(key);
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
360 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
361 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
362 else
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
363 #endif
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
364 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
365 hash = PyObject_Hash(key);
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
366 if (hash == -1)
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
367 return -1;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
368 }
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
369 /* if fill >= 2/3 size, double in size */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 if (mp->ma_fill*3 >= mp->ma_size*2) {
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
371 if (dictresize(mp, mp->ma_used*2) != 0) {
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 if (mp->ma_fill+1 > mp->ma_size)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
376 Py_INCREF(value);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
377 Py_INCREF(key);
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
378 insertdict(mp, key, hash, value);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
383 PyDict_DelItem(op, key)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
384 PyObject *op;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
385 PyObject *key;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 {
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
387 register dictobject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 register long hash;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
389 register dictentry *ep;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
390 PyObject *old_value, *old_key;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
391
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
392 if (!PyDict_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
393 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 }
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
396 #ifdef CACHE_HASH
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
397 if (!PyString_Check(key) ||
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
398 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
399 #endif
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
400 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
401 hash = PyObject_Hash(key);
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
402 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
403 return -1;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
404 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
405 mp = (dictobject *)op;
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
406 if (((dictobject *)op)->ma_table == NULL)
2056
aff5d7d06735 * Objects/mappingobject.c (mappingremove): don't call
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
407 goto empty;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
408 ep = lookdict(mp, key, hash);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 if (ep->me_value == NULL) {
2056
aff5d7d06735 * Objects/mappingobject.c (mappingremove): don't call
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
410 empty:
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
411 PyErr_SetObject(PyExc_KeyError, key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 }
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
414 old_key = ep->me_key;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
415 Py_INCREF(dummy);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 ep->me_key = dummy;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
417 old_value = ep->me_value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 ep->me_value = NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 mp->ma_used--;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
420 Py_DECREF(old_value);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
421 Py_DECREF(old_key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
425 void
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
426 PyDict_Clear(op)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
427 PyObject *op;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 {
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
429 int i, n;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
430 register dictentry *table;
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
431 dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
432 if (!PyDict_Check(op))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
433 return;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
434 mp = (dictobject *)op;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
435 table = mp->ma_table;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
436 if (table == NULL)
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
437 return;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
438 n = mp->ma_size;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
439 mp->ma_size = mp->ma_used = mp->ma_fill = 0;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
440 mp->ma_table = NULL;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
441 for (i = 0; i < n; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
442 Py_XDECREF(table[i].me_key);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
443 Py_XDECREF(table[i].me_value);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
445 PyMem_DEL(table);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
448 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
449 PyDict_Next(op, ppos, pkey, pvalue)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
450 PyObject *op;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
451 int *ppos;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
452 PyObject **pkey;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
453 PyObject **pvalue;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
455 int i;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
456 register dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
457 if (!PyDict_Check(op))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
458 return 0;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
459 mp = (dictobject *)op;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
460 i = *ppos;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
461 if (i < 0)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
462 return 0;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
463 while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
464 i++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
465 *ppos = i+1;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
466 if (i >= mp->ma_size)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
467 return 0;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
468 if (pkey)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
469 *pkey = mp->ma_table[i].me_key;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
470 if (pvalue)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
471 *pvalue = mp->ma_table[i].me_value;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
472 return 1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 /* Methods */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
476
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 static void
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
478 dict_dealloc(mp)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
479 register dictobject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 register int i;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
482 register dictentry *ep;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 if (ep->me_key != NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
485 Py_DECREF(ep->me_key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 if (ep->me_value != NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
487 Py_DECREF(ep->me_value);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
489 PyMem_XDEL(mp->ma_table);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
490 PyMem_DEL(mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 static int
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
494 dict_print(mp, fp, flags)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
495 register dictobject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 register FILE *fp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 register int flags;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 register int i;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 register int any;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
501 register dictentry *ep;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 fprintf(fp, "{");
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 any = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 if (ep->me_value != NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 if (any++ > 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 fprintf(fp, ", ");
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
508 if (PyObject_Print((PyObject *)ep->me_key, fp, 0) != 0)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 fprintf(fp, ": ");
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
511 if (PyObject_Print(ep->me_value, fp, 0) != 0)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 fprintf(fp, "}");
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
518
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
519 static PyObject *
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
520 dict_repr(mp)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
521 dictobject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
523 auto PyObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
524 PyObject *sepa, *colon;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 register int i;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 register int any;
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
527 register dictentry *ep;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
528 v = PyString_FromString("{");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
529 sepa = PyString_FromString(", ");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
530 colon = PyString_FromString(": ");
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 any = 0;
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1414
diff changeset
532 for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) {
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 if (ep->me_value != NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 if (any++)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
535 PyString_Concat(&v, sepa);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
536 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key));
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
537 PyString_Concat(&v, colon);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
538 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value));
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
541 PyString_ConcatAndDel(&v, PyString_FromString("}"));
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
542 Py_XDECREF(sepa);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
543 Py_XDECREF(colon);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
546
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 static int
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
548 dict_length(mp)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
549 dictobject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 return mp->ma_used;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
553
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
554 static PyObject *
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
555 dict_subscript(mp, key)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
556 dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
557 register PyObject *key;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
559 PyObject *v;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
560 long hash;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
561 if (mp->ma_table == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
562 PyErr_SetObject(PyExc_KeyError, key);
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
563 return NULL;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
564 }
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
565 #ifdef CACHE_HASH
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
566 if (!PyString_Check(key) ||
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
567 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
568 #endif
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
569 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
570 hash = PyObject_Hash(key);
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
571 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
572 return NULL;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
573 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
574 v = lookdict(mp, key, hash) -> me_value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 if (v == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
576 PyErr_SetObject(PyExc_KeyError, key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
578 Py_INCREF(v);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
581
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 static int
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
583 dict_ass_sub(mp, v, w)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
584 dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
585 PyObject *v, *w;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 if (w == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
588 return PyDict_DelItem((PyObject *)mp, v);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
590 return PyDict_SetItem((PyObject *)mp, v, w);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
593 static PyMappingMethods dict_as_mapping = {
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
594 (inquiry)dict_length, /*mp_length*/
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
595 (binaryfunc)dict_subscript, /*mp_subscript*/
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
596 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
598
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
599 static PyObject *
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
600 dict_keys(mp, args)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
601 register dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
602 PyObject *args;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
604 register PyObject *v;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 register int i, j;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
606 if (!PyArg_NoArgs(args))
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
608 v = PyList_New(mp->ma_used);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 if (v == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 for (i = 0, j = 0; i < mp->ma_size; i++) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 if (mp->ma_table[i].me_value != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
613 PyObject *key = mp->ma_table[i].me_key;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
614 Py_INCREF(key);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
615 PyList_SetItem(v, j, key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 j++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
621
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
622 static PyObject *
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
623 dict_values(mp, args)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
624 register dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
625 PyObject *args;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
626 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
627 register PyObject *v;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
628 register int i, j;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
629 if (!PyArg_NoArgs(args))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
630 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
631 v = PyList_New(mp->ma_used);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
632 if (v == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
633 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
634 for (i = 0, j = 0; i < mp->ma_size; i++) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
635 if (mp->ma_table[i].me_value != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
636 PyObject *value = mp->ma_table[i].me_value;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
637 Py_INCREF(value);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
638 PyList_SetItem(v, j, value);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
639 j++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
640 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
641 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
642 return v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
643 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
644
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
645 static PyObject *
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
646 dict_items(mp, args)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
647 register dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
648 PyObject *args;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
649 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
650 register PyObject *v;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
651 register int i, j;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
652 if (!PyArg_NoArgs(args))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
653 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
654 v = PyList_New(mp->ma_used);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
655 if (v == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
656 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
657 for (i = 0, j = 0; i < mp->ma_size; i++) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
658 if (mp->ma_table[i].me_value != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
659 PyObject *key = mp->ma_table[i].me_key;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
660 PyObject *value = mp->ma_table[i].me_value;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
661 PyObject *item = PyTuple_New(2);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
662 if (item == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
663 Py_DECREF(v);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
664 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
665 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
666 Py_INCREF(key);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
667 PyTuple_SetItem(item, 0, key);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
668 Py_INCREF(value);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
669 PyTuple_SetItem(item, 1, value);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
670 PyList_SetItem(v, j, item);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
671 j++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
672 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
673 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
674 return v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
675 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
676
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
677 static PyObject *
5673
a9ffdff0557f Renamed dict.absorb() (too spungy) to dict.update().
Guido van Rossum <guido@python.org>
parents: 5658
diff changeset
678 dict_update(mp, args)
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
679 register dictobject *mp;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
680 PyObject *args;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
681 {
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
682 register int i;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
683 dictobject *other;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
684 dictentry *entry;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
685 if (!PyArg_Parse(args, "O!", &PyDict_Type, &other))
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
686 return NULL;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
687 if (other == mp)
5673
a9ffdff0557f Renamed dict.absorb() (too spungy) to dict.update().
Guido van Rossum <guido@python.org>
parents: 5658
diff changeset
688 goto done; /* a.update(a); nothing to do */
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
689 /* Do one big resize at the start, rather than incrementally
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
690 resizing as we insert new items. Expect that there will be
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
691 no (or few) overlapping keys. */
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
692 if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) {
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
693 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
694 return NULL;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
695 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
696 for (i = 0; i < other->ma_size; i++) {
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
697 entry = &other->ma_table[i];
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
698 if (entry->me_value != NULL) {
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
699 Py_INCREF(entry->me_key);
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
700 Py_INCREF(entry->me_value);
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
701 insertdict(mp, entry->me_key, entry->me_hash,
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
702 entry->me_value);
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
703 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
704 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
705 done:
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
706 Py_INCREF(Py_None);
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
707 return Py_None;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
708 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
709
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
710 static PyObject *
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
711 dict_copy(mp, args)
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
712 register dictobject *mp;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
713 PyObject *args;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
714 {
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
715 register int i;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
716 dictobject *copy;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
717 dictentry *entry;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
718 if (!PyArg_Parse(args, ""))
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
719 return NULL;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
720 copy = (dictobject *)PyDict_New();
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
721 if (copy == NULL)
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
722 return NULL;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
723 if (mp->ma_used > 0) {
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
724 if (dictresize(copy, mp->ma_used*3/2) != 0)
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
725 return NULL;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
726 for (i = 0; i < mp->ma_size; i++) {
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
727 entry = &mp->ma_table[i];
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
728 if (entry->me_value != NULL) {
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
729 Py_INCREF(entry->me_key);
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
730 Py_INCREF(entry->me_value);
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
731 insertdict(copy, entry->me_key, entry->me_hash,
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
732 entry->me_value);
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
733 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
734 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
735 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
736 return (PyObject *)copy;
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
737 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
738
1398
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
739 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
740 PyDict_Size(mp)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
741 PyObject *mp;
1398
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
742 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
743 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
744 PyErr_BadInternalCall();
1413
c914b10bf2ed * timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents: 1406
diff changeset
745 return 0;
1398
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
746 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
747 return ((dictobject *)mp)->ma_used;
1398
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
748 }
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
749
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
750 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
751 PyDict_Keys(mp)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
752 PyObject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
754 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
755 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
758 return dict_keys((dictobject *)mp, (PyObject *)NULL);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
760
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
761 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
762 PyDict_Values(mp)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
763 PyObject *mp;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
764 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
765 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
766 PyErr_BadInternalCall();
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
767 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
768 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
769 return dict_values((dictobject *)mp, (PyObject *)NULL);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
770 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
771
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
772 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
773 PyDict_Items(mp)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
774 PyObject *mp;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
775 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
776 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
777 PyErr_BadInternalCall();
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
778 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
779 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
780 return dict_items((dictobject *)mp, (PyObject *)NULL);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
781 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
782
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
783 #define NEWCMP
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
784
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
785 #ifdef NEWCMP
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
786
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
787 /* Subroutine which returns the smallest key in a for which b's value
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
788 is different or absent. The value is returned too, through the
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
789 pval argument. No reference counts are incremented. */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
790
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
791 static PyObject *
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
792 characterize(a, b, pval)
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
793 dictobject *a;
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
794 dictobject *b;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
795 PyObject **pval;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
796 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
797 PyObject *diff = NULL;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
798 int i;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
799
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
800 *pval = NULL;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
801 for (i = 0; i < a->ma_size; i++) {
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
802 if (a->ma_table[i].me_value != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
803 PyObject *key = a->ma_table[i].me_key;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
804 PyObject *aval, *bval;
5609
f28eea02cf94 PyObject_Compare can now return an error. Unfortunately, there are a
Guido van Rossum <guido@python.org>
parents: 5561
diff changeset
805 /* XXX What if PyObject_Compare raises an exception? */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
806 if (diff != NULL && PyObject_Compare(key, diff) > 0)
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
807 continue;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
808 aval = a->ma_table[i].me_value;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
809 bval = PyDict_GetItem((PyObject *)b, key);
5609
f28eea02cf94 PyObject_Compare can now return an error. Unfortunately, there are a
Guido van Rossum <guido@python.org>
parents: 5561
diff changeset
810 /* XXX What if PyObject_Compare raises an exception? */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
811 if (bval == NULL || PyObject_Compare(aval, bval) != 0)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
812 {
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
813 diff = key;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
814 *pval = aval;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
815 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
816 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
817 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
818 return diff;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
819 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
820
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
821 static int
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
822 dict_compare(a, b)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
823 dictobject *a, *b;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
824 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
825 PyObject *adiff, *bdiff, *aval, *bval;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
826 int res;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
827
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
828 /* Compare lengths first */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
829 if (a->ma_used < b->ma_used)
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
830 return -1; /* a is shorter */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
831 else if (a->ma_used > b->ma_used)
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
832 return 1; /* b is shorter */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
833 /* Same length -- check all keys */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
834 adiff = characterize(a, b, &aval);
5609
f28eea02cf94 PyObject_Compare can now return an error. Unfortunately, there are a
Guido van Rossum <guido@python.org>
parents: 5561
diff changeset
835 if (PyErr_Occurred())
f28eea02cf94 PyObject_Compare can now return an error. Unfortunately, there are a
Guido van Rossum <guido@python.org>
parents: 5561
diff changeset
836 return -1;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
837 if (adiff == NULL)
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
838 return 0; /* a is a subset with the same length */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
839 bdiff = characterize(b, a, &bval);
5609
f28eea02cf94 PyObject_Compare can now return an error. Unfortunately, there are a
Guido van Rossum <guido@python.org>
parents: 5561
diff changeset
840 if (PyErr_Occurred())
f28eea02cf94 PyObject_Compare can now return an error. Unfortunately, there are a
Guido van Rossum <guido@python.org>
parents: 5561
diff changeset
841 return -1;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
842 /* bdiff == NULL would be impossible now */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
843 res = PyObject_Compare(adiff, bdiff);
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
844 if (res == 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
845 res = PyObject_Compare(aval, bval);
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
846 return res;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
847 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
848
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
849 #else /* !NEWCMP */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
850
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 static int
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
852 dict_compare(a, b)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
853 dictobject *a, *b;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
855 PyObject *akeys, *bkeys;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 int i, n, res;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
857 if (a == b)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
858 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
859 if (a->ma_used == 0) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
860 if (b->ma_used != 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
861 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
862 else
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
863 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
864 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
865 else {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
866 if (b->ma_used == 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
867 return 1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
868 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
869 akeys = dict_keys(a, (PyObject *)NULL);
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
870 bkeys = dict_keys(b, (PyObject *)NULL);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
871 if (akeys == NULL || bkeys == NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
872 /* Oops, out of memory -- what to do? */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 /* For now, sort on address! */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
874 Py_XDECREF(akeys);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
875 Py_XDECREF(bkeys);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
876 if (a < b)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
877 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 else
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
879 return 1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
881 PyList_Sort(akeys);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
882 PyList_Sort(bkeys);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
883 n = a->ma_used < b->ma_used ? a->ma_used : b->ma_used; /* smallest */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
884 res = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
885 for (i = 0; i < n; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
886 PyObject *akey, *bkey, *aval, *bval;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
887 long ahash, bhash;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
888 akey = PyList_GetItem(akeys, i);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
889 bkey = PyList_GetItem(bkeys, i);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
890 res = PyObject_Compare(akey, bkey);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
891 if (res != 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
892 break;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
893 #ifdef CACHE_HASH
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
894 if (!PyString_Check(akey) ||
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
895 (ahash = ((PyStringObject *) akey)->ob_shash) == -1)
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
896 #endif
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
897 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
898 ahash = PyObject_Hash(akey);
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
899 if (ahash == -1)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
900 PyErr_Clear(); /* Don't want errors here */
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
901 }
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
902 #ifdef CACHE_HASH
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
903 if (!PyString_Check(bkey) ||
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
904 (bhash = ((PyStringObject *) bkey)->ob_shash) == -1)
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
905 #endif
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
906 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
907 bhash = PyObject_Hash(bkey);
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
908 if (bhash == -1)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
909 PyErr_Clear(); /* Don't want errors here */
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
910 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
911 aval = lookdict(a, akey, ahash) -> me_value;
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
912 bval = lookdict(b, bkey, bhash) -> me_value;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
913 res = PyObject_Compare(aval, bval);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
914 if (res != 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
915 break;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
916 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
917 if (res == 0) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
918 if (a->ma_used < b->ma_used)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
919 res = -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
920 else if (a->ma_used > b->ma_used)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
921 res = 1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
922 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
923 Py_DECREF(akeys);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
924 Py_DECREF(bkeys);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
925 return res;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
926 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
927
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
928 #endif /* !NEWCMP */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
929
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
930 static PyObject *
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
931 dict_has_key(mp, args)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
932 register dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
933 PyObject *args;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
934 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
935 PyObject *key;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
936 long hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
937 register long ok;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
938 if (!PyArg_Parse(args, "O", &key))
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
939 return NULL;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
940 #ifdef CACHE_HASH
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
941 if (!PyString_Check(key) ||
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
942 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
943 #endif
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
944 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
945 hash = PyObject_Hash(key);
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
946 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
947 return NULL;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
948 }
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
949 ok = mp->ma_size != 0 && lookdict(mp, key, hash)->me_value != NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
950 return PyInt_FromLong(ok);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
951 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
952
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
953 static PyObject *
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
954 dict_clear(mp, args)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
955 register dictobject *mp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
956 PyObject *args;
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
957 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
958 if (!PyArg_NoArgs(args))
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
959 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
960 PyDict_Clear((PyObject *)mp);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
961 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
962 return Py_None;
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
963 }
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
964
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
965 static PyMethodDef mapp_methods[] = {
5784
c043901fa1fe Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents: 5673
diff changeset
966 {"has_key", (PyCFunction)dict_has_key},
c043901fa1fe Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents: 5673
diff changeset
967 {"keys", (PyCFunction)dict_keys},
c043901fa1fe Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents: 5673
diff changeset
968 {"items", (PyCFunction)dict_items},
c043901fa1fe Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents: 5673
diff changeset
969 {"values", (PyCFunction)dict_values},
5673
a9ffdff0557f Renamed dict.absorb() (too spungy) to dict.update().
Guido van Rossum <guido@python.org>
parents: 5658
diff changeset
970 {"update", (PyCFunction)dict_update},
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
971 {"clear", (PyCFunction)dict_clear},
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
972 {"copy", (PyCFunction)dict_copy},
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 {NULL, NULL} /* sentinel */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
975
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
976 static PyObject *
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
977 dict_getattr(mp, name)
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
978 dictobject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
979 char *name;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
980 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
981 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
982 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
983
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
984 PyTypeObject PyDict_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
985 PyObject_HEAD_INIT(&PyType_Type)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
986 0,
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
987 "dictionary",
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
988 sizeof(dictobject),
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
989 0,
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
990 (destructor)dict_dealloc, /*tp_dealloc*/
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
991 (printfunc)dict_print, /*tp_print*/
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
992 (getattrfunc)dict_getattr, /*tp_getattr*/
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 0, /*tp_setattr*/
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
994 (cmpfunc)dict_compare, /*tp_compare*/
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
995 (reprfunc)dict_repr, /*tp_repr*/
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
996 0, /*tp_as_number*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 0, /*tp_as_sequence*/
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
998 &dict_as_mapping, /*tp_as_mapping*/
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
999 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1000
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1001 /* For backward compatibility with old dictionary interface */
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1002
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1003 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1004 PyDict_GetItemString(v, key)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1005 PyObject *v;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006 char *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007 {
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1008 PyObject *kv, *rv;
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1009 kv = PyString_FromString(key);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1010 if (kv == NULL)
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1011 return NULL;
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1012 PyString_InternInPlace(&kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1013 rv = PyDict_GetItem(v, kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1014 Py_DECREF(kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1015 return rv;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1016 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1017
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1019 PyDict_SetItemString(v, key, item)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1020 PyObject *v;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1021 char *key;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1022 PyObject *item;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 {
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1024 PyObject *kv;
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1025 int err;
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1026 kv = PyString_FromString(key);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1027 if (kv == NULL)
5561
4dcf26fe4da5 Moved PyObject_{Get,Set}Attr to object.c.
Guido van Rossum <guido@python.org>
parents: 5540
diff changeset
1028 return -1;
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1029 PyString_InternInPlace(&kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1030 err = PyDict_SetItem(v, kv, item);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1031 Py_DECREF(kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1032 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1034
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1036 PyDict_DelItemString(v, key)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1037 PyObject *v;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1038 char *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 {
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1040 PyObject *kv;
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1041 int err;
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1042 kv = PyString_FromString(key);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1043 if (kv == NULL)
5561
4dcf26fe4da5 Moved PyObject_{Get,Set}Attr to object.c.
Guido van Rossum <guido@python.org>
parents: 5540
diff changeset
1044 return -1;
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1045 PyString_InternInPlace(&kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1046 err = PyDict_DelItem(v, kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1047 Py_DECREF(kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
1048 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1049 }