00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #define __STDC_LIMIT_MACROS
00041 #include <stdint.h>
00042 #ifdef USE_PCH
00043 #include "lno_pch.h"
00044 #endif // USE_PCH
00045 #pragma hdrstop
00046
00047 #include "lnopt_main.h"
00048 #include "dnf.h"
00049
00050
00051 LINEAR_CLAUSE::LINEAR_CLAUSE(SYSTEM_OF_EQUATIONS *soe,
00052 MEM_POOL *pool)
00053 {
00054 _t = CLAUSE_ATOM;
00055 _m = pool;
00056 _u._atom = CXX_NEW (SYSTEM_OF_EQUATIONS (soe, pool), pool);
00057 }
00058
00059 LINEAR_CLAUSE::LINEAR_CLAUSE(SYSTEM_OF_EQUATIONS **soe_list,
00060 INT32 count,
00061 MEM_POOL *pool)
00062 {
00063 INT32 i;
00064
00065 _t = CLAUSE_DISJ;
00066 _m = pool;
00067 _u._disj._conj = CXX_NEW_ARRAY (SYSTEM_OF_EQUATIONS *,
00068 count, pool);
00069 _u._disj._nconj = count;
00070 for (i = 0; i < count; i++) {
00071 _u._disj._conj[i] =
00072 CXX_NEW (SYSTEM_OF_EQUATIONS (soe_list[i], pool),
00073 pool);
00074 }
00075 }
00076
00077 LINEAR_CLAUSE*
00078 _xcombine_atom_with_disj(LINEAR_CLAUSE *l1,
00079 LINEAR_CLAUSE *l2)
00080 {
00081 assert (CLAUSE_ATOM == l1->CLAUSE_type());
00082 assert (CLAUSE_DISJ == l2->CLAUSE_type());
00083 LINEAR_CLAUSE *result;
00084 SYSTEM_OF_EQUATIONS **soe_list;
00085 INT32 i;
00086
00087 soe_list = CXX_NEW_ARRAY (SYSTEM_OF_EQUATIONS*,
00088 l2->CLAUSE_nconj(),
00089 l2->CLAUSE_mem_pool());
00090
00091 for (i = 0; i < l2->CLAUSE_nconj(); i++) {
00092 soe_list[i] = l2->CLAUSE_conj_ith (i);
00093 }
00094 result = CXX_NEW (LINEAR_CLAUSE (soe_list,
00095 l2->CLAUSE_nconj (),
00096 l2->CLAUSE_mem_pool ()),
00097 l2->CLAUSE_mem_pool());
00098 assert (result->CLAUSE_nconj() == l2->CLAUSE_nconj());
00099 for (i = 0; i < result->CLAUSE_nconj (); i++) {
00100 result->CLAUSE_conj_ith (i)->Add_Soe (l1->CLAUSE_atom());
00101 }
00102 return result;
00103 }
00104
00105 LINEAR_CLAUSE*
00106 _xcombine_disj_with_disj(LINEAR_CLAUSE *l1,
00107 LINEAR_CLAUSE *l2)
00108 {
00109 assert (CLAUSE_DISJ == l1->CLAUSE_type());
00110 assert (CLAUSE_DISJ == l2->CLAUSE_type());
00111
00112 LINEAR_CLAUSE *result;
00113 SYSTEM_OF_EQUATIONS **soe_list;
00114 SYSTEM_OF_EQUATIONS *tmp;
00115 INT32 i, j;
00116 INT32 count;
00117
00118 count = 0;
00119
00120
00121
00122
00123
00124 soe_list = CXX_NEW_ARRAY (SYSTEM_OF_EQUATIONS*,
00125 l1->CLAUSE_nconj() * l2->CLAUSE_nconj(),
00126 l2->CLAUSE_mem_pool());
00127 for (i = 0; i < l1->CLAUSE_nconj (); i++) {
00128 for (j = 0; j < l2->CLAUSE_nconj (); j++) {
00129 soe_list[count] =
00130 CXX_NEW (SYSTEM_OF_EQUATIONS (l1->CLAUSE_conj_ith (i),
00131 l1->CLAUSE_mem_pool()),
00132 l1->CLAUSE_mem_pool());
00133 count++;
00134 }
00135 }
00136 assert (count == l1->CLAUSE_nconj () * l2->CLAUSE_nconj ());
00137 result = CXX_NEW (LINEAR_CLAUSE (soe_list, count,
00138 l2->CLAUSE_mem_pool()),
00139 l2->CLAUSE_mem_pool());
00140 count = 0;
00141 for (i = 0; i < l1->CLAUSE_nconj (); i++) {
00142 for (j = 0; j < l2->CLAUSE_nconj (); j++) {
00143 result->CLAUSE_conj_ith(count)->Add_Soe(l2->CLAUSE_conj_ith(j));
00144 count++;
00145 }
00146 }
00147 assert (count == l1->CLAUSE_nconj () * l2->CLAUSE_nconj ());
00148 return result;
00149 }
00150
00151 LINEAR_CLAUSE*
00152 combine_clauses(LINEAR_CLAUSE *l1,
00153 LINEAR_CLAUSE *l2)
00154 {
00155 LINEAR_CLAUSE *result;
00156
00157 if (CLAUSE_ATOM == (l1->CLAUSE_type()) &&
00158 CLAUSE_ATOM == (l2->CLAUSE_type())) {
00159 result = CXX_NEW (LINEAR_CLAUSE (l1->CLAUSE_atom(),
00160 l1->CLAUSE_mem_pool()),
00161 l1->CLAUSE_mem_pool());
00162 result->CLAUSE_atom()->Add_Soe (l2->CLAUSE_atom());
00163 return result;
00164 }
00165 else if (CLAUSE_ATOM == (l1->CLAUSE_type()) &&
00166 CLAUSE_DISJ == (l2->CLAUSE_type())) {
00167 return _xcombine_atom_with_disj (l1, l2);
00168 }
00169 else if (CLAUSE_DISJ == (l1->CLAUSE_type()) &&
00170 CLAUSE_ATOM == (l2->CLAUSE_type())) {
00171 return _xcombine_atom_with_disj (l2, l1);
00172 }
00173 else
00174 return _xcombine_disj_with_disj (l1, l2);
00175 }
00176
00177 BOOL
00178 LINEAR_CLAUSE::Is_Consistent()
00179 {
00180 INT32 i;
00181
00182 switch (_t) {
00183 case CLAUSE_ATOM:
00184 return _u._atom->Is_Consistent();
00185 case CLAUSE_DISJ:
00186 for (i = 0; i < _u._disj._nconj; i++) {
00187 if (_u._disj._conj[i]->Is_Consistent())
00188 return TRUE;
00189 }
00190 return FALSE;
00191 default:
00192 assert(0);
00193 return FALSE;
00194 }
00195 }
00196
00197 void
00198 LINEAR_CLAUSE::Print(FILE *fp) const
00199 {
00200 INT32 i;
00201
00202 switch (_t) {
00203 case CLAUSE_ATOM:
00204 _u._atom->Print(fp);
00205 break;
00206 case CLAUSE_DISJ:
00207 fprintf(fp, "[[[");
00208 for (i = 0; i < _u._disj._nconj; i++) {
00209 fprintf (fp, "\t{{");
00210 _u._disj._conj[i]->Print (fp);
00211 fprintf (fp, "\t}}\n");
00212 }
00213 fprintf(fp, "]]]\n");
00214 break;
00215 }
00216 }