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