-
Notifications
You must be signed in to change notification settings - Fork 18
/
bliftoaig.c
3952 lines (3399 loc) · 94.4 KB
/
bliftoaig.c
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
Copyright (c) 2012, Fabio Somenzi, University of Colorado.
Copyright (c) 2006-2011, Armin Biere, Johannes Kepler University.
Copyright (c) 2006, Marc Herbstritt, University of Freiburg.
Copyright (c) 1995-2004, Regents of the University of Colorado.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the authors, nor the names of contributors, nor the name
of the copyright holders, including the name of the Johannes Kepler
University, the name of the University of Freiburg, or the name of the
University of Colorado may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/
/*------------------------------------------------------------------------*/
/* The BLIF parsing routines are borrowed from CUDD and are slightly */
/* adjusted to AIGER. The files are maintained in directory cudd_util. */
/* Some CUDD routines that were initially coupled with the CUDD BDD */
/* manager are adapted to AIGER, see below. */
/*------------------------------------------------------------------------*/
#include "aiger.h"
/*------------------------------------------------------------------------*/
/* The following code is taken from CUDD 2.4.1. It is mainly a cut&paste */
/* of util.h (and corresponding .c-files), st.[h|c], and bnet.[h|c]. */
/*------------------------------------------------------------------------*/
/*--CUDD::util::begin-----------------------------------------------------*/
#define NIL(type) ((type *) 0)
#define ALLOC(type, num) \
((type *) malloc((long) sizeof(type) * (long) (num)))
#define REALLOC(type, obj, num) \
((type *) realloc((char *) (obj), (long) sizeof(type) * (long) (num)))
#define FREE(obj) \
((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define ABS(a) ((a) < 0 ? -(a) : (a))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
/*--CUDD::util::end-------------------------------------------------------*/
/*--CUDD::st::begin-------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Constant declarations */
/*---------------------------------------------------------------------------*/
#define ST_DEFAULT_MAX_DENSITY 5
#define ST_DEFAULT_INIT_TABLE_SIZE 11
#define ST_DEFAULT_GROW_FACTOR 2.0
#define ST_DEFAULT_REORDER_FLAG 0
#define ST_OUT_OF_MEM -10000
/*---------------------------------------------------------------------------*/
/* Type declarations */
/*---------------------------------------------------------------------------*/
typedef struct st_table_entry st_table_entry;
struct st_table_entry
{
char *key;
char *record;
st_table_entry *next;
};
typedef struct st_table st_table;
struct st_table
{
int (*compare) (const char *, const char *);
int (*hash) (char *, int);
int num_bins;
int num_entries;
int max_density;
int reorder_flag;
double grow_factor;
st_table_entry **bins;
};
typedef struct st_generator st_generator;
struct st_generator
{
st_table *table;
st_table_entry *entry;
int index;
};
enum st_retval
{ ST_CONTINUE, ST_STOP, ST_DELETE };
typedef enum st_retval (*ST_PFSR) (char *, char *, char *);
typedef int (*ST_PFICPCP) (const char *, const char *); /* type for comparison function */
typedef int (*ST_PFICPI) (char *, int); /* type for hash function */
/**AutomaticStart*************************************************************/
/*---------------------------------------------------------------------------*/
/* Function prototypes */
/*---------------------------------------------------------------------------*/
extern st_table *st_init_table_with_params (ST_PFICPCP, ST_PFICPI, int, int,
double, int);
extern st_table *st_init_table (ST_PFICPCP, ST_PFICPI);
extern void st_free_table (st_table *);
extern int st_lookup (st_table *, void *, void *);
extern int st_lookup_int (st_table *, void *, int *);
extern int st_insert (st_table *, void *, void *);
extern int st_add_direct (st_table *, void *, void *);
extern int st_find_or_add (st_table *, void *, void *);
extern int st_find (st_table *, void *, void *);
extern st_table *st_copy (st_table *);
extern int st_delete (st_table *, void *, void *);
extern int st_delete_int (st_table *, void *, int *);
extern int st_foreach (st_table *, ST_PFSR, char *);
extern int st_strhash (char *, int);
extern int st_numhash (char *, int);
extern int st_ptrhash (char *, int);
extern int st_numcmp (const char *, const char *);
extern int st_ptrcmp (const char *, const char *);
extern st_generator *st_init_gen (st_table *);
extern int st_gen (st_generator *, void *, void *);
extern int st_gen_int (st_generator *, void *, int *);
extern void st_free_gen (st_generator *);
/**AutomaticEnd***************************************************************/
/**CFile***********************************************************************
FileName [st.c]
PackageName [st]
Synopsis [Symbol table package.]
Description [The st library provides functions to create, maintain,
and query symbol tables.]
SeeAlso []
Author []
Copyright []
******************************************************************************/
#define ST_NUMCMP(x,y) ((x) != (y))
#define ST_NUMHASH(x,size) (ABS((long)x)%(size))
#define st_shift ((sizeof (void*) == 8) ? 3 : 2)
#define ST_PTRHASH(x,size) ((unsigned int)((unsigned long)(x)>>st_shift)%size)
#define EQUAL(func, x, y) \
((((func) == st_numcmp) || ((func) == st_ptrcmp)) ?\
(ST_NUMCMP((x),(y)) == 0) : ((*func)((x), (y)) == 0))
#define do_hash(key, table)\
((int)((table->hash == st_ptrhash) ? ST_PTRHASH((char *)(key),(table)->num_bins) :\
(table->hash == st_numhash) ? ST_NUMHASH((char *)(key), (table)->num_bins) :\
(*table->hash)((char *)(key), (table)->num_bins)))
#define PTR_NOT_EQUAL(table, ptr, user_key)\
(ptr != NIL(st_table_entry) && !EQUAL(table->compare, (char *)user_key, (ptr)->key))
#define FIND_ENTRY(table, hash_val, key, ptr, last) \
(last) = &(table)->bins[hash_val];\
(ptr) = *(last);\
while (PTR_NOT_EQUAL((table), (ptr), (key))) {\
(last) = &(ptr)->next; (ptr) = *(last);\
}\
if ((ptr) != NIL(st_table_entry) && (table)->reorder_flag) {\
*(last) = (ptr)->next;\
(ptr)->next = (table)->bins[hash_val];\
(table)->bins[hash_val] = (ptr);\
}
/**AutomaticStart*************************************************************/
/*---------------------------------------------------------------------------*/
/* Static function prototypes */
/*---------------------------------------------------------------------------*/
static int rehash (st_table *);
/**AutomaticEnd***************************************************************/
/*---------------------------------------------------------------------------*/
/* Definition of exported functions */
/*---------------------------------------------------------------------------*/
/**Function********************************************************************
Synopsis [Create and initialize a table.]
Description [Create and initialize a table with the comparison function
compare_fn and hash function hash_fn. compare_fn is
<pre>
int compare_fn(const char *key1, const char *key2)
</pre>
It returns <,=,> 0 depending on whether key1 <,=,> key2 by some measure.<p>
hash_fn is
<pre>
int hash_fn(char *key, int modulus)
</pre>
It returns a integer between 0 and modulus-1 such that if
compare_fn(key1,key2) == 0 then hash_fn(key1) == hash_fn(key2).<p>
There are five predefined hash and comparison functions in st.
For keys as numbers:
<pre>
st_numhash(key, modulus) { return (unsigned int) key % modulus; }
</pre>
<pre>
st_numcmp(x,y) { return (int) x - (int) y; }
</pre>
For keys as pointers:
<pre>
st_ptrhash(key, modulus) { return ((unsigned int) key/4) % modulus }
</pre>
<pre>
st_ptrcmp(x,y) { return (int) x - (int) y; }
</pre>
For keys as strings:
<pre>
st_strhash(x,y) - a reasonable hashing function for strings
</pre>
<pre>
strcmp(x,y) - the standard library function
</pre>
It is recommended to use these particular functions if they fit your
needs, since st will recognize certain of them and run more quickly
because of it.]
SideEffects [None]
SeeAlso [st_init_table_with_params st_free_table]
******************************************************************************/
st_table *
st_init_table (ST_PFICPCP compare, ST_PFICPI hash)
{
return st_init_table_with_params (compare, hash, ST_DEFAULT_INIT_TABLE_SIZE,
ST_DEFAULT_MAX_DENSITY,
ST_DEFAULT_GROW_FACTOR,
ST_DEFAULT_REORDER_FLAG);
} /* st_init_table */
/**Function********************************************************************
Synopsis [Create a table with given parameters.]
Description [The full blown table initializer. compare and hash are
the same as in st_init_table. density is the largest the average
number of entries per hash bin there should be before the table is
grown. grow_factor is the factor the table is grown by when it
becomes too full. size is the initial number of bins to be allocated
for the hash table. If reorder_flag is non-zero, then every time an
entry is found, it is moved to the top of the chain.<p>
st_init_table(compare, hash) is equivelent to
<pre>
st_init_table_with_params(compare, hash, ST_DEFAULT_INIT_TABLE_SIZE,
ST_DEFAULT_MAX_DENSITY,
ST_DEFAULT_GROW_FACTOR,
ST_DEFAULT_REORDER_FLAG);
</pre>
]
SideEffects [None]
SeeAlso [st_init_table st_free_table]
******************************************************************************/
st_table *
st_init_table_with_params (ST_PFICPCP compare,
ST_PFICPI hash,
int size,
int density, double grow_factor, int reorder_flag)
{
int i;
st_table *newt;
newt = ALLOC (st_table, 1);
if (newt == NIL (st_table))
{
return NIL (st_table);
}
newt->compare = compare;
newt->hash = hash;
newt->num_entries = 0;
newt->max_density = density;
newt->grow_factor = grow_factor;
newt->reorder_flag = reorder_flag;
if (size <= 0)
{
size = 1;
}
newt->num_bins = size;
newt->bins = ALLOC (st_table_entry *, size);
if (newt->bins == NIL (st_table_entry *))
{
FREE (newt);
return NIL (st_table);
}
for (i = 0; i < size; i++)
{
newt->bins[i] = 0;
}
return newt;
} /* st_init_table_with_params */
/**Function********************************************************************
Synopsis [Free a table.]
Description [Any internal storage associated with table is freed.
It is the user's responsibility to free any storage associated
with the pointers he placed in the table (by perhaps using
st_foreach).]
SideEffects [None]
SeeAlso [st_init_table st_init_table_with_params]
******************************************************************************/
void
st_free_table (st_table * table)
{
st_table_entry *ptr, *next;
int i;
for (i = 0; i < table->num_bins; i++)
{
ptr = table->bins[i];
while (ptr != NIL (st_table_entry))
{
next = ptr->next;
FREE (ptr);
ptr = next;
}
}
FREE (table->bins);
FREE (table);
} /* st_free_table */
/**Function********************************************************************
Synopsis [Lookup up `key' in `table'.]
Description [Lookup up `key' in `table'. If an entry is found, 1 is
returned and if `value' is not nil, the variable it points to is set
to the associated value. If an entry is not found, 0 is returned
and the variable pointed by value is unchanged.]
SideEffects [None]
SeeAlso [st_lookup_int]
******************************************************************************/
int
st_lookup (st_table * table, void *key, void *value)
{
int hash_val;
st_table_entry *ptr, **last;
hash_val = do_hash (key, table);
FIND_ENTRY (table, hash_val, key, ptr, last);
if (ptr == NIL (st_table_entry))
{
return 0;
}
else
{
if (value != NIL (void))
{
*(char **) value = ptr->record;
}
return 1;
}
} /* st_lookup */
/**Function********************************************************************
Synopsis [Lookup up `key' in `table'.]
Description [Lookup up `key' in `table'. If an entry is found, 1 is
returned and if `value' is not nil, the variable it points to is
set to the associated integer value. If an entry is not found, 0 is
return and the variable pointed by `value' is unchanged.]
SideEffects [None]
SeeAlso [st_lookup]
******************************************************************************/
int
st_lookup_int (st_table * table, void *key, int *value)
{
int hash_val;
st_table_entry *ptr, **last;
hash_val = do_hash (key, table);
FIND_ENTRY (table, hash_val, key, ptr, last);
if (ptr == NIL (st_table_entry))
{
return 0;
}
else
{
if (value != NIL (int))
{
*value = (int) (long) ptr->record;
}
return 1;
}
} /* st_lookup_int */
/**Function********************************************************************
Synopsis [Insert value in table under the key 'key'.]
Description [Insert value in table under the key 'key'. Returns 1
if there was an entry already under the key; 0 if there was no entry
under the key and insertion was successful; ST_OUT_OF_MEM otherwise.
In either of the first two cases the new value is added.]
SideEffects [None]
SeeAlso []
******************************************************************************/
int
st_insert (st_table * table, void *key, void *value)
{
int hash_val;
st_table_entry *newt;
st_table_entry *ptr, **last;
hash_val = do_hash (key, table);
FIND_ENTRY (table, hash_val, key, ptr, last);
if (ptr == NIL (st_table_entry))
{
if (table->num_entries / table->num_bins >= table->max_density)
{
if (rehash (table) == ST_OUT_OF_MEM)
{
return ST_OUT_OF_MEM;
}
hash_val = do_hash (key, table);
}
newt = ALLOC (st_table_entry, 1);
if (newt == NIL (st_table_entry))
{
return ST_OUT_OF_MEM;
}
newt->key = (char *) key;
newt->record = (char *) value;
newt->next = table->bins[hash_val];
table->bins[hash_val] = newt;
table->num_entries++;
return 0;
}
else
{
ptr->record = (char *) value;
return 1;
}
} /* st_insert */
/**Function********************************************************************
Synopsis [Place 'value' in 'table' under the key 'key'.]
Description [Place 'value' in 'table' under the key 'key'. This is
done without checking if 'key' is in 'table' already. This should
only be used if you are sure there is not already an entry for
'key', since it is undefined which entry you would later get from
st_lookup or st_find_or_add. Returns 1 if successful; ST_OUT_OF_MEM
otherwise.]
SideEffects [None]
SeeAlso []
******************************************************************************/
int
st_add_direct (st_table * table, void *key, void *value)
{
int hash_val;
st_table_entry *newt;
hash_val = do_hash (key, table);
if (table->num_entries / table->num_bins >= table->max_density)
{
if (rehash (table) == ST_OUT_OF_MEM)
{
return ST_OUT_OF_MEM;
}
}
hash_val = do_hash (key, table);
newt = ALLOC (st_table_entry, 1);
if (newt == NIL (st_table_entry))
{
return ST_OUT_OF_MEM;
}
newt->key = (char *) key;
newt->record = (char *) value;
newt->next = table->bins[hash_val];
table->bins[hash_val] = newt;
table->num_entries++;
return 1;
} /* st_add_direct */
/**Function********************************************************************
Synopsis [Lookup `key' in `table'.]
Description [Lookup `key' in `table'. If not found, create an
entry. In either case set slot to point to the field in the entry
where the value is stored. The value associated with `key' may then
be changed by accessing directly through slot. Returns 1 if an
entry already existed, 0 if it did not exist and creation was
successful; ST_OUT_OF_MEM otherwise. As an example:
<pre>
char **slot;
</pre>
<pre>
char *key;
</pre>
<pre>
char *value = (char *) item_ptr <-- ptr to a malloc'd structure
</pre>
<pre>
if (st_find_or_add(table, key, &slot) == 1) {
</pre>
<pre>
FREE(*slot); <-- free the old value of the record
</pre>
<pre>
}
</pre>
<pre>
*slot = value; <-- attach the new value to the record
</pre>
This replaces the equivelent code:
<pre>
if (st_lookup(table, key, &ovalue) == 1) {
</pre>
<pre>
FREE(ovalue);
</pre>
<pre>
}
</pre>
<pre>
st_insert(table, key, value);
</pre>
]
SideEffects [None]
SeeAlso [st_find]
******************************************************************************/
int
st_find_or_add (st_table * table, void *key, void *slot)
{
int hash_val;
st_table_entry *newt, *ptr, **last;
hash_val = do_hash (key, table);
FIND_ENTRY (table, hash_val, key, ptr, last);
if (ptr == NIL (st_table_entry))
{
if (table->num_entries / table->num_bins >= table->max_density)
{
if (rehash (table) == ST_OUT_OF_MEM)
{
return ST_OUT_OF_MEM;
}
hash_val = do_hash (key, table);
}
newt = ALLOC (st_table_entry, 1);
if (newt == NIL (st_table_entry))
{
return ST_OUT_OF_MEM;
}
newt->key = (char *) key;
newt->record = (char *) 0;
newt->next = table->bins[hash_val];
table->bins[hash_val] = newt;
table->num_entries++;
if (slot != NIL (void))
*(char ***) slot = &newt->record;
return 0;
}
else
{
if (slot != NIL (void))
*(char ***) slot = &ptr->record;
return 1;
}
} /* st_find_or_add */
/**Function********************************************************************
Synopsis [Lookup `key' in `table'.]
Description [Like st_find_or_add, but does not create an entry if
one is not found.]
SideEffects [None]
SeeAlso [st_find_or_add]
******************************************************************************/
int
st_find (st_table * table, void *key, void *slot)
{
int hash_val;
st_table_entry *ptr, **last;
hash_val = do_hash (key, table);
FIND_ENTRY (table, hash_val, key, ptr, last);
if (ptr == NIL (st_table_entry))
{
return 0;
}
else
{
if (slot != NIL (void))
{
*(char ***) slot = &ptr->record;
}
return 1;
}
} /* st_find */
/**Function********************************************************************
Synopsis [Return a copy of old_table and all its members.]
Description [Return a copy of old_table and all its members.
(st_table *) 0 is returned if there was insufficient memory to do
the copy.]
SideEffects [None]
SeeAlso []
******************************************************************************/
st_table *
st_copy (st_table * old_table)
{
st_table *new_table;
st_table_entry *ptr, *newptr, *next, *newt;
int i, j, num_bins = old_table->num_bins;
new_table = ALLOC (st_table, 1);
if (new_table == NIL (st_table))
{
return NIL (st_table);
}
*new_table = *old_table;
new_table->bins = ALLOC (st_table_entry *, num_bins);
if (new_table->bins == NIL (st_table_entry *))
{
FREE (new_table);
return NIL (st_table);
}
for (i = 0; i < num_bins; i++)
{
new_table->bins[i] = NIL (st_table_entry);
ptr = old_table->bins[i];
while (ptr != NIL (st_table_entry))
{
newt = ALLOC (st_table_entry, 1);
if (newt == NIL (st_table_entry))
{
for (j = 0; j <= i; j++)
{
newptr = new_table->bins[j];
while (newptr != NIL (st_table_entry))
{
next = newptr->next;
FREE (newptr);
newptr = next;
}
}
FREE (new_table->bins);
FREE (new_table);
return NIL (st_table);
}
*newt = *ptr;
newt->next = new_table->bins[i];
new_table->bins[i] = newt;
ptr = ptr->next;
}
}
return new_table;
} /* st_copy */
/**Function********************************************************************
Synopsis [Delete the entry with the key pointed to by `keyp'.]
Description [Delete the entry with the key pointed to by `keyp'. If
the entry is found, 1 is returned, the variable pointed by `keyp' is
set to the actual key and the variable pointed by `value' is set to
the corresponding entry. (This allows the freeing of the associated
storage.) If the entry is not found, then 0 is returned and nothing
is changed.]
SideEffects [None]
SeeAlso [st_delete_int]
******************************************************************************/
int
st_delete (st_table * table, void *keyp, void *value)
{
int hash_val;
char *key = *(char **) keyp;
st_table_entry *ptr, **last;
hash_val = do_hash (key, table);
FIND_ENTRY (table, hash_val, key, ptr, last);
if (ptr == NIL (st_table_entry))
{
return 0;
}
*last = ptr->next;
if (value != NIL (void))
*(char **) value = ptr->record;
*(char **) keyp = ptr->key;
FREE (ptr);
table->num_entries--;
return 1;
} /* st_delete */
/**Function********************************************************************
Synopsis [Delete the entry with the key pointed to by `keyp'.]
Description [Delete the entry with the key pointed to by `keyp'.
`value' must be a pointer to an integer. If the entry is found, 1
is returned, the variable pointed by `keyp' is set to the actual key
and the variable pointed by `value' is set to the corresponding
entry. (This allows the freeing of the associated storage.) If the
entry is not found, then 0 is returned and nothing is changed.]
SideEffects [None]
SeeAlso [st_delete]
******************************************************************************/
int
st_delete_int (st_table * table, void *keyp, int *value)
{
int hash_val;
char *key = *(char **) keyp;
st_table_entry *ptr, **last;
hash_val = do_hash (key, table);
FIND_ENTRY (table, hash_val, key, ptr, last);
if (ptr == NIL (st_table_entry))
{
return 0;
}
*last = ptr->next;
if (value != NIL (int))
*value = (int) (long) ptr->record;
*(char **) keyp = ptr->key;
FREE (ptr);
table->num_entries--;
return 1;
} /* st_delete_int */
/**Function********************************************************************
Synopsis [Iterates over the elements of a table.]
Description [For each (key, value) record in `table', st_foreach
call func with the arguments
<pre>
(*func)(key, value, arg)
</pre>
If func returns ST_CONTINUE, st_foreach continues processing
entries. If func returns ST_STOP, st_foreach stops processing and
returns immediately. If func returns ST_DELETE, then the entry is
deleted from the symbol table and st_foreach continues. In the case
of ST_DELETE, it is func's responsibility to free the key and value,
if necessary.<p>
The routine returns 1 if all items in the table were generated and 0
if the generation sequence was aborted using ST_STOP. The order in
which the records are visited will be seemingly random.]
SideEffects [None]
SeeAlso [st_foreach_item st_foreach_item_int]
******************************************************************************/
int
st_foreach (st_table * table, ST_PFSR func, char *arg)
{
st_table_entry *ptr, **last;
enum st_retval retval;
int i;
for (i = 0; i < table->num_bins; i++)
{
last = &table->bins[i];
ptr = *last;
while (ptr != NIL (st_table_entry))
{
retval = (*func) (ptr->key, ptr->record, arg);
switch (retval)
{
case ST_CONTINUE:
last = &ptr->next;
ptr = *last;
break;
case ST_STOP:
return 0;
case ST_DELETE:
*last = ptr->next;
table->num_entries--; /* cstevens@ic */
FREE (ptr);
ptr = *last;
}
}
}
return 1;
} /* st_foreach */
int
st_strhash (char *string, int modulus)
{
int val = 0;
int c;
while ((c = *string++) != '\0')
{
val = val * 997 + c;
}
return ((val < 0) ? -val : val) % modulus;
} /* st_strhash */
int
st_numhash (char *x, int size)
{
return ST_NUMHASH (x, size);
}
int
st_ptrhash (char *x, int size)
{
return ST_PTRHASH (x, size);
}
int
st_numcmp (const char *x, const char *y)
{
return ST_NUMCMP (x, y);
}
int
st_ptrcmp (const char *x, const char *y)
{
return ST_NUMCMP (x, y);
}
/**Function********************************************************************
Synopsis [Initializes a generator.]
Description [Returns a generator handle which when used with
st_gen() will progressively return each (key, value) record in
`table'.]
SideEffects [None]
SeeAlso [st_free_gen]
******************************************************************************/
st_generator *
st_init_gen (st_table * table)
{
st_generator *gen;
gen = ALLOC (st_generator, 1);
if (gen == NIL (st_generator))
{
return NIL (st_generator);
}
gen->table = table;
gen->entry = NIL (st_table_entry);
gen->index = 0;
return gen;
} /* st_init_gen */
/**Function********************************************************************
Synopsis [returns the next (key, value) pair in the generation
sequence. ]
Description [Given a generator returned by st_init_gen(), this
routine returns the next (key, value) pair in the generation
sequence. The pointer `value_p' can be zero which means no value
will be returned. When there are no more items in the generation
sequence, the routine returns 0.
While using a generation sequence, deleting any (key, value) pair