1993-03-27 18:11:32 +00:00
|
|
|
/***********************************************************
|
1995-01-04 19:07:38 +00:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1993-03-27 18:11:32 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1993-03-27 18:11:32 +00:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 14:44:06 +00:00
|
|
|
both that copyright notice and this permission notice appear in
|
1993-03-27 18:11:32 +00:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 14:44:06 +00:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1993-03-27 18:11:32 +00:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Mapping object implementation; using a hash table */
|
|
|
|
|
1993-03-29 10:43:31 +00:00
|
|
|
/* This file should really be called "dictobject.c", since "mapping"
|
|
|
|
is the generic name for objects with an unorderred arbitrary key
|
|
|
|
set (just like lists are sequences), but since it improves (and was
|
|
|
|
originally derived from) a file by that name I had to change its
|
|
|
|
name. For the user these objects are still called "dictionaries". */
|
|
|
|
|
1993-03-27 18:11:32 +00:00
|
|
|
#include "allobjects.h"
|
|
|
|
#include "modsupport.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1997-01-28 00:00:11 +00:00
|
|
|
* MINSIZE is the minimum size of a mapping.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MINSIZE 4
|
|
|
|
|
|
|
|
/*
|
|
|
|
Table of irreducible polynomials to efficiently cycle through
|
|
|
|
GF(2^n)-{0}, 2<=n<=30.
|
1993-03-27 18:11:32 +00:00
|
|
|
*/
|
1997-01-28 00:00:11 +00:00
|
|
|
static long polys[] = {
|
1997-01-29 04:45:16 +00:00
|
|
|
4 + 3,
|
|
|
|
8 + 3,
|
|
|
|
16 + 3,
|
|
|
|
32 + 5,
|
|
|
|
64 + 3,
|
|
|
|
128 + 3,
|
|
|
|
256 + 29,
|
|
|
|
512 + 17,
|
|
|
|
1024 + 9,
|
|
|
|
2048 + 5,
|
|
|
|
4096 + 83,
|
|
|
|
8192 + 27,
|
|
|
|
16384 + 43,
|
|
|
|
32768 + 3,
|
|
|
|
65536 + 45,
|
|
|
|
131072 + 9,
|
|
|
|
262144 + 39,
|
|
|
|
524288 + 39,
|
|
|
|
1048576 + 9,
|
|
|
|
2097152 + 5,
|
|
|
|
4194304 + 3,
|
|
|
|
8388608 + 33,
|
|
|
|
16777216 + 27,
|
|
|
|
33554432 + 9,
|
|
|
|
67108864 + 71,
|
|
|
|
134217728 + 39,
|
|
|
|
268435456 + 9,
|
|
|
|
536870912 + 5,
|
|
|
|
1073741824 + 83,
|
|
|
|
0
|
1993-03-27 18:11:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Object used as dummy key to fill deleted entries */
|
|
|
|
static object *dummy; /* Initialized by first call to newmappingobject() */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Invariant for entries: when in use, de_value is not NULL and de_key is
|
|
|
|
not NULL and not dummy; when not in use, de_value is NULL and de_key
|
|
|
|
is either NULL or dummy. A dummy key value cannot be replaced by
|
|
|
|
NULL, since otherwise other keys may be lost.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
long me_hash;
|
|
|
|
object *me_key;
|
|
|
|
object *me_value;
|
|
|
|
} mappingentry;
|
|
|
|
|
|
|
|
/*
|
|
|
|
To ensure the lookup algorithm terminates, the table size must be a
|
|
|
|
prime number and there must be at least one NULL key in the table.
|
|
|
|
The value ma_fill is the number of non-NULL keys; ma_used is the number
|
|
|
|
of non-NULL, non-dummy keys.
|
|
|
|
To avoid slowing down lookups on a near-full table, we resize the table
|
|
|
|
when it is more than half filled.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
OB_HEAD
|
|
|
|
int ma_fill;
|
|
|
|
int ma_used;
|
|
|
|
int ma_size;
|
1997-01-28 00:00:11 +00:00
|
|
|
int ma_poly;
|
1993-03-27 18:11:32 +00:00
|
|
|
mappingentry *ma_table;
|
|
|
|
} mappingobject;
|
|
|
|
|
|
|
|
object *
|
|
|
|
newmappingobject()
|
|
|
|
{
|
|
|
|
register mappingobject *mp;
|
|
|
|
if (dummy == NULL) { /* Auto-initialize dummy */
|
|
|
|
dummy = newstringobject("<dummy key>");
|
|
|
|
if (dummy == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mp = NEWOBJ(mappingobject, &Mappingtype);
|
|
|
|
if (mp == NULL)
|
|
|
|
return NULL;
|
1995-01-02 19:07:15 +00:00
|
|
|
mp->ma_size = 0;
|
1997-01-28 00:00:11 +00:00
|
|
|
mp->ma_poly = 0;
|
1995-01-02 19:07:15 +00:00
|
|
|
mp->ma_table = NULL;
|
1993-03-27 18:11:32 +00:00
|
|
|
mp->ma_fill = 0;
|
|
|
|
mp->ma_used = 0;
|
|
|
|
return (object *)mp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
The basic lookup function used by all operations.
|
1997-01-28 00:00:11 +00:00
|
|
|
This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
|
1993-03-27 18:11:32 +00:00
|
|
|
Open addressing is preferred over chaining since the link overhead for
|
|
|
|
chaining would be substantial (100% with typical malloc overhead).
|
1997-01-28 00:00:11 +00:00
|
|
|
However, instead of going through the table at constant steps, we cycle
|
|
|
|
through the values of GF(2^n)-{0}. This avoids modulo computations, being
|
|
|
|
much cheaper on RISC machines, without leading to clustering.
|
1993-03-27 18:11:32 +00:00
|
|
|
|
|
|
|
First a 32-bit hash value, 'sum', is computed from the key string.
|
|
|
|
The first character is added an extra time shifted by 8 to avoid hashing
|
|
|
|
single-character keys (often heavily used variables) too close together.
|
|
|
|
All arithmetic on sum should ignore overflow.
|
|
|
|
|
|
|
|
The initial probe index is then computed as sum mod the table size.
|
1997-01-28 00:00:11 +00:00
|
|
|
Subsequent probe indices use the values of x^i in GF(2^n) as an offset,
|
|
|
|
where x is a root. The initial value is derived from sum, too.
|
|
|
|
|
|
|
|
(This version is due to Reimer Behrends, some ideas are also due to
|
|
|
|
Jyrki Alakuijala.)
|
1993-03-27 18:11:32 +00:00
|
|
|
*/
|
|
|
|
static mappingentry *lookmapping PROTO((mappingobject *, object *, long));
|
|
|
|
static mappingentry *
|
|
|
|
lookmapping(mp, key, hash)
|
1997-01-16 21:06:45 +00:00
|
|
|
mappingobject *mp;
|
1993-03-27 18:11:32 +00:00
|
|
|
object *key;
|
|
|
|
long hash;
|
|
|
|
{
|
1997-01-28 00:00:11 +00:00
|
|
|
register int i;
|
|
|
|
register unsigned incr;
|
|
|
|
register unsigned long sum = (unsigned long) hash;
|
|
|
|
register mappingentry *freeslot = NULL;
|
1997-04-09 19:41:24 +00:00
|
|
|
register unsigned int mask = mp->ma_size-1;
|
1997-01-29 15:53:56 +00:00
|
|
|
mappingentry *ep0 = mp->ma_table;
|
|
|
|
register mappingentry *ep;
|
1997-01-28 00:00:11 +00:00
|
|
|
/* We must come up with (i, incr) such that 0 <= i < ma_size
|
|
|
|
and 0 < incr < ma_size and both are a function of hash */
|
|
|
|
i = (~sum) & mask;
|
|
|
|
/* We use ~sum instead if sum, as degenerate hash functions, such
|
|
|
|
as for ints <sigh>, can have lots of leading zeros. It's not
|
|
|
|
really a performance risk, but better safe than sorry. */
|
1997-01-29 15:53:56 +00:00
|
|
|
ep = &ep0[i];
|
1997-01-28 00:00:11 +00:00
|
|
|
if (ep->me_key == NULL)
|
1997-01-16 21:06:45 +00:00
|
|
|
return ep;
|
1997-01-28 00:00:11 +00:00
|
|
|
if (ep->me_key == dummy)
|
1997-01-16 21:06:45 +00:00
|
|
|
freeslot = ep;
|
1997-01-28 00:00:11 +00:00
|
|
|
else if (ep->me_key == key ||
|
|
|
|
(ep->me_hash == hash && cmpobject(ep->me_key, key) == 0)) {
|
|
|
|
return ep;
|
1997-01-18 07:55:05 +00:00
|
|
|
}
|
1997-01-29 15:53:56 +00:00
|
|
|
/* Derive incr from sum, just to make it more arbitrary. Note that
|
1997-01-28 00:00:11 +00:00
|
|
|
incr must not be 0, or we will get into an infinite loop.*/
|
1997-01-29 15:53:56 +00:00
|
|
|
incr = (sum ^ (sum >> 3)) & mask;
|
1997-01-28 00:00:11 +00:00
|
|
|
if (!incr)
|
|
|
|
incr = mask;
|
|
|
|
if (incr > mask) /* Cycle through GF(2^n)-{0} */
|
|
|
|
incr ^= mp->ma_poly; /* This will implicitly clear the
|
|
|
|
highest bit */
|
|
|
|
for (;;) {
|
1997-01-29 15:53:56 +00:00
|
|
|
ep = &ep0[(i+incr)&mask];
|
1997-01-28 00:00:11 +00:00
|
|
|
if (ep->me_key == NULL) {
|
|
|
|
if (freeslot != NULL)
|
|
|
|
return freeslot;
|
|
|
|
else
|
1993-03-27 18:11:32 +00:00
|
|
|
return ep;
|
1997-01-28 00:00:11 +00:00
|
|
|
}
|
|
|
|
if (ep->me_key == dummy) {
|
|
|
|
if (freeslot == NULL)
|
1993-03-27 18:11:32 +00:00
|
|
|
freeslot = ep;
|
|
|
|
}
|
1997-01-28 00:00:11 +00:00
|
|
|
else if (ep->me_key == key ||
|
|
|
|
(ep->me_hash == hash &&
|
|
|
|
cmpobject(ep->me_key, key) == 0)) {
|
1993-03-27 18:11:32 +00:00
|
|
|
return ep;
|
1997-01-28 00:00:11 +00:00
|
|
|
}
|
|
|
|
/* Cycle through GF(2^n)-{0} */
|
|
|
|
incr = incr << 1;
|
|
|
|
if (incr > mask)
|
|
|
|
incr ^= mp->ma_poly;
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Internal routine to insert a new item into the table.
|
|
|
|
Used both by the internal resize routine and by the public insert routine.
|
|
|
|
Eats a reference to key and one to value.
|
|
|
|
*/
|
|
|
|
static void insertmapping PROTO((mappingobject *, object *, long, object *));
|
|
|
|
static void
|
|
|
|
insertmapping(mp, key, hash, value)
|
|
|
|
register mappingobject *mp;
|
|
|
|
object *key;
|
|
|
|
long hash;
|
|
|
|
object *value;
|
|
|
|
{
|
1995-01-02 19:07:15 +00:00
|
|
|
object *old_value;
|
1993-03-27 18:11:32 +00:00
|
|
|
register mappingentry *ep;
|
|
|
|
ep = lookmapping(mp, key, hash);
|
|
|
|
if (ep->me_value != NULL) {
|
1995-01-02 19:07:15 +00:00
|
|
|
old_value = ep->me_value;
|
|
|
|
ep->me_value = value;
|
|
|
|
DECREF(old_value); /* which **CAN** re-enter */
|
1993-03-27 18:11:32 +00:00
|
|
|
DECREF(key);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (ep->me_key == NULL)
|
|
|
|
mp->ma_fill++;
|
|
|
|
else
|
|
|
|
DECREF(ep->me_key);
|
|
|
|
ep->me_key = key;
|
|
|
|
ep->me_hash = hash;
|
1995-01-02 19:07:15 +00:00
|
|
|
ep->me_value = value;
|
1993-03-27 18:11:32 +00:00
|
|
|
mp->ma_used++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Restructure the table by allocating a new table and reinserting all
|
|
|
|
items again. When entries have been deleted, the new table may
|
|
|
|
actually be smaller than the old one.
|
|
|
|
*/
|
|
|
|
static int mappingresize PROTO((mappingobject *));
|
|
|
|
static int
|
|
|
|
mappingresize(mp)
|
|
|
|
mappingobject *mp;
|
|
|
|
{
|
|
|
|
register int oldsize = mp->ma_size;
|
1997-01-28 00:00:11 +00:00
|
|
|
register int newsize, newpoly;
|
1993-03-27 18:11:32 +00:00
|
|
|
register mappingentry *oldtable = mp->ma_table;
|
|
|
|
register mappingentry *newtable;
|
|
|
|
register mappingentry *ep;
|
|
|
|
register int i;
|
|
|
|
newsize = mp->ma_size;
|
1997-01-28 00:00:11 +00:00
|
|
|
for (i = 0, newsize = MINSIZE; ; i++, newsize <<= 1) {
|
|
|
|
if (i > sizeof(polys)/sizeof(polys[0])) {
|
|
|
|
/* Ran out of polynomials */
|
1994-08-30 08:27:36 +00:00
|
|
|
err_nomem();
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-28 00:00:11 +00:00
|
|
|
if (newsize > mp->ma_used*2) {
|
|
|
|
newpoly = polys[i];
|
1993-03-27 18:11:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newtable = (mappingentry *) calloc(sizeof(mappingentry), newsize);
|
|
|
|
if (newtable == NULL) {
|
|
|
|
err_nomem();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mp->ma_size = newsize;
|
1997-01-28 00:00:11 +00:00
|
|
|
mp->ma_poly = newpoly;
|
1993-03-27 18:11:32 +00:00
|
|
|
mp->ma_table = newtable;
|
|
|
|
mp->ma_fill = 0;
|
|
|
|
mp->ma_used = 0;
|
1995-01-02 19:07:15 +00:00
|
|
|
|
|
|
|
/* Make two passes, so we can avoid decrefs
|
|
|
|
(and possible side effects) till the table is copied */
|
1993-03-27 18:11:32 +00:00
|
|
|
for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
|
|
|
|
if (ep->me_value != NULL)
|
|
|
|
insertmapping(mp,ep->me_key,ep->me_hash,ep->me_value);
|
1995-01-02 19:07:15 +00:00
|
|
|
}
|
|
|
|
for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
|
|
|
|
if (ep->me_value == NULL)
|
1993-03-27 18:11:32 +00:00
|
|
|
XDECREF(ep->me_key);
|
|
|
|
}
|
1995-01-02 19:07:15 +00:00
|
|
|
|
|
|
|
XDEL(oldtable);
|
1993-03-27 18:11:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
|
|
|
mappinglookup(op, key)
|
|
|
|
object *op;
|
|
|
|
object *key;
|
|
|
|
{
|
|
|
|
long hash;
|
|
|
|
if (!is_mappingobject(op)) {
|
|
|
|
err_badcall();
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-02 19:07:15 +00:00
|
|
|
if (((mappingobject *)op)->ma_table == NULL)
|
|
|
|
return NULL;
|
1993-10-22 12:04:32 +00:00
|
|
|
#ifdef CACHE_HASH
|
1997-01-23 19:39:29 +00:00
|
|
|
if (!is_stringobject(key) ||
|
|
|
|
(hash = ((stringobject *) key)->ob_shash) == -1)
|
1993-10-22 12:04:32 +00:00
|
|
|
#endif
|
1997-01-23 19:39:29 +00:00
|
|
|
{
|
|
|
|
hash = hashobject(key);
|
|
|
|
if (hash == -1)
|
|
|
|
return NULL;
|
|
|
|
}
|
1993-03-27 18:11:32 +00:00
|
|
|
return lookmapping((mappingobject *)op, key, hash) -> me_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
mappinginsert(op, key, value)
|
|
|
|
register object *op;
|
|
|
|
object *key;
|
|
|
|
object *value;
|
|
|
|
{
|
|
|
|
register mappingobject *mp;
|
|
|
|
register long hash;
|
|
|
|
if (!is_mappingobject(op)) {
|
|
|
|
err_badcall();
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-18 07:55:05 +00:00
|
|
|
mp = (mappingobject *)op;
|
1993-10-22 12:04:32 +00:00
|
|
|
#ifdef CACHE_HASH
|
1997-01-18 07:55:05 +00:00
|
|
|
if (is_stringobject(key)) {
|
|
|
|
#ifdef INTERN_STRINGS
|
|
|
|
if (((stringobject *)key)->ob_sinterned != NULL) {
|
|
|
|
key = ((stringobject *)key)->ob_sinterned;
|
|
|
|
hash = ((stringobject *)key)->ob_shash;
|
|
|
|
}
|
|
|
|
else
|
1993-10-22 12:04:32 +00:00
|
|
|
#endif
|
1997-01-18 07:55:05 +00:00
|
|
|
{
|
|
|
|
hash = ((stringobject *)key)->ob_shash;
|
|
|
|
if (hash == -1)
|
|
|
|
hash = hashobject(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
hash = hashobject(key);
|
|
|
|
if (hash == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
1993-03-27 18:11:32 +00:00
|
|
|
/* if fill >= 2/3 size, resize */
|
|
|
|
if (mp->ma_fill*3 >= mp->ma_size*2) {
|
|
|
|
if (mappingresize(mp) != 0) {
|
|
|
|
if (mp->ma_fill+1 > mp->ma_size)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
INCREF(value);
|
|
|
|
INCREF(key);
|
|
|
|
insertmapping(mp, key, hash, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
mappingremove(op, key)
|
|
|
|
object *op;
|
|
|
|
object *key;
|
|
|
|
{
|
|
|
|
register mappingobject *mp;
|
|
|
|
register long hash;
|
|
|
|
register mappingentry *ep;
|
1995-01-02 19:07:15 +00:00
|
|
|
object *old_value, *old_key;
|
|
|
|
|
1993-03-27 18:11:32 +00:00
|
|
|
if (!is_mappingobject(op)) {
|
|
|
|
err_badcall();
|
|
|
|
return -1;
|
|
|
|
}
|
1993-10-22 12:04:32 +00:00
|
|
|
#ifdef CACHE_HASH
|
1997-01-23 19:39:29 +00:00
|
|
|
if (!is_stringobject(key) ||
|
|
|
|
(hash = ((stringobject *) key)->ob_shash) == -1)
|
1993-10-22 12:04:32 +00:00
|
|
|
#endif
|
1997-01-23 19:39:29 +00:00
|
|
|
{
|
|
|
|
hash = hashobject(key);
|
|
|
|
if (hash == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
1993-03-27 18:11:32 +00:00
|
|
|
mp = (mappingobject *)op;
|
1995-01-02 19:42:39 +00:00
|
|
|
if (((mappingobject *)op)->ma_table == NULL)
|
|
|
|
goto empty;
|
1993-03-27 18:11:32 +00:00
|
|
|
ep = lookmapping(mp, key, hash);
|
|
|
|
if (ep->me_value == NULL) {
|
1995-01-02 19:42:39 +00:00
|
|
|
empty:
|
1993-03-27 18:11:32 +00:00
|
|
|
err_setval(KeyError, key);
|
|
|
|
return -1;
|
|
|
|
}
|
1995-01-02 19:07:15 +00:00
|
|
|
old_key = ep->me_key;
|
1993-03-27 18:11:32 +00:00
|
|
|
INCREF(dummy);
|
|
|
|
ep->me_key = dummy;
|
1995-01-02 19:07:15 +00:00
|
|
|
old_value = ep->me_value;
|
1993-03-27 18:11:32 +00:00
|
|
|
ep->me_value = NULL;
|
|
|
|
mp->ma_used--;
|
1995-01-02 19:07:15 +00:00
|
|
|
DECREF(old_value);
|
|
|
|
DECREF(old_key);
|
1993-03-27 18:11:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-05-19 14:50:45 +00:00
|
|
|
void
|
|
|
|
mappingclear(op)
|
|
|
|
object *op;
|
1993-03-27 18:11:32 +00:00
|
|
|
{
|
1995-01-02 19:07:15 +00:00
|
|
|
int i, n;
|
|
|
|
register mappingentry *table;
|
|
|
|
mappingobject *mp;
|
1993-05-19 14:50:45 +00:00
|
|
|
if (!is_mappingobject(op))
|
|
|
|
return;
|
|
|
|
mp = (mappingobject *)op;
|
1995-01-02 19:07:15 +00:00
|
|
|
table = mp->ma_table;
|
|
|
|
if (table == NULL)
|
|
|
|
return;
|
|
|
|
n = mp->ma_size;
|
|
|
|
mp->ma_size = mp->ma_used = mp->ma_fill = 0;
|
|
|
|
mp->ma_table = NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
XDECREF(table[i].me_key);
|
|
|
|
XDECREF(table[i].me_value);
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
1995-01-02 19:07:15 +00:00
|
|
|
DEL(table);
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
|
|
|
|
1993-05-19 14:50:45 +00:00
|
|
|
int
|
|
|
|
mappinggetnext(op, ppos, pkey, pvalue)
|
1993-03-27 18:11:32 +00:00
|
|
|
object *op;
|
1993-05-19 14:50:45 +00:00
|
|
|
int *ppos;
|
|
|
|
object **pkey;
|
|
|
|
object **pvalue;
|
1993-03-27 18:11:32 +00:00
|
|
|
{
|
1993-05-19 14:50:45 +00:00
|
|
|
int i;
|
1993-03-27 18:11:32 +00:00
|
|
|
register mappingobject *mp;
|
1993-05-19 14:50:45 +00:00
|
|
|
if (!is_dictobject(op))
|
|
|
|
return 0;
|
1993-03-27 18:11:32 +00:00
|
|
|
mp = (mappingobject *)op;
|
1993-05-19 14:50:45 +00:00
|
|
|
i = *ppos;
|
|
|
|
if (i < 0)
|
|
|
|
return 0;
|
|
|
|
while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
|
|
|
|
i++;
|
|
|
|
*ppos = i+1;
|
|
|
|
if (i >= mp->ma_size)
|
|
|
|
return 0;
|
|
|
|
if (pkey)
|
|
|
|
*pkey = mp->ma_table[i].me_key;
|
|
|
|
if (pvalue)
|
|
|
|
*pvalue = mp->ma_table[i].me_value;
|
|
|
|
return 1;
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
static void
|
|
|
|
mapping_dealloc(mp)
|
|
|
|
register mappingobject *mp;
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register mappingentry *ep;
|
|
|
|
for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
|
|
|
|
if (ep->me_key != NULL)
|
|
|
|
DECREF(ep->me_key);
|
|
|
|
if (ep->me_value != NULL)
|
|
|
|
DECREF(ep->me_value);
|
|
|
|
}
|
1995-01-02 19:07:15 +00:00
|
|
|
XDEL(mp->ma_table);
|
1993-03-27 18:11:32 +00:00
|
|
|
DEL(mp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mapping_print(mp, fp, flags)
|
|
|
|
register mappingobject *mp;
|
|
|
|
register FILE *fp;
|
|
|
|
register int flags;
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register int any;
|
|
|
|
register mappingentry *ep;
|
|
|
|
fprintf(fp, "{");
|
|
|
|
any = 0;
|
|
|
|
for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
|
|
|
|
if (ep->me_value != NULL) {
|
|
|
|
if (any++ > 0)
|
|
|
|
fprintf(fp, ", ");
|
1993-11-10 09:23:53 +00:00
|
|
|
if (printobject((object *)ep->me_key, fp, 0) != 0)
|
1993-03-27 18:11:32 +00:00
|
|
|
return -1;
|
|
|
|
fprintf(fp, ": ");
|
1993-11-10 09:23:53 +00:00
|
|
|
if (printobject(ep->me_value, fp, 0) != 0)
|
1993-03-27 18:11:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(fp, "}");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
mapping_repr(mp)
|
|
|
|
mappingobject *mp;
|
|
|
|
{
|
|
|
|
auto object *v;
|
|
|
|
object *sepa, *colon;
|
|
|
|
register int i;
|
|
|
|
register int any;
|
|
|
|
register mappingentry *ep;
|
|
|
|
v = newstringobject("{");
|
|
|
|
sepa = newstringobject(", ");
|
|
|
|
colon = newstringobject(": ");
|
|
|
|
any = 0;
|
1994-08-30 08:27:36 +00:00
|
|
|
for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) {
|
1993-03-27 18:11:32 +00:00
|
|
|
if (ep->me_value != NULL) {
|
|
|
|
if (any++)
|
|
|
|
joinstring(&v, sepa);
|
1994-08-30 08:27:36 +00:00
|
|
|
joinstring_decref(&v, reprobject(ep->me_key));
|
1993-03-27 18:11:32 +00:00
|
|
|
joinstring(&v, colon);
|
1994-08-30 08:27:36 +00:00
|
|
|
joinstring_decref(&v, reprobject(ep->me_value));
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
1994-08-30 08:27:36 +00:00
|
|
|
joinstring_decref(&v, newstringobject("}"));
|
1993-03-27 18:11:32 +00:00
|
|
|
XDECREF(sepa);
|
|
|
|
XDECREF(colon);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mapping_length(mp)
|
|
|
|
mappingobject *mp;
|
|
|
|
{
|
|
|
|
return mp->ma_used;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
mapping_subscript(mp, key)
|
|
|
|
mappingobject *mp;
|
|
|
|
register object *key;
|
|
|
|
{
|
|
|
|
object *v;
|
1993-10-22 12:04:32 +00:00
|
|
|
long hash;
|
1995-01-02 19:07:15 +00:00
|
|
|
if (mp->ma_table == NULL) {
|
|
|
|
err_setval(KeyError, key);
|
|
|
|
return NULL;
|
|
|
|
}
|
1993-10-22 12:04:32 +00:00
|
|
|
#ifdef CACHE_HASH
|
1997-01-23 19:39:29 +00:00
|
|
|
if (!is_stringobject(key) ||
|
|
|
|
(hash = ((stringobject *) key)->ob_shash) == -1)
|
1993-10-22 12:04:32 +00:00
|
|
|
#endif
|
1997-01-23 19:39:29 +00:00
|
|
|
{
|
|
|
|
hash = hashobject(key);
|
|
|
|
if (hash == -1)
|
|
|
|
return NULL;
|
|
|
|
}
|
1993-03-27 18:11:32 +00:00
|
|
|
v = lookmapping(mp, key, hash) -> me_value;
|
|
|
|
if (v == NULL)
|
|
|
|
err_setval(KeyError, key);
|
|
|
|
else
|
|
|
|
INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mapping_ass_sub(mp, v, w)
|
|
|
|
mappingobject *mp;
|
|
|
|
object *v, *w;
|
|
|
|
{
|
|
|
|
if (w == NULL)
|
|
|
|
return mappingremove((object *)mp, v);
|
|
|
|
else
|
|
|
|
return mappinginsert((object *)mp, v, w);
|
|
|
|
}
|
|
|
|
|
|
|
|
static mapping_methods mapping_as_mapping = {
|
1994-08-30 08:27:36 +00:00
|
|
|
(inquiry)mapping_length, /*mp_length*/
|
|
|
|
(binaryfunc)mapping_subscript, /*mp_subscript*/
|
|
|
|
(objobjargproc)mapping_ass_sub, /*mp_ass_subscript*/
|
1993-03-27 18:11:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static object *
|
|
|
|
mapping_keys(mp, args)
|
|
|
|
register mappingobject *mp;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
register object *v;
|
|
|
|
register int i, j;
|
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
|
|
|
v = newlistobject(mp->ma_used);
|
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0, j = 0; i < mp->ma_size; i++) {
|
|
|
|
if (mp->ma_table[i].me_value != NULL) {
|
|
|
|
object *key = mp->ma_table[i].me_key;
|
|
|
|
INCREF(key);
|
|
|
|
setlistitem(v, j, key);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1993-05-19 14:50:45 +00:00
|
|
|
static object *
|
|
|
|
mapping_values(mp, args)
|
|
|
|
register mappingobject *mp;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
register object *v;
|
|
|
|
register int i, j;
|
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
|
|
|
v = newlistobject(mp->ma_used);
|
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0, j = 0; i < mp->ma_size; i++) {
|
|
|
|
if (mp->ma_table[i].me_value != NULL) {
|
|
|
|
object *value = mp->ma_table[i].me_value;
|
|
|
|
INCREF(value);
|
|
|
|
setlistitem(v, j, value);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
mapping_items(mp, args)
|
|
|
|
register mappingobject *mp;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
register object *v;
|
|
|
|
register int i, j;
|
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
|
|
|
v = newlistobject(mp->ma_used);
|
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0, j = 0; i < mp->ma_size; i++) {
|
|
|
|
if (mp->ma_table[i].me_value != NULL) {
|
|
|
|
object *key = mp->ma_table[i].me_key;
|
|
|
|
object *value = mp->ma_table[i].me_value;
|
|
|
|
object *item = newtupleobject(2);
|
|
|
|
if (item == NULL) {
|
|
|
|
DECREF(v);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
INCREF(key);
|
|
|
|
settupleitem(item, 0, key);
|
|
|
|
INCREF(value);
|
|
|
|
settupleitem(item, 1, value);
|
|
|
|
setlistitem(v, j, item);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1993-11-05 10:18:44 +00:00
|
|
|
int
|
|
|
|
getmappingsize(mp)
|
|
|
|
object *mp;
|
|
|
|
{
|
|
|
|
if (mp == NULL || !is_mappingobject(mp)) {
|
|
|
|
err_badcall();
|
1993-11-23 17:53:17 +00:00
|
|
|
return 0;
|
1993-11-05 10:18:44 +00:00
|
|
|
}
|
|
|
|
return ((mappingobject *)mp)->ma_used;
|
|
|
|
}
|
|
|
|
|
1993-03-27 18:11:32 +00:00
|
|
|
object *
|
|
|
|
getmappingkeys(mp)
|
|
|
|
object *mp;
|
|
|
|
{
|
|
|
|
if (mp == NULL || !is_mappingobject(mp)) {
|
|
|
|
err_badcall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return mapping_keys((mappingobject *)mp, (object *)NULL);
|
|
|
|
}
|
|
|
|
|
1993-05-19 14:50:45 +00:00
|
|
|
object *
|
|
|
|
getmappingvalues(mp)
|
|
|
|
object *mp;
|
|
|
|
{
|
|
|
|
if (mp == NULL || !is_mappingobject(mp)) {
|
|
|
|
err_badcall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return mapping_values((mappingobject *)mp, (object *)NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
|
|
|
getmappingitems(mp)
|
|
|
|
object *mp;
|
|
|
|
{
|
|
|
|
if (mp == NULL || !is_mappingobject(mp)) {
|
|
|
|
err_badcall();
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-30 08:27:36 +00:00
|
|
|
return mapping_items((mappingobject *)mp, (object *)NULL);
|
1993-05-19 14:50:45 +00:00
|
|
|
}
|
|
|
|
|
1996-12-05 21:55:55 +00:00
|
|
|
#define NEWCMP
|
|
|
|
|
|
|
|
#ifdef NEWCMP
|
|
|
|
|
|
|
|
/* Subroutine which returns the smallest key in a for which b's value
|
|
|
|
is different or absent. The value is returned too, through the
|
|
|
|
pval argument. No reference counts are incremented. */
|
|
|
|
|
|
|
|
static object *
|
|
|
|
characterize(a, b, pval)
|
|
|
|
mappingobject *a;
|
|
|
|
mappingobject *b;
|
|
|
|
object **pval;
|
|
|
|
{
|
|
|
|
object *diff = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*pval = NULL;
|
|
|
|
for (i = 0; i < a->ma_size; i++) {
|
|
|
|
if (a->ma_table[i].me_value != NULL) {
|
|
|
|
object *key = a->ma_table[i].me_key;
|
|
|
|
object *aval, *bval;
|
|
|
|
if (diff != NULL && cmpobject(key, diff) > 0)
|
|
|
|
continue;
|
|
|
|
aval = a->ma_table[i].me_value;
|
|
|
|
bval = mappinglookup((object *)b, key);
|
|
|
|
if (bval == NULL || cmpobject(aval, bval) != 0) {
|
|
|
|
diff = key;
|
|
|
|
*pval = aval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mapping_compare(a, b)
|
|
|
|
mappingobject *a, *b;
|
|
|
|
{
|
|
|
|
object *adiff, *bdiff, *aval, *bval;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* Compare lengths first */
|
|
|
|
if (a->ma_used < b->ma_used)
|
|
|
|
return -1; /* a is shorter */
|
|
|
|
else if (a->ma_used > b->ma_used)
|
|
|
|
return 1; /* b is shorter */
|
|
|
|
/* Same length -- check all keys */
|
|
|
|
adiff = characterize(a, b, &aval);
|
|
|
|
if (adiff == NULL)
|
|
|
|
return 0; /* a is a subset with the same length */
|
|
|
|
bdiff = characterize(b, a, &bval);
|
|
|
|
/* bdiff == NULL would be impossible now */
|
|
|
|
res = cmpobject(adiff, bdiff);
|
|
|
|
if (res == 0)
|
|
|
|
res = cmpobject(aval, bval);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !NEWCMP */
|
|
|
|
|
1993-03-27 18:11:32 +00:00
|
|
|
static int
|
|
|
|
mapping_compare(a, b)
|
|
|
|
mappingobject *a, *b;
|
|
|
|
{
|
|
|
|
object *akeys, *bkeys;
|
|
|
|
int i, n, res;
|
|
|
|
if (a == b)
|
|
|
|
return 0;
|
|
|
|
if (a->ma_used == 0) {
|
|
|
|
if (b->ma_used != 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (b->ma_used == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
akeys = mapping_keys(a, (object *)NULL);
|
|
|
|
bkeys = mapping_keys(b, (object *)NULL);
|
|
|
|
if (akeys == NULL || bkeys == NULL) {
|
|
|
|
/* Oops, out of memory -- what to do? */
|
|
|
|
/* For now, sort on address! */
|
|
|
|
XDECREF(akeys);
|
|
|
|
XDECREF(bkeys);
|
|
|
|
if (a < b)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
sortlist(akeys);
|
|
|
|
sortlist(bkeys);
|
|
|
|
n = a->ma_used < b->ma_used ? a->ma_used : b->ma_used; /* smallest */
|
|
|
|
res = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
object *akey, *bkey, *aval, *bval;
|
|
|
|
long ahash, bhash;
|
|
|
|
akey = getlistitem(akeys, i);
|
|
|
|
bkey = getlistitem(bkeys, i);
|
|
|
|
res = cmpobject(akey, bkey);
|
|
|
|
if (res != 0)
|
|
|
|
break;
|
1993-10-22 12:04:32 +00:00
|
|
|
#ifdef CACHE_HASH
|
1997-01-23 19:39:29 +00:00
|
|
|
if (!is_stringobject(akey) ||
|
|
|
|
(ahash = ((stringobject *) akey)->ob_shash) == -1)
|
1993-10-22 12:04:32 +00:00
|
|
|
#endif
|
1997-01-23 19:39:29 +00:00
|
|
|
{
|
|
|
|
ahash = hashobject(akey);
|
|
|
|
if (ahash == -1)
|
|
|
|
err_clear(); /* Don't want errors here */
|
|
|
|
}
|
1993-10-22 12:04:32 +00:00
|
|
|
#ifdef CACHE_HASH
|
1997-01-23 19:39:29 +00:00
|
|
|
if (!is_stringobject(bkey) ||
|
|
|
|
(bhash = ((stringobject *) bkey)->ob_shash) == -1)
|
1993-10-22 12:04:32 +00:00
|
|
|
#endif
|
1997-01-23 19:39:29 +00:00
|
|
|
{
|
|
|
|
bhash = hashobject(bkey);
|
|
|
|
if (bhash == -1)
|
|
|
|
err_clear(); /* Don't want errors here */
|
|
|
|
}
|
1993-03-27 18:11:32 +00:00
|
|
|
aval = lookmapping(a, akey, ahash) -> me_value;
|
|
|
|
bval = lookmapping(b, bkey, bhash) -> me_value;
|
|
|
|
res = cmpobject(aval, bval);
|
|
|
|
if (res != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
if (a->ma_used < b->ma_used)
|
|
|
|
res = -1;
|
|
|
|
else if (a->ma_used > b->ma_used)
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
DECREF(akeys);
|
|
|
|
DECREF(bkeys);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1996-12-05 21:55:55 +00:00
|
|
|
#endif /* !NEWCMP */
|
|
|
|
|
1993-03-27 18:11:32 +00:00
|
|
|
static object *
|
|
|
|
mapping_has_key(mp, args)
|
|
|
|
register mappingobject *mp;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *key;
|
|
|
|
long hash;
|
|
|
|
register long ok;
|
|
|
|
if (!getargs(args, "O", &key))
|
|
|
|
return NULL;
|
1993-10-22 12:04:32 +00:00
|
|
|
#ifdef CACHE_HASH
|
1997-01-23 19:39:29 +00:00
|
|
|
if (!is_stringobject(key) ||
|
|
|
|
(hash = ((stringobject *) key)->ob_shash) == -1)
|
1993-10-22 12:04:32 +00:00
|
|
|
#endif
|
1997-01-23 19:39:29 +00:00
|
|
|
{
|
|
|
|
hash = hashobject(key);
|
|
|
|
if (hash == -1)
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-02 19:07:15 +00:00
|
|
|
ok = mp->ma_size != 0 && lookmapping(mp, key, hash)->me_value != NULL;
|
1993-03-27 18:11:32 +00:00
|
|
|
return newintobject(ok);
|
|
|
|
}
|
|
|
|
|
1997-03-21 21:55:12 +00:00
|
|
|
static object *
|
|
|
|
mapping_clear(mp, args)
|
|
|
|
register mappingobject *mp;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
|
|
|
mappingclear((object *)mp);
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1993-03-27 18:11:32 +00:00
|
|
|
static struct methodlist mapp_methods[] = {
|
1997-03-21 21:55:12 +00:00
|
|
|
{"clear", (method)mapping_clear},
|
1994-08-30 08:27:36 +00:00
|
|
|
{"has_key", (method)mapping_has_key},
|
|
|
|
{"items", (method)mapping_items},
|
|
|
|
{"keys", (method)mapping_keys},
|
|
|
|
{"values", (method)mapping_values},
|
1993-03-27 18:11:32 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static object *
|
|
|
|
mapping_getattr(mp, name)
|
|
|
|
mappingobject *mp;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
return findmethod(mapp_methods, (object *)mp, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
typeobject Mappingtype = {
|
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
1993-03-29 10:43:31 +00:00
|
|
|
"dictionary",
|
1993-03-27 18:11:32 +00:00
|
|
|
sizeof(mappingobject),
|
|
|
|
0,
|
1994-08-30 08:27:36 +00:00
|
|
|
(destructor)mapping_dealloc, /*tp_dealloc*/
|
|
|
|
(printfunc)mapping_print, /*tp_print*/
|
|
|
|
(getattrfunc)mapping_getattr, /*tp_getattr*/
|
1993-03-27 18:11:32 +00:00
|
|
|
0, /*tp_setattr*/
|
1994-08-30 08:27:36 +00:00
|
|
|
(cmpfunc)mapping_compare, /*tp_compare*/
|
|
|
|
(reprfunc)mapping_repr, /*tp_repr*/
|
1993-03-27 18:11:32 +00:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
&mapping_as_mapping, /*tp_as_mapping*/
|
|
|
|
};
|
|
|
|
|
|
|
|
/* For backward compatibility with old dictionary interface */
|
|
|
|
|
|
|
|
static object *last_name_object;
|
1995-12-08 01:16:31 +00:00
|
|
|
static char *last_name_char; /* NULL or == getstringvalue(last_name_object) */
|
1993-03-27 18:11:32 +00:00
|
|
|
|
|
|
|
object *
|
|
|
|
getattro(v, name)
|
|
|
|
object *v;
|
|
|
|
object *name;
|
|
|
|
{
|
1996-08-09 20:52:03 +00:00
|
|
|
if (v->ob_type->tp_getattro != NULL)
|
|
|
|
return (*v->ob_type->tp_getattro)(v, name);
|
|
|
|
|
1993-03-27 18:11:32 +00:00
|
|
|
if (name != last_name_object) {
|
|
|
|
XDECREF(last_name_object);
|
|
|
|
INCREF(name);
|
|
|
|
last_name_object = name;
|
|
|
|
last_name_char = getstringvalue(name);
|
|
|
|
}
|
|
|
|
return getattr(v, last_name_char);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
setattro(v, name, value)
|
|
|
|
object *v;
|
|
|
|
object *name;
|
|
|
|
object *value;
|
|
|
|
{
|
1997-01-18 07:55:05 +00:00
|
|
|
int err;
|
|
|
|
INCREF(name);
|
|
|
|
PyString_InternInPlace(&name);
|
1996-08-09 20:52:03 +00:00
|
|
|
if (v->ob_type->tp_setattro != NULL)
|
1997-01-18 07:55:05 +00:00
|
|
|
err = (*v->ob_type->tp_setattro)(v, name, value);
|
|
|
|
else {
|
|
|
|
if (name != last_name_object) {
|
|
|
|
XDECREF(last_name_object);
|
|
|
|
INCREF(name);
|
|
|
|
last_name_object = name;
|
|
|
|
last_name_char = getstringvalue(name);
|
|
|
|
}
|
|
|
|
err = setattr(v, last_name_char, value);
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
1997-01-18 07:55:05 +00:00
|
|
|
DECREF(name);
|
|
|
|
return err;
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
|
|
|
dictlookup(v, key)
|
|
|
|
object *v;
|
|
|
|
char *key;
|
|
|
|
{
|
1995-12-08 01:16:31 +00:00
|
|
|
if (key != last_name_char) {
|
1993-03-27 18:11:32 +00:00
|
|
|
XDECREF(last_name_object);
|
|
|
|
last_name_object = newstringobject(key);
|
|
|
|
if (last_name_object == NULL) {
|
|
|
|
last_name_char = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-18 07:55:05 +00:00
|
|
|
PyString_InternInPlace(&last_name_object);
|
1995-12-08 01:16:31 +00:00
|
|
|
last_name_char = getstringvalue(last_name_object);
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
|
|
|
return mappinglookup(v, last_name_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dictinsert(v, key, item)
|
|
|
|
object *v;
|
|
|
|
char *key;
|
|
|
|
object *item;
|
|
|
|
{
|
1995-12-08 01:16:31 +00:00
|
|
|
if (key != last_name_char) {
|
1993-03-27 18:11:32 +00:00
|
|
|
XDECREF(last_name_object);
|
|
|
|
last_name_object = newstringobject(key);
|
|
|
|
if (last_name_object == NULL) {
|
|
|
|
last_name_char = NULL;
|
1993-07-29 08:25:09 +00:00
|
|
|
return -1;
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
1997-01-18 07:55:05 +00:00
|
|
|
PyString_InternInPlace(&last_name_object);
|
1995-12-08 01:16:31 +00:00
|
|
|
last_name_char = getstringvalue(last_name_object);
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
|
|
|
return mappinginsert(v, last_name_object, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dictremove(v, key)
|
|
|
|
object *v;
|
|
|
|
char *key;
|
|
|
|
{
|
1995-12-08 01:16:31 +00:00
|
|
|
if (key != last_name_char) {
|
1993-03-27 18:11:32 +00:00
|
|
|
XDECREF(last_name_object);
|
|
|
|
last_name_object = newstringobject(key);
|
|
|
|
if (last_name_object == NULL) {
|
|
|
|
last_name_char = NULL;
|
1993-07-29 08:25:09 +00:00
|
|
|
return -1;
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
1995-12-08 01:16:31 +00:00
|
|
|
last_name_char = getstringvalue(last_name_object);
|
1993-03-27 18:11:32 +00:00
|
|
|
}
|
|
|
|
return mappingremove(v, last_name_object);
|
|
|
|
}
|