annotate Objects/mappingobject.c @ 1330:409ea74feb21 legacy-trunk

Minor fixes / changes for Mac compatibility.
author Guido van Rossum <guido@python.org>
date Thu, 29 Jul 1993 08:25:09 +0000
parents 9fed5726a36d
children 043da19302ef
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 /***********************************************************
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Amsterdam, The Netherlands.
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
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 documentation for any purpose and without fee is hereby granted,
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
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 both that copyright notice and this permission notice appear in
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
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 distribution of the software without specific, written prior permission.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 ******************************************************************/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 /* Mapping object implementation; using a hash table */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
27 /* This file should really be called "dictobject.c", since "mapping"
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
28 is the generic name for objects with an unorderred arbitrary key
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
29 set (just like lists are sequences), but since it improves (and was
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
30 originally derived from) a file by that name I had to change its
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
31 name. For the user these objects are still called "dictionaries". */
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
32
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 #include "allobjects.h"
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #include "modsupport.h"
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 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 Table of primes suitable as keys, in ascending order.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 The first line are the largest primes less than some powers of two,
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 the second line is the largest prime less than 6000,
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 and the third line is a selection from Knuth, Vol. 3, Sec. 6.1, Table 1.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 The final value is a sentinel and should cause the memory allocation
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 of that many entries to fail (if none of the earlier values cause such
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 failure already).
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 static unsigned int primes[] = {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 3, 7, 13, 31, 61, 127, 251, 509, 1021, 2017, 4093,
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 5987,
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 9551, 15683, 19609, 31397,
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 0xffffffff /* All bits set -- truncation OK */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 /* Object used as dummy key to fill deleted entries */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 static object *dummy; /* Initialized by first call to newmappingobject() */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 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
58 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
59 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
60 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
61 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 typedef struct {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 long me_hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 object *me_key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 object *me_value;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 } mappingentry;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 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
70 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
71 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
72 of non-NULL, non-dummy keys.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 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
74 when it is more than half filled.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 typedef struct {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 OB_HEAD
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 int ma_fill;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 int ma_used;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 int ma_size;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 mappingentry *ma_table;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 } mappingobject;
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 object *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 newmappingobject()
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 register mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if (dummy == NULL) { /* Auto-initialize dummy */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 dummy = newstringobject("<dummy key>");
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 if (dummy == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 mp = NEWOBJ(mappingobject, &Mappingtype);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 if (mp == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 mp->ma_size = primes[0];
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 mp->ma_table = (mappingentry *) calloc(sizeof(mappingentry), mp->ma_size);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 if (mp->ma_table == NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 DEL(mp);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 return err_nomem();
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 mp->ma_fill = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 mp->ma_used = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 return (object *)mp;
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
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 The basic lookup function used by all operations.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 This is essentially Algorithm D from Knuth Vol. 3, Sec. 6.4.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 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
111 chaining would be substantial (100% with typical malloc overhead).
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 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
114 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
115 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
116 All arithmetic on sum should ignore overflow.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 The initial probe index is then computed as sum mod the table size.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 Subsequent probe indices are incr apart (mod table size), where incr
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 is also derived from sum, with the additional requirement that it is
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 relative prime to the table size (i.e., 1 <= incr < size, since the size
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 is a prime number). My choice for incr is somewhat arbitrary.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 static mappingentry *lookmapping PROTO((mappingobject *, object *, long));
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 static mappingentry *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 lookmapping(mp, key, hash)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 register mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 object *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 long hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 register int i, incr;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 register unsigned long sum = (unsigned long) hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 register mappingentry *freeslot = NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 /* We must come up with (i, incr) such that 0 <= i < ma_size
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 and 0 < incr < ma_size and both are a function of hash */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 i = sum % mp->ma_size;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 do {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 sum = sum + sum + sum + 1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 incr = sum % mp->ma_size;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 } while (incr == 0);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 for (;;) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 register mappingentry *ep = &mp->ma_table[i];
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 if (ep->me_key == NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 if (freeslot != NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 return freeslot;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 else
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 return ep;
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 if (ep->me_key == dummy) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 if (freeslot != NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 freeslot = ep;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 else if (ep->me_hash == hash &&
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 cmpobject(ep->me_key, key) == 0) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 return ep;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 i = (i + incr) % mp->ma_size;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 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
163 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
164 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
165 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 static void insertmapping PROTO((mappingobject *, object *, long, object *));
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 static void
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 insertmapping(mp, key, hash, value)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 register mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 object *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 long hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 object *value;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 register mappingentry *ep;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 ep = lookmapping(mp, key, hash);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 if (ep->me_value != NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 DECREF(ep->me_value);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 DECREF(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 else {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 if (ep->me_key == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 mp->ma_fill++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 else
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 DECREF(ep->me_key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 ep->me_key = key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 ep->me_hash = hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 mp->ma_used++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 ep->me_value = value;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 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
194 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
195 actually be smaller than the old one.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 static int mappingresize PROTO((mappingobject *));
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 static int
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 mappingresize(mp)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 register int oldsize = mp->ma_size;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 register int newsize;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 register mappingentry *oldtable = mp->ma_table;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 register mappingentry *newtable;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 register mappingentry *ep;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 register int i;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 newsize = mp->ma_size;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 for (i = 0; ; i++) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 if (primes[i] > mp->ma_used*2) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 newsize = primes[i];
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 break;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 newtable = (mappingentry *) calloc(sizeof(mappingentry), newsize);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 if (newtable == NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 err_nomem();
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 return -1;
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 mp->ma_size = newsize;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 mp->ma_table = newtable;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 mp->ma_fill = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 mp->ma_used = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 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
225 if (ep->me_value != NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 insertmapping(mp,ep->me_key,ep->me_hash,ep->me_value);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 else {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 XDECREF(ep->me_key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 DEL(oldtable);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 object *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 mappinglookup(op, key)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 object *op;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 object *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 long hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 if (!is_mappingobject(op)) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 err_badcall();
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 hash = hashobject(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 if (hash == -1)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 return lookmapping((mappingobject *)op, key, hash) -> me_value;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 int
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 mappinginsert(op, key, value)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 register object *op;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 object *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 object *value;
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 register mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 register long hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 if (!is_mappingobject(op)) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 err_badcall();
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 hash = hashobject(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 if (hash == -1)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 mp = (mappingobject *)op;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 /* if fill >= 2/3 size, resize */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 if (mp->ma_fill*3 >= mp->ma_size*2) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 if (mappingresize(mp) != 0) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 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
271 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 INCREF(value);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 INCREF(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 insertmapping(mp, key, hash, value);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 int
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 mappingremove(op, key)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 object *op;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 object *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 register mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 register long hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 register mappingentry *ep;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 if (!is_mappingobject(op)) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 err_badcall();
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 hash = hashobject(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 if (hash == -1)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 mp = (mappingobject *)op;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 ep = lookmapping(mp, key, hash);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 if (ep->me_value == NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 err_setval(KeyError, key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 DECREF(ep->me_key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 INCREF(dummy);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 ep->me_key = dummy;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 DECREF(ep->me_value);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 ep->me_value = NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 mp->ma_used--;
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
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
310 void
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
311 mappingclear(op)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
312 object *op;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
314 int i;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
315 register mappingobject *mp;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
316 if (!is_mappingobject(op))
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
317 return;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
318 mp = (mappingobject *)op;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
319 for (i = 0; i < mp->ma_size; i++) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
320 XDECREF(mp->ma_table[i].me_key);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
321 XDECREF(mp->ma_table[i].me_value);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
322 mp->ma_table[i].me_key = NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
323 mp->ma_table[i].me_value = NULL;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 }
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
325 mp->ma_used = 0;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
328 int
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
329 mappinggetnext(op, ppos, pkey, pvalue)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 object *op;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
331 int *ppos;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
332 object **pkey;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
333 object **pvalue;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
335 int i;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 register mappingobject *mp;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
337 if (!is_dictobject(op))
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
338 return 0;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 mp = (mappingobject *)op;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
340 i = *ppos;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
341 if (i < 0)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
342 return 0;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
343 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
344 i++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
345 *ppos = i+1;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
346 if (i >= mp->ma_size)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
347 return 0;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
348 if (pkey)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
349 *pkey = mp->ma_table[i].me_key;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
350 if (pvalue)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
351 *pvalue = mp->ma_table[i].me_value;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
352 return 1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 /* Methods */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 static void
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 mapping_dealloc(mp)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 register mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 register int i;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 register mappingentry *ep;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 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
364 if (ep->me_key != NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 DECREF(ep->me_key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 if (ep->me_value != NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 DECREF(ep->me_value);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 if (mp->ma_table != NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 DEL(mp->ma_table);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 DEL(mp);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 static int
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 mapping_print(mp, fp, flags)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 register mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 register FILE *fp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 register int flags;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 register int i;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 register int any;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 register mappingentry *ep;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 fprintf(fp, "{");
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 any = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 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
386 if (ep->me_value != NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 if (any++ > 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 fprintf(fp, ", ");
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 if (printobject((object *)ep->me_key, fp, flags) != 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 fprintf(fp, ": ");
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 if (printobject(ep->me_value, fp, flags) != 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 fprintf(fp, "}");
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
399
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 static void
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 js(pv, w)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 object **pv;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 object *w;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 joinstring(pv, w);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 XDECREF(w);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 static object *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 mapping_repr(mp)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 auto object *v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 object *sepa, *colon;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 register int i;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 register int any;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 register mappingentry *ep;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 v = newstringobject("{");
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 sepa = newstringobject(", ");
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 colon = newstringobject(": ");
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 any = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 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
423 if (ep->me_value != NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 if (any++)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 joinstring(&v, sepa);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 js(&v, reprobject(ep->me_key));
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 joinstring(&v, colon);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 js(&v, reprobject(ep->me_value));
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 js(&v, newstringobject("}"));
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 XDECREF(sepa);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 XDECREF(colon);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 static int
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 mapping_length(mp)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 return mp->ma_used;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 static object *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 mapping_subscript(mp, key)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 register object *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 object *v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 long hash = hashobject(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 if (hash == -1)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 v = lookmapping(mp, key, hash) -> me_value;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 if (v == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 err_setval(KeyError, key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 else
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 INCREF(v);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 static int
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 mapping_ass_sub(mp, v, w)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 object *v, *w;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 if (w == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 return mappingremove((object *)mp, v);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 else
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 return mappinginsert((object *)mp, v, w);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
471
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 static mapping_methods mapping_as_mapping = {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 mapping_length, /*mp_length*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 mapping_subscript, /*mp_subscript*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 mapping_ass_sub, /*mp_ass_subscript*/
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
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 static object *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 mapping_keys(mp, args)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 register mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 object *args;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 register object *v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 register int i, j;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 if (!getnoarg(args))
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 v = newlistobject(mp->ma_used);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 if (v == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 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
491 if (mp->ma_table[i].me_value != NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 object *key = mp->ma_table[i].me_key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 INCREF(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 setlistitem(v, j, key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 j++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
501 static object *
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
502 mapping_values(mp, args)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
503 register mappingobject *mp;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
504 object *args;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
505 {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
506 register object *v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
507 register int i, j;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
508 if (!getnoarg(args))
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
509 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
510 v = newlistobject(mp->ma_used);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
511 if (v == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
512 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
513 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
514 if (mp->ma_table[i].me_value != NULL) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
515 object *value = mp->ma_table[i].me_value;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
516 INCREF(value);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
517 setlistitem(v, j, value);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
518 j++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
519 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
520 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
521 return v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
522 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
523
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
524 static object *
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
525 mapping_items(mp, args)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
526 register mappingobject *mp;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
527 object *args;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
528 {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
529 register object *v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
530 register int i, j;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
531 if (!getnoarg(args))
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
532 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
533 v = newlistobject(mp->ma_used);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
534 if (v == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
535 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
536 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
537 if (mp->ma_table[i].me_value != NULL) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
538 object *key = mp->ma_table[i].me_key;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
539 object *value = mp->ma_table[i].me_value;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
540 object *item = newtupleobject(2);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
541 if (item == NULL) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
542 DECREF(v);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
543 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
544 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
545 INCREF(key);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
546 settupleitem(item, 0, key);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
547 INCREF(value);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
548 settupleitem(item, 1, value);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
549 setlistitem(v, j, item);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
550 j++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
551 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
552 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
553 return v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
554 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
555
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 object *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 getmappingkeys(mp)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 object *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 if (mp == NULL || !is_mappingobject(mp)) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 err_badcall();
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 return mapping_keys((mappingobject *)mp, (object *)NULL);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
566
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
567 object *
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
568 getmappingvalues(mp)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
569 object *mp;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
570 {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
571 if (mp == NULL || !is_mappingobject(mp)) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
572 err_badcall();
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
573 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
574 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
575 return mapping_values((mappingobject *)mp, (object *)NULL);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
576 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
577
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
578 object *
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
579 getmappingitems(mp)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
580 object *mp;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
581 {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
582 if (mp == NULL || !is_mappingobject(mp)) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
583 err_badcall();
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
584 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
585 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
586 return mapping_values((mappingobject *)mp, (object *)NULL);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
587 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
588
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 static int
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 mapping_compare(a, b)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 mappingobject *a, *b;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 object *akeys, *bkeys;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 int i, n, res;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 if (a == b)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 if (a->ma_used == 0) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 if (b->ma_used != 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 else
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 else {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 if (b->ma_used == 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 return 1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 akeys = mapping_keys(a, (object *)NULL);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 bkeys = mapping_keys(b, (object *)NULL);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 if (akeys == NULL || bkeys == NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 /* Oops, out of memory -- what to do? */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 /* For now, sort on address! */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 XDECREF(akeys);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 XDECREF(bkeys);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 if (a < b)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 else
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 return 1;
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 sortlist(akeys);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 sortlist(bkeys);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 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
622 res = 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 for (i = 0; i < n; i++) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 object *akey, *bkey, *aval, *bval;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 long ahash, bhash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 akey = getlistitem(akeys, i);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 bkey = getlistitem(bkeys, i);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 res = cmpobject(akey, bkey);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 if (res != 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 break;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 ahash = hashobject(akey);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 if (ahash == -1)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 err_clear(); /* Don't want errors here */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 bhash = hashobject(bkey);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
635 if (bhash == -1)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 err_clear(); /* Don't want errors here */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 aval = lookmapping(a, akey, ahash) -> me_value;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 bval = lookmapping(b, bkey, bhash) -> me_value;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 res = cmpobject(aval, bval);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 if (res != 0)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 break;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 if (res == 0) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
644 if (a->ma_used < b->ma_used)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 res = -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 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
647 res = 1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 DECREF(akeys);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 DECREF(bkeys);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
651 return res;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
653
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 static object *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 mapping_has_key(mp, args)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 register mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 object *args;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 object *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 long hash;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
661 register long ok;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 if (!getargs(args, "O", &key))
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 hash = hashobject(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 if (hash == -1)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 ok = lookmapping(mp, key, hash)->me_value != NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 return newintobject(ok);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
669 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
670
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 static struct methodlist mapp_methods[] = {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
672 {"has_key", mapping_has_key},
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
673 {"items", mapping_items},
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 {"keys", mapping_keys},
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
675 {"values", mapping_values},
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 {NULL, NULL} /* sentinel */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
678
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 static object *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 mapping_getattr(mp, name)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
681 mappingobject *mp;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
682 char *name;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
684 return findmethod(mapp_methods, (object *)mp, name);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
685 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
686
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 typeobject Mappingtype = {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 OB_HEAD_INIT(&Typetype)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 0,
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
690 "dictionary",
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 sizeof(mappingobject),
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 0,
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 mapping_dealloc, /*tp_dealloc*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 mapping_print, /*tp_print*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 mapping_getattr, /*tp_getattr*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 0, /*tp_setattr*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 mapping_compare, /*tp_compare*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 mapping_repr, /*tp_repr*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 0, /*tp_as_number*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 0, /*tp_as_sequence*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 &mapping_as_mapping, /*tp_as_mapping*/
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
703
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 /* For backward compatibility with old dictionary interface */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
705
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 static object *last_name_object;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 static char *last_name_char;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 object *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 getattro(v, name)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 object *v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 object *name;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 if (name != last_name_object) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 XDECREF(last_name_object);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
716 INCREF(name);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 last_name_object = name;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 last_name_char = getstringvalue(name);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 return getattr(v, last_name_char);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
722
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 int
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 setattro(v, name, value)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 object *v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 object *name;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 object *value;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 if (name != last_name_object) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 XDECREF(last_name_object);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 INCREF(name);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 last_name_object = name;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 last_name_char = getstringvalue(name);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 return setattr(v, last_name_char, value);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
737
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 object *
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 dictlookup(v, key)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 object *v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
741 char *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
742 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
743 if (key != last_name_char) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
744 XDECREF(last_name_object);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
745 last_name_object = newstringobject(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 if (last_name_object == NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
747 last_name_char = NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 last_name_char = key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 return mappinglookup(v, last_name_object);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
754
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 int
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 dictinsert(v, key, item)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 object *v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
758 char *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 object *item;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
761 if (key != last_name_char) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
762 XDECREF(last_name_object);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 last_name_object = newstringobject(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
764 if (last_name_object == NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 last_name_char = NULL;
1330
409ea74feb21 Minor fixes / changes for Mac compatibility.
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
766 return -1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 last_name_char = key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 return mappinginsert(v, last_name_object, item);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
771 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
772
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
773 int
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
774 dictremove(v, key)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 object *v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 char *key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
777 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 if (key != last_name_char) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 XDECREF(last_name_object);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 last_name_object = newstringobject(key);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 if (last_name_object == NULL) {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
782 last_name_char = NULL;
1330
409ea74feb21 Minor fixes / changes for Mac compatibility.
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
783 return -1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 last_name_char = key;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
787 return mappingremove(v, last_name_object);
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
788 }