SVF
st.c
Go to the documentation of this file.
1 
20 #include "CUDD/util.h"
21 #include "CUDD/st.h"
22 
23 /*---------------------------------------------------------------------------*/
24 /* Constant declarations */
25 /*---------------------------------------------------------------------------*/
26 
27 /*---------------------------------------------------------------------------*/
28 /* Stucture declarations */
29 /*---------------------------------------------------------------------------*/
30 
31 /*---------------------------------------------------------------------------*/
32 /* Type declarations */
33 /*---------------------------------------------------------------------------*/
34 
35 /*---------------------------------------------------------------------------*/
36 /* Variable declarations */
37 /*---------------------------------------------------------------------------*/
38 
39 #ifndef lint
40 static char rcsid[] UTIL_UNUSED = " $Id: st.c,v 1.12 2010/04/22 19:00:55 fabio Exp fabio $";
41 #endif
42 
43 /*---------------------------------------------------------------------------*/
44 /* Macro declarations */
45 /*---------------------------------------------------------------------------*/
46 
47 #define ST_NUMCMP(x,y) ((x) != (y))
48 
49 #define ST_NUMHASH(x,size) ((unsigned long)(x)%(size))
50 
51 #if SIZEOF_VOID_P == 8
52 #define st_shift 3
53 #else
54 #define st_shift 2
55 #endif
56 
57 #define ST_PTRHASH(x,size) ((unsigned int)((unsigned long)(x)>>st_shift)%size)
58 
59 #define EQUAL(func, x, y) \
60  ((((func) == st_numcmp) || ((func) == st_ptrcmp)) ?\
61  (ST_NUMCMP((x),(y)) == 0) : ((*func)((x), (y)) == 0))
62 
63 #define do_hash(key, table)\
64  ((int)((table->hash == st_ptrhash) ? ST_PTRHASH((char *)(key),(table)->num_bins) :\
65  (table->hash == st_numhash) ? ST_NUMHASH((char *)(key), (table)->num_bins) :\
66  (*table->hash)((char *)(key), (table)->num_bins)))
67 
68 #define PTR_NOT_EQUAL(table, ptr, user_key)\
69 (ptr != NIL(st_table_entry) && !EQUAL(table->compare, (char *)user_key, (ptr)->key))
70 
71 #define FIND_ENTRY(table, hash_val, key, ptr, last) \
72  (last) = &(table)->bins[hash_val];\
73  (ptr) = *(last);\
74  while (PTR_NOT_EQUAL((table), (ptr), (key))) {\
75  (last) = &(ptr)->next; (ptr) = *(last);\
76  }\
77  if ((ptr) != NIL(st_table_entry) && (table)->reorder_flag) {\
78  *(last) = (ptr)->next;\
79  (ptr)->next = (table)->bins[hash_val];\
80  (table)->bins[hash_val] = (ptr);\
81  }
82 
83 /* This macro does not check if memory allocation fails. Use at you own risk */
84 #define ADD_DIRECT(table, key, value, hash_val, newt)\
85 {\
86  if (table->num_entries/table->num_bins >= table->max_density) {\
87  rehash(table);\
88  hash_val = do_hash(key,table);\
89  }\
90  \
91  newt = ALLOC(st_table_entry, 1);\
92  \
93  newt->key = (char *)key;\
94  newt->record = value;\
95  newt->next = table->bins[hash_val];\
96  table->bins[hash_val] = newt;\
97  table->num_entries++;\
98 }
99 
102 /*---------------------------------------------------------------------------*/
103 /* Static function prototypes */
104 /*---------------------------------------------------------------------------*/
105 
106 static int rehash (st_table *);
107 
111 /*---------------------------------------------------------------------------*/
112 /* Definition of exported functions */
113 /*---------------------------------------------------------------------------*/
114 
162 st_table *
164 {
169 
170 } /* st_init_table */
171 
172 
198 st_table *
200  ST_PFICPCP compare,
201  ST_PFICPI hash,
202  int size,
203  int density,
204  double grow_factor,
205  int reorder_flag)
206 {
207  int i;
208  st_table *newt;
209 
210  newt = ALLOC(st_table, 1);
211  if (newt == NIL(st_table)) {
212  return NIL(st_table);
213  }
214  newt->compare = compare;
215  newt->hash = hash;
216  newt->num_entries = 0;
217  newt->max_density = density;
218  newt->grow_factor = grow_factor;
219  newt->reorder_flag = reorder_flag;
220  if (size <= 0) {
221  size = 1;
222  }
223  newt->num_bins = size;
224  newt->bins = ALLOC(st_table_entry *, size);
225  if (newt->bins == NIL(st_table_entry *)) {
226  FREE(newt);
227  return NIL(st_table);
228  }
229  for(i = 0; i < size; i++) {
230  newt->bins[i] = 0;
231  }
232  return newt;
233 
234 } /* st_init_table_with_params */
235 
236 
251 void
253 {
254  st_table_entry *ptr, *next;
255  int i;
256 
257  for(i = 0; i < table->num_bins ; i++) {
258  ptr = table->bins[i];
259  while (ptr != NIL(st_table_entry)) {
260  next = ptr->next;
261  FREE(ptr);
262  ptr = next;
263  }
264  }
265  FREE(table->bins);
266  FREE(table);
267 
268 } /* st_free_table */
269 
270 
285 int
286 st_lookup(st_table *table, void *key, void *value)
287 {
288  int hash_val;
289  st_table_entry *ptr, **last;
290 
291  hash_val = do_hash(key, table);
292 
293  FIND_ENTRY(table, hash_val, key, ptr, last);
294 
295  if (ptr == NIL(st_table_entry)) {
296  return 0;
297  } else {
298  if (value != NIL(void)) {
299  *(char **)value = ptr->record;
300  }
301  return 1;
302  }
303 
304 } /* st_lookup */
305 
306 
321 int
322 st_lookup_int(st_table *table, void *key, int *value)
323 {
324  int hash_val;
325  st_table_entry *ptr, **last;
326 
327  hash_val = do_hash(key, table);
328 
329  FIND_ENTRY(table, hash_val, key, ptr, last);
330 
331  if (ptr == NIL(st_table_entry)) {
332  return 0;
333  } else {
334  if (value != NIL(int)) {
335  *value = (int) (long) ptr->record;
336  }
337  return 1;
338  }
339 
340 } /* st_lookup_int */
341 
342 
357 int
358 st_insert(st_table *table, void *key, void *value)
359 {
360  int hash_val;
361  st_table_entry *newt;
362  st_table_entry *ptr, **last;
363 
364  hash_val = do_hash(key, table);
365 
366  FIND_ENTRY(table, hash_val, key, ptr, last);
367 
368  if (ptr == NIL(st_table_entry)) {
369  if (table->num_entries/table->num_bins >= table->max_density) {
370  if (rehash(table) == ST_OUT_OF_MEM) {
371  return ST_OUT_OF_MEM;
372  }
373  hash_val = do_hash(key, table);
374  }
375  newt = ALLOC(st_table_entry, 1);
376  if (newt == NIL(st_table_entry)) {
377  return ST_OUT_OF_MEM;
378  }
379  newt->key = (char *)key;
380  newt->record = (char *)value;
381  newt->next = table->bins[hash_val];
382  table->bins[hash_val] = newt;
383  table->num_entries++;
384  return 0;
385  } else {
386  ptr->record = (char *)value;
387  return 1;
388  }
389 
390 } /* st_insert */
391 
392 
409 int
410 st_add_direct(st_table *table, void *key, void *value)
411 {
412  int hash_val;
413  st_table_entry *newt;
414 
415  hash_val = do_hash(key, table);
416  if (table->num_entries / table->num_bins >= table->max_density) {
417  if (rehash(table) == ST_OUT_OF_MEM) {
418  return ST_OUT_OF_MEM;
419  }
420  }
421  hash_val = do_hash(key, table);
422  newt = ALLOC(st_table_entry, 1);
423  if (newt == NIL(st_table_entry)) {
424  return ST_OUT_OF_MEM;
425  }
426  newt->key = (char *)key;
427  newt->record = (char *)value;
428  newt->next = table->bins[hash_val];
429  table->bins[hash_val] = newt;
430  table->num_entries++;
431  return 1;
432 
433 } /* st_add_direct */
434 
435 
487 int
488 st_find_or_add(st_table *table, void *key, void *slot)
489 {
490  int hash_val;
491  st_table_entry *newt, *ptr, **last;
492 
493  hash_val = do_hash(key, table);
494 
495  FIND_ENTRY(table, hash_val, key, ptr, last);
496 
497  if (ptr == NIL(st_table_entry)) {
498  if (table->num_entries / table->num_bins >= table->max_density) {
499  if (rehash(table) == ST_OUT_OF_MEM) {
500  return ST_OUT_OF_MEM;
501  }
502  hash_val = do_hash(key, table);
503  }
504  newt = ALLOC(st_table_entry, 1);
505  if (newt == NIL(st_table_entry)) {
506  return ST_OUT_OF_MEM;
507  }
508  newt->key = (char *)key;
509  newt->record = (char *) 0;
510  newt->next = table->bins[hash_val];
511  table->bins[hash_val] = newt;
512  table->num_entries++;
513  if (slot != NIL(void)) *(char ***)slot = &newt->record;
514  return 0;
515  } else {
516  if (slot != NIL(void)) *(char ***)slot = &ptr->record;
517  return 1;
518  }
519 
520 } /* st_find_or_add */
521 
522 
535 int
536 st_find(st_table *table, void *key, void *slot)
537 {
538  int hash_val;
539  st_table_entry *ptr, **last;
540 
541  hash_val = do_hash(key, table);
542 
543  FIND_ENTRY(table, hash_val, key, ptr, last);
544 
545  if (ptr == NIL(st_table_entry)) {
546  return 0;
547  } else {
548  if (slot != NIL(void)) {
549  *(char ***)slot = &ptr->record;
550  }
551  return 1;
552  }
553 
554 } /* st_find */
555 
556 
570 st_table *
571 st_copy(st_table *old_table)
572 {
573  st_table *new_table;
574  st_table_entry *ptr, *newptr, *next, *newt;
575  int i, j, num_bins = old_table->num_bins;
576 
577  new_table = ALLOC(st_table, 1);
578  if (new_table == NIL(st_table)) {
579  return NIL(st_table);
580  }
581 
582  *new_table = *old_table;
583  new_table->bins = ALLOC(st_table_entry *, num_bins);
584  if (new_table->bins == NIL(st_table_entry *)) {
585  FREE(new_table);
586  return NIL(st_table);
587  }
588  for(i = 0; i < num_bins ; i++) {
589  new_table->bins[i] = NIL(st_table_entry);
590  ptr = old_table->bins[i];
591  while (ptr != NIL(st_table_entry)) {
592  newt = ALLOC(st_table_entry, 1);
593  if (newt == NIL(st_table_entry)) {
594  for (j = 0; j <= i; j++) {
595  newptr = new_table->bins[j];
596  while (newptr != NIL(st_table_entry)) {
597  next = newptr->next;
598  FREE(newptr);
599  newptr = next;
600  }
601  }
602  FREE(new_table->bins);
603  FREE(new_table);
604  return NIL(st_table);
605  }
606  *newt = *ptr;
607  newt->next = new_table->bins[i];
608  new_table->bins[i] = newt;
609  ptr = ptr->next;
610  }
611  }
612  return new_table;
613 
614 } /* st_copy */
615 
616 
633 int
634 st_delete(st_table *table, void *keyp, void *value)
635 {
636  int hash_val;
637  char *key = *(char **)keyp;
638  st_table_entry *ptr, **last;
639 
640  hash_val = do_hash(key, table);
641 
642  FIND_ENTRY(table, hash_val, key, ptr ,last);
643 
644  if (ptr == NIL(st_table_entry)) {
645  return 0;
646  }
647 
648  *last = ptr->next;
649  if (value != NIL(void)) *(char **)value = ptr->record;
650  *(char **)keyp = ptr->key;
651  FREE(ptr);
652  table->num_entries--;
653  return 1;
654 
655 } /* st_delete */
656 
657 
674 int
675 st_delete_int(st_table *table, void *keyp, int *value)
676 {
677  int hash_val;
678  char *key = *(char **)keyp;
679  st_table_entry *ptr, **last;
680 
681  hash_val = do_hash(key, table);
682 
683  FIND_ENTRY(table, hash_val, key, ptr ,last);
684 
685  if (ptr == NIL(st_table_entry)) {
686  return 0;
687  }
688 
689  *last = ptr->next;
690  if (value != NIL(int)) *value = (int) (long) ptr->record;
691  *(char **)keyp = ptr->key;
692  FREE(ptr);
693  table->num_entries--;
694  return 1;
695 
696 } /* st_delete_int */
697 
698 
724 int
725 st_foreach(st_table *table, ST_PFSR func, char *arg)
726 {
727  st_table_entry *ptr, **last;
728  enum st_retval retval;
729  int i;
730 
731  for(i = 0; i < table->num_bins; i++) {
732  last = &table->bins[i]; ptr = *last;
733  while (ptr != NIL(st_table_entry)) {
734  retval = (*func)(ptr->key, ptr->record, arg);
735  switch (retval) {
736  case ST_CONTINUE:
737  last = &ptr->next; ptr = *last;
738  break;
739  case ST_STOP:
740  return 0;
741  case ST_DELETE:
742  *last = ptr->next;
743  table->num_entries--; /* cstevens@ic */
744  FREE(ptr);
745  ptr = *last;
746  }
747  }
748  }
749  return 1;
750 
751 } /* st_foreach */
752 
753 
765 int
766 st_strhash(char *string, int modulus)
767 {
768  int val = 0;
769  int c;
770 
771  while ((c = *string++) != '\0') {
772  val = val*997 + c;
773  }
774 
775  return ((val < 0) ? -val : val)%modulus;
776 
777 } /* st_strhash */
778 
779 
791 int
792 st_numhash(char *x, int size)
793 {
794  return ST_NUMHASH(x, size);
795 
796 } /* st_numhash */
797 
798 
810 int
811 st_ptrhash(char *x, int size)
812 {
813  return ST_PTRHASH(x, size);
814 
815 } /* st_ptrhash */
816 
817 
829 int
830 st_numcmp(const char *x, const char *y)
831 {
832  return ST_NUMCMP(x, y);
833 
834 } /* st_numcmp */
835 
836 
848 int
849 st_ptrcmp(const char *x, const char *y)
850 {
851  return ST_NUMCMP(x, y);
852 
853 } /* st_ptrcmp */
854 
855 
869 st_generator *
871 {
872  st_generator *gen;
873 
874  gen = ALLOC(st_generator, 1);
875  if (gen == NIL(st_generator)) {
876  return NIL(st_generator);
877  }
878  gen->table = table;
879  gen->entry = NIL(st_table_entry);
880  gen->index = 0;
881  return gen;
882 
883 } /* st_init_gen */
884 
885 
907 int
908 st_gen(st_generator *gen, void *key_p, void *value_p)
909 {
910  int i;
911 
912  if (gen->entry == NIL(st_table_entry)) {
913  /* try to find next entry */
914  for(i = gen->index; i < gen->table->num_bins; i++) {
915  if (gen->table->bins[i] != NIL(st_table_entry)) {
916  gen->index = i+1;
917  gen->entry = gen->table->bins[i];
918  break;
919  }
920  }
921  if (gen->entry == NIL(st_table_entry)) {
922  return 0; /* that's all folks ! */
923  }
924  }
925  *(char **)key_p = gen->entry->key;
926  if (value_p != NIL(void)) {
927  *(char **)value_p = gen->entry->record;
928  }
929  gen->entry = gen->entry->next;
930  return 1;
931 
932 } /* st_gen */
933 
934 
952 int
953 st_gen_int(st_generator *gen, void *key_p, int *value_p)
954 {
955  int i;
956 
957  if (gen->entry == NIL(st_table_entry)) {
958  /* try to find next entry */
959  for(i = gen->index; i < gen->table->num_bins; i++) {
960  if (gen->table->bins[i] != NIL(st_table_entry)) {
961  gen->index = i+1;
962  gen->entry = gen->table->bins[i];
963  break;
964  }
965  }
966  if (gen->entry == NIL(st_table_entry)) {
967  return 0; /* that's all folks ! */
968  }
969  }
970  *(char **)key_p = gen->entry->key;
971  if (value_p != NIL(int)) {
972  *value_p = (int) (long) gen->entry->record;
973  }
974  gen->entry = gen->entry->next;
975  return 1;
976 
977 } /* st_gen_int */
978 
979 
993 void
995 {
996  FREE(gen);
997 
998 } /* st_free_gen */
999 
1000 
1001 /*---------------------------------------------------------------------------*/
1002 /* Definition of internal functions */
1003 /*---------------------------------------------------------------------------*/
1004 
1005 /*---------------------------------------------------------------------------*/
1006 /* Definition of static functions */
1007 /*---------------------------------------------------------------------------*/
1008 
1020 static int
1022 {
1023  st_table_entry *ptr, *next, **old_bins;
1024  int i, old_num_bins, hash_val, old_num_entries;
1025 
1026  /* save old values */
1027  old_bins = table->bins;
1028  old_num_bins = table->num_bins;
1029  old_num_entries = table->num_entries;
1030 
1031  /* rehash */
1032  table->num_bins = (int) (table->grow_factor * old_num_bins);
1033  if (table->num_bins % 2 == 0) {
1034  table->num_bins += 1;
1035  }
1036  table->num_entries = 0;
1037  table->bins = ALLOC(st_table_entry *, table->num_bins);
1038  if (table->bins == NIL(st_table_entry *)) {
1039  table->bins = old_bins;
1040  table->num_bins = old_num_bins;
1041  table->num_entries = old_num_entries;
1042  return ST_OUT_OF_MEM;
1043  }
1044  /* initialize */
1045  for (i = 0; i < table->num_bins; i++) {
1046  table->bins[i] = 0;
1047  }
1048 
1049  /* copy data over */
1050  for (i = 0; i < old_num_bins; i++) {
1051  ptr = old_bins[i];
1052  while (ptr != NIL(st_table_entry)) {
1053  next = ptr->next;
1054  hash_val = do_hash(ptr->key, table);
1055  ptr->next = table->bins[hash_val];
1056  table->bins[hash_val] = ptr;
1057  table->num_entries++;
1058  ptr = next;
1059  }
1060  }
1061  FREE(old_bins);
1062 
1063  return 1;
1064 
1065 } /* rehash */
#define ST_DEFAULT_REORDER_FLAG
Definition: st.h:40
Definition: st.h:78
int num_bins
Definition: st.h:63
Definition: st.h:60
Definition: st.h:78
int st_delete_int(st_table *table, void *keyp, int *value)
Definition: st.c:675
st_table * st_init_table_with_params(ST_PFICPCP compare, ST_PFICPI hash, int size, int density, double grow_factor, int reorder_flag)
Definition: st.c:199
#define FREE(obj)
Definition: util.h:80
int st_ptrhash(char *x, int size)
Definition: st.c:811
Definition: st.h:78
#define FIND_ENTRY(table, hash_val, key, ptr, last)
Definition: st.c:71
#define ST_DEFAULT_MAX_DENSITY
Definition: st.h:37
#define ST_DEFAULT_GROW_FACTOR
Definition: st.h:39
int st_strhash(char *string, int modulus)
Definition: st.c:766
st_table_entry * next
Definition: st.h:56
int(* ST_PFICPI)(char *, int)
Definition: st.h:84
enum st_retval(* ST_PFSR)(char *, char *, char *)
Definition: st.h:80
int st_lookup_int(st_table *table, void *key, int *value)
Definition: st.c:322
int max_density
Definition: st.h:65
int(* hash)(char *, int)
Definition: st.h:62
#define ST_NUMCMP(x, y)
Definition: st.c:47
int st_ptrcmp(const char *x, const char *y)
Definition: st.c:849
static char rcsid [] UTIL_UNUSED
Definition: st.c:40
int num_entries
Definition: st.h:64
st_table * table
Definition: st.h:73
Definition: st.h:53
double grow_factor
Definition: st.h:67
#define NIL(type)
Definition: util.h:44
int st_gen_int(st_generator *gen, void *key_p, int *value_p)
Definition: st.c:953
#define ALLOC(type, num)
Definition: util.h:76
st_generator * st_init_gen(st_table *table)
Definition: st.c:870
int(* compare)(const char *, const char *)
Definition: st.h:61
int st_find_or_add(st_table *table, void *key, void *slot)
Definition: st.c:488
int st_gen(st_generator *gen, void *key_p, void *value_p)
Definition: st.c:908
st_retval
Definition: st.h:78
int index
Definition: st.h:75
#define ST_OUT_OF_MEM
Definition: st.h:41
char * record
Definition: st.h:55
char * key
Definition: st.h:54
int st_lookup(st_table *table, void *key, void *value)
Definition: st.c:286
int(* ST_PFICPCP)(const char *, const char *)
Definition: st.h:82
int st_insert(st_table *table, void *key, void *value)
Definition: st.c:358
st_table * st_init_table(ST_PFICPCP compare, ST_PFICPI hash)
Definition: st.c:163
st_table * st_copy(st_table *old_table)
Definition: st.c:571
#define do_hash(key, table)
Definition: st.c:63
void st_free_gen(st_generator *gen)
Definition: st.c:994
st_table_entry ** bins
Definition: st.h:68
#define ST_PTRHASH(x, size)
Definition: st.c:57
int st_numcmp(const char *x, const char *y)
Definition: st.c:830
int st_delete(st_table *table, void *keyp, void *value)
Definition: st.c:634
int reorder_flag
Definition: st.h:66
#define ST_NUMHASH(x, size)
Definition: st.c:49
int st_numhash(char *x, int size)
Definition: st.c:792
void st_free_table(st_table *table)
Definition: st.c:252
#define ST_DEFAULT_INIT_TABLE_SIZE
Definition: st.h:38
int st_foreach(st_table *table, ST_PFSR func, char *arg)
Definition: st.c:725
st_table_entry * entry
Definition: st.h:74
static int rehash(st_table *)
Definition: st.c:1021
int st_add_direct(st_table *table, void *key, void *value)
Definition: st.c:410
int st_find(st_table *table, void *key, void *slot)
Definition: st.c:536
if(DEFINED IN_SOURCE_BUILD) set(LLVM_LINK_COMPONENTS BitWriter Core IPO IrReader InstCombine Instrumentation Target Linker Analysis ScalarOpts Support Svf Cudd) add_llvm_tool(dvf dda.cpp) else() add_executable(dvf dda.cpp) target_link_libraries(dvf Svf Cudd $
Definition: CMakeLists.txt:2