summaryrefslogtreecommitdiffstats
path: root/src/aig/gia/giaCSat3.c
blob: c34c84ca636147f470abec114b163cc85c52be45 (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
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
/**CFile****************************************************************

  FileName    [giaCSat.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [A simple circuit-based solver.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: giaCSat.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "gia.h"

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

typedef struct Cbs3_Par_t_ Cbs3_Par_t;
struct Cbs3_Par_t_
{
    // conflict limits
    int           nBTLimit;     // limit on the number of conflicts
    int           nJustLimit;   // limit on the size of justification queue
    int           nRestLimit;   // limit on the number of restarts
    // current parameters
    int           nBTThis;      // number of conflicts
    int           nJustThis;    // max size of the frontier
    int           nBTTotal;     // total number of conflicts
    int           nJustTotal;   // total size of the frontier
    // other
    int           fVerbose;
};

typedef struct Cbs3_Que_t_ Cbs3_Que_t;
struct Cbs3_Que_t_
{
    int           iHead;        // beginning of the queue
    int           iTail;        // end of the queue
    int           nSize;        // allocated size
    int *         pData;        // nodes stored in the queue
};
 
typedef struct Cbs3_Man_t_ Cbs3_Man_t;
struct Cbs3_Man_t_
{
    Cbs3_Par_t    Pars;         // parameters
    Gia_Man_t *   pAig;         // AIG manager
    Cbs3_Que_t    pProp;        // propagation queue
    Cbs3_Que_t    pJust;        // justification queue
    Cbs3_Que_t    pClauses;     // clause queue
    Vec_Int_t *   vModel;       // satisfying assignment
    Vec_Int_t *   vTemp;        // temporary storage
    // circuit structure
    int           nVars;
    int           nVarsAlloc;
    int           var_inc;
    Vec_Int_t     vMap;
    Vec_Int_t     vRef;
    Vec_Int_t     vFans;
    Vec_Wec_t     vImps;
    // internal data
    Vec_Str_t     vAssign;
    Vec_Str_t     vMark;
    Vec_Int_t     vLevReason;
    Vec_Int_t     vActs;
    Vec_Int_t     vWatches;
    Vec_Int_t     vWatchUpds;
    // SAT calls statistics
    int           nSatUnsat;    // the number of proofs
    int           nSatSat;      // the number of failure
    int           nSatUndec;    // the number of timeouts
    int           nSatTotal;    // the number of calls
    // conflicts
    int           nConfUnsat;   // conflicts in unsat problems
    int           nConfSat;     // conflicts in sat problems
    int           nConfUndec;   // conflicts in undec problems
    // runtime stats
    abctime       timeJFront;
    abctime       timeSatLoad;  // SAT solver loading time
    abctime       timeSatUnsat; // unsat
    abctime       timeSatSat;   // sat
    abctime       timeSatUndec; // undecided
    abctime       timeTotal;    // total runtime
    // other statistics
    int           nPropCalls[3];
    int           nFails[2];
    int           nClauseConf;
    int           nDecs;
};

static inline int   Cbs3_VarUnused( Cbs3_Man_t * p, int iVar )                     { return Vec_IntEntry(&p->vLevReason, 3*iVar) == -1; }
static inline void  Cbs3_VarSetUnused( Cbs3_Man_t * p, int iVar )                  { Vec_IntWriteEntry(&p->vLevReason, 3*iVar, -1);     } 

static inline int   Cbs3_VarMark0( Cbs3_Man_t * p, int iVar )                      { return Vec_StrEntry(&p->vMark, iVar);              }
static inline void  Cbs3_VarSetMark0( Cbs3_Man_t * p, int iVar, int Value )        { Vec_StrWriteEntry(&p->vMark, iVar, (char)Value);   } 

static inline int   Cbs3_VarIsAssigned( Cbs3_Man_t * p, int iVar )                 { return Vec_StrEntry(&p->vAssign, iVar) < 2;        }
static inline void  Cbs3_VarUnassign( Cbs3_Man_t * p, int iVar )                   { assert( Cbs3_VarIsAssigned(p, iVar)); Vec_StrWriteEntry(&p->vAssign, iVar, (char)(2+Vec_StrEntry(&p->vAssign, iVar))); Cbs3_VarSetUnused(p, iVar); }

static inline int   Cbs3_VarValue( Cbs3_Man_t * p, int iVar )                      { return Vec_StrEntry(&p->vAssign, iVar);            }
static inline void  Cbs3_VarSetValue( Cbs3_Man_t * p, int iVar, int v )            { assert( !Cbs3_VarIsAssigned(p, iVar)); Vec_StrWriteEntry(&p->vAssign, iVar, (char)v);                                  }

static inline int   Cbs3_VarLit0( Cbs3_Man_t * p, int iVar )                       { return Vec_IntEntry( &p->vFans, Abc_Var2Lit(iVar, 0) );                             } 
static inline int   Cbs3_VarLit1( Cbs3_Man_t * p, int iVar )                       { return Vec_IntEntry( &p->vFans, Abc_Var2Lit(iVar, 1) );                             } 
static inline int   Cbs3_VarIsPi( Cbs3_Man_t * p, int iVar )                       { return Vec_IntEntry( &p->vFans, Abc_Var2Lit(iVar, 0) ) == 0;                        } 
static inline int   Cbs3_VarIsJust( Cbs3_Man_t * p, int iVar )                     { int * pLits = Vec_IntEntryP(&p->vFans, Abc_Var2Lit(iVar, 0)); return pLits[0] > 0 && Cbs3_VarValue(p, Abc_Lit2Var(pLits[0])) >= 2 && Cbs3_VarValue(p, Abc_Lit2Var(pLits[1])) >= 2; } 

static inline int   Cbs3_VarDecLevel( Cbs3_Man_t * p, int iVar )                   { assert( !Cbs3_VarUnused(p, iVar) ); return Vec_IntEntry(&p->vLevReason, 3*iVar);    }
static inline int   Cbs3_VarReason0( Cbs3_Man_t * p, int iVar )                    { assert( !Cbs3_VarUnused(p, iVar) ); return Vec_IntEntry(&p->vLevReason, 3*iVar+1);  }
static inline int   Cbs3_VarReason1( Cbs3_Man_t * p, int iVar )                    { assert( !Cbs3_VarUnused(p, iVar) ); return Vec_IntEntry(&p->vLevReason, 3*iVar+2);  }
static inline int * Cbs3_VarReasonP( Cbs3_Man_t * p, int iVar )                    { assert( !Cbs3_VarUnused(p, iVar) ); return Vec_IntEntryP(&p->vLevReason, 3*iVar+1); }
static inline int   Cbs3_ClauseDecLevel( Cbs3_Man_t * p, int hClause )             { return Cbs3_VarDecLevel( p, p->pClauses.pData[hClause] );                           }

static inline int   Cbs3_ClauseSize( Cbs3_Man_t * p, int hClause )                 { return p->pClauses.pData[hClause];                                                  }
static inline int * Cbs3_ClauseLits( Cbs3_Man_t * p, int hClause )                 { return p->pClauses.pData+hClause+1;                                                 }
static inline int   Cbs3_ClauseLit( Cbs3_Man_t * p, int hClause, int i )           { return p->pClauses.pData[hClause+1+i];                                              }
static inline int * Cbs3_ClauseNext1p( Cbs3_Man_t * p, int hClause )               { return p->pClauses.pData+hClause+Cbs3_ClauseSize(p, hClause)+2;                     }

static inline void  Cbs3_ClauseSetSize( Cbs3_Man_t * p, int hClause, int x )       { p->pClauses.pData[hClause] = x;                                                     }
static inline void  Cbs3_ClauseSetLit( Cbs3_Man_t * p, int hClause, int i, int x ) { p->pClauses.pData[hClause+i+1] = x;                                                 }
static inline void  Cbs3_ClauseSetNext( Cbs3_Man_t * p, int hClause, int n, int x ){ p->pClauses.pData[hClause+Cbs3_ClauseSize(p, hClause)+1+n] = x;                     }


#define Cbs3_QueForEachEntry( Que, iObj, i )                         \
    for ( i = (Que).iHead; (i < (Que).iTail) && ((iObj) = (Que).pData[i]); i++ )

#define Cbs3_ClauseForEachEntry( p, hClause, iObj, i )               \
    for ( i = 1; i <= Cbs3_ClauseSize(p, hClause) && (iObj = (p)->pClauses.pData[hClause+i]); i++ )
#define Cbs3_ClauseForEachEntry1( p, hClause, iObj, i )              \
    for ( i = 2; i <= Cbs3_ClauseSize(p, hClause) && (iObj = (p)->pClauses.pData[hClause+i]); i++ )

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Sets default values of the parameters.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cbs3_SetDefaultParams( Cbs3_Par_t * pPars )
{
    memset( pPars, 0, sizeof(Cbs3_Par_t) );
    pPars->nBTLimit    =  1000;   // limit on the number of conflicts
    pPars->nJustLimit  =   500;   // limit on the size of justification queue
    pPars->nRestLimit  =    10;   // limit on the number of restarts
    pPars->fVerbose    =     1;   // print detailed statistics
}
void Cbs3_ManSetConflictNum( Cbs3_Man_t * p, int Num )
{
    p->Pars.nBTLimit = Num;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Cbs3_Man_t * Cbs3_ManAlloc( Gia_Man_t * pGia )
{
    Cbs3_Man_t * p;
    p = ABC_CALLOC( Cbs3_Man_t, 1 );
    p->pProp.nSize = p->pJust.nSize = p->pClauses.nSize = 10000;
    p->pProp.pData = ABC_ALLOC( int, p->pProp.nSize );
    p->pJust.pData = ABC_ALLOC( int, p->pJust.nSize );
    p->pClauses.pData = ABC_ALLOC( int, p->pClauses.nSize );
    p->pClauses.iHead = p->pClauses.iTail = 1;
    p->vModel   = Vec_IntAlloc( 1000 );
    p->vTemp    = Vec_IntAlloc( 1000 );
    p->pAig     = pGia;
    Cbs3_SetDefaultParams( &p->Pars );
    // circuit structure
    Vec_IntPush( &p->vMap, -1 );
    Vec_IntPush( &p->vRef, -1 );
    Vec_IntPushTwo( &p->vFans, -1, -1 );
    Vec_WecPushLevel( &p->vImps );
    Vec_WecPushLevel( &p->vImps );
    p->nVars = 1;
    // internal data
    p->nVarsAlloc = 1000;
    Vec_StrFill( &p->vAssign,      p->nVarsAlloc, 2 );
    Vec_StrFill( &p->vMark,        p->nVarsAlloc, 0 );
    Vec_IntFill( &p->vLevReason, 3*p->nVarsAlloc, -1 );
    Vec_IntFill( &p->vActs,        p->nVarsAlloc, 0 );
    Vec_IntFill( &p->vWatches,   2*p->nVarsAlloc, 0 );
    Vec_IntGrow( &p->vWatchUpds, 1000 );
    return p;
}
static inline void Cbs3_ManReset( Cbs3_Man_t * p )
{
    assert( p->nVars == Vec_IntSize(&p->vMap) );
    Vec_IntShrink( &p->vMap, 1 );
    Vec_IntShrink( &p->vRef, 1 );
    Vec_IntShrink( &p->vFans, 2 );
    Vec_WecShrink( &p->vImps, 2 );
    p->nVars = 1;
}
static inline void Cbs3_ManGrow( Cbs3_Man_t * p )
{
    if ( p->nVarsAlloc < p->nVars )
    {
        p->nVarsAlloc = 2*p->nVars;
        Vec_StrFill( &p->vAssign,      p->nVarsAlloc, 2 );
        Vec_StrFill( &p->vMark,        p->nVarsAlloc, 0 );
        Vec_IntFill( &p->vLevReason, 3*p->nVarsAlloc, -1 );
        Vec_IntFill( &p->vActs,        p->nVarsAlloc, 0 );
        Vec_IntFill( &p->vWatches,   2*p->nVarsAlloc, 0 );
    }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cbs3_ManStop( Cbs3_Man_t * p )
{
    // circuit structure
    Vec_IntErase( &p->vMap );
    Vec_IntErase( &p->vRef );
    Vec_IntErase( &p->vFans );
    Vec_WecErase( &p->vImps );
    // internal data
    Vec_StrErase( &p->vAssign );
    Vec_StrErase( &p->vMark );
    Vec_IntErase( &p->vLevReason );
    Vec_IntErase( &p->vActs );
    Vec_IntErase( &p->vWatches );
    Vec_IntErase( &p->vWatchUpds );
    Vec_IntFree( p->vModel );
    Vec_IntFree( p->vTemp );
    ABC_FREE( p->pClauses.pData );
    ABC_FREE( p->pProp.pData );
    ABC_FREE( p->pJust.pData );
    ABC_FREE( p );
}
int Cbs3_ManMemory( Cbs3_Man_t * p )
{
    int nMem = sizeof(Cbs3_Man_t);
    nMem += (int)Vec_IntMemory( &p->vMap );
    nMem += (int)Vec_IntMemory( &p->vRef );
    nMem += (int)Vec_IntMemory( &p->vFans );
    nMem += (int)Vec_WecMemory( &p->vImps );
    nMem += (int)Vec_StrMemory( &p->vAssign );
    nMem += (int)Vec_StrMemory( &p->vMark );
    nMem += (int)Vec_IntMemory( &p->vActs );
    nMem += (int)Vec_IntMemory( &p->vWatches );
    nMem += (int)Vec_IntMemory( &p->vWatchUpds );
    nMem += (int)Vec_IntMemory( p->vModel );
    nMem += (int)Vec_IntMemory( p->vTemp );
    nMem += 4*p->pClauses.nSize;
    nMem += 4*p->pProp.nSize;
    nMem += 4*p->pJust.nSize;
    return nMem;
}

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

  Synopsis    [Returns satisfying assignment.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Cbs3_ReadModel( Cbs3_Man_t * p )
{
    return p->vModel;
}


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

  Synopsis    [Activity.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
//#define USE_ACTIVITY

#ifdef USE_ACTIVITY
static inline void Cbs3_ActReset( Cbs3_Man_t * p ) 
{
    int i, * pAct = Vec_IntArray(&p->vActs);
    for ( i = 0; i < p->nVars; i++ )
        pAct[i] = (1 << 10);
    p->var_inc = (1 << 5);
}
static inline void Cbs3_ActRescale( Cbs3_Man_t * p ) 
{
    int i, * pAct = Vec_IntArray(&p->vActs);
    for ( i = 0; i < p->nVars; i++ )
        pAct[i] >>= 19;
    p->var_inc >>= 19;
    p->var_inc = Abc_MaxInt( (unsigned)p->var_inc, (1<<5) );
}
static inline void Cbs3_ActBumpVar( Cbs3_Man_t * p, int iVar ) 
{
    int * pAct = Vec_IntArray(&p->vActs);
    pAct[iVar] += p->var_inc;
    if ((unsigned)pAct[iVar] & 0x80000000)
        Cbs3_ActRescale(p);
}
static inline void Cbs3_ActDecay( Cbs3_Man_t * p ) 
{
    p->var_inc += (p->var_inc >>  4); 
}
#else
static inline void Cbs3_ActReset( Cbs3_Man_t * p )              {}
static inline void Cbs3_ActRescale( Cbs3_Man_t * p )            {}
static inline void Cbs3_ActBumpVar( Cbs3_Man_t * p, int iVar )  {}
static inline void Cbs3_ActDecay( Cbs3_Man_t * p )              {}
#endif


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

  Synopsis    [Returns 1 if the solver is out of limits.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cbs3_ManCheckLimits( Cbs3_Man_t * p )
{
    p->nFails[0] += p->Pars.nJustThis > p->Pars.nJustLimit;
    p->nFails[1] += p->Pars.nBTThis > p->Pars.nBTLimit;
    return p->Pars.nJustThis > p->Pars.nJustLimit || p->Pars.nBTThis > p->Pars.nBTLimit;
}

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

  Synopsis    [Saves the satisfying assignment as an array of literals.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Cbs3_ManSaveModel( Cbs3_Man_t * p, Vec_Int_t * vCex )
{
    int i, iLit;
    Vec_IntClear( vCex );
    p->pProp.iHead = 0;
    Cbs3_QueForEachEntry( p->pProp, iLit, i )
        if ( Cbs3_VarIsPi(p, Abc_Lit2Var(iLit)) )
            Vec_IntPush( vCex, Abc_Lit2LitV(Vec_IntArray(&p->vMap), iLit)-2 );
} 
static inline void Cbs3_ManSaveModelAll( Cbs3_Man_t * p, Vec_Int_t * vCex )
{
    int i, iLit;
    Vec_IntClear( vCex );
    p->pProp.iHead = 0;
    Cbs3_QueForEachEntry( p->pProp, iLit, i )
    {
        int iVar = Abc_Lit2Var(iLit);
        Vec_IntPush( vCex, Abc_Var2Lit(iVar, !Cbs3_VarValue(p, iVar)) );
    }
} 

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cbs3_QueIsEmpty( Cbs3_Que_t * p )
{
    return p->iHead == p->iTail;
}
static inline int Cbs3_QueSize( Cbs3_Que_t * p )
{
    return p->iTail - p->iHead;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Cbs3_QuePush( Cbs3_Que_t * p, int iObj )
{
    if ( p->iTail == p->nSize )
    {
        p->nSize *= 2;
        p->pData = ABC_REALLOC( int, p->pData, p->nSize ); 
    }
    p->pData[p->iTail++] = iObj;
}
static inline void Cbs3_QueGrow( Cbs3_Que_t * p, int Plus )
{
    if ( p->iTail + Plus > p->nSize )
    {
        p->nSize *= 2;
        p->pData = ABC_REALLOC( int, p->pData, p->nSize ); 
    }
    assert( p->iTail + Plus <= p->nSize );
}

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

  Synopsis    [Returns 1 if the object in the queue.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cbs3_QueHasNode( Cbs3_Que_t * p, int iObj )
{
    int i, iTemp;
    Cbs3_QueForEachEntry( *p, iTemp, i )
        if ( iTemp == iObj )
            return 1;
    return 0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Cbs3_QueStore( Cbs3_Que_t * p, int * piHeadOld, int * piTailOld )
{
    int i;
    *piHeadOld = p->iHead;
    *piTailOld = p->iTail;
    for ( i = *piHeadOld; i < *piTailOld; i++ )
        Cbs3_QuePush( p, p->pData[i] );
    p->iHead = *piTailOld;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Cbs3_QueRestore( Cbs3_Que_t * p, int iHeadOld, int iTailOld )
{
    p->iHead = iHeadOld;
    p->iTail = iTailOld;
}

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

  Synopsis    [Find variable with the highest ID.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cbs3_ManDecide( Cbs3_Man_t * p )
{
    int i, iObj, iObjMax = 0;
#ifdef USE_ACTIVITY
    Cbs3_QueForEachEntry( p->pJust, iObj, i )
        if ( iObjMax == 0 || 
             Vec_IntEntry(&p->vActs, iObjMax) <  Vec_IntEntry(&p->vActs, iObj) || 
            (Vec_IntEntry(&p->vActs, iObjMax) == Vec_IntEntry(&p->vActs, iObj) && Vec_IntEntry(&p->vMap, iObjMax) < Vec_IntEntry(&p->vMap, iObj)) )
             iObjMax = iObj;
#else
    Cbs3_QueForEachEntry( p->pJust, iObj, i )
//       if ( iObjMax == 0 || iObjMax < iObj )
       if ( iObjMax == 0 || Vec_IntEntry(&p->vMap, iObjMax) < Vec_IntEntry(&p->vMap, iObj) )
            iObjMax = iObj;
#endif
    return iObjMax;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Cbs3_ManCancelUntil( Cbs3_Man_t * p, int iBound )
{
    int i, iLit;
    assert( iBound <= p->pProp.iTail );
    p->pProp.iHead = iBound;
    Cbs3_QueForEachEntry( p->pProp, iLit, i )
        Cbs3_VarUnassign( p, Abc_Lit2Var(iLit) );
    p->pProp.iTail = iBound;
}

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

  Synopsis    [Assigns the variables a value.]

  Description [Returns 1 if conflict; 0 if no conflict.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Cbs3_ManAssign( Cbs3_Man_t * p, int iLit, int Level, int iRes0, int iRes1 )
{
    int iObj = Abc_Lit2Var(iLit);
    assert( Cbs3_VarUnused(p, iObj) );
    assert( !Cbs3_VarIsAssigned(p, iObj) );
    Cbs3_VarSetValue( p, iObj, !Abc_LitIsCompl(iLit) );
    Cbs3_QuePush( &p->pProp, iLit );
    Vec_IntWriteEntry( &p->vLevReason, 3*iObj,   Level );
    Vec_IntWriteEntry( &p->vLevReason, 3*iObj+1, iRes0 );
    Vec_IntWriteEntry( &p->vLevReason, 3*iObj+2, iRes1 );
}




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

  Synopsis    [Prints conflict clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Cbs3_ManPrintClause( Cbs3_Man_t * p, int Level, int hClause )
{
    int i, iLit;
    assert( Cbs3_QueIsEmpty( &p->pClauses ) );
    printf( "Level %2d : ", Level );
    Cbs3_ClauseForEachEntry( p, hClause, iLit, i )
        printf( "%c%d ", Abc_LitIsCompl(iLit) ? '-':'+', Abc_Lit2Var(iLit) );
//        printf( "%d=%d(%d) ", iObj, Cbs3_VarValue(p, Abc_Lit2Var(iLit)), Cbs3_VarDecLevel(p, Abc_Lit2Var(iLit)) );
    printf( "\n" );
}
static inline void Cbs3_ManPrintCube( Cbs3_Man_t * p, int Level, int hClause )
{
    int i, iObj;
    assert( Cbs3_QueIsEmpty( &p->pClauses ) );
    printf( "Level %2d : ", Level );
    Cbs3_ClauseForEachEntry( p, hClause, iObj, i )
        printf( "%c%d ", Cbs3_VarValue(p, iObj)? '+':'-', iObj );
    printf( "\n" );
}

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

  Synopsis    [Finalized the clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Cbs3_ManCleanWatch( Cbs3_Man_t * p )
{
    int i, iLit;
    Vec_IntForEachEntry( &p->vWatchUpds, iLit, i )
        Vec_IntWriteEntry( &p->vWatches, iLit, 0 );
    Vec_IntClear( &p->vWatchUpds );
    //Vec_IntForEachEntry( &p->vWatches, iLit, i )
    //    assert( iLit == 0 );
}
static inline void Cbs3_ManWatchClause( Cbs3_Man_t * p, int hClause, int Lit )
{
    int * pLits = Cbs3_ClauseLits( p, hClause );
    int * pPlace = Vec_IntEntryP( &p->vWatches, Abc_LitNot(Lit) );
    if ( *pPlace == 0 )
        Vec_IntPush( &p->vWatchUpds, Abc_LitNot(Lit) );
/*
    if ( pClause->pLits[0] == Lit )
        pClause->pNext0 = p->pWatches[lit_neg(Lit)];  
    else
    {
        assert( pClause->pLits[1] == Lit );
        pClause->pNext1 = p->pWatches[lit_neg(Lit)];  
    }
    p->pWatches[lit_neg(Lit)] = pClause;
*/
    assert( Lit == pLits[0] || Lit == pLits[1] );
    Cbs3_ClauseSetNext( p, hClause, Lit == pLits[1], *pPlace );
    *pPlace = hClause;
}
static inline int Cbs3_QueFinish( Cbs3_Man_t * p, int Level )
{
    Cbs3_Que_t * pQue = &(p->pClauses); 
    int i, iObj, hClauseC, hClause = pQue->iHead, Size = pQue->iTail - pQue->iHead - 1;
    assert( pQue->iHead+1 < pQue->iTail );
    Cbs3_ClauseSetSize( p, pQue->iHead, Size );
    hClauseC = pQue->iHead = pQue->iTail;
    //printf( "Adding cube: " ); Cbs3_ManPrintCube(p, Level, hClause);
    if ( Size == 1 )
        return hClause;
    // create watched clause
    pQue->iHead = hClause;
    Cbs3_QueForEachEntry( p->pClauses, iObj, i )
    {
        if ( i == hClauseC )
            break;
        else if ( i == hClause ) // nlits
            Cbs3_QuePush( pQue, iObj );
        else // literals
            Cbs3_QuePush( pQue, Abc_Var2Lit(iObj, Cbs3_VarValue(p, iObj)) ); // complement
    }
    Cbs3_QuePush( pQue, 0 ); // next0
    Cbs3_QuePush( pQue, 0 ); // next1
    pQue->iHead = pQue->iTail;
    Cbs3_ManWatchClause( p, hClauseC, Cbs3_ClauseLit(p, hClauseC, 0) );
    Cbs3_ManWatchClause( p, hClauseC, Cbs3_ClauseLit(p, hClauseC, 1) );
    //printf( "Adding clause %d: ", hClauseC ); Cbs3_ManPrintClause(p, Level, hClauseC);
    Cbs3_ActDecay( p );
    return hClause;
}

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

  Synopsis    [Returns conflict clause.]

  Description [Performs conflict analysis.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cbs3_ManDeriveReason( Cbs3_Man_t * p, int Level )
{
    Cbs3_Que_t * pQue = &(p->pClauses);
    int i, k, iObj, iLitLevel, * pReason;
    assert( pQue->pData[pQue->iHead] == 0 );
    assert( pQue->pData[pQue->iHead+1] == 0 );
    assert( pQue->iHead + 2 < pQue->iTail );
    //for ( i = pQue->iHead + 2; i < pQue->iTail; i++ )
    //    assert( !Cbs3_VarMark0(p, pQue->pData[i]) );
    // compact literals
    Vec_IntClear( p->vTemp );
    for ( i = k = pQue->iHead + 2; i < pQue->iTail; i++ )
    {
        iObj = pQue->pData[i];
        if ( Cbs3_VarMark0(p, iObj) ) // unassigned - seen again
            continue;
        //if ( Vec_IntEntry(&p->vActivity, iObj) == 0 )
        //    Vec_IntPush( &p->vActStore, iObj );
        //Vec_IntAddToEntry( &p->vActivity, iObj, 1 );
        // assigned - seen first time
        Cbs3_VarSetMark0(p, iObj, 1);
        Cbs3_ActBumpVar(p, iObj);
        Vec_IntPush( p->vTemp, iObj );
        // check decision level
        iLitLevel = Cbs3_VarDecLevel( p, iObj );
        if ( iLitLevel < Level )
        {
            pQue->pData[k++] = iObj;
            continue;
        }
        assert( iLitLevel == Level );
        pReason = Cbs3_VarReasonP( p, iObj );
        if ( pReason[0] == 0 && pReason[1] == 0 ) // no reason
        {
            assert( pQue->pData[pQue->iHead+1] == 0 );
            pQue->pData[pQue->iHead+1] = iObj;
        }
        else if ( pReason[0] != 0 ) // circuit reason
        {
            Cbs3_QuePush( pQue, pReason[0] );
            if ( pReason[1] )
            Cbs3_QuePush( pQue, pReason[1] );
        }
        else // clause reason
        {
            int i, * pLits, nLits = Cbs3_ClauseSize( p, pReason[1] );
            assert( pReason[1] );
            Cbs3_QueGrow( pQue, nLits );
            pLits = Cbs3_ClauseLits( p, pReason[1] );
            assert( iObj == Abc_Lit2Var(pLits[0]) );
            for ( i = 1; i < nLits; i++ )
                Cbs3_QuePush( pQue, Abc_Lit2Var(pLits[i]) );
        }
    }
    assert( pQue->pData[pQue->iHead] == 0 );
    assert( pQue->pData[pQue->iHead+1] != 0 );
    pQue->iTail = k;
    // clear the marks
    Vec_IntForEachEntry( p->vTemp, iObj, i )
        Cbs3_VarSetMark0(p, iObj, 0);
    return Cbs3_QueFinish( p, Level );
}
static inline int Cbs3_ManAnalyze( Cbs3_Man_t * p, int Level, int iVar, int iFan0, int iFan1 )
{
    Cbs3_Que_t * pQue = &(p->pClauses); 
    assert( Cbs3_VarIsAssigned(p, iVar) );
    assert( Cbs3_QueIsEmpty( pQue ) );
    Cbs3_QuePush( pQue, 0 );
    Cbs3_QuePush( pQue, 0 );
    if ( iFan0 ) // circuit conflict
    {
        assert( Cbs3_VarIsAssigned(p, iFan0) );
        assert( iFan1 == 0 || Cbs3_VarIsAssigned(p, iFan1) );
        Cbs3_QuePush( pQue, iVar );
        Cbs3_QuePush( pQue, iFan0 );
        if ( iFan1 )
        Cbs3_QuePush( pQue, iFan1 );
    }
    else // clause conflict
    {
        int i, * pLits, nLits = Cbs3_ClauseSize( p, iFan1 );
        assert( iFan1 );
        Cbs3_QueGrow( pQue, nLits );
        pLits = Cbs3_ClauseLits( p, iFan1 );
        assert( iVar == Abc_Lit2Var(pLits[0]) );
        assert( Cbs3_VarValue(p, iVar) == Abc_LitIsCompl(pLits[0]) );
        for ( i = 0; i < nLits; i++ )
            Cbs3_QuePush( pQue, Abc_Lit2Var(pLits[i]) );
    }
    return Cbs3_ManDeriveReason( p, Level );
}


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

  Synopsis    [Propagate one assignment.]

  Description [Returns handle of the conflict clause, if conflict occurs.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cbs3_ManPropagateClauses( Cbs3_Man_t * p, int Level, int Lit )
{
    int i, Value, Cur, LitF = Abc_LitNot(Lit);
    int * pPrev = Vec_IntEntryP( &p->vWatches, Lit );
    //for ( pCur = p->pWatches[Lit]; pCur; pCur = *ppPrev )
    for ( Cur = *pPrev; Cur; Cur = *pPrev )
    {
        int nLits = Cbs3_ClauseSize( p, Cur );
        int * pLits = Cbs3_ClauseLits( p, Cur );
        p->nPropCalls[1]++;
//printf( "  Watching literal %c%d on level %d.\n", Abc_LitIsCompl(Lit) ? '-':'+', Abc_Lit2Var(Lit), Level );
        // make sure the false literal is in the second literal of the clause
        //if ( pCur->pLits[0] == LitF )
        if ( pLits[0] == LitF )
        {
            //pCur->pLits[0] = pCur->pLits[1];
            pLits[0] = pLits[1];
            //pCur->pLits[1] = LitF;
            pLits[1] = LitF;
            //pTemp = pCur->pNext0;
            //pCur->pNext0 = pCur->pNext1;
            //pCur->pNext1 = pTemp;
            ABC_SWAP( int, pLits[nLits], pLits[nLits+1] );
        }
        //assert( pCur->pLits[1] == LitF );
        assert( pLits[1] == LitF );

        // if the first literal is true, the clause is satisfied
        //if ( pCur->pLits[0] == p->pAssigns[lit_var(pCur->pLits[0])] )
        if ( Cbs3_VarValue(p, Abc_Lit2Var(pLits[0])) == !Abc_LitIsCompl(pLits[0]) )
        {
            //ppPrev = &pCur->pNext1;
            pPrev = Cbs3_ClauseNext1p(p, Cur);
            continue;
        }

        // look for a new literal to watch
        for ( i = 2; i < nLits; i++ )
        {
            // skip the case when the literal is false
            //if ( lit_neg(pCur->pLits[i]) == p->pAssigns[lit_var(pCur->pLits[i])] )
            if ( Cbs3_VarValue(p, Abc_Lit2Var(pLits[i])) == Abc_LitIsCompl(pLits[i]) )
                continue;
            // the literal is either true or unassigned - watch it
            //pCur->pLits[1] = pCur->pLits[i];
            //pCur->pLits[i] = LitF;
            pLits[1] = pLits[i];
            pLits[i] = LitF;
            // remove this clause from the watch list of Lit
            //*ppPrev = pCur->pNext1;
            *pPrev = *Cbs3_ClauseNext1p(p, Cur);
            // add this clause to the watch list of pCur->pLits[i] (now it is pCur->pLits[1])
            //Intb_ManWatchClause( p, pCur, pCur->pLits[1] );
            Cbs3_ManWatchClause( p, Cur, Cbs3_ClauseLit(p, Cur, 1) );
            break;
        }
        if ( i < nLits ) // found new watch
            continue;

        // clause is unit - enqueue new implication
        //if ( Inta_ManEnqueue(p, pCur->pLits[0], pCur) )
        //{
        //    ppPrev = &pCur->pNext1;
        //    continue;
        //}

        // clause is unit - enqueue new implication
        Value = Cbs3_VarValue(p, Abc_Lit2Var(pLits[0]));
        if ( Value >= 2 ) // unassigned
        {
            Cbs3_ManAssign( p, pLits[0], Level, 0, Cur );
            pPrev = Cbs3_ClauseNext1p(p, Cur);
            continue;
        }

        // conflict detected - return the conflict clause
        //return pCur;
        if ( Value == Abc_LitIsCompl(pLits[0]) )
        {
            p->nClauseConf++;
            return Cbs3_ManAnalyze( p, Level, Abc_Lit2Var(pLits[0]), 0, Cur );
        }
    }
    return 0;
}


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

  Synopsis    [Performs resolution of two clauses.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cbs3_ManResolve( Cbs3_Man_t * p, int Level, int hClause0, int hClause1 )
{
    Cbs3_Que_t * pQue = &(p->pClauses);
    int i, iObj, LevelMax = -1, LevelCur;
    assert( pQue->pData[hClause0+1] != 0 );
    assert( pQue->pData[hClause0+1] == pQue->pData[hClause1+1] );
    //Cbs3_ClauseForEachEntry1( p, hClause0, iObj, i )
    //    assert( !Cbs3_VarMark0(p, iObj) );
    //Cbs3_ClauseForEachEntry1( p, hClause1, iObj, i )
    //    assert( !Cbs3_VarMark0(p, iObj) );
    assert( Cbs3_QueIsEmpty( pQue ) );
    Cbs3_QuePush( pQue, 0 );
    Cbs3_QuePush( pQue, 0 );
//    for ( i = hClause0 + 1; (iObj = pQue->pData[i]); i++ )
    Cbs3_ClauseForEachEntry1( p, hClause0, iObj, i )
    {
        if ( Cbs3_VarMark0(p, iObj) ) // unassigned - seen again
            continue;
        //if ( Vec_IntEntry(&p->vActivity, iObj) == 0 )
        //    Vec_IntPush( &p->vActStore, iObj );
        //Vec_IntAddToEntry( &p->vActivity, iObj, 1 );
        // assigned - seen first time
        Cbs3_VarSetMark0(p, iObj, 1);
        Cbs3_ActBumpVar(p, iObj);
        Cbs3_QuePush( pQue, iObj );
        LevelCur = Cbs3_VarDecLevel( p, iObj );
        if ( LevelMax < LevelCur )
            LevelMax = LevelCur;
    }
//    for ( i = hClause1 + 1; (iObj = pQue->pData[i]); i++ )
    Cbs3_ClauseForEachEntry1( p, hClause1, iObj, i )
    {
        if ( Cbs3_VarMark0(p, iObj) ) // unassigned - seen again
            continue;
        //if ( Vec_IntEntry(&p->vActivity, iObj) == 0 )
        //    Vec_IntPush( &p->vActStore, iObj );
        //Vec_IntAddToEntry( &p->vActivity, iObj, 1 );
        // assigned - seen first time
        Cbs3_VarSetMark0(p, iObj, 1);
        Cbs3_ActBumpVar(p, iObj);
        Cbs3_QuePush( pQue, iObj );
        LevelCur = Cbs3_VarDecLevel( p, iObj );
        if ( LevelMax < LevelCur )
            LevelMax = LevelCur;
    }
    for ( i = pQue->iHead + 2; i < pQue->iTail; i++ )
        Cbs3_VarSetMark0(p, pQue->pData[i], 0);
    return Cbs3_ManDeriveReason( p, LevelMax );
}

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

  Synopsis    [Propagates a variable.]

  Description [Returns clause handle if conflict; 0 if no conflict.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cbs3_ManUpdateJFrontier( Cbs3_Man_t * p )
{
    //abctime clk = Abc_Clock();
    int iVar, iLit, i, k = p->pJust.iTail;
    Cbs3_QueGrow( &p->pJust, Cbs3_QueSize(&p->pJust) + Cbs3_QueSize(&p->pProp) );
    Cbs3_QueForEachEntry( p->pJust, iVar, i )
        if ( Cbs3_VarIsJust(p, iVar) )
            p->pJust.pData[k++] = iVar;
    Cbs3_QueForEachEntry( p->pProp, iLit, i )
        if ( Cbs3_VarIsJust(p, Abc_Lit2Var(iLit)) )
            p->pJust.pData[k++] = Abc_Lit2Var(iLit);
    p->pJust.iHead = p->pJust.iTail;
    p->pJust.iTail = k;
    //p->timeJFront += Abc_Clock() - clk;
}
int Cbs3_ManPropagateNew( Cbs3_Man_t * p, int Level )
{
    int i, k, iLit, hClause, nLits, * pLits;
    p->nPropCalls[0]++;
    Cbs3_QueForEachEntry( p->pProp, iLit, i )
    {
        if ( (hClause = Cbs3_ManPropagateClauses(p, Level, iLit)) )
            return hClause;
        p->nPropCalls[2]++;
        nLits = Vec_IntSize(Vec_WecEntry(&p->vImps, iLit));
        pLits = Vec_IntArray(Vec_WecEntry(&p->vImps, iLit)); 
        for ( k = 0; k < nLits; k += 2 )
        {
            int Value0 = Cbs3_VarValue(p, Abc_Lit2Var(pLits[k]));
            int Value1 = pLits[k+1] ? Cbs3_VarValue(p, Abc_Lit2Var(pLits[k+1])) : -1;
            if ( Value1 == -1 || Value1 == Abc_LitIsCompl(pLits[k+1]) ) // pLits[k+1] is false
            {
                if ( Value0 >= 2 ) // pLits[k] is unassigned
                    Cbs3_ManAssign( p, pLits[k], Level, Abc_Lit2Var(iLit), Abc_Lit2Var(pLits[k+1]) );
                else if ( Value0 == Abc_LitIsCompl(pLits[k]) ) // pLits[k] is false
                    return Cbs3_ManAnalyze( p, Level, Abc_Lit2Var(iLit), Abc_Lit2Var(pLits[k]), Abc_Lit2Var(pLits[k+1]) );
            }
            if ( Value1 != -1 && Value0 == Abc_LitIsCompl(pLits[k]) ) // pLits[k] is false
            {
                if ( Value1 >= 2 ) // pLits[k+1] is unassigned
                    Cbs3_ManAssign( p, pLits[k+1], Level, Abc_Lit2Var(iLit), Abc_Lit2Var(pLits[k]) );
                else if ( Value1 == Abc_LitIsCompl(pLits[k+1]) ) // pLits[k+1] is false
                    return Cbs3_ManAnalyze( p, Level, Abc_Lit2Var(iLit), Abc_Lit2Var(pLits[k]), Abc_Lit2Var(pLits[k+1]) );
            }
        }
    }
    Cbs3_ManUpdateJFrontier( p );
    // finalize propagation queue
    p->pProp.iHead = p->pProp.iTail;
    return 0;
}

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

  Synopsis    [Solve the problem recursively.]

  Description [Returns learnt clause if unsat, NULL if sat or undecided.]
               
  SideEffects []

  SeeAlso     []
 
***********************************************************************/
int Cbs3_ManSolve2_rec( Cbs3_Man_t * p, int Level )
{ 
    Cbs3_Que_t * pQue = &(p->pClauses);
    int iPropHead, iJustHead, iJustTail;
    int hClause, hLearn0, hLearn1, iVar, iDecLit;
    int nRef0, nRef1;
    // propagate assignments
    assert( !Cbs3_QueIsEmpty(&p->pProp) );
    //if ( (hClause = Cbs3_ManPropagate( p, Level )) )
    if ( (hClause = Cbs3_ManPropagateNew( p, Level )) )
        return hClause;
    // check for satisfying assignment
    assert( Cbs3_QueIsEmpty(&p->pProp) );
    if ( Cbs3_QueIsEmpty(&p->pJust) )
        return 0;
    // quit using resource limits
    p->Pars.nJustThis = Abc_MaxInt( p->Pars.nJustThis, p->pJust.iTail - p->pJust.iHead );
    if ( Cbs3_ManCheckLimits( p ) )
        return 0;
    // remember the state before branching
    iPropHead = p->pProp.iHead;
    iJustHead = p->pJust.iHead;
    iJustTail = p->pJust.iTail;
    // find the decision variable
    p->nDecs++;
    iVar = Cbs3_ManDecide( p );
    assert( !Cbs3_VarIsPi(p, iVar) );
    assert( Cbs3_VarIsJust(p, iVar) );
    // chose decision variable using fanout count
    nRef0 = Vec_IntEntry(&p->vRef, Abc_Lit2Var(Cbs3_VarLit0(p, iVar)));
    nRef1 = Vec_IntEntry(&p->vRef, Abc_Lit2Var(Cbs3_VarLit1(p, iVar)));
//    if ( nRef0 >= nRef1 || (nRef0 == nRef1) && (Abc_Random(0) & 1) )
    if ( nRef0 >= nRef1 )
        iDecLit = Abc_LitNot(Cbs3_VarLit0(p, iVar));
    else
        iDecLit = Abc_LitNot(Cbs3_VarLit1(p, iVar));
    // decide on first fanin
    Cbs3_ManAssign( p, iDecLit, Level+1, 0, 0 );
    if ( !(hLearn0 = Cbs3_ManSolve2_rec( p, Level+1 )) )
        return 0;
    if ( pQue->pData[hLearn0+1] != Abc_Lit2Var(iDecLit) )
        return hLearn0;
    Cbs3_ManCancelUntil( p, iPropHead );
    Cbs3_QueRestore( &p->pJust, iJustHead, iJustTail );
    // decide on second fanin
    Cbs3_ManAssign( p, Abc_LitNot(iDecLit), Level+1, 0, 0 );
    if ( !(hLearn1 = Cbs3_ManSolve2_rec( p, Level+1 )) )
        return 0;
    if ( pQue->pData[hLearn1+1] != Abc_Lit2Var(iDecLit) )
        return hLearn1;
    hClause = Cbs3_ManResolve( p, Level, hLearn0, hLearn1 );
    p->Pars.nBTThis++;
    return hClause;
}

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

  Synopsis    [Looking for a satisfying assignment of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cbs3_ManSolveInt( Cbs3_Man_t * p, int iLit )
{
    int RetValue = 0;
    assert( !p->pProp.iHead && !p->pProp.iTail );
    assert( !p->pJust.iHead && !p->pJust.iTail );
    p->Pars.nBTThis = p->Pars.nJustThis = 0;
    Cbs3_ManAssign( p, iLit, 0, 0, 0 );
    if ( !Cbs3_ManSolve2_rec(p, 0) && !Cbs3_ManCheckLimits(p) )
        Cbs3_ManSaveModel( p, p->vModel );
    else
        RetValue = 1;
    Cbs3_ManCancelUntil( p, 0 );
    p->pJust.iHead = p->pJust.iTail = 0;
    p->Pars.nBTTotal += p->Pars.nBTThis;
    p->Pars.nJustTotal = Abc_MaxInt( p->Pars.nJustTotal, p->Pars.nJustThis );
    if ( Cbs3_ManCheckLimits( p ) )
        RetValue = -1;
    return RetValue;
}
int Cbs3_ManSolve( Cbs3_Man_t * p, int iLit, int nRestarts )
{
    int i, RetValue = -1;
    assert( p->pClauses.iHead == 1 && p->pClauses.iTail == 1 );
    for ( i = 0; i < nRestarts; i++ )
        if ( (RetValue = Cbs3_ManSolveInt(p, iLit)) != -1 )
            break;
    Cbs3_ManCleanWatch( p );
    p->pClauses.iHead = p->pClauses.iTail = 1;
    return RetValue;
}

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

  Synopsis    [Prints statistics of the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cbs3_ManSatPrintStats( Cbs3_Man_t * p )
{
    printf( "CO = %8d  ", Gia_ManCoNum(p->pAig) );
    printf( "AND = %8d  ", Gia_ManAndNum(p->pAig) );
    printf( "Conf = %6d  ", p->Pars.nBTLimit );
    printf( "Restart = %2d  ", p->Pars.nRestLimit );
    printf( "JustMax = %5d  ", p->Pars.nJustLimit );
    printf( "\n" );
    printf( "Unsat calls %6d  (%6.2f %%)   Ave conf = %8.1f   ", 
        p->nSatUnsat, p->nSatTotal? 100.0*p->nSatUnsat/p->nSatTotal :0.0, p->nSatUnsat? 1.0*p->nConfUnsat/p->nSatUnsat :0.0 );
    ABC_PRTP( "Time", p->timeSatUnsat, p->timeTotal );
    printf( "Sat   calls %6d  (%6.2f %%)   Ave conf = %8.1f   ", 
        p->nSatSat,   p->nSatTotal? 100.0*p->nSatSat/p->nSatTotal :0.0, p->nSatSat? 1.0*p->nConfSat/p->nSatSat : 0.0 );
    ABC_PRTP( "Time", p->timeSatSat,   p->timeTotal );
    printf( "Undef calls %6d  (%6.2f %%)   Ave conf = %8.1f   ", 
        p->nSatUndec, p->nSatTotal? 100.0*p->nSatUndec/p->nSatTotal :0.0, p->nSatUndec? 1.0*p->nConfUndec/p->nSatUndec : 0.0 );
    ABC_PRTP( "Time", p->timeSatUndec, p->timeTotal );
    ABC_PRT( "Total time", p->timeTotal );
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Cbs3_ManAddVar( Cbs3_Man_t * p, int iGiaObj )
{
    assert( Vec_IntSize(&p->vMap) == p->nVars );
    Vec_IntPush( &p->vMap, iGiaObj );
    Vec_IntPush( &p->vRef, Gia_ObjRefNumId(p->pAig, iGiaObj) );
    Vec_IntPushTwo( &p->vFans, 0, 0 );
    Vec_WecPushLevel(&p->vImps);
    Vec_WecPushLevel(&p->vImps);
    return Abc_Var2Lit( p->nVars++, 0 );
}
static inline void Cbs3_ManAddConstr( Cbs3_Man_t * p, int x, int x0, int x1 )
{
    Vec_WecPushTwo( &p->vImps,   x ,   x0,   0  ); // ~x +  x0
    Vec_WecPushTwo( &p->vImps,   x ,   x1,   0  ); // ~x +  x1
    Vec_WecPushTwo( &p->vImps, 1^x0, 1^x ,   0  ); // ~x +  x0
    Vec_WecPushTwo( &p->vImps, 1^x1, 1^x ,   0  ); // ~x +  x1
    Vec_WecPushTwo( &p->vImps, 1^x , 1^x0, 1^x1 ); //  x + ~x0 + ~x1
    Vec_WecPushTwo( &p->vImps,   x0,   x , 1^x1 ); //  x + ~x0 + ~x1
    Vec_WecPushTwo( &p->vImps,   x1,   x , 1^x0 ); //  x + ~x0 + ~x1
}
static inline void Cbs3_ManAddAnd( Cbs3_Man_t * p, int x, int x0, int x1 )
{
    assert( x > 0 && x0 > 0 && x1 > 0 );
    Vec_IntWriteEntry( &p->vFans, x,   x0 );
    Vec_IntWriteEntry( &p->vFans, x+1, x1 );
    Cbs3_ManAddConstr( p, x, x0, x1 );
}
static inline int Cbs3_ManToSolver1_rec( Cbs3_Man_t * pSol, Gia_Man_t * p, int iObj, int Depth )
{
    Gia_Obj_t * pObj = Gia_ManObj(p, iObj); int Lit0, Lit1;
    if ( Gia_ObjUpdateTravIdCurrentId(p, iObj) )
        return pObj->Value;
    pObj->Value = Cbs3_ManAddVar( pSol, iObj );
    if ( Gia_ObjIsCi(pObj) || Depth == 0 )
        return pObj->Value;
    assert( Gia_ObjIsAnd(pObj) );
    Lit0 = Cbs3_ManToSolver1_rec( pSol, p, Gia_ObjFaninId0(pObj, iObj), Depth - Gia_ObjFaninC0(pObj) );
    Lit1 = Cbs3_ManToSolver1_rec( pSol, p, Gia_ObjFaninId1(pObj, iObj), Depth - Gia_ObjFaninC1(pObj) );
    Cbs3_ManAddAnd( pSol, pObj->Value, Lit0 ^ Gia_ObjFaninC0(pObj), Lit1 ^ Gia_ObjFaninC1(pObj) );
    return pObj->Value;
}
static inline int Cbs3_ManToSolver1( Cbs3_Man_t * pSol, Gia_Man_t * p, Gia_Obj_t * pRoot, int nRestarts, int Depth )
{
    //abctime clk = Abc_Clock();
    assert( Gia_ObjIsCo(pRoot) );
    Cbs3_ManReset( pSol );
    Gia_ManIncrementTravId( p );
    Cbs3_ManToSolver1_rec( pSol, p, Gia_ObjFaninId0p(p, pRoot), Depth );
    Cbs3_ManGrow( pSol );
    Cbs3_ActReset( pSol );
    //pSol->timeSatLoad += Abc_Clock() - clk;
    return Cbs3_ManSolve( pSol, Gia_ObjFanin0Copy(pRoot), nRestarts );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Cbs3_ManPrepare( Cbs3_Man_t * p )
{
    int x, x0, x1;
    Vec_WecInit( &p->vImps, Abc_Var2Lit(p->nVars, 0) );
    Vec_IntForEachEntryDoubleStart( &p->vFans, x0, x1, x, 2 )
        if ( x0 ) Cbs3_ManAddConstr( p, x, x0, x1 );
}
static inline int Cbs3_ManAddNode( Cbs3_Man_t * p, int iGiaObj, int iLit0, int iLit1 )
{
    assert( Vec_IntSize(&p->vMap) == p->nVars );
    Vec_IntPush( &p->vMap, iGiaObj );
    Vec_IntPush( &p->vRef, Gia_ObjRefNumId(p->pAig, iGiaObj) );
    Vec_IntPushTwo( &p->vFans, iLit0, iLit1 );
    return Abc_Var2Lit( p->nVars++, 0 );
}
static inline int Cbs3_ManToSolver2_rec( Cbs3_Man_t * pSol, Gia_Man_t * p, int iObj, int Depth )
{
    Gia_Obj_t * pObj = Gia_ManObj(p, iObj); int Lit0, Lit1;
    if ( Gia_ObjUpdateTravIdCurrentId(p, iObj) )
        return pObj->Value;
    if ( Gia_ObjIsCi(pObj) || Depth == 0 )
        return pObj->Value = Cbs3_ManAddNode(pSol, iObj, 0, 0);
    assert( Gia_ObjIsAnd(pObj) );
    Lit0 = Cbs3_ManToSolver2_rec( pSol, p, Gia_ObjFaninId0(pObj, iObj), Depth - Gia_ObjFaninC0(pObj) );
    Lit1 = Cbs3_ManToSolver2_rec( pSol, p, Gia_ObjFaninId1(pObj, iObj), Depth - Gia_ObjFaninC1(pObj) );
    return pObj->Value = Cbs3_ManAddNode(pSol, iObj, Lit0 ^ Gia_ObjFaninC0(pObj), Lit1 ^ Gia_ObjFaninC1(pObj));
}
static inline int Cbs3_ManToSolver2( Cbs3_Man_t * pSol, Gia_Man_t * p, Gia_Obj_t * pRoot, int nRestarts, int Depth )
{
    //abctime clk = Abc_Clock();
    assert( Gia_ObjIsCo(pRoot) );
    Cbs3_ManReset( pSol );
    Gia_ManIncrementTravId( p );
    Cbs3_ManToSolver2_rec( pSol, p, Gia_ObjFaninId0p(p, pRoot), Depth );
    Cbs3_ManGrow( pSol );
    Cbs3_ManPrepare( pSol );
    Cbs3_ActReset( pSol );
    //pSol->timeSatLoad += Abc_Clock() - clk;
    return Cbs3_ManSolve( pSol, Gia_ObjFanin0Copy(pRoot), nRestarts );
}


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

  Synopsis    [Procedure to test the new SAT solver.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Cbs3_ManSolveMiterNc( Gia_Man_t * pAig, int nConfs, int nRestarts, Vec_Str_t ** pvStatus, int fVerbose )
{
    extern void Cec_ManSatAddToStore( Vec_Int_t * vCexStore, Vec_Int_t * vCex, int Out );
    Cbs3_Man_t * p; 
    Vec_Int_t * vCex, * vVisit, * vCexStore;
    Vec_Str_t * vStatus;
    Gia_Obj_t * pRoot; 
    int i, status; // 1 = unsat, 0 = sat, -1 = undec
    abctime clk, clkTotal = Abc_Clock();
    //assert( Gia_ManRegNum(pAig) == 0 );
    Gia_ManCreateRefs( pAig );
    //Gia_ManLevelNum( pAig );
    // create logic network
    p = Cbs3_ManAlloc( pAig );
    p->Pars.nBTLimit = nConfs;
    p->Pars.nRestLimit = nRestarts;
    // create resulting data-structures
    vStatus   = Vec_StrAlloc( Gia_ManPoNum(pAig) );
    vCexStore = Vec_IntAlloc( 10000 );
    vVisit    = Vec_IntAlloc( 100 );
    vCex      = Cbs3_ReadModel( p );
    // solve for each output
    Gia_ManForEachCo( pAig, pRoot, i )
    {
        if ( Gia_ObjIsConst0(Gia_ObjFanin0(pRoot)) )
        {
            Vec_IntClear( vCex );
            Vec_StrPush( vStatus, (char)(!Gia_ObjFaninC0(pRoot)) );
            if ( Gia_ObjFaninC0(pRoot) ) // const1
                Cec_ManSatAddToStore( vCexStore, vCex, i ); // trivial counter-example
            continue;
        }
        clk = Abc_Clock();
        status = Cbs3_ManToSolver2( p, pAig, pRoot, p->Pars.nRestLimit, 10000 );
        Vec_StrPush( vStatus, (char)status );
        if ( status == -1 )
        {
            p->nSatUndec++;
            p->nConfUndec += p->Pars.nBTThis;
            Cec_ManSatAddToStore( vCexStore, NULL, i ); // timeout
            p->timeSatUndec += Abc_Clock() - clk;
            continue;
        }
        if ( status == 1 )
        {
            p->nSatUnsat++;
            p->nConfUnsat += p->Pars.nBTThis;
            p->timeSatUnsat += Abc_Clock() - clk;
            continue;
        }
        p->nSatSat++;
        p->nConfSat += p->Pars.nBTThis;
        //Gia_SatVerifyPattern( pAig, pRoot, vCex, vVisit );
        Cec_ManSatAddToStore( vCexStore, vCex, i );
        p->timeSatSat += Abc_Clock() - clk;
    }
    Vec_IntFree( vVisit );
    p->nSatTotal = Gia_ManPoNum(pAig);
    p->timeTotal = Abc_Clock() - clkTotal;
    if ( fVerbose )
        Cbs3_ManSatPrintStats( p );
    if ( fVerbose )
    {
        printf( "Prop1 = %d.  Prop2 = %d.  Prop3 = %d.  ClaConf = %d.   FailJ = %d.  FailC = %d.   ", p->nPropCalls[0], p->nPropCalls[1], p->nPropCalls[2], p->nClauseConf, p->nFails[0], p->nFails[1] );
        printf( "Mem usage %.2f MB.\n", 1.0*Cbs3_ManMemory(p)/(1<<20) );
        //Abc_PrintTime( 1, "JFront", p->timeJFront );
        //Abc_PrintTime( 1, "Loading", p->timeSatLoad );
        //printf( "Decisions = %d.\n", p->nDecs );
    }
    Cbs3_ManStop( p );
    *pvStatus = vStatus;
    return vCexStore;
}


////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END