summaryrefslogtreecommitdiffstats
path: root/src/bdd/cudd/cuddSubsetHB.c
blob: 43aaf7446d1688849e2b9d00d0319a2daf1eba21 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
/**CFile***********************************************************************

  FileName    [cuddSubsetHB.c]

  PackageName [cudd]

  Synopsis    [Procedure to subset the given BDD by choosing the heavier
        branches]


  Description [External procedures provided by this module:
                <ul>
        <li> Cudd_SubsetHeavyBranch()
        <li> Cudd_SupersetHeavyBranch()
        </ul>
           Internal procedures included in this module:
        <ul>
        <li> cuddSubsetHeavyBranch()
        </ul>
           Static procedures included in this module:
        <ul>
        <li> ResizeCountMintermPages();
        <li> ResizeNodeDataPages()
        <li> ResizeCountNodePages()
        <li> SubsetCountMintermAux()
        <li> SubsetCountMinterm()
        <li> SubsetCountNodesAux()
        <li> SubsetCountNodes()
        <li> BuildSubsetBdd()
        </ul>
        ]

  SeeAlso     [cuddSubsetSP.c]

  Author      [Kavita Ravi]

  Copyright   [This file was created at the University of Colorado at
               Boulder.  The University of Colorado at Boulder makes no
           warranty about the suitability of this software for any
           purpose.  It is presented on an AS IS basis.]

******************************************************************************/

#ifdef __STDC__
#include <float.h>
#else
#define DBL_MAX_EXP 1024
#endif
#include "util.h"
#include "cuddInt.h"

/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

#define    DEFAULT_PAGE_SIZE 2048
#define    DEFAULT_NODE_DATA_PAGE_SIZE 1024
#define INITIAL_PAGES 128


/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/

/* data structure to store the information on each node. It keeps
 * the number of minterms represented by the DAG rooted at this node
 * in terms of the number of variables specified by the user, number
 * of nodes in this DAG and the number of nodes of its child with
 * lesser number of minterms that are not shared by the child with
 * more minterms
 */
struct NodeData {
    double *mintermPointer;
    int *nodesPointer;
    int *lightChildNodesPointer;
};

/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/

typedef struct NodeData NodeData_t;

/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

#ifndef lint
static char rcsid[] DD_UNUSED = "$Id: cuddSubsetHB.c,v 1.1.1.1 2003/02/24 22:23:53 wjiang Exp $";
#endif

static int memOut;
#ifdef DEBUG
static    int        num_calls;
#endif

static    DdNode            *zero, *one; /* constant functions */
static    double        **mintermPages;    /* pointers to the pages */
static    int        **nodePages; /* pointers to the pages */
static    int        **lightNodePages; /* pointers to the pages */
static    double        *currentMintermPage; /* pointer to the current
                           page */
static  double         max; /* to store the 2^n value of the number
                  * of variables */

static    int        *currentNodePage; /* pointer to the current
                           page */
static    int        *currentLightNodePage; /* pointer to the
                        *  current page */
static    int        pageIndex; /* index to next element */
static    int        page; /* index to current page */
static    int        pageSize = DEFAULT_PAGE_SIZE; /* page size */
static  int             maxPages; /* number of page pointers */

static    NodeData_t    *currentNodeDataPage; /* pointer to the current
                         page */
static    int        nodeDataPage; /* index to next element */
static    int        nodeDataPageIndex; /* index to next element */
static    NodeData_t    **nodeDataPages; /* index to current page */
static    int        nodeDataPageSize = DEFAULT_NODE_DATA_PAGE_SIZE;
                                                     /* page size */
static  int             maxNodeDataPages; /* number of page pointers */


/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/

/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

static void ResizeNodeDataPages ARGS(());
static void ResizeCountMintermPages ARGS(());
static void ResizeCountNodePages ARGS(());
static double SubsetCountMintermAux ARGS((DdNode *node, double max, st_table *table));
static st_table * SubsetCountMinterm ARGS((DdNode *node, int nvars));
static int SubsetCountNodesAux ARGS((DdNode *node, st_table *table, double max));
static int SubsetCountNodes ARGS((DdNode *node, st_table *table, int nvars));
static void StoreNodes ARGS((st_table *storeTable, DdManager *dd, DdNode *node));
static DdNode * BuildSubsetBdd ARGS((DdManager *dd, DdNode *node, int *size, st_table *visitedTable, int threshold, st_table *storeTable, st_table *approxTable));

/**AutomaticEnd***************************************************************/


/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/

/**Function********************************************************************

  Synopsis    [Extracts a dense subset from a BDD with the heavy branch
  heuristic.]

  Description [Extracts a dense subset from a BDD. This procedure
  builds a subset by throwing away one of the children of each node,
  starting from the root, until the result is small enough. The child
  that is eliminated from the result is the one that contributes the
  fewer minterms.  Returns a pointer to the BDD of the subset if
  successful. NULL if the procedure runs out of memory. The parameter
  numVars is the maximum number of variables to be used in minterm
  calculation and node count calculation.  The optimal number should
  be as close as possible to the size of the support of f.  However,
  it is safe to pass the value returned by Cudd_ReadSize for numVars
  when the number of variables is under 1023.  If numVars is larger
  than 1023, it will overflow. If a 0 parameter is passed then the
  procedure will compute a value which will avoid overflow but will
  cause underflow with 2046 variables or more.]

  SideEffects [None]

  SeeAlso     [Cudd_SubsetShortPaths Cudd_SupersetHeavyBranch Cudd_ReadSize]

******************************************************************************/
DdNode *
Cudd_SubsetHeavyBranch(
  DdManager * dd /* manager */,
  DdNode * f /* function to be subset */,
  int  numVars /* number of variables in the support of f */,
  int  threshold /* maximum number of nodes in the subset */)
{
    DdNode *subset;

    memOut = 0;
    do {
    dd->reordered = 0;
    subset = cuddSubsetHeavyBranch(dd, f, numVars, threshold);
    } while ((dd->reordered == 1) && (!memOut));

    return(subset);

} /* end of Cudd_SubsetHeavyBranch */


/**Function********************************************************************

  Synopsis    [Extracts a dense superset from a BDD with the heavy branch
  heuristic.]

  Description [Extracts a dense superset from a BDD. The procedure is
  identical to the subset procedure except for the fact that it
  receives the complement of the given function. Extracting the subset
  of the complement function is equivalent to extracting the superset
  of the function. This procedure builds a superset by throwing away
  one of the children of each node starting from the root of the
  complement function, until the result is small enough. The child
  that is eliminated from the result is the one that contributes the
  fewer minterms.
  Returns a pointer to the BDD of the superset if successful. NULL if
  intermediate result causes the procedure to run out of memory. The
  parameter numVars is the maximum number of variables to be used in
  minterm calculation and node count calculation.  The optimal number
  should be as close as possible to the size of the support of f.
  However, it is safe to pass the value returned by Cudd_ReadSize for
  numVars when the number of variables is under 1023.  If numVars is
  larger than 1023, it will overflow. If a 0 parameter is passed then
  the procedure will compute a value which will avoid overflow but
  will cause underflow with 2046 variables or more.]

  SideEffects [None]

  SeeAlso     [Cudd_SubsetHeavyBranch Cudd_SupersetShortPaths Cudd_ReadSize]

******************************************************************************/
DdNode *
Cudd_SupersetHeavyBranch(
  DdManager * dd /* manager */,
  DdNode * f /* function to be superset */,
  int  numVars /* number of variables in the support of f */,
  int  threshold /* maximum number of nodes in the superset */)
{
    DdNode *subset, *g;

    g = Cudd_Not(f);    
    memOut = 0;
    do {
    dd->reordered = 0;
    subset = cuddSubsetHeavyBranch(dd, g, numVars, threshold);
    } while ((dd->reordered == 1) && (!memOut));
    
    return(Cudd_NotCond(subset, (subset != NULL)));
    
} /* end of Cudd_SupersetHeavyBranch */


/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [The main procedure that returns a subset by choosing the heavier
  branch in the BDD.]

  Description [Here a subset BDD is built by throwing away one of the
  children. Starting at root, annotate each node with the number of
  minterms (in terms of the total number of variables specified -
  numVars), number of nodes taken by the DAG rooted at this node and
  number of additional nodes taken by the child that has the lesser
  minterms. The child with the lower number of minterms is thrown away
  and a dyanmic count of the nodes of the subset is kept. Once the
  threshold is reached the subset is returned to the calling
  procedure.]

  SideEffects [None]

  SeeAlso     [Cudd_SubsetHeavyBranch]

******************************************************************************/
DdNode *
cuddSubsetHeavyBranch(
  DdManager * dd /* DD manager */,
  DdNode * f /* current DD */,
  int  numVars /* maximum number of variables */,
  int  threshold /* threshold size for the subset */)
{

    int i, *size;
    st_table *visitedTable;
    int numNodes;
    NodeData_t *currNodeQual;
    DdNode *subset;
    double minN;
    st_table *storeTable, *approxTable;
    char *key, *value;
    st_generator *stGen;
    
    if (f == NULL) {
    fprintf(dd->err, "Cannot subset, nil object\n");
    dd->errorCode = CUDD_INVALID_ARG;
    return(NULL);
    }

    one     = Cudd_ReadOne(dd);
    zero = Cudd_Not(one);

    /* If user does not know numVars value, set it to the maximum
     * exponent that the pow function can take. The -1 is due to the
     * discrepancy in the value that pow takes and the value that
     * log gives.
     */
    if (numVars == 0) {
    /* set default value */
    numVars = DBL_MAX_EXP - 1;
    }

    if (Cudd_IsConstant(f)) {
    return(f);
    }

    max = pow(2.0, (double)numVars);

    /* Create visited table where structures for node data are allocated and
       stored in a st_table */
    visitedTable = SubsetCountMinterm(f, numVars);
    if ((visitedTable == NULL) || memOut) {
    (void) fprintf(dd->err, "Out-of-memory; Cannot subset\n");
    dd->errorCode = CUDD_MEMORY_OUT;
    return(0);
    }
    numNodes = SubsetCountNodes(f, visitedTable, numVars);
    if (memOut) {
    (void) fprintf(dd->err, "Out-of-memory; Cannot subset\n");
    dd->errorCode = CUDD_MEMORY_OUT;
    return(0);
    }

    if (st_lookup(visitedTable, (char *)f, (char **)&currNodeQual)) {
    minN = *(((NodeData_t *)currNodeQual)->mintermPointer);
    } else {
    fprintf(dd->err,
        "Something is wrong, ought to be node quality table\n");
    dd->errorCode = CUDD_INTERNAL_ERROR;
    }

    size = ALLOC(int, 1);
    if (size == NULL) {
    dd->errorCode = CUDD_MEMORY_OUT;
    return(NULL);
    }
    *size = numNodes;

#ifdef DEBUG
    num_calls = 0;
#endif
    /* table to store nodes being created. */
    storeTable = st_init_table(st_ptrcmp, st_ptrhash);
    /* insert the constant */
    cuddRef(one);
    if (st_insert(storeTable, (char *)Cudd_ReadOne(dd), NIL(char)) ==
    ST_OUT_OF_MEM) {
    fprintf(dd->out, "Something wrong, st_table insert failed\n");
    }
    /* table to store approximations of nodes */
    approxTable = st_init_table(st_ptrcmp, st_ptrhash);
    subset = (DdNode *)BuildSubsetBdd(dd, f, size, visitedTable, threshold,
                      storeTable, approxTable);
    if (subset != NULL) {
    cuddRef(subset);
    }

    stGen = st_init_gen(approxTable);
    if (stGen == NULL) {
    st_free_table(approxTable);
    return(NULL);
    }
    while(st_gen(stGen, (char **)&key, (char **)&value)) {
    Cudd_RecursiveDeref(dd, (DdNode *)value);
    }
    st_free_gen(stGen); stGen = NULL;
    st_free_table(approxTable);

    stGen = st_init_gen(storeTable);
    if (stGen == NULL) {
    st_free_table(storeTable);
    return(NULL);
    }
    while(st_gen(stGen, (char **)&key, (char **)&value)) {
    Cudd_RecursiveDeref(dd, (DdNode *)key);
    }
    st_free_gen(stGen); stGen = NULL;
    st_free_table(storeTable);

    for (i = 0; i <= page; i++) {
    FREE(mintermPages[i]);
    }
    FREE(mintermPages);
    for (i = 0; i <= page; i++) {
    FREE(nodePages[i]);
    }
    FREE(nodePages);
    for (i = 0; i <= page; i++) {
    FREE(lightNodePages[i]);
    }
    FREE(lightNodePages);
    for (i = 0; i <= nodeDataPage; i++) {
    FREE(nodeDataPages[i]);
    }
    FREE(nodeDataPages);
    st_free_table(visitedTable);
    FREE(size);
#if 0
    (void) Cudd_DebugCheck(dd);
    (void) Cudd_CheckKeys(dd);
#endif

    if (subset != NULL) {
#ifdef DD_DEBUG
      if (!Cudd_bddLeq(dd, subset, f)) {
        fprintf(dd->err, "Wrong subset\n");
        dd->errorCode = CUDD_INTERNAL_ERROR;
        return(NULL);
      }
#endif
        cuddDeref(subset);
        return(subset);
    } else {
        return(NULL);
    }
} /* end of cuddSubsetHeavyBranch */


/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Resize the number of pages allocated to store the node data.]

  Description [Resize the number of pages allocated to store the node data
  The procedure  moves the counter to the next page when the end of
  the page is reached and allocates new pages when necessary.]

  SideEffects [Changes the size of pages, page, page index, maximum
  number of pages freeing stuff in case of memory out. ]

  SeeAlso     []

******************************************************************************/
static void
ResizeNodeDataPages(
   )
{
    int i;
    NodeData_t **newNodeDataPages;

    nodeDataPage++;
    /* If the current page index is larger than the number of pages
     * allocated, allocate a new page array. Page numbers are incremented by
     * INITIAL_PAGES
     */
    if (nodeDataPage == maxNodeDataPages) {
    newNodeDataPages = ALLOC(NodeData_t *,maxNodeDataPages + INITIAL_PAGES);
    if (newNodeDataPages == NULL) {
        for (i = 0; i < nodeDataPage; i++) FREE(nodeDataPages[i]);
        FREE(nodeDataPages);
        memOut = 1;
        return;
    } else {
        for (i = 0; i < maxNodeDataPages; i++) {
        newNodeDataPages[i] = nodeDataPages[i];
        }
        /* Increase total page count */
        maxNodeDataPages += INITIAL_PAGES;
        FREE(nodeDataPages);
        nodeDataPages = newNodeDataPages;
    }
    }
    /* Allocate a new page */
    currentNodeDataPage = nodeDataPages[nodeDataPage] =
    ALLOC(NodeData_t ,nodeDataPageSize);
    if (currentNodeDataPage == NULL) {
    for (i = 0; i < nodeDataPage; i++) FREE(nodeDataPages[i]);
    FREE(nodeDataPages);
    memOut = 1;
    return;
    }
    /* reset page index */
    nodeDataPageIndex = 0;
    return;

} /* end of ResizeNodeDataPages */


/**Function********************************************************************

  Synopsis    [Resize the number of pages allocated to store the minterm
  counts. ]

  Description [Resize the number of pages allocated to store the minterm
  counts.  The procedure  moves the counter to the next page when the
  end of the page is reached and allocates new pages when necessary.]

  SideEffects [Changes the size of minterm pages, page, page index, maximum 
  number of pages freeing stuff in case of memory out. ]

  SeeAlso     []

******************************************************************************/
static void
ResizeCountMintermPages(
   )
{
    int i;
    double **newMintermPages;

    page++;
    /* If the current page index is larger than the number of pages
     * allocated, allocate a new page array. Page numbers are incremented by
     * INITIAL_PAGES
     */
    if (page == maxPages) {
    newMintermPages = ALLOC(double *,maxPages + INITIAL_PAGES);
    if (newMintermPages == NULL) {
        for (i = 0; i < page; i++) FREE(mintermPages[i]);
        FREE(mintermPages);
        memOut = 1;
        return;
    } else {
        for (i = 0; i < maxPages; i++) {
        newMintermPages[i] = mintermPages[i];
        }
        /* Increase total page count */
        maxPages += INITIAL_PAGES;
        FREE(mintermPages);
        mintermPages = newMintermPages;
    }
    }
    /* Allocate a new page */
    currentMintermPage = mintermPages[page] = ALLOC(double,pageSize);
    if (currentMintermPage == NULL) {
    for (i = 0; i < page; i++) FREE(mintermPages[i]);
    FREE(mintermPages);
    memOut = 1;
    return;
    }
    /* reset page index */
    pageIndex = 0;
    return;

} /* end of ResizeCountMintermPages */


/**Function********************************************************************

  Synopsis    [Resize the number of pages allocated to store the node counts.]

  Description [Resize the number of pages allocated to store the node counts.
  The procedure  moves the counter to the next page when the end of
  the page is reached and allocates new pages when necessary.]

  SideEffects [Changes the size of pages, page, page index, maximum
  number of pages freeing stuff in case of memory out.]

  SeeAlso     []

******************************************************************************/
static void
ResizeCountNodePages(
   )
{
    int i;
    int **newNodePages;

    page++;

    /* If the current page index is larger than the number of pages
     * allocated, allocate a new page array. The number of pages is incremented
     * by INITIAL_PAGES.
     */
    if (page == maxPages) {
    newNodePages = ALLOC(int *,maxPages + INITIAL_PAGES);
    if (newNodePages == NULL) {
        for (i = 0; i < page; i++) FREE(nodePages[i]);
        FREE(nodePages);
        for (i = 0; i < page; i++) FREE(lightNodePages[i]);
        FREE(lightNodePages);
        memOut = 1;
        return;
    } else {
        for (i = 0; i < maxPages; i++) {
        newNodePages[i] = nodePages[i];
        }
        FREE(nodePages);
        nodePages = newNodePages;
    }

    newNodePages = ALLOC(int *,maxPages + INITIAL_PAGES);
    if (newNodePages == NULL) {
        for (i = 0; i < page; i++) FREE(nodePages[i]);
        FREE(nodePages);
        for (i = 0; i < page; i++) FREE(lightNodePages[i]);
        FREE(lightNodePages);
        memOut = 1;
        return;
    } else {
        for (i = 0; i < maxPages; i++) {
        newNodePages[i] = lightNodePages[i];
        }
        FREE(lightNodePages);
        lightNodePages = newNodePages;
    }
    /* Increase total page count */
    maxPages += INITIAL_PAGES;
    }
    /* Allocate a new page */
    currentNodePage = nodePages[page] = ALLOC(int,pageSize);
    if (currentNodePage == NULL) {
    for (i = 0; i < page; i++) FREE(nodePages[i]);
    FREE(nodePages);
    for (i = 0; i < page; i++) FREE(lightNodePages[i]);
    FREE(lightNodePages);
    memOut = 1;
    return;
    }
    /* Allocate a new page */
    currentLightNodePage = lightNodePages[page] = ALLOC(int,pageSize);
    if (currentLightNodePage == NULL) {
    for (i = 0; i <= page; i++) FREE(nodePages[i]);
    FREE(nodePages);
    for (i = 0; i < page; i++) FREE(lightNodePages[i]);
    FREE(lightNodePages);
    memOut = 1;
    return;
    }
    /* reset page index */
    pageIndex = 0;
    return;

} /* end of ResizeCountNodePages */


/**Function********************************************************************

  Synopsis    [Recursively counts minterms of each node in the DAG.]

  Description [Recursively counts minterms of each node in the DAG.
  Similar to the cuddCountMintermAux which recursively counts the
  number of minterms for the dag rooted at each node in terms of the
  total number of variables (max). This procedure creates the node
  data structure and stores the minterm count as part of the node
  data structure. ]

  SideEffects [Creates structures of type node quality and fills the st_table]

  SeeAlso     [SubsetCountMinterm]

******************************************************************************/
static double
SubsetCountMintermAux(
  DdNode * node /* function to analyze */,
  double  max /* number of minterms of constant 1 */,
  st_table * table /* visitedTable table */)
{

    DdNode    *N,*Nv,*Nnv; /* nodes to store cofactors  */
    double    min,*pmin; /* minterm count */
    double    min1, min2; /* minterm count */
    NodeData_t *dummy;
    NodeData_t *newEntry;
    int i;

#ifdef DEBUG
    num_calls++;
#endif

    /* Constant case */
    if (Cudd_IsConstant(node)) {
    if (node == zero) {
        return(0.0);
    } else {
        return(max);
    }
    } else {

    /* check if entry for this node exists */
    if (st_lookup(table,(char *)node, (char **)&dummy)) {
        min = *(dummy->mintermPointer);
        return(min);
    }

    /* Make the node regular to extract cofactors */
    N = Cudd_Regular(node);

    /* store the cofactors */
    Nv = Cudd_T(N);
    Nnv = Cudd_E(N);
    
    Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
    Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));

    min1 =  SubsetCountMintermAux(Nv, max,table)/2.0;
    if (memOut) return(0.0);
    min2 =  SubsetCountMintermAux(Nnv,max,table)/2.0;
    if (memOut) return(0.0);
    min = (min1+min2);

    /* if page index is at the bottom, then create a new page */
    if (pageIndex == pageSize) ResizeCountMintermPages();
    if (memOut) {
        for (i = 0; i <= nodeDataPage; i++) FREE(nodeDataPages[i]);
        FREE(nodeDataPages);
        st_free_table(table);
        return(0.0);
    }

    /* point to the correct location in the page */
    pmin = currentMintermPage+pageIndex;
    pageIndex++;

    /* store the minterm count of this node in the page */
    *pmin = min;

    /* Note I allocate the struct here. Freeing taken care of later */
    if (nodeDataPageIndex == nodeDataPageSize) ResizeNodeDataPages();
    if (memOut) {
        for (i = 0; i <= page; i++) FREE(mintermPages[i]);
        FREE(mintermPages);
        st_free_table(table);
        return(0.0);
    }

    newEntry = currentNodeDataPage + nodeDataPageIndex;
    nodeDataPageIndex++;

    /* points to the correct location in the page */
    newEntry->mintermPointer = pmin;
    /* initialize this field of the Node Quality structure */
    newEntry->nodesPointer = NULL;

    /* insert entry for the node in the table */
    if (st_insert(table,(char *)node, (char *)newEntry) == ST_OUT_OF_MEM) {
        memOut = 1;
        for (i = 0; i <= page; i++) FREE(mintermPages[i]);
        FREE(mintermPages);
        for (i = 0; i <= nodeDataPage; i++) FREE(nodeDataPages[i]);
        FREE(nodeDataPages);
        st_free_table(table);
        return(0.0);
    }
    return(min);
    }

} /* end of SubsetCountMintermAux */


/**Function********************************************************************

  Synopsis    [Counts minterms of each node in the DAG]

  Description [Counts minterms of each node in the DAG. Similar to the
  Cudd_CountMinterm procedure except this returns the minterm count for
  all the nodes in the bdd in an st_table.]

  SideEffects [none]

  SeeAlso     [SubsetCountMintermAux]

******************************************************************************/
static st_table *
SubsetCountMinterm(
  DdNode * node /* function to be analyzed */,
  int nvars /* number of variables node depends on */)
{
    st_table    *table;
    double    num;
    int i;


#ifdef DEBUG
    num_calls = 0;
#endif

    max = pow(2.0,(double) nvars);
    table = st_init_table(st_ptrcmp,st_ptrhash);
    if (table == NULL) goto OUT_OF_MEM;
    maxPages = INITIAL_PAGES;
    mintermPages = ALLOC(double *,maxPages);
    if (mintermPages == NULL) {
    st_free_table(table);
    goto OUT_OF_MEM;
    }
    page = 0;
    currentMintermPage = ALLOC(double,pageSize);
    mintermPages[page] = currentMintermPage;
    if (currentMintermPage == NULL) {
    FREE(mintermPages);
    st_free_table(table);
    goto OUT_OF_MEM;
    }
    pageIndex = 0;
    maxNodeDataPages = INITIAL_PAGES;
    nodeDataPages = ALLOC(NodeData_t *, maxNodeDataPages);
    if (nodeDataPages == NULL) {
    for (i = 0; i <= page ; i++) FREE(mintermPages[i]);
    FREE(mintermPages);
    st_free_table(table);
    goto OUT_OF_MEM;
    }
    nodeDataPage = 0;
    currentNodeDataPage = ALLOC(NodeData_t ,nodeDataPageSize);
    nodeDataPages[nodeDataPage] = currentNodeDataPage;
    if (currentNodeDataPage == NULL) {
    for (i = 0; i <= page ; i++) FREE(mintermPages[i]);
    FREE(mintermPages);
    FREE(nodeDataPages);
    st_free_table(table);
    goto OUT_OF_MEM;
    }
    nodeDataPageIndex = 0;

    num = SubsetCountMintermAux(node,max,table);
    if (memOut) goto OUT_OF_MEM;
    return(table);

OUT_OF_MEM:
    memOut = 1;
    return(NULL);

} /* end of SubsetCountMinterm */


/**Function********************************************************************

  Synopsis    [Recursively counts the number of nodes under the dag.
  Also counts the number of nodes under the lighter child of
  this node.]

  Description [Recursively counts the number of nodes under the dag.
  Also counts the number of nodes under the lighter child of
  this node. . Note that the same dag may be the lighter child of two
  different nodes and have different counts. As with the minterm counts,
  the node counts are stored in pages to be space efficient and the
  address for these node counts are stored in an st_table associated
  to each node. ]

  SideEffects [Updates the node data table with node counts]

  SeeAlso     [SubsetCountNodes]

******************************************************************************/
static int
SubsetCountNodesAux(
  DdNode * node /* current node */,
  st_table * table /* table to update node count, also serves as visited table. */,
  double  max /* maximum number of variables */)
{
    int tval, eval, i;
    DdNode *N, *Nv, *Nnv;
    double minNv, minNnv;
    NodeData_t *dummyN, *dummyNv, *dummyNnv, *dummyNBar;
    int *pmin, *pminBar, *val;

    if ((node == NULL) || Cudd_IsConstant(node))
    return(0);

    /* if this node has been processed do nothing */
    if (st_lookup(table, (char *)node, (char **)&dummyN) == 1) {
    val = dummyN->nodesPointer;
    if (val != NULL)
        return(0);
    } else {
    return(0);
    }

    N  = Cudd_Regular(node);
    Nv = Cudd_T(N);
    Nnv = Cudd_E(N);
    
    Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
    Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));

    /* find the minterm counts for the THEN and ELSE branches */
    if (Cudd_IsConstant(Nv)) {
    if (Nv == zero) {
        minNv = 0.0;
    } else {
        minNv = max;
    }
    } else {
    if (st_lookup(table, (char *)Nv, (char **)&dummyNv) == 1)
        minNv = *(dummyNv->mintermPointer);
    else {
        return(0);
    }
    }
    if (Cudd_IsConstant(Nnv)) {
    if (Nnv == zero) {
        minNnv = 0.0;
    } else {
        minNnv = max;
    }
    } else {
    if (st_lookup(table, (char *)Nnv, (char **)&dummyNnv) == 1) {
        minNnv = *(dummyNnv->mintermPointer);
    }
    else {
        return(0);
    }
    }


    /* recur based on which has larger minterm, */
    if (minNv >= minNnv) {
    tval = SubsetCountNodesAux(Nv, table, max);
    if (memOut) return(0);
    eval = SubsetCountNodesAux(Nnv, table, max);
    if (memOut) return(0);

    /* store the node count of the lighter child. */
    if (pageIndex == pageSize) ResizeCountNodePages();
    if (memOut) {
        for (i = 0; i <= page; i++) FREE(mintermPages[i]);
        FREE(mintermPages);
        for (i = 0; i <= nodeDataPage; i++) FREE(nodeDataPages[i]);
        FREE(nodeDataPages);
        st_free_table(table);
        return(0);
    }
    pmin = currentLightNodePage + pageIndex;
    *pmin = eval; /* Here the ELSE child is lighter */
    dummyN->lightChildNodesPointer = pmin;

    } else {
    eval = SubsetCountNodesAux(Nnv, table, max);
    if (memOut) return(0);
    tval = SubsetCountNodesAux(Nv, table, max);
    if (memOut) return(0);

    /* store the node count of the lighter child. */
    if (pageIndex == pageSize) ResizeCountNodePages();
    if (memOut) {
        for (i = 0; i <= page; i++) FREE(mintermPages[i]);
        FREE(mintermPages);
        for (i = 0; i <= nodeDataPage; i++) FREE(nodeDataPages[i]);
        FREE(nodeDataPages);
        st_free_table(table);
        return(0);
    }
    pmin = currentLightNodePage + pageIndex;
    *pmin = tval; /* Here the THEN child is lighter */
    dummyN->lightChildNodesPointer = pmin;

    }
    /* updating the page index for node count storage. */
    pmin = currentNodePage + pageIndex;
    *pmin = tval + eval + 1;
    dummyN->nodesPointer = pmin;

    /* pageIndex is parallel page index for count_nodes and count_lightNodes */
    pageIndex++;

    /* if this node has been reached first, it belongs to a heavier
       branch. Its complement will be reached later on a lighter branch.
       Hence the complement has zero node count. */

    if (st_lookup(table, (char *)Cudd_Not(node), (char **)&dummyNBar) == 1)  {
    if (pageIndex == pageSize) ResizeCountNodePages();
    if (memOut) {
        for (i = 0; i < page; i++) FREE(mintermPages[i]);
        FREE(mintermPages);
        for (i = 0; i < nodeDataPage; i++) FREE(nodeDataPages[i]);
        FREE(nodeDataPages);
        st_free_table(table);
        return(0);
    }
    pminBar = currentLightNodePage + pageIndex;
    *pminBar = 0;
    dummyNBar->lightChildNodesPointer = pminBar;
    /* The lighter child has less nodes than the parent.
     * So if parent 0 then lighter child zero
     */
    if (pageIndex == pageSize) ResizeCountNodePages();
    if (memOut) {
        for (i = 0; i < page; i++) FREE(mintermPages[i]);
        FREE(mintermPages);
        for (i = 0; i < nodeDataPage; i++) FREE(nodeDataPages[i]);
        FREE(nodeDataPages);
        st_free_table(table);
        return(0);
    }
    pminBar = currentNodePage + pageIndex;
    *pminBar = 0;
    dummyNBar->nodesPointer = pminBar ; /* maybe should point to zero */

    pageIndex++;
    }
    return(*pmin);
} /*end of SubsetCountNodesAux */


/**Function********************************************************************

  Synopsis    [Counts the nodes under the current node and its lighter child]

  Description [Counts the nodes under the current node and its lighter
  child. Calls a recursive procedure to count the number of nodes of
  a DAG rooted at a particular node and the number of nodes taken by its
  lighter child.]

  SideEffects [None]

  SeeAlso     [SubsetCountNodesAux]

******************************************************************************/
static int
SubsetCountNodes(
  DdNode * node /* function to be analyzed */,
  st_table * table /* node quality table */,
  int  nvars /* number of variables node depends on */)
{
    int    num;
    int i;

#ifdef DEBUG
    num_calls = 0;
#endif

    max = pow(2.0,(double) nvars);
    maxPages = INITIAL_PAGES;
    nodePages = ALLOC(int *,maxPages);
    if (nodePages == NULL)  {
    goto OUT_OF_MEM;
    }

    lightNodePages = ALLOC(int *,maxPages);
    if (lightNodePages == NULL) {
    for (i = 0; i <= page; i++) FREE(mintermPages[i]);
    FREE(mintermPages);
    for (i = 0; i <= nodeDataPage; i++) FREE(nodeDataPages[i]);
    FREE(nodeDataPages);
    FREE(nodePages);
    goto OUT_OF_MEM;
    }

    page = 0;
    currentNodePage = nodePages[page] = ALLOC(int,pageSize);
    if (currentNodePage == NULL) {
    for (i = 0; i <= page; i++) FREE(mintermPages[i]);
    FREE(mintermPages);
    for (i = 0; i <= nodeDataPage; i++) FREE(nodeDataPages[i]);
    FREE(nodeDataPages);
    FREE(lightNodePages);
    FREE(nodePages);
    goto OUT_OF_MEM;
    }

    currentLightNodePage = lightNodePages[page] = ALLOC(int,pageSize);
    if (currentLightNodePage == NULL) {
    for (i = 0; i <= page; i++) FREE(mintermPages[i]);
    FREE(mintermPages);
    for (i = 0; i <= nodeDataPage; i++) FREE(nodeDataPages[i]);
    FREE(nodeDataPages);
    FREE(currentNodePage);
    FREE(lightNodePages);
    FREE(nodePages);
    goto OUT_OF_MEM;
    }

    pageIndex = 0;
    num = SubsetCountNodesAux(node,table,max);
    if (memOut) goto OUT_OF_MEM;
    return(num);

OUT_OF_MEM:
    memOut = 1;
    return(0);

} /* end of SubsetCountNodes */


/**Function********************************************************************

  Synopsis    [Procedure to recursively store nodes that are retained in the subset.]

  Description [rocedure to recursively store nodes that are retained in the subset.]

  SideEffects [None]

  SeeAlso     [StoreNodes]

******************************************************************************/
static void
StoreNodes(
  st_table * storeTable,
  DdManager * dd,
  DdNode * node)
{
    char *dummy;
    DdNode *N, *Nt, *Ne;
    if (Cudd_IsConstant(dd)) {
    return;
    }
    N = Cudd_Regular(node);
    if (st_lookup(storeTable, (char *)N, (char **)&dummy)) {
    return;
    }
    cuddRef(N);
    if (st_insert(storeTable, (char *)N, NIL(char)) == ST_OUT_OF_MEM) {
    fprintf(dd->err,"Something wrong, st_table insert failed\n");
    }

    Nt = Cudd_T(N);
    Ne = Cudd_E(N);

    StoreNodes(storeTable, dd, Nt);
    StoreNodes(storeTable, dd, Ne);
    return;

}


/**Function********************************************************************

  Synopsis    [Builds the subset BDD using the heavy branch method.] 

  Description [The procedure carries out the building of the subset BDD
  starting at the root. Using the three different counts labelling each node,
  the procedure chooses the heavier branch starting from the root and keeps
  track of the number of nodes it discards at each step, thus keeping count
  of the size of the subset BDD dynamically. Once the threshold is satisfied,
  the procedure then calls ITE to build the BDD.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static DdNode *
BuildSubsetBdd(
  DdManager * dd /* DD manager */,
  DdNode * node /* current node */,
  int * size /* current size of the subset */,
  st_table * visitedTable /* visited table storing all node data */,
  int  threshold,
  st_table * storeTable,
  st_table * approxTable)
{

    DdNode *Nv, *Nnv, *N, *topv, *neW;
    double minNv, minNnv;
    NodeData_t *currNodeQual;
    NodeData_t *currNodeQualT;
    NodeData_t *currNodeQualE;
    DdNode *ThenBranch, *ElseBranch;
    unsigned int topid;
    char *dummy;

#ifdef DEBUG
    num_calls++;
#endif
    /*If the size of the subset is below the threshold, dont do
      anything. */
    if ((*size) <= threshold) {
      /* store nodes below this, so we can recombine if possible */
      StoreNodes(storeTable, dd, node);
      return(node);
    }

    if (Cudd_IsConstant(node))
    return(node);

    /* Look up minterm count for this node. */
    if (!st_lookup(visitedTable, (char *)node, (char **)&currNodeQual)) {
    fprintf(dd->err,
        "Something is wrong, ought to be in node quality table\n");
    }

    /* Get children. */
    N = Cudd_Regular(node);
    Nv = Cudd_T(N);
    Nnv = Cudd_E(N);

    /* complement if necessary */
    Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
    Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));

    if (!Cudd_IsConstant(Nv)) {
        /* find out minterms and nodes contributed by then child */
        if (!st_lookup(visitedTable, (char *)Nv,
               (char **)&currNodeQualT)) {
        fprintf(dd->out,"Something wrong, couldnt find nodes in node quality table\n");
        dd->errorCode = CUDD_INTERNAL_ERROR;
        return(NULL);
        }
    else {
        minNv = *(((NodeData_t *)currNodeQualT)->mintermPointer);
    }
    } else {
    if (Nv == zero) {
        minNv = 0;
    } else  {
        minNv = max;
    }
    }
    if (!Cudd_IsConstant(Nnv)) {
        /* find out minterms and nodes contributed by else child */
    if (!st_lookup(visitedTable, (char *)Nnv, (char **)&currNodeQualE)) {
        fprintf(dd->out,"Something wrong, couldnt find nodes in node quality table\n");
        dd->errorCode = CUDD_INTERNAL_ERROR;
        return(NULL);
    } else {
        minNnv = *(((NodeData_t *)currNodeQualE)->mintermPointer);
    }
    } else {
    if (Nnv == zero) {
        minNnv = 0;
    } else {
        minNnv = max;
    }
    }

    /* keep track of size of subset by subtracting the number of
     * differential nodes contributed by lighter child
     */
    *size = (*(size)) - (int)*(currNodeQual->lightChildNodesPointer);
    if (minNv >= minNnv) { /*SubsetCountNodesAux procedure takes
                 the Then branch in case of a tie */

        /* recur with the Then branch */
    ThenBranch = (DdNode *)BuildSubsetBdd(dd, Nv, size,
          visitedTable, threshold, storeTable, approxTable);
    if (ThenBranch == NULL) {
        return(NULL);
    }
    cuddRef(ThenBranch);
    /* The Else branch is either a node that already exists in the
     * subset, or one whose approximation has been computed, or
     * Zero.
     */
    if (st_lookup(storeTable, (char *)Cudd_Regular(Nnv), (char **)&dummy)) {
      ElseBranch = Nnv;
      cuddRef(ElseBranch);
    } else {
      if (st_lookup(approxTable, (char *)Nnv, (char **)&dummy)) {
        ElseBranch = (DdNode *)dummy;
        cuddRef(ElseBranch);
      } else {
        ElseBranch = zero;
        cuddRef(ElseBranch);
      }
    }
    
    }
    else {
        /* recur with the Else branch */
        ElseBranch = (DdNode *)BuildSubsetBdd(dd, Nnv, size,
              visitedTable, threshold, storeTable, approxTable);
    if (ElseBranch == NULL) {
        return(NULL);
    }
    cuddRef(ElseBranch);
    /* The Then branch is either a node that already exists in the
     * subset, or one whose approximation has been computed, or
     * Zero.
     */
    if (st_lookup(storeTable, (char *)Cudd_Regular(Nv), (char **)&dummy)) {
      ThenBranch = Nv;
      cuddRef(ThenBranch);
    } else {
      if (st_lookup(approxTable, (char *)Nv, (char **)&dummy)) {
        ThenBranch = (DdNode *)dummy;
        cuddRef(ThenBranch);
      } else {
        ThenBranch = zero;
        cuddRef(ThenBranch);
      }
    }
    }

    /* construct the Bdd with the top variable and the two children */
    topid = Cudd_NodeReadIndex(N);
    topv = Cudd_ReadVars(dd, topid);
    cuddRef(topv);
    neW =  cuddBddIteRecur(dd, topv, ThenBranch, ElseBranch);
    if (neW != NULL) {
      cuddRef(neW);
    }
    Cudd_RecursiveDeref(dd, topv);
    Cudd_RecursiveDeref(dd, ThenBranch);
    Cudd_RecursiveDeref(dd, ElseBranch);

      
    if (neW == NULL)
    return(NULL);
    else {
        /* store this node in the store table */
        if (!st_lookup(storeTable, (char *)Cudd_Regular(neW), (char **)&dummy)) {
      cuddRef(neW);
      st_insert(storeTable, (char *)Cudd_Regular(neW), (char *)NIL(char));
      
        }
    /* store the approximation for this node */
    if (N !=  Cudd_Regular(neW)) {
          if (st_lookup(approxTable, (char *)node, (char **)&dummy)) {
            fprintf(dd->err, "This node should not be in the approximated table\n");
        } else {
            cuddRef(neW);
            st_insert(approxTable, (char *)node, (char *)neW);
        }
    }
        cuddDeref(neW);
        return(neW);
    }
} /* end of BuildSubsetBdd */