/dgc-0.98/cube/dcube.c

# · C · 5900 lines · 4299 code · 777 blank · 824 comment · 1768 complexity · 3ee80781952e9c843ac71d24272cd8a3 MD5 · raw file

  1. /*
  2. dynamic cube
  3. Copyright (C) 2001 Oliver Kraus (olikraus@yahoo.com)
  4. This file is part of DGC.
  5. DGC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. DGC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with DGC; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. /*! \defgroup dclist Sum of Product Management */
  18. #include <stdarg.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <assert.h>
  23. #include "b_io.h"
  24. #include "b_ff.h"
  25. #include "dcube.h"
  26. #include "mcov.h"
  27. #include "mwc.h"
  28. #include "matrix.h"
  29. /*-- dcInSetAll -------------------------------------------------------------*/
  30. void dcInSetAll(pinfo *pi, dcube *c, c_int v)
  31. {
  32. int i;
  33. for( i = 0; i < pi->in_words; i++ )
  34. c->in[i] = v;
  35. }
  36. /*-- dcCopy -----------------------------------------------------------------*/
  37. void dcCopy(pinfo *pi, dcube *dest, dcube *src)
  38. {
  39. int i;
  40. for( i = 0; i < pi->in_out_words_min; i++ )
  41. {
  42. dest->in[i] = src->in[i];
  43. dest->out[i] = src->out[i];
  44. }
  45. for( i = pi->in_out_words_min; i < pi->in_words; i++ )
  46. dest->in[i] = src->in[i];
  47. for( i = pi->in_out_words_min; i < pi->out_words; i++ )
  48. dest->out[i] = src->out[i];
  49. dest->n = src->n;
  50. }
  51. /*-- dcCopyOut --------------------------------------------------------------*/
  52. void dcCopyOut(pinfo *pi, dcube *dest, dcube *src)
  53. {
  54. int i;
  55. for( i = 0; i < pi->out_words; i++ )
  56. dest->out[i] = src->out[i];
  57. }
  58. /*-- dcCopyIn ---------------------------------------------------------------*/
  59. void dcCopyIn(pinfo *pi, dcube *dest, dcube *src)
  60. {
  61. int i;
  62. for( i = 0; i < pi->in_words; i++ )
  63. dest->in[i] = src->in[i];
  64. }
  65. /*-- dcCopyInToIn -----------------------------------------------------------*/
  66. void dcCopyInToIn(pinfo *pi_dest, dcube *dest, int dest_offset, pinfo *pi_src, dcube *src)
  67. {
  68. int i;
  69. int s;
  70. for( i = 0; i < pi_src->in_cnt; i++ )
  71. {
  72. s = dcGetIn(src, i);
  73. dcSetIn(dest, i+dest_offset, s);
  74. }
  75. }
  76. /*-- dcCopyInToInRange ------------------------------------------------------*/
  77. void dcCopyInToInRange(pinfo *pi_dest, dcube *dest, int dest_offset,
  78. pinfo *pi_src, dcube *src, int src_offset, int src_cnt)
  79. {
  80. int i;
  81. int s;
  82. assert(src_offset+src_cnt <= pi_src->in_cnt);
  83. for( i = src_offset; i < src_offset+src_cnt; i++ )
  84. {
  85. s = dcGetIn(src, i);
  86. dcSetIn(dest, i+dest_offset-src_offset, s);
  87. }
  88. }
  89. /*-- dcCopyOutToIn ----------------------------------------------------------*/
  90. void dcCopyOutToIn(pinfo *pi_dest, dcube *dest, int dest_offset, pinfo *pi_src, dcube *src)
  91. {
  92. int i;
  93. int s;
  94. for( i = 0; i < pi_src->out_cnt; i++ )
  95. {
  96. s = dcGetOut(src, i) + 1;
  97. dcSetIn(dest, i+dest_offset, s);
  98. }
  99. }
  100. /*-- dcCopyOutToInRange -----------------------------------------------------*/
  101. void dcCopyOutToInRange(pinfo *pi_dest, dcube *dest, int dest_offset,
  102. pinfo *pi_src, dcube *src, int src_offset, int src_cnt)
  103. {
  104. int i;
  105. int s;
  106. assert(src_offset+src_cnt <= pi_src->in_cnt);
  107. for( i = src_offset; i < src_offset+src_cnt; i++ )
  108. {
  109. s = dcGetOut(src, i) + 1;
  110. dcSetIn(dest, i+dest_offset-src_offset, s);
  111. }
  112. }
  113. /*-- dcCopyInToOut ----------------------------------------------------------*/
  114. void dcCopyInToOut(pinfo *pi_dest, dcube *dest, int dest_offset, pinfo *pi_src, dcube *src)
  115. {
  116. int i;
  117. int s;
  118. for( i = 0; i < pi_src->in_cnt; i++ )
  119. {
  120. s = dcGetIn(src, i) - 1;
  121. dcSetOut(dest, i+dest_offset, s);
  122. }
  123. }
  124. /*-- dcCopyInToOutRange -----------------------------------------------------*/
  125. void dcCopyInToOutRange(pinfo *pi_dest, dcube *dest, int dest_offset,
  126. pinfo *pi_src, dcube *src, int src_offset, int src_cnt)
  127. {
  128. int i;
  129. int s;
  130. assert(src_offset+src_cnt <= pi_src->in_cnt);
  131. for( i = src_offset; i < src_offset+src_cnt; i++ )
  132. {
  133. s = dcGetIn(src, i) - 1;
  134. dcSetOut(dest, i+dest_offset-src_offset, s);
  135. }
  136. }
  137. /*-- dcCopyOutToOut ---------------------------------------------------------*/
  138. void dcCopyOutToOut(pinfo *pi_dest, dcube *dest, int dest_offset,
  139. pinfo *pi_src, dcube *src)
  140. {
  141. int i, cnt;
  142. int s;
  143. cnt = pi_src->out_cnt;
  144. if ( cnt > pi_dest->out_cnt-dest_offset )
  145. cnt = pi_dest->out_cnt-dest_offset;
  146. for( i = 0; i < cnt; i++ )
  147. {
  148. s = dcGetOut(src, i);
  149. dcSetOut(dest, i+dest_offset, s);
  150. }
  151. }
  152. /*-- dcCopyOutToOutRange ----------------------------------------------------*/
  153. void dcCopyOutToOutRange(pinfo *pi_dest, dcube *dest, int dest_offset,
  154. pinfo *pi_src, dcube *src, int src_offset, int src_cnt)
  155. {
  156. int i;
  157. int s;
  158. assert(src_offset+src_cnt <= pi_src->out_cnt);
  159. assert(dest_offset+src_cnt < pi_dest->out_cnt);
  160. for( i = src_offset; i < src_offset+src_cnt; i++ )
  161. {
  162. s = dcGetOut(src, i);
  163. dcSetOut(dest, i+dest_offset-src_offset, s);
  164. }
  165. }
  166. /*-- dcOutSetAll ------------------------------------------------------------*/
  167. void dcOutSetAll(pinfo *pi, dcube *c, c_int v)
  168. {
  169. int i;
  170. for( i = 0; i < pi->out_words; i++ )
  171. c->out[i] = v;
  172. }
  173. /*-- dcAllClear -------------------------------------------------------------*/
  174. void dcAllClear(pinfo *pi, dcube *c)
  175. {
  176. dcInSetAll(pi, c, 0);
  177. dcOutSetAll(pi, c, 0);
  178. }
  179. /*-- dcSetTautology ---------------------------------------------------------*/
  180. void dcSetTautology(pinfo *pi, dcube *c)
  181. {
  182. dcInSetAll(pi, c, CUBE_IN_MASK_DC);
  183. dcOutSetAll(pi, c, CUBE_OUT_MASK);
  184. if ( pi->out_words > 0 )
  185. c->out[pi->out_words-1] = pi->out_last_mask;
  186. }
  187. /*-- dcSetOutTautology ------------------------------------------------------*/
  188. void dcSetOutTautology(pinfo *pi, dcube *c)
  189. {
  190. dcOutSetAll(pi, c, CUBE_OUT_MASK);
  191. if ( pi->out_words > 0 )
  192. c->out[pi->out_words-1] = pi->out_last_mask;
  193. }
  194. /*-- dcInitMem --------------------------------------------------------------*/
  195. int dcInitMem(pinfo *pi, dcube *c)
  196. {
  197. c->in = NULL;
  198. c->out = NULL;
  199. return dcAdjustByPinfo(pi, c);
  200. }
  201. /*-- dcInit -----------------------------------------------------------------*/
  202. int dcInit(pinfo *pi, dcube *c)
  203. {
  204. c->in = NULL;
  205. c->out = NULL;
  206. if ( dcAdjustByPinfo(pi, c) == 0 )
  207. return 0;
  208. dcInSetAll(pi, c, CUBE_IN_MASK_DC);
  209. dcOutSetAll(pi, c, 0);
  210. c->n = 0;
  211. return 1;
  212. }
  213. /*-- dcInitVA --------------------------------------------------------------*/
  214. int dcInitVA(pinfo *pi, int n, ...)
  215. {
  216. va_list va;
  217. int i;
  218. va_start(va, n);
  219. for( i = 0; i < n; i++ )
  220. if ( dcInit(pi, va_arg(va, dcube *)) == 0 )
  221. break;
  222. va_end(va);
  223. if ( i < n )
  224. {
  225. va_start(va, n);
  226. while( i-- >= 0 )
  227. dcDestroy(va_arg(va, dcube *));
  228. va_end(va);
  229. return 0;
  230. }
  231. return 1;
  232. }
  233. /*-- dcDestroy --------------------------------------------------------------*/
  234. void dcDestroy(dcube *c)
  235. {
  236. if ( c->in != NULL )
  237. free(c->in);
  238. if ( c->out != NULL )
  239. free(c->out);
  240. c->in = NULL;
  241. c->out = NULL;
  242. }
  243. /*-- dcDestroyVA ------------------------------------------------------------*/
  244. void dcDestroyVA(int n, ...)
  245. {
  246. va_list va;
  247. int i;
  248. va_start(va, n);
  249. for( i = 0; i < n; i++ )
  250. dcDestroy(va_arg(va, dcube *));
  251. va_end(va);
  252. }
  253. /*-- dcAdjust ---------------------------------------------------------------*/
  254. int dcAdjust(dcube *c, int in_words, int out_words)
  255. {
  256. void *ptr;
  257. if ( in_words == 0 )
  258. {
  259. if ( c->in != NULL )
  260. free(c->in);
  261. c->in = NULL;
  262. }
  263. else
  264. {
  265. if ( c->in == NULL )
  266. ptr = malloc(sizeof(c_int)*in_words);
  267. else
  268. ptr = realloc(c->in, sizeof(c_int)*in_words);
  269. if ( ptr == NULL )
  270. return 0;
  271. c->in = (c_int *)ptr;
  272. }
  273. if ( out_words == 0 )
  274. {
  275. if ( c->out != NULL )
  276. free(c->out);
  277. c->out = NULL;
  278. }
  279. else
  280. {
  281. if ( c->out == NULL )
  282. ptr = malloc(sizeof(c_int)*out_words);
  283. else
  284. ptr = realloc(c->out, sizeof(c_int)*out_words);
  285. if ( ptr == NULL )
  286. return 0;
  287. c->out = (c_int *)ptr;
  288. }
  289. return 1;
  290. }
  291. /*-- dcAdjustByPinfo --------------------------------------------------------*/
  292. int dcAdjustByPinfo(pinfo *pi, dcube *c)
  293. {
  294. return dcAdjust(c, pi->in_words, pi->out_words);
  295. }
  296. /*-- dcAdjustByPinfoVA ------------------------------------------------------*/
  297. int dcAdjustByPinfoVA(pinfo *pi, int n, ...)
  298. {
  299. va_list va;
  300. int i;
  301. va_start(va, n);
  302. for( i = 0; i < n; i++ )
  303. if ( dcAdjustByPinfo(pi, va_arg(va, dcube *)) == 0 )
  304. {
  305. va_end(va);
  306. return 0;
  307. }
  308. va_end(va);
  309. return 1;
  310. }
  311. /*-- dcSetIn ----------------------------------------------------------------*/
  312. void dcSetIn(dcube *c, int pos, int code)
  313. {
  314. c->in[pos/CUBE_SIGNALS_PER_IN_WORD] &= ~(3<<((pos&(CUBE_SIGNALS_PER_IN_WORD-1))*2));
  315. c->in[pos/CUBE_SIGNALS_PER_IN_WORD] |= code<<((pos&(CUBE_SIGNALS_PER_IN_WORD-1))*2);
  316. }
  317. /*-- dcGetIn ----------------------------------------------------------------*/
  318. int dcGetIn(dcube *c, int pos)
  319. {
  320. return (c->in[pos/CUBE_SIGNALS_PER_IN_WORD] >>
  321. ((pos&(CUBE_SIGNALS_PER_IN_WORD-1))*2)) & 3;
  322. }
  323. /*-- dcSetOut ---------------------------------------------------------------*/
  324. void dcSetOut(dcube *c, int pos, int code)
  325. {
  326. c->out[pos/CUBE_SIGNALS_PER_OUT_WORD] &= ~(1<<(pos&(CUBE_SIGNALS_PER_OUT_WORD-1)));
  327. c->out[pos/CUBE_SIGNALS_PER_OUT_WORD] |= (code<<(pos&(CUBE_SIGNALS_PER_OUT_WORD-1)));
  328. }
  329. /*-- dcGetOut ----------------------------------------------------------------*/
  330. int dcGetOut(dcube *c, int pos)
  331. {
  332. return (c->out[pos/CUBE_SIGNALS_PER_OUT_WORD] >>
  333. (pos&(CUBE_SIGNALS_PER_OUT_WORD-1))) & 1;
  334. }
  335. /*-- dcSetByStr -------------------------------------------------------------*/
  336. int dcSetByStr(pinfo *pi, dcube *c, char *str)
  337. {
  338. size_t i = 0;
  339. if ( dcAdjustByPinfo(pi, c) == 0 )
  340. return 0;
  341. dcInSetAll(pi, c, CUBE_IN_MASK_DC);
  342. dcOutSetAll(pi, c, 0);
  343. while( i < pi->in_cnt )
  344. switch(*str++)
  345. {
  346. case '0': dcSetIn(c, i++, 1); break;
  347. case '1': dcSetIn(c, i++, 2); break;
  348. case '-': dcSetIn(c, i++, 3); break;
  349. case '\0': return 0;
  350. }
  351. i = 0;
  352. while( i < pi->out_cnt )
  353. switch(*str++)
  354. {
  355. case '0': dcSetOut(c, i++, 0); break;
  356. case '1': dcSetOut(c, i++, 1); break;
  357. case '\0': return 0;
  358. }
  359. return 1;
  360. }
  361. /*-- dcSetInByStr -----------------------------------------------------------*/
  362. int dcSetInByStr(pinfo *pi, dcube *c, char *str)
  363. {
  364. size_t i = 0;
  365. if ( dcAdjustByPinfo(pi, c) == 0 )
  366. return 0;
  367. dcInSetAll(pi, c, CUBE_IN_MASK_DC);
  368. i = 0;
  369. while( i < pi->in_cnt )
  370. switch(*str++)
  371. {
  372. case '0': dcSetIn(c, i++, 1); break;
  373. case '1': dcSetIn(c, i++, 2); break;
  374. case '-': dcSetIn(c, i++, 3); break;
  375. case '\0': return 0;
  376. }
  377. return 1;
  378. }
  379. /*-- dcSetOutByStr ----------------------------------------------------------*/
  380. int dcSetOutByStr(pinfo *pi, dcube *c, char *str)
  381. {
  382. size_t i = 0;
  383. if ( dcAdjustByPinfo(pi, c) == 0 )
  384. return 0;
  385. dcInSetAll(pi, c, 0);
  386. i = 0;
  387. while( i < pi->out_cnt )
  388. switch(*str++)
  389. {
  390. case '0': dcSetOut(c, i++, 0); break;
  391. case '1': dcSetOut(c, i++, 1); break;
  392. case '\0': return 0;
  393. }
  394. return 1;
  395. }
  396. /*-- dcSetAllByStr ----------------------------------------------------------*/
  397. /* dcSetAllByStr ... dcSetAllByStrPtr */
  398. char *dcSetAllByStr(pinfo *pi, int in_cnt, int out_cnt, dcube *c_on, dcube *c_dc, char *str)
  399. {
  400. size_t i = 0;
  401. if ( dcAdjustByPinfo(pi, c_on) == 0 )
  402. return NULL;
  403. if ( dcAdjustByPinfo(pi, c_dc) == 0 )
  404. return NULL;
  405. dcInSetAll(pi, c_on, CUBE_IN_MASK_DC);
  406. dcOutSetAll(pi, c_on, 0);
  407. dcInSetAll(pi, c_dc, CUBE_IN_MASK_DC);
  408. dcOutSetAll(pi, c_dc, 0);
  409. while( i < in_cnt )
  410. {
  411. switch(*str++)
  412. {
  413. case '0': dcSetIn(c_on, i, 1); dcSetIn(c_dc, i, 1); i++; break;
  414. case '1': dcSetIn(c_on, i, 2); dcSetIn(c_dc, i, 2); i++; break;
  415. case '-': dcSetIn(c_on, i, 3); dcSetIn(c_dc, i, 3); i++; break;
  416. case '\0': return 0;
  417. }
  418. }
  419. i = 0;
  420. while( i < out_cnt )
  421. {
  422. switch(*str)
  423. {
  424. case '0': dcSetOut(c_on, i, 0); dcSetOut(c_dc, i, 0); i++; break;
  425. case '1': dcSetOut(c_on, i, 1); dcSetOut(c_dc, i, 0); i++; break;
  426. case '2': dcSetOut(c_on, i, 0); dcSetOut(c_dc, i, 1); i++; break;
  427. case '-': dcSetOut(c_on, i, 0); dcSetOut(c_dc, i, 1); i++; break;
  428. case '\0': return 0;
  429. default:
  430. if ( *str >= 'a' && *str <= 'z' )
  431. return 0;
  432. if ( *str >= 'A' && *str <= 'Z' )
  433. return 0;
  434. if ( *str == '_' )
  435. return 0;
  436. }
  437. str++;
  438. }
  439. return str;
  440. }
  441. /*-- dcToStr ----------------------------------------------------------------*/
  442. char *dcToStr(pinfo *pi, dcube *c, char *sep, char *post)
  443. {
  444. static char s[1024*8];
  445. int i, l;
  446. for( i = 0; i < pi->in_cnt; i++ )
  447. s[i] = "x01-"[dcGetIn(c, i)];
  448. strcpy(s+pi->in_cnt, sep);
  449. l = pi->in_cnt+strlen(sep);
  450. for( i = 0; i < pi->out_cnt; i++ )
  451. s[i+l] = "01"[dcGetOut(c, i)];
  452. strcpy(s+l+pi->out_cnt, post);
  453. return s;
  454. }
  455. char *dcToStr2(pinfo *pi, dcube *c, char *sep, char *post)
  456. {
  457. static char s[1024*8];
  458. int i, l;
  459. for( i = 0; i < pi->in_cnt; i++ )
  460. s[i] = "x01-"[dcGetIn(c, i)];
  461. strcpy(s+pi->in_cnt, sep);
  462. l = pi->in_cnt+strlen(sep);
  463. for( i = 0; i < pi->out_cnt; i++ )
  464. s[i+l] = "01"[dcGetOut(c, i)];
  465. strcpy(s+l+pi->out_cnt, post);
  466. return s;
  467. }
  468. char *dcToStr3(pinfo *pi, dcube *c, char *sep, char *post)
  469. {
  470. static char s[1024*8];
  471. int i, l;
  472. for( i = 0; i < pi->in_cnt; i++ )
  473. s[i] = "x01-"[dcGetIn(c, i)];
  474. strcpy(s+pi->in_cnt, sep);
  475. l = pi->in_cnt+strlen(sep);
  476. for( i = 0; i < pi->out_cnt; i++ )
  477. s[i+l] = "01"[dcGetOut(c, i)];
  478. strcpy(s+l+pi->out_cnt, post);
  479. return s;
  480. }
  481. /*-- dcOutToStr -------------------------------------------------------------*/
  482. char *dcOutToStr(pinfo *pi, dcube *c, char *post)
  483. {
  484. static char s[1024*16];
  485. int i;
  486. for( i = 0; i < pi->out_cnt; i++ )
  487. s[i] = "01"[dcGetOut(c, i)];
  488. strcpy(s+pi->out_cnt, post);
  489. return s;
  490. }
  491. /*-- dcInToStr --------------------------------------------------------------*/
  492. char *dcInToStr(pinfo *pi, dcube *c, char *post)
  493. {
  494. static char s[1024*16];
  495. int i, l;
  496. for( i = 0; i < pi->in_cnt; i++ )
  497. s[i] = "x01-"[dcGetIn(c, i)];
  498. strcpy(s+pi->in_cnt, post);
  499. return s;
  500. }
  501. /*-- dcInc ------------------------------------------------------------------*/
  502. /* assume that there are no don't cares */
  503. /* interpret the cube as a binary number and add 1 */
  504. /* returns 0 if there was a overflow */
  505. int dcInc(pinfo *pi, dcube *c)
  506. {
  507. int i = 0;
  508. for(;;)
  509. {
  510. if ( dcGetIn(c, i) == 1 )
  511. {
  512. dcSetIn(c, i, 2);
  513. break;
  514. }
  515. else
  516. {
  517. dcSetIn(c, i, 1);
  518. i++;
  519. }
  520. if ( i >= pi->in_cnt )
  521. return 0;
  522. }
  523. return 1;
  524. }
  525. /*-- dcIncOut ---------------------------------------------------------------*/
  526. /* assume that there are no don't cares */
  527. /* interpret the cube as a binary number and add 1 */
  528. /* returns 0 if there was a overflow */
  529. int dcIncOut(pinfo *pi, dcube *c)
  530. {
  531. int i = 0;
  532. for(;;)
  533. {
  534. if ( dcGetOut(c, i) == 0 )
  535. {
  536. dcSetOut(c, i, 1);
  537. break;
  538. }
  539. else
  540. {
  541. dcSetOut(c, i, 0);
  542. i++;
  543. }
  544. if ( i >= pi->out_cnt )
  545. return 0;
  546. }
  547. return 1;
  548. }
  549. /*-- dcIsEqualIn ------------------------------------------------------------*/
  550. int dcIsEqualIn(pinfo *pi, dcube *a, dcube *b)
  551. {
  552. register int i;
  553. for( i = 0; i < pi->in_words; i++ )
  554. if ( a->in[i] != b->in[i] )
  555. return 0;
  556. return 1;
  557. }
  558. /*-- dcIsEqualOut -----------------------------------------------------------*/
  559. int dcIsEqualOut(pinfo *pi, dcube *a, dcube *b)
  560. {
  561. register int i;
  562. for( i = 0; i < pi->out_words; i++ )
  563. if ( a->out[i] != b->out[i] )
  564. return 0;
  565. return 1;
  566. }
  567. /*-- dcIsEqualOutCnt --------------------------------------------------------*/
  568. int dcIsEqualOutCnt(dcube *a, dcube *b, int off, int cnt)
  569. {
  570. register int i;
  571. for( i = off; i < off+cnt; i++ )
  572. if ( dcGetOut(a, i) != dcGetOut(b, i) )
  573. return 0;
  574. return 1;
  575. }
  576. /*-- dcIsEqualOutRange ------------------------------------------------------*/
  577. int dcIsEqualOutRange(dcube *a, int off_a, dcube *b, int off_b, int cnt)
  578. {
  579. register int i;
  580. for( i = 0; i < cnt; i++ )
  581. if ( dcGetOut(a, i+off_a) != dcGetOut(b, i+off_b) )
  582. return 0;
  583. return 1;
  584. }
  585. /*-- dcIsEqual --------------------------------------------------------------*/
  586. int dcIsEqual(pinfo *pi, dcube *a, dcube *b)
  587. {
  588. register int i;
  589. for( i = 0; i < pi->in_words; i++ )
  590. if ( a->in[i] != b->in[i] )
  591. return 0;
  592. for( i = 0; i < pi->out_words; i++ )
  593. if ( a->out[i] != b->out[i] )
  594. return 0;
  595. return 1;
  596. }
  597. /*-- dcIntersection ---------------------------------------------------------*/
  598. int dcIntersectionOld(pinfo *pi, dcube *r, dcube *a, dcube *b)
  599. {
  600. register int i;
  601. if ( dcDeltaOut(pi, a, b) > 0 )
  602. return 0;
  603. if ( dcIsDeltaInNoneZero(pi, a, b) > 0 )
  604. return 0;
  605. for( i = 0; i < pi->in_words; i++ )
  606. r->in[i] = a->in[i] & b->in[i];
  607. for( i = 0; i < pi->out_words; i++ )
  608. r->out[i] = a->out[i] & b->out[i];
  609. return 1;
  610. }
  611. int dcIntersection(pinfo *pi, dcube *r, dcube *a, dcube *b)
  612. {
  613. register int i;
  614. register c_int c;
  615. for( i = 0; i < pi->in_words; i++ )
  616. {
  617. c = a->in[i] & b->in[i]; /* Problem: Wie oft kommt 00 vor? */
  618. r->in[i] = c;
  619. c |= c>>1; /* Reduktion: Wie oft kommt x0 vor? */
  620. c = ~c; /* Invertierung: Wie oft kommt x1 vor? */
  621. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  622. if ( c > 0 )
  623. return 0;
  624. }
  625. if ( pi->out_cnt == 0 )
  626. return 1;
  627. c = 0;
  628. for( i = 0; i < pi->out_words; i++ )
  629. {
  630. r->out[i] = a->out[i] & b->out[i];
  631. c |= r->out[i];
  632. }
  633. if ( c == 0 )
  634. return 0;
  635. return 1;
  636. }
  637. /*-- dcIsInSubSet -----------------------------------------------------------*/
  638. /* Ist der Eingangs-Teil von b Teilmenge vom Eingangs-Teil von a? */
  639. /* Ja: Rueckgabe ist 1, nein: Rueckgabe ist 0 */
  640. int dcIsInSubSet(pinfo *pi, dcube *a, dcube *b)
  641. {
  642. register int i;
  643. for( i = 0; i < pi->in_words; i++ )
  644. if ( (a->in[i] & b->in[i]) != b->in[i] )
  645. return 0;
  646. return 1;
  647. }
  648. /*-- dcIsOutSubSet ----------------------------------------------------------*/
  649. /* Ist der Eingangs-Teil von b Teilmenge vom Eingangs-Teil von a? */
  650. /* Ja: Rueckgabe ist 1, nein: Rueckgabe ist 0 */
  651. int dcIsOutSubSet(pinfo *pi, dcube *a, dcube *b)
  652. {
  653. register int i;
  654. for( i = 0; i < pi->out_words; i++ )
  655. if ( (a->out[i] & b->out[i]) != b->out[i] )
  656. return 0;
  657. return 1;
  658. }
  659. /*-- dcIsSubSet -------------------------------------------------------------*/
  660. /* Ist b Teilmenge von a? */
  661. /* Ja: Rueckgabe ist 1, nein: Rueckgabe ist 0 */
  662. int dcIsSubSetReadable(pinfo *pi, dcube *a, dcube *b)
  663. {
  664. register int i;
  665. for( i = 0; i < pi->in_words; i++ )
  666. if ( (a->in[i] & b->in[i]) != b->in[i] )
  667. return 0;
  668. for( i = 0; i < pi->out_words; i++ )
  669. if ( (a->out[i] & b->out[i]) != b->out[i] )
  670. return 0;
  671. return 1;
  672. }
  673. int dcIsSubSet(pinfo *pi, dcube *a, dcube *b)
  674. {
  675. register int i;
  676. for( i = 0; i < pi->in_out_words_min; i++ )
  677. {
  678. if ( (~a->in[i] & b->in[i]) != 0 )
  679. return 0;
  680. if ( (~a->out[i] & b->out[i]) != 0 )
  681. return 0;
  682. }
  683. for( i = pi->in_out_words_min; i < pi->in_words; i++ )
  684. if ( (~a->in[i] & b->in[i]) != 0 )
  685. return 0;
  686. for( i = pi->in_out_words_min; i < pi->out_words; i++ )
  687. if ( (~a->out[i] & b->out[i]) != 0 )
  688. return 0;
  689. return 1;
  690. }
  691. int dcIsSubSet4(pinfo *pi, dcube *a1, dcube *a2, dcube *a3, dcube *a4, dcube *b)
  692. {
  693. register int i;
  694. register c_int r1, r2, r3, r4;
  695. r1 = 0;
  696. r2 = 0;
  697. r3 = 0;
  698. r4 = 0;
  699. for( i = 0; i < pi->in_words; i++ )
  700. {
  701. r1 |= (~a1->in[i] & b->in[i]);
  702. r2 |= (~a2->in[i] & b->in[i]);
  703. r3 |= (~a3->in[i] & b->in[i]);
  704. r4 |= (~a4->in[i] & b->in[i]);
  705. if ( r1 != 0 && r2 != 0 && r3 != 0 && r4 != 0 )
  706. return 0;
  707. }
  708. for( i = 0; i < pi->out_words; i++ )
  709. {
  710. r1 |= (~a1->out[i] & b->out[i]);
  711. r2 |= (~a2->out[i] & b->out[i]);
  712. r3 |= (~a3->out[i] & b->out[i]);
  713. r4 |= (~a4->out[i] & b->out[i]);
  714. if ( r1 != 0 && r2 != 0 && r3 != 0 && r4 != 0 )
  715. return 0;
  716. }
  717. return 1;
  718. }
  719. int dcIsSubSet6n(pinfo *pi, dcube *a, dcube *b1, dcube *b2, dcube *b3, dcube *b4, dcube *b5, dcube *b6)
  720. {
  721. register int i;
  722. register c_int r1, r2, r3, r4, r5, r6;
  723. register c_int aw;
  724. r1 = 0;
  725. r2 = 0;
  726. r3 = 0;
  727. r4 = 0;
  728. r5 = 0;
  729. r6 = 0;
  730. for( i = 0; i < pi->in_words; i++ )
  731. {
  732. aw = ~a->in[i];
  733. r1 |= (aw & b1->in[i]);
  734. r2 |= (aw & b2->in[i]);
  735. r3 |= (aw & b3->in[i]);
  736. r4 |= (aw & b4->in[i]);
  737. r5 |= (aw & b5->in[i]);
  738. r6 |= (aw & b6->in[i]);
  739. if ( r1 != 0 && r2 != 0 && r3 != 0 && r4 != 0 && r5 != 0 && r6 != 0 )
  740. return 0;
  741. }
  742. for( i = 0; i < pi->out_words; i++ )
  743. {
  744. aw = ~a->out[i];
  745. r1 |= (aw & b1->out[i]);
  746. r2 |= (aw & b2->out[i]);
  747. r3 |= (aw & b3->out[i]);
  748. r4 |= (aw & b4->out[i]);
  749. r5 |= (aw & b5->out[i]);
  750. r6 |= (aw & b6->out[i]);
  751. if ( r1 != 0 && r2 != 0 && r3 != 0 && r4 != 0 && r5 != 0 && r6 != 0 )
  752. return 0;
  753. }
  754. return 1;
  755. }
  756. /*-- dcInDCCnt --------------------------------------------------------------*/
  757. int dcInDCCnt(pinfo *pi, dcube *cube)
  758. {
  759. register c_int c;
  760. register int bc = 0;
  761. register int i;
  762. for( i = 0; i < pi->in_words; i++ )
  763. {
  764. c = cube->in[i]; /* Problem: Wie oft kommt 11 vor? */
  765. c = ~c; /* Problem: Wie oft kommt 00 vor? */
  766. c |= c>>1; /* Reduktion: Wie oft kommt x0 vor? */
  767. c = ~c; /* Invertierung: Wie oft kommt x1 vor? */
  768. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  769. bc += bitcount(c);
  770. }
  771. if ( ((pi->in_cnt)&(CUBE_SIGNALS_PER_IN_WORD-1)) != 0 )
  772. bc -= CUBE_SIGNALS_PER_IN_WORD-((pi->in_cnt)&(CUBE_SIGNALS_PER_IN_WORD-1));
  773. return bc;
  774. }
  775. /*-- dcInDCMask--------------------------------------------------------------*/
  776. void dcInDCMask(pinfo *pi, dcube *cube)
  777. {
  778. register c_int c;
  779. register int i;
  780. for( i = 0; i < pi->in_words; i++ )
  781. {
  782. c = cube->in[i]; /* Problem: Wie oft kommt 11 vor? */
  783. c &= c>>1; /* Reduktion: Wie oft kommt x1 vor? */
  784. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  785. c |= c<<1;
  786. cube->in[i] = c;
  787. }
  788. }
  789. /*-- dcInZeroCnt ------------------------------------------------------------*/
  790. int dcInZeroCnt(pinfo *pi, dcube *cube)
  791. {
  792. register c_int c;
  793. register int bc = 0;
  794. register int i;
  795. for( i = 0; i < pi->in_words; i++ )
  796. {
  797. c = cube->in[i]; /* Problem: Wie oft kommt 11 vor? */
  798. c = ~c; /* Problem: Wie oft kommt 00 vor? */
  799. c |= c>>1; /* Reduktion: Wie oft kommt x0 vor? */
  800. c = ~c; /* Invertierung: Wie oft kommt x1 vor? */
  801. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  802. c |= c<<1;
  803. c = cube->in[i] & ~c;
  804. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  805. bc += bitcount(c);
  806. }
  807. return bc;
  808. }
  809. /*-- dcInOneCnt -------------------------------------------------------------*/
  810. int dcInOneCnt(pinfo *pi, dcube *cube)
  811. {
  812. register c_int c;
  813. register int bc = 0;
  814. register int i;
  815. for( i = 0; i < pi->in_words; i++ )
  816. {
  817. c = cube->in[i]; /* Problem: Wie oft kommt 11 vor? */
  818. c = ~c; /* Problem: Wie oft kommt 00 vor? */
  819. c |= c>>1; /* Reduktion: Wie oft kommt x0 vor? */
  820. c = ~c; /* Invertierung: Wie oft kommt x1 vor? */
  821. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  822. c |= c<<1;
  823. c = cube->in[i] & ~c;
  824. c = c>>1;
  825. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  826. bc += bitcount(c);
  827. }
  828. return bc;
  829. }
  830. /*-- dcDeltaIn --------------------------------------------------------------*/
  831. int dcDeltaIn(pinfo *pi, dcube *a, dcube *b)
  832. {
  833. register c_int c;
  834. register int bc = 0;
  835. register int i;
  836. for( i = 0; i < pi->in_words; i++ )
  837. {
  838. c = a->in[i] & b->in[i]; /* Problem: Wie oft kommt 00 vor? */
  839. c |= c>>1; /* Reduktion: Wie oft kommt x0 vor? */
  840. c = ~c; /* Invertierung: Wie oft kommt x1 vor? */
  841. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  842. bc += bitcount(c);
  843. }
  844. return bc;
  845. }
  846. /*-- dcOutCnt ---------------------------------------------------------------*/
  847. int dcOutCnt(pinfo *pi, dcube *c)
  848. {
  849. register int i;
  850. register int bc = 0;
  851. for( i = 0; i < pi->out_words; i++ )
  852. bc += bitcount(c->out[i]);
  853. return bc;
  854. }
  855. /*-- dcInvIn ----------------------------------------------------------------*/
  856. int dcInvIn(pinfo *pi, dcube *cube)
  857. {
  858. register c_int c;
  859. register int i;
  860. for( i = 0; i < pi->in_words; i++ )
  861. {
  862. c = cube->in[i]; /* Problem: Wie oft kommt 11 vor? */
  863. c &= c>>1; /* Reduktion: Wie oft kommt x1 vor? */
  864. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  865. c |= c<<1; /* neue maske: 11 */
  866. cube->in[i] = ((~cube->in[i]) | c); /* 01 -> 10, 10 -> 01, 11 -> 11 */
  867. }
  868. return 0;
  869. }
  870. /*-- dcInvOut ---------------------------------------------------------------*/
  871. void dcInvOut(pinfo *pi, dcube *c)
  872. {
  873. register int i;
  874. for( i = 0; i < pi->out_words; i++ )
  875. c->out[i] = ~c->out[i];
  876. if ( pi->out_words > 0 )
  877. c->out[pi->out_words-1] &= pi->out_last_mask;
  878. }
  879. /*-- dcDeltaOut -------------------------------------------------------------*/
  880. int dcDeltaOut(pinfo *pi, dcube *a, dcube *b)
  881. {
  882. register int i;
  883. if ( pi->out_cnt == 0 )
  884. return 0;
  885. for( i = 0; i < pi->out_words; i++ )
  886. if ( (a->out[i] & b->out[i]) != 0 )
  887. return 0;
  888. return 1;
  889. }
  890. /*-- dcDelta ----------------------------------------------------------------*/
  891. int dcDelta(pinfo *pi, dcube *a, dcube *b)
  892. {
  893. return dcDeltaIn(pi, a, b) + dcDeltaOut(pi, a, b);
  894. }
  895. /*-- dcIsOutIllegal ---------------------------------------------------------*/
  896. int dcIsOutIllegal(pinfo *pi, dcube *c)
  897. {
  898. register int i;
  899. if ( pi->out_words == 0 )
  900. return 0;
  901. for( i = 0; i < pi->out_words; i++ )
  902. {
  903. if ( c->out[i] != 0 )
  904. break;
  905. }
  906. if ( i >= pi->out_words )
  907. return 1;
  908. return 0;
  909. }
  910. /*-- dcIsDeltaInNoneZero ----------------------------------------------------*/
  911. int dcIsDeltaInNoneZero(pinfo *pi, dcube *a, dcube *b)
  912. {
  913. register c_int c;
  914. register int i;
  915. for( i = 0; i < pi->in_words; i++ )
  916. {
  917. c = a->in[i] & b->in[i]; /* Problem: Wie oft kommt 00 vor? */
  918. c |= c>>1; /* Reduktion: Wie oft kommt x0 vor? */
  919. c = ~c; /* Invertierung: Wie oft kommt x1 vor? */
  920. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  921. if ( c > 0 )
  922. return 1;
  923. }
  924. return 0;
  925. }
  926. /*-- dcIsInIllegal ----------------------------------------------------------*/
  927. int dcIsInIllegal(pinfo *pi, dcube *cube)
  928. {
  929. register c_int c;
  930. register int i;
  931. for( i = 0; i < pi->in_words; i++ )
  932. {
  933. c = cube->in[i]; /* Problem: Wie oft kommt 00 vor? */
  934. c |= c>>1; /* Reduktion: Wie oft kommt x0 vor? */
  935. c = ~c; /* Invertierung: Wie oft kommt x1 vor? */
  936. c &= CUBE_IN_MASK_ZERO; /* Maskierung: Wie oft kommt 01 vor? */
  937. if ( c > 0 )
  938. return 1;
  939. }
  940. return 0;
  941. }
  942. /*-- dcIsDeltaNoneZero ------------------------------------------------------*/
  943. int dcIsDeltaNoneZero(pinfo *pi, dcube *a, dcube *b)
  944. {
  945. if ( dcDeltaOut(pi, a, b) > 0 )
  946. return 1;
  947. return dcIsDeltaInNoneZero(pi, a, b);
  948. }
  949. /*-- dcIsIllegal ------------------------------------------------------------*/
  950. int dcIsIllegal(pinfo *pi, dcube *c)
  951. {
  952. if ( dcIsInIllegal(pi, c) != 0 )
  953. return 1;
  954. if ( dcIsOutIllegal(pi, c) != 0 )
  955. return 1;
  956. return 0;
  957. }
  958. /*-- dcCofactor -------------------------------------------------------------*/
  959. /* Berechnet den Kofaktor von a bezueglich b. */
  960. /* Das Ergebnis wird in r abgelegt. */
  961. /* Der Reuckgabewert ist 0, wenn kein Ergebnis */
  962. /* berechnet wurde. */
  963. static void dc_cofactor(pinfo *pi, dcube *r, dcube *a, dcube *b)
  964. {
  965. register int i;
  966. for( i = 0; i < pi->in_words; i++ )
  967. r->in[i] = a->in[i] | ~b->in[i];
  968. if ( pi->out_words > 0 )
  969. {
  970. for( i = 0; i < pi->out_words-1; i++ )
  971. r->out[i] = a->out[i] | (~b->out[i]);
  972. r->out[i] = a->out[i] | ((~b->out[i]) & pi->out_last_mask);
  973. }
  974. r->n = a->n; /* notwendig fuer den irredundant algorithmus */
  975. }
  976. int dcCofactor(pinfo *pi, dcube *r, dcube *a, dcube *b)
  977. {
  978. if ( dcIsDeltaNoneZero(pi, a, b) != 0 )
  979. return 0;
  980. dc_cofactor(pi, r, a, b);
  981. return 1;
  982. }
  983. /* cofactoren, die teilmenge des cofactors sind */
  984. int dcSubCofactor(pinfo *pi, dcube *r, dcube *a, dcube *b)
  985. {
  986. if ( dcIsSubSet(pi, b, a) == 0 )
  987. return 0;
  988. dc_cofactor(pi, r, a, b);
  989. return 1;
  990. }
  991. /* cofactoren, die reduziert werden muessen */
  992. int dcRedCofactor(pinfo *pi, dcube *r, dcube *a, dcube *b)
  993. {
  994. if ( dcIsSubSet(pi, b, a) != 0 )
  995. return 0;
  996. return dcCofactor(pi, r, a, b);
  997. }
  998. /*-- dcConsensus ------------------------------------------------------------*/
  999. int dcConsensus(pinfo *pi, dcube *r, dcube *a, dcube *b)
  1000. {
  1001. int i;
  1002. int d_in = dcDeltaIn(pi, a, b);
  1003. int d_out = dcDeltaOut(pi, a, b);
  1004. if ( d_in == 0 )
  1005. {
  1006. for( i = 0; i < pi->in_words; i++ )
  1007. r->in[i] = a->in[i] & b->in[i];
  1008. for( i = 0; i < pi->out_words; i++ )
  1009. r->out[i] = a->out[i] | b->out[i];
  1010. }
  1011. else if ( d_in == 1 && d_out == 0 )
  1012. {
  1013. c_int c;
  1014. c_int m;
  1015. for( i = 0; i < pi->in_words; i++ )
  1016. {
  1017. c = a->in[i] & b->in[i];
  1018. m = (~((c)|(c>>1))) & CUBE_IN_MASK_ZERO;
  1019. r->in[i] = c | m | (m<<1);
  1020. }
  1021. for( i = 0; i < pi->out_words; i++ )
  1022. r->out[i] = a->out[i] & b->out[i];
  1023. }
  1024. else
  1025. {
  1026. return 0;
  1027. }
  1028. return 1;
  1029. }
  1030. /*-- dcSharpIn --------------------------------------------------------------*/
  1031. int dcSharpIn(pinfo *pi, dcube *r, dcube *a, dcube *b, int k)
  1032. {
  1033. register int i;
  1034. dcAllClear(pi, &(pi->tmp[5]));
  1035. dcSetIn(&(pi->tmp[5]), k, 3);
  1036. for( i = 0; i < pi->in_words; i++ )
  1037. {
  1038. if ( pi->tmp[5].in[i] != 0 )
  1039. {
  1040. if ( ((a->in[i] & ~b->in[i]) & pi->tmp[5].in[i]) == 0 )
  1041. return 0;
  1042. r->in[i] = (a->in[i] & ~b->in[i]) & pi->tmp[5].in[i];
  1043. r->in[i] |= a->in[i] & ~pi->tmp[5].in[i];
  1044. }
  1045. else
  1046. {
  1047. r->in[i] = a->in[i];
  1048. }
  1049. }
  1050. for( i = 0; i < pi->out_words; i++ )
  1051. r->out[i] = a->out[i];
  1052. return 1;
  1053. }
  1054. /*-- dcSharpOut -------------------------------------------------------------*/
  1055. int dcSharpOut(pinfo *pi, dcube *r, dcube *a, dcube *b)
  1056. {
  1057. int i;
  1058. for( i = 0; i < pi->out_words; i++ )
  1059. if ( (a->out[i] & ~b->out[i]) != 0 )
  1060. break;
  1061. if ( i >= pi->out_words )
  1062. return 0;
  1063. for( i = 0; i < pi->in_words; i++ )
  1064. r->in[i] = a->in[i];
  1065. for( i = 0; i < pi->out_words; i++ )
  1066. r->out[i] = a->out[i] & ~b->out[i];
  1067. return 1;
  1068. }
  1069. /*-- dcD1SharpIn ------------------------------------------------------------*/
  1070. int dcD1SharpIn(pinfo *pi, dcube *r, dcube *a, dcube *b, int k)
  1071. {
  1072. register int i;
  1073. dcAllClear(pi, &(pi->tmp[5]));
  1074. dcSetIn(&(pi->tmp[5]), k, 3);
  1075. for( i = 0; i < pi->in_words; i++ )
  1076. {
  1077. if ( pi->tmp[5].in[i] != 0 )
  1078. {
  1079. if ( ((a->in[i] & ~b->in[i]) & pi->tmp[5].in[i]) == 0 )
  1080. return 0;
  1081. r->in[i] = (a->in[i] & ~b->in[i]) & pi->tmp[5].in[i];
  1082. r->in[i] |= b->in[i] & ~pi->tmp[5].in[i];
  1083. }
  1084. else
  1085. {
  1086. r->in[i] = b->in[i];
  1087. }
  1088. }
  1089. for( i = 0; i < pi->out_words; i++ )
  1090. r->out[i] = b->out[i];
  1091. return 1;
  1092. }
  1093. /*-- dcD1SharpOut -----------------------------------------------------------*/
  1094. int dcD1SharpOut(pinfo *pi, dcube *r, dcube *a, dcube *b)
  1095. {
  1096. int i;
  1097. for( i = 0; i < pi->out_words; i++ )
  1098. if ( (a->out[i] & ~b->out[i]) != 0 )
  1099. break;
  1100. if ( i >= pi->out_words )
  1101. return 0;
  1102. for( i = 0; i < pi->in_words; i++ )
  1103. r->in[i] = b->in[i];
  1104. for( i = 0; i < pi->out_words; i++ )
  1105. r->out[i] = a->out[i] & ~b->out[i];
  1106. return 1;
  1107. }
  1108. /*-- dcGetCofactorForSplit --------------------------------------------------*/
  1109. int dcGetBinateInVarCofactor(pinfo *pi, dcube *r, dcube *rinv, dclist cl, dcube *cof)
  1110. {
  1111. int i;
  1112. for( i = 0; i < pi->in_cnt; i++ )
  1113. {
  1114. if ( dclIsBinateInVar(cl, i) != 0 )
  1115. {
  1116. dcCopy(pi, r, cof);
  1117. dcCopy(pi, rinv, cof);
  1118. dcInSetAll(pi, r, CUBE_IN_MASK_DC);
  1119. dcInSetAll(pi, rinv, CUBE_IN_MASK_DC);
  1120. dcSetIn(r, i, 2);
  1121. dcSetIn(rinv, i, 1);
  1122. return 1;
  1123. }
  1124. }
  1125. return 0;
  1126. }
  1127. int dcGetNoneDCInVarCofactor(pinfo *pi, dcube *r, dcube *rinv, dclist cl, dcube *cof)
  1128. {
  1129. int i;
  1130. for( i = 0; i < pi->in_cnt; i++ )
  1131. {
  1132. if ( dclIsDCInVar(pi, cl, i) == 0 )
  1133. {
  1134. dcCopy(pi, r, cof);
  1135. dcCopy(pi, rinv, cof);
  1136. dcInSetAll(pi, r, CUBE_IN_MASK_DC);
  1137. dcInSetAll(pi, rinv, CUBE_IN_MASK_DC);
  1138. dcSetIn(r, i, 2);
  1139. dcSetIn(rinv, i, 1);
  1140. return 1;
  1141. }
  1142. }
  1143. return 0;
  1144. }
  1145. int dcGetOutVarCofactor(pinfo *pi, dcube *r, dcube *rinv, dclist cl, dcube *cof)
  1146. {
  1147. int i, j;
  1148. int aktive_bit_cnt;
  1149. aktive_bit_cnt = 0;
  1150. for( i = 0; i < pi->out_cnt; i++ )
  1151. if ( dcGetOut(cof, i) != 0 )
  1152. aktive_bit_cnt++;
  1153. if ( aktive_bit_cnt <= 1 )
  1154. return 0;
  1155. dcInSetAll(pi, r, CUBE_IN_MASK_DC);
  1156. dcOutSetAll(pi, r, 0);
  1157. dcInSetAll(pi, rinv, CUBE_IN_MASK_DC);
  1158. dcOutSetAll(pi, rinv, 0);
  1159. j = 0;
  1160. for( i = 0; i < pi->out_cnt; i++ )
  1161. if ( dcGetOut(cof, i) != 0 )
  1162. {
  1163. if ( j < aktive_bit_cnt/2 )
  1164. dcSetOut(r, i, 1);
  1165. else
  1166. dcSetOut(rinv, i, 1);
  1167. j++;
  1168. }
  1169. return 1;
  1170. }
  1171. int dcGetCofactorForSplit(pinfo *pi, dcube *l, dcube *r, dclist cl, dcube *cof)
  1172. {
  1173. /*
  1174. if ( dcGetBinateInVarCofactor(pi, l, r, cl, cof) != 0 )
  1175. return 1;
  1176. if ( dcGetOutVarCofactor(pi, l, r, cl, cof) != 0 )
  1177. return 1;
  1178. return 0;
  1179. */
  1180. /*
  1181. if ( pinfoGetInVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  1182. return 1;
  1183. if ( pinfoGetOutVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  1184. return 1;
  1185. */
  1186. return pinfoGetDCubeCofactorForSplitting(pi, l, r, cl, cof);
  1187. }
  1188. int dcGetNoneDCCofactorForSplit(pinfo *pi, dcube *l, dcube *r, dclist cl, dcube *cof)
  1189. {
  1190. /*
  1191. if ( pinfoGetOutVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  1192. return 1;
  1193. */
  1194. /*
  1195. if ( pinfoGetInVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  1196. return 1;
  1197. if ( dcGetNoneDCInVarCofactor(pi, l, r, cl, cof) != 0 )
  1198. return 1;
  1199. if ( pinfoGetOutVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  1200. return 1;
  1201. */
  1202. /*
  1203. if ( pinfoGetDCubeCofactorForSplitting(pi, l, r, cl, cof) != 0 )
  1204. return 1;
  1205. */
  1206. /* reihenfolge wichtig??? zuerst eingangs variablen */
  1207. if ( pinfoGetInVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  1208. return 1;
  1209. if ( dcGetOutVarCofactor(pi, l, r, cl, cof) != 0 )
  1210. return 1;
  1211. if ( dcGetNoneDCInVarCofactor(pi, l, r, cl, cof) != 0 )
  1212. return 1;
  1213. /* jetzt die ausgangsvariablen */
  1214. /* die pinfo out var function zerstoert das ergebnis hmmmm??? */
  1215. return 0;
  1216. }
  1217. /*-- dcIsInTautology --------------------------------------------------------*/
  1218. int dcIsInTautology(pinfo *pi, dcube *c)
  1219. {
  1220. register int i;
  1221. for( i = 0; i < pi->in_words; i++ )
  1222. if ( c->in[i] != CUBE_IN_MASK_DC )
  1223. return 0;
  1224. return 1;
  1225. }
  1226. /*-- dcIsTautology ----------------------------------------------------------*/
  1227. int dcIsTautology(pinfo *pi, dcube *c)
  1228. {
  1229. register int i;
  1230. for( i = 0; i < pi->in_words; i++ )
  1231. if ( c->in[i] != CUBE_IN_MASK_DC )
  1232. return 0;
  1233. if ( pi->out_words > 0 )
  1234. {
  1235. for( i = 0; i < pi->out_words-1; i++ )
  1236. {
  1237. if ( c->out[i] != CUBE_OUT_MASK )
  1238. return 0;
  1239. }
  1240. if ( (c->out[i]&pi->out_last_mask) != pi->out_last_mask )
  1241. return 0;
  1242. }
  1243. return 1;
  1244. }
  1245. /*-- dcNot ------------------------------------------------------------------*/
  1246. void dcNot(pinfo *pi, dcube *c)
  1247. {
  1248. register int i;
  1249. for( i = 0; i < pi->in_words; i++ )
  1250. c->in[i] = ~c->in[i];
  1251. for( i = 0; i < pi->out_words; i++ )
  1252. c->out[i] = ~c->out[i];
  1253. if ( pi->out_words > 0 )
  1254. c->out[pi->out_words-1] &= pi->out_last_mask;
  1255. }
  1256. /*-- dcOrIn -----------------------------------------------------------------*/
  1257. void dcOrIn(pinfo *pi, dcube *r, dcube *a, dcube *b)
  1258. {
  1259. register int i;
  1260. for( i = 0; i < pi->in_words; i++ )
  1261. r->in[i] = a->in[i] | b->in[i];
  1262. }
  1263. /*-- dcOrOut ----------------------------------------------------------------*/
  1264. void dcOrOut(pinfo *pi, dcube *r, dcube *a, dcube *b)
  1265. {
  1266. register int i;
  1267. for( i = 0; i < pi->out_words; i++ )
  1268. r->out[i] = a->out[i] | b->out[i];
  1269. }
  1270. /*-- dcOr -------------------------------------------------------------------*/
  1271. void dcOr(pinfo *pi, dcube *r, dcube *a, dcube *b)
  1272. {
  1273. register int i;
  1274. for( i = 0; i < pi->in_words; i++ )
  1275. r->in[i] = a->in[i] | b->in[i];
  1276. for( i = 0; i < pi->out_words; i++ )
  1277. r->out[i] = a->out[i] | b->out[i];
  1278. }
  1279. /*-- dcAnd ------------------------------------------------------------------*/
  1280. void dcAnd(pinfo *pi, dcube *r, dcube *a, dcube *b)
  1281. {
  1282. register int i;
  1283. for( i = 0; i < pi->in_words; i++ )
  1284. r->in[i] = a->in[i] & b->in[i];
  1285. for( i = 0; i < pi->out_words; i++ )
  1286. r->out[i] = a->out[i] & b->out[i];
  1287. }
  1288. /*-- dcAndIn ----------------------------------------------------------------*/
  1289. void dcAndIn(pinfo *pi, dcube *r, dcube *a, dcube *b)
  1290. {
  1291. register int i;
  1292. for( i = 0; i < pi->in_words; i++ )
  1293. r->in[i] = a->in[i] & b->in[i];
  1294. }
  1295. /*-- dcNotAndOut -----------------------------------------------------------*/
  1296. void dcNotAndOut(pinfo *pi, dcube *r, dcube *a, dcube *b)
  1297. {
  1298. register int i;
  1299. for( i = 0; i < pi->out_words; i++ )
  1300. r->out[i] = (~a->out[i]) & b->out[i];
  1301. }
  1302. /*-- dcXorOut -----------------------------------------------------------*/
  1303. void dcXorOut(pinfo *pi, dcube *r, dcube *a, dcube *b)
  1304. {
  1305. register int i;
  1306. for( i = 0; i < pi->out_words; i++ )
  1307. r->out[i] = a->out[i] ^ b->out[i];
  1308. }
  1309. /*-- dcGetLiteralCnt --------------------------------------------------------*/
  1310. int dcGetLiteralCnt(pinfo *pi, dcube *c)
  1311. {
  1312. return pi->in_cnt - dcInDCCnt(pi, c) + dcOutCnt(pi, c);
  1313. }
  1314. /*-- dcGetDichotomyWeight ---------------------------------------------------*/
  1315. int dcGetDichotomyWeight(pinfo *pi, dcube *c)
  1316. {
  1317. int z = dcInZeroCnt(pi, c);
  1318. int o = dcInOneCnt(pi, c);
  1319. int w;
  1320. w = z-o;
  1321. if ( w < 0 )
  1322. w = -w;
  1323. w = pi->in_cnt - w + 1;
  1324. return w;
  1325. }
  1326. /*-- dcExpand1 --------------------------------------------------------------*/
  1327. /* Spezial Expand algorithmus fuer das URP Complement */
  1328. static void dcExpand1(pinfo *pi, dcube *c, dclist cl_off)
  1329. {
  1330. dcube *b = &(pi->tmp[7]);
  1331. int delta_in;
  1332. int delta_out;
  1333. int i, cnt = dclCnt(cl_off);
  1334. dcSetTautology(pi, b);
  1335. dcInSetAll(pi, b, 0);
  1336. dcOutSetAll(pi, b, 0);
  1337. for( i = 0; i < cnt; i++ )
  1338. {
  1339. delta_in = dcDeltaIn(pi, c, dclGet(cl_off, i));
  1340. delta_out = dcDeltaOut(pi, c, dclGet(cl_off, i));
  1341. if ( delta_in + delta_out == 1 )
  1342. dcOr(pi, b, b, dclGet(cl_off, i));
  1343. }
  1344. dcNot(pi, b);
  1345. dcOr(pi, c, c, b);
  1346. }
  1347. /*-- dcWriteBin -------------------------------------------------------------*/
  1348. int dcWriteBin(pinfo *pi, dcube *c, FILE *fp)
  1349. {
  1350. int i, j;
  1351. dcube *a = &(pi->tmp[7]);
  1352. c_int w;
  1353. if ( pi->in_words != 0 )
  1354. {
  1355. for( i = 0; i < pi->in_words; i++ )
  1356. {
  1357. w = c->in[i];
  1358. for( j = 0; j < sizeof(c_int); j++ )
  1359. {
  1360. ((unsigned char *)a->in)[i*sizeof(c_int)+j] = (unsigned char)(w&255);
  1361. w>>=8;
  1362. }
  1363. }
  1364. if ( b_io_Write(fp, pi->in_words*sizeof(c_int), (unsigned char *)a->in) == 0 )
  1365. return 0;
  1366. }
  1367. if ( pi->out_words != 0 )
  1368. {
  1369. for( i = 0; i < pi->out_words; i++ )
  1370. {
  1371. w = c->out[i];
  1372. for( j = 0; j < sizeof(c_int); j++ )
  1373. {
  1374. ((unsigned char *)a->out)[i*sizeof(c_int)+j] = (unsigned char)(w&255);
  1375. w>>=8;
  1376. }
  1377. }
  1378. if ( b_io_Write(fp, pi->out_words*sizeof(c_int), (unsigned char *)a->out) == 0 )
  1379. return 0;
  1380. }
  1381. if ( b_io_WriteInt(fp, c->n) == 0 )
  1382. return 0;
  1383. return 1;
  1384. }
  1385. /*-- dcReadBin --------------------------------------------------------------*/
  1386. int dcReadBin(pinfo *pi, dcube *c, FILE *fp)
  1387. {
  1388. int i, j;
  1389. dcube *a = &(pi->tmp[7]);
  1390. c_int w;
  1391. if ( pi->in_words != 0 )
  1392. {
  1393. if ( b_io_Read(fp, pi->in_words*sizeof(c_int), (unsigned char *)a->in) == 0 )
  1394. return 0;
  1395. for( i = 0; i < pi->in_words; i++ )
  1396. {
  1397. w = 0;
  1398. for( j = 0; j < sizeof(c_int); j++ )
  1399. {
  1400. w<<=8;
  1401. w |= (c_int)((unsigned char *)a->in)[i*sizeof(c_int)+sizeof(c_int)-1-j];
  1402. }
  1403. c->in[i] = w;
  1404. }
  1405. }
  1406. if ( pi->out_words != 0 )
  1407. {
  1408. if ( b_io_Read(fp, pi->out_words*sizeof(c_int), (unsigned char *)a->out) == 0 )
  1409. return 0;
  1410. for( i = 0; i < pi->out_words; i++ )
  1411. {
  1412. w = 0;
  1413. for( j = 0; j < sizeof(c_int); j++ )
  1414. {
  1415. w<<=8;
  1416. w |= (c_int)((unsigned char *)a->out)[i*sizeof(c_int)+sizeof(c_int)-1-j];
  1417. }
  1418. c->out[i] = w;
  1419. }
  1420. }
  1421. if ( b_io_ReadInt(fp, &(c->n)) == 0 )
  1422. return 0;
  1423. return 1;
  1424. }
  1425. /*===========================================================================*/
  1426. /*-- dclInit ----------------------------------------------------------------*/
  1427. int dclInit(dclist *cl)
  1428. {
  1429. *cl = (dclist)malloc(sizeof(struct _dclist_struct));
  1430. if ( *cl == NULL )
  1431. return 0;
  1432. (*cl)->list = NULL;
  1433. (*cl)->max = 0;
  1434. (*cl)->cnt = 0;
  1435. (*cl)->flag_list = NULL;
  1436. return 1;
  1437. }
  1438. /*-- dclInitCached ----------------------------------------------------------*/
  1439. int dclInitCached(pinfo *pi, dclist *cl)
  1440. {
  1441. if ( pi->cache_cnt == 0 )
  1442. return dclInit(cl);
  1443. pi->cache_cnt--;
  1444. *cl = pi->cache_cl[pi->cache_cnt];
  1445. return 1;
  1446. }
  1447. /*-- dclInitVA --------------------------------------------------------------*/
  1448. int dclInitVA(int n, ...)
  1449. {
  1450. va_list va;
  1451. int i;
  1452. va_start(va, n);
  1453. for( i = 0; i < n; i++ )
  1454. if ( dclInit(va_arg(va, dclist *)) == 0 )
  1455. break;
  1456. va_end(va);
  1457. if ( i < n )
  1458. {
  1459. va_start(va, n);
  1460. while( i-- >= 0 )
  1461. dclDestroy(*va_arg(va, dclist *));
  1462. va_end(va);
  1463. return 0;
  1464. }
  1465. return 1;
  1466. }
  1467. /*-- dclInitCachedVA --------------------------------------------------------*/
  1468. int dclInitCachedVA(pinfo *pi, int n, ...)
  1469. {
  1470. va_list va;
  1471. int i;
  1472. va_start(va, n);
  1473. for( i = 0; i < n; i++ )
  1474. if ( dclInitCached(pi, va_arg(va, dclist *)) == 0 )
  1475. break;
  1476. va_end(va);
  1477. if ( i < n )
  1478. {
  1479. va_start(va, n);
  1480. while( i-- >= 0 )
  1481. dclDestroyCached(pi, *va_arg(va, dclist *));
  1482. va_end(va);
  1483. return 0;
  1484. }
  1485. return 1;
  1486. }
  1487. /*-- dclDestroy -------------------------------------------------------------*/
  1488. static void dcl_destroy(dclist cl)
  1489. {
  1490. int i;
  1491. for( i = 0; i < cl->max; i++ )
  1492. dcDestroy(cl->list+i);
  1493. if ( cl->list != NULL )
  1494. free(cl->list);
  1495. if ( cl->flag_list != NULL )
  1496. free(cl->flag_list);
  1497. cl->flag_list = NULL;
  1498. cl->list = NULL;
  1499. cl->max = 0;
  1500. cl->cnt = 0;
  1501. }
  1502. void dclDestroy(dclist cl)
  1503. {
  1504. dcl_destroy(cl);
  1505. free(cl);
  1506. }
  1507. /*-- dclDestroyCached -------------------------------------------------------*/
  1508. void dclDestroyCached(pinfo *pi, dclist cl)
  1509. {
  1510. if ( pi->cache_cnt >= PINFO_CACHE_CL )
  1511. dclDestroy(cl);
  1512. else
  1513. {
  1514. dclClear(cl);
  1515. pi->cache_cl[pi->cache_cnt++] = cl;
  1516. }
  1517. }
  1518. /*-- dclDestroyVA -----------------------------------------------------------*/
  1519. void dclDestroyVA(int n, ...)
  1520. {
  1521. va_list va;
  1522. int i;
  1523. va_start(va, n);
  1524. for( i = 0; i < n; i++ )
  1525. dclDestroy(va_arg(va, dclist));
  1526. va_end(va);
  1527. }
  1528. /*-- dclDestroyCachedVA -----------------------------------------------------*/
  1529. void dclDestroyCachedVA(pinfo *pi, int n, ...)
  1530. {
  1531. va_list va;
  1532. int i;
  1533. va_start(va, n);
  1534. for( i = 0; i < n; i++ )
  1535. dclDestroyCached(pi, va_arg(va, dclist));
  1536. va_end(va);
  1537. }
  1538. /*-- dclExpandFlagListTo ----------------------------------------------------*/
  1539. int dclExpandFlagListTo(dclist cl, int max)
  1540. {
  1541. void *ptr;
  1542. /* allocate at least one byte */
  1543. if ( max <= 0 )
  1544. max = 1;
  1545. if ( cl->flag_list != NULL )
  1546. {
  1547. if ( max == 0 )
  1548. {
  1549. /* 14 jan 2002: assigning a NULL pointer seems to be an error... */
  1550. /*
  1551. free(cl->flag_list);
  1552. cl->flag_list = NULL;
  1553. return 1;
  1554. */
  1555. /* instead, allocate at least one byte */
  1556. max = 1;
  1557. }
  1558. ptr = realloc(cl->flag_list, max);
  1559. }
  1560. else
  1561. {
  1562. ptr = malloc(max);
  1563. }
  1564. if ( ptr == NULL )
  1565. return 0;
  1566. cl->flag_list = (char *)ptr;
  1567. return 1;
  1568. }
  1569. /*-- dclExpandTo ------------------------------------------------------------*/
  1570. int dclExpandTo(pinfo *pi, dclist cl, int max)
  1571. {
  1572. void *ptr;
  1573. max = (max+31)&~31;
  1574. if ( max <= cl->max )
  1575. return 1;
  1576. if ( cl->list == NULL )
  1577. ptr = malloc(max*sizeof(dcube));
  1578. else
  1579. ptr = realloc(cl->list, max*sizeof(dcube));
  1580. if ( ptr == NULL )
  1581. return 0;
  1582. cl->list = (dcube *)ptr;
  1583. if ( cl->flag_list != NULL )
  1584. if ( dclExpandFlagListTo(cl, max) == 0 )
  1585. return 0;
  1586. while(cl->max < max)
  1587. {
  1588. if ( dcInitMem(pi, cl->list+cl->max) == 0 )
  1589. return 0;
  1590. if ( cl->flag_list != NULL )
  1591. cl->flag_list[cl->max] = 0;
  1592. cl->max++;
  1593. }
  1594. return 1;
  1595. }
  1596. /*-- dclAddEmpty ------------------------------------------------------------*/
  1597. /* returns position or -1 */
  1598. int dclAddEmpty(pinfo *pi, dclist cl)
  1599. {
  1600. if ( dclCnt(cl) >= cl->max )
  1601. if ( dclExpandTo(pi, cl, dclCnt(cl)+1) == 0 )
  1602. return -1;
  1603. dcInSetAll(pi, cl->list+cl->cnt, CUBE_IN_MASK_DC);
  1604. dcOutSetAll(pi, cl->list+cl->cnt, 0);
  1605. if ( cl->flag_list != NULL )
  1606. cl->flag_list[cl->cnt] = 0;
  1607. cl->cnt++;
  1608. return cl->cnt-1;
  1609. }
  1610. dcube *dclAddEmptyCube(pinfo *pi, dclist cl)
  1611. {
  1612. int pos;
  1613. pos = dclAddEmpty(pi, cl);
  1614. if ( pos < 0 )
  1615. return NULL;
  1616. return dclGet(cl, pos);
  1617. }
  1618. /*-- dclAdd -----------------------------------------------------------------*/
  1619. /* returns position or -1 */
  1620. int dclAdd(pinfo *pi, dclist cl, dcube *c)
  1621. {
  1622. if ( dclCnt(cl) >= cl->max )
  1623. {
  1624. /* there is an interessting possible bug here */
  1625. /* if c is an element of cl, c might become invalid */
  1626. /* if cl->list is expanded and moved to another */
  1627. /* location in the memory, c becomes invalid */
  1628. if ( c >= cl->list && c < cl->list+cl->max )
  1629. {
  1630. dcCopy(pi, pi->tmp+12, c);
  1631. c = pi->tmp+12;
  1632. }
  1633. if ( dclExpandTo(pi, cl, dclCnt(cl)+1) == 0 )
  1634. return -1;
  1635. assert(cl->max > cl->cnt);
  1636. }
  1637. {
  1638. register dcube *d;
  1639. int i;
  1640. d = cl->list+cl->cnt;
  1641. for( i = 0; i < pi->in_out_words_min; i++ )
  1642. {
  1643. d->in[i] = c->in[i];
  1644. d->out[i] = c->out[i];
  1645. }
  1646. for( i = pi->in_out_words_min; i < pi->in_words; i++ )
  1647. d->in[i] = c->in[i];
  1648. for( i = pi->in_out_words_min; i < pi->out_words; i++ )
  1649. d->out[i] = c->out[i];
  1650. d->n = c->n;
  1651. }
  1652. /* dcCopy(pi, cl->list+cl->cnt, c); */
  1653. if ( cl->flag_list != NULL )
  1654. cl->flag_list[cl->cnt] = 0;
  1655. cl->cnt++;
  1656. return cl->cnt-1;
  1657. }
  1658. /*-- dclAddUnique -----------------------------------------------------------*/
  1659. /* returns position or -1 */
  1660. int dclAddUnique(pinfo *pi, dclist cl, dcube *c)
  1661. {
  1662. int i, cnt = dclCnt(cl);
  1663. for( i = 0; i < cnt; i++ )
  1664. if ( dcIsEqual(pi, dclGet(cl, i), c) != 0 )
  1665. return i;
  1666. return dclAdd(pi, cl, c);
  1667. }
  1668. /*-- dclJoin ----------------------------------------------------------------*/
  1669. int dclJoin(pinfo *pi, dclist dest, dclist src)
  1670. {
  1671. int i, cnt = dclCnt(src);
  1672. if ( dclExpandTo(pi, dest, dclCnt(dest)+dclCnt(src)) == 0 )
  1673. return 0;
  1674. for( i = 0; i < cnt; i++ )
  1675. if ( dclAdd(pi, dest, dclGet(src, i)) < 0 )
  1676. return 0;
  1677. return 1;
  1678. }
  1679. /*-- dclCopy ----------------------------------------------------------------*/
  1680. int dclCopy(pinfo *pi, dclist dest, dclist src)
  1681. {
  1682. dclClear(dest);
  1683. return dclJoin(pi, dest, src);
  1684. }
  1685. /*-- dclClearFlags ----------------------------------------------------------*/
  1686. int dclClearFlags(dclist cl)
  1687. {
  1688. if ( dclExpandFlagListTo(cl, cl->max) == 0 )
  1689. return 0;
  1690. if ( cl->max == 0 )
  1691. return 1;
  1692. memset(cl->flag_list, 0, cl->max);
  1693. return 1;
  1694. }
  1695. /*-- dclSetFlag -------------------------------------------------------------*/
  1696. /*
  1697. void dclSetFlag(dclist cl, int pos)
  1698. {
  1699. cl->flag_list[pos] = 1;
  1700. }
  1701. */
  1702. /*-- dclIsFlag --------------------------------------------------------------*/
  1703. /*
  1704. int dclIsFlag(dclist cl, int pos)
  1705. {
  1706. return (int)cl->flag_list[pos];
  1707. }
  1708. */
  1709. /*-- dclDeleteCubesWithFlag -------------------------------------------------*/
  1710. void dclDeleteCubesWithFlag(pinfo *pi, dclist cl)
  1711. {
  1712. int src, dest;
  1713. src = 0;
  1714. dest = 0;
  1715. while( src < cl->cnt )
  1716. {
  1717. if ( cl->flag_list[src] != 0 )
  1718. {
  1719. src++;
  1720. }
  1721. else
  1722. {
  1723. if ( src != dest )
  1724. {
  1725. dcCopy(pi, cl->list+dest, cl->list+src);
  1726. cl->flag_list[dest] = cl->flag_list[src];
  1727. }
  1728. dest++;
  1729. src++;
  1730. }
  1731. }
  1732. memset(cl->flag_list, 0, cl->cnt);
  1733. cl->cnt = dest;
  1734. }
  1735. /*-- dclCopyCubesWithFlag ----------------------------------------------------*/
  1736. int dclCopyCubesWithFlag(pinfo *pi, dclist dest, dclist src)
  1737. {
  1738. int i, cnt = dclCnt(src);
  1739. dclClear(dest);
  1740. for( i = 0; i < cnt; i++ )
  1741. if ( dclIsFlag(src, i) != 0 )
  1742. if ( dclAdd(pi, dest, dclGet(src, i)) < 0 )
  1743. return 0;
  1744. return 1;
  1745. }
  1746. /*-- dclDeleteCube ----------------------------------------------------------*/
  1747. void dclDeleteCube(pinfo *pi, dclist cl, int pos)
  1748. {
  1749. if ( cl->cnt == 0 )
  1750. return;
  1751. if ( pos >= cl->cnt )
  1752. return;
  1753. pos++;
  1754. while( pos < cl->cnt )
  1755. {
  1756. dcCopy(pi, cl->list+pos-1, cl->list+pos);
  1757. pos++;
  1758. }
  1759. /* dcDestroy(cl->list+cl->cnt-1); */
  1760. cl->cnt--;
  1761. }
  1762. /*-- dclDeleteByCube --------------------------------------------------------*/
  1763. int dclDeleteByCube(pinfo *pi, dclist cl, dcube *c)
  1764. {
  1765. int i, cnt = dclCnt(cl);
  1766. if ( dclClearFlags(cl) == 0 )
  1767. return 0;
  1768. for( i = 0; i < cnt; i++ )
  1769. {
  1770. if ( dcIsEqual(pi, c, dclGet(cl, i)) != 0 )
  1771. dclSetFlag(cl, i);
  1772. }
  1773. dclDeleteCubesWithFlag(pi, cl);
  1774. return 1;
  1775. }
  1776. /*-- dclDeleteByCubeList ----------------------------------------------------*/
  1777. int dclDeleteByCubeList(pinfo *pi, dclist cl, dclist del)
  1778. {
  1779. int i, cnt = dclCnt(del);
  1780. for( i = 0; i < cnt; i++ )
  1781. if ( dclDeleteByCube(pi, cl, dclGet(del, i)) == 0 )
  1782. return 0;
  1783. return 1;
  1784. }
  1785. /*-- dclClear ---------------------------------------------------------------*/
  1786. void dclClear(dclist cl)
  1787. {
  1788. cl->cnt = 0;
  1789. }
  1790. /*-- dclRealClear -----------------------------------------------------------*/
  1791. void dclRealClear(dclist cl)
  1792. {
  1793. dcl_destroy(cl);
  1794. }
  1795. /*-- dclCnt -----------------------------------------------------------------*/
  1796. /*
  1797. int dclCnt(dclist cl)
  1798. {
  1799. return cl->cnt;
  1800. }
  1801. */
  1802. /*-- dclGet -----------------------------------------------------------------*/
  1803. /*
  1804. dcube *dclGet(dclist cl, int pos)
  1805. {
  1806. return cl->list + pos;
  1807. }
  1808. */
  1809. /*-- dclGetPinfoOutLength ---------------------------------------------------*/
  1810. int dclSetPinfoByLength(pinfo *pi, dclist cl)
  1811. {
  1812. if ( pinfoSetInCnt(pi, 1) == 0 )
  1813. return 0;
  1814. if ( pinfoSetOutCnt(pi, dclCnt(cl)) == 0 )
  1815. return 0;
  1816. return 1;
  1817. }
  1818. /*-- dclInvertOutMatrix -----------------------------------------------------*/
  1819. int dclInvertOutMatrix(pinfo *dest_pi, dclist dest_cl, pinfo *src_pi, dclist src_cl)
  1820. {
  1821. int i, cnt = dclCnt(src_cl);
  1822. int j;
  1823. dcube *inv = &(dest_pi->tmp[1]);
  1824. dclRealClear(dest_cl);
  1825. if ( dclSetPinfoByLength(dest_pi, src_cl) == 0 )
  1826. return 0;
  1827. for( j = 0; j < src_pi->out_cnt; j++ )
  1828. {
  1829. dcSetTautology(dest_pi, inv);
  1830. for( i = 0; i < cnt; i++ )
  1831. if ( dcGetOut(dclGet(src_cl, i), j) == 0 )
  1832. dcSetOut(inv, i, 0);
  1833. if ( dclAdd(dest_pi, dest_cl, inv) < 0 )
  1834. return 0;
  1835. }
  1836. return 1;
  1837. }
  1838. /*-- dclReadFP --------------------------------------------------------------*/
  1839. #define DCL_LINE_LEN (1024*32)
  1840. int dclReadFP(pinfo *pi, dclist cl, FILE *fp)
  1841. {
  1842. static char s[DCL_LINE_LEN];
  1843. dcube c;
  1844. int is_cube_init = 0;
  1845. dclRealClear(cl);
  1846. for(;;)
  1847. {
  1848. if ( fgets(s, DCL_LINE_LEN, fp) == NULL )
  1849. break;
  1850. if ( s[0] == '#' )
  1851. {
  1852. /* nothing */
  1853. }
  1854. else if ( s[0] == '.' && is_cube_init == 0 )
  1855. {
  1856. if ( strncmp(s, ".ilb ", 5) == 0 )
  1857. {
  1858. /* input label names */
  1859. if ( pinfoImportInLabels(pi, s+5, " \t\n\r") == 0 )
  1860. return 0;
  1861. }
  1862. else if ( strncmp(s, ".olb ", 5) == 0 || strncmp(s, ".ob ", 4) == 0 )
  1863. {
  1864. /* this is not a valid espresso command... */
  1865. /* output label names */
  1866. if ( pinfoImportOutLabels(pi, s+5, " \t\n\r") == 0 )
  1867. return 0;
  1868. }
  1869. else if ( strncmp(s, ".ob ", 4) == 0 )
  1870. {
  1871. /* output label names */
  1872. if ( pinfoImportOutLabels(pi, s+4, " \t\n\r") == 0 )
  1873. return 0;
  1874. }
  1875. else if ( strncmp(s, ".i ", 3) == 0 )
  1876. pinfoSetInCnt(pi, atoi(s+3));
  1877. else if ( strncmp(s, ".o ", 3) == 0 )
  1878. pinfoSetOutCnt(pi, atoi(s+3));
  1879. }
  1880. else if ( s[0] == '.' )
  1881. {
  1882. /* ignore */
  1883. }
  1884. else
  1885. {
  1886. if ( is_cube_init == 0 )
  1887. {
  1888. if ( dcInit(pi, &c) == 0 )
  1889. return 0;
  1890. is_cube_init = 1;
  1891. }
  1892. if ( dcSetByStr(pi, &c, s) == 0 )
  1893. return dcDestroy(&c), 0;
  1894. if ( dcIsOutIllegal(pi, &c) == 0 )
  1895. if ( dclAdd(pi, cl, &c) < 0 )
  1896. return dcDestroy(&c), 0;
  1897. }
  1898. }
  1899. if ( is_cube_init != 0 )
  1900. dcDestroy(&c);
  1901. return 1;
  1902. }
  1903. /*-- dclReadCNFFP -----------------------------------------------------------*/
  1904. int dclReadCNFFP(pinfo *pi, dclist cl, FILE *fp)
  1905. {
  1906. static char s[DCL_LINE_LEN];
  1907. dcube *c = &(pi->tmp[3]);
  1908. char *t;
  1909. int is_init = 0;
  1910. int cnt = -1;
  1911. int pos;
  1912. pinfoSetOutCnt(pi, 1);
  1913. dclRealClear(cl);
  1914. for(;;)
  1915. {
  1916. if ( fgets(s, DCL_LINE_LEN, fp) == NULL )
  1917. break;
  1918. if ( s[0] == '\0' || s[0] == 'c' )
  1919. {
  1920. /* comment line */
  1921. }
  1922. else if ( s[0] == 'p' && is_init == 0 )
  1923. {
  1924. t = s+1;
  1925. while(*t <= ' ' && *t > 0)
  1926. t++;
  1927. if ( strncmp(t, "cnf", 3) != 0 )
  1928. return 0;
  1929. t+=3;
  1930. while(*t <= ' ' && *t > 0)
  1931. t++;
  1932. pinfoSetInCnt(pi, atoi(t));
  1933. cnt = atoi(t);
  1934. is_init = 1;
  1935. dcInSetAll(pi, c, CUBE_IN_MASK_DC);
  1936. dcOutSetAll(pi, c, 0);
  1937. dcSetOut(c, 0, 1);
  1938. }
  1939. else if ( is_init != 0 )
  1940. {
  1941. t = s;
  1942. for(;;)
  1943. {
  1944. while(*t <= ' ' && *t > 0)
  1945. t++;
  1946. if ( *t == '\0' )
  1947. break;
  1948. pos = strtol(t, &t, 10);
  1949. if ( pos == 0 )
  1950. {
  1951. if ( cl != NULL )
  1952. if ( pi->out_cnt == 0 || dcIsOutIllegal(pi, c) == 0 )
  1953. if ( dclAdd(pi, cl, c) < 0 )
  1954. return 0;
  1955. dcInSetAll(pi, c, CUBE_IN_MASK_DC);
  1956. dcOutSetAll(pi, c, 0);
  1957. dcSetOut(c, 0, 1);
  1958. }
  1959. else if ( pos < 0 )
  1960. {
  1961. dcSetIn(c, -pos-1, 1);
  1962. }
  1963. else
  1964. {
  1965. dcSetIn(c, pos-1, 2);
  1966. }
  1967. }
  1968. }
  1969. }
  1970. return 1;
  1971. }
  1972. /*-- dclReadCNF -------------------------------------------------------------*/
  1973. int dclReadCNF(pinfo *pi, dclist cl, const char *filename)
  1974. {
  1975. FILE *fp;
  1976. int ret;
  1977. fp = b_fopen(filename, NULL, ".cnf", "r");
  1978. if ( fp == NULL )
  1979. return 0;
  1980. ret = dclReadCNFFP(pi, cl, fp);
  1981. fclose(fp);
  1982. return ret;
  1983. }
  1984. /*-- dclReadPLAFP -----------------------------------------------------------*/
  1985. int dclReadPLAFP(pinfo *pi, dclist cl_on, dclist cl_dc, FILE *fp)
  1986. {
  1987. static char buf[DCL_LINE_LEN];
  1988. static char *s = buf;
  1989. dcube *c_on = &(pi->tmp[3]);
  1990. dcube *c_dc = &(pi->tmp[4]);
  1991. int is_init = 0;
  1992. if ( cl_on != NULL )
  1993. dclRealClear(cl_on);
  1994. if ( cl_dc != NULL )
  1995. dclRealClear(cl_dc);
  1996. for(;;)
  1997. {
  1998. s = buf;
  1999. if ( fgets(s, DCL_LINE_LEN, fp) == NULL )
  2000. break;
  2001. if ( s[0] == '#' )
  2002. {
  2003. /* nothing */
  2004. }
  2005. else if ( s[0] == '\0' )
  2006. {
  2007. /* nothing */
  2008. }
  2009. else if ( s[0] == '.' && is_init == 0 )
  2010. {
  2011. if ( strncmp(s, ".ilb ", 5) == 0 )
  2012. {
  2013. /* input label names */
  2014. if ( pinfoImportInLabels(pi, s+5, " \t\n\r") == 0 )
  2015. return 0;
  2016. }
  2017. else if ( strncmp(s, ".olb ", 5) == 0 )
  2018. {
  2019. /* this is not a valid espresso command... */
  2020. /* output label names */
  2021. if ( pinfoImportOutLabels(pi, s+5, " \t\n\r") == 0 )
  2022. return 0;
  2023. }
  2024. else if ( strncmp(s, ".ob ", 4) == 0 )
  2025. {
  2026. /* output label names */
  2027. if ( pinfoImportOutLabels(pi, s+4, " \t\n\r") == 0 )
  2028. return 0;
  2029. }
  2030. else if ( strncmp(s, ".i ", 3) == 0 )
  2031. pinfoSetInCnt(pi, atoi(s+3));
  2032. else if ( strncmp(s, ".o ", 3) == 0 )
  2033. pinfoSetOutCnt(pi, atoi(s+3));
  2034. }
  2035. else if ( s[0] == '.' )
  2036. {
  2037. /* ignore */
  2038. }
  2039. else if ( s[0] != '\0' )
  2040. {
  2041. is_init = 1;
  2042. if ( pi->in_cnt == 0 || pi->out_cnt == 0 )
  2043. return 0;
  2044. while( (*s) > '\0' && (*s) < ' ' )
  2045. s++;
  2046. if ( *s == '\0' )
  2047. continue;
  2048. s = dcSetAllByStr(pi, pi->in_cnt, pi->out_cnt, c_on, c_dc, s);
  2049. if ( s == NULL )
  2050. return 0;
  2051. while( (*s) > '\0' && (*s) < ' ' )
  2052. s++;
  2053. if ( *s != '#' && *s != '\0' )
  2054. return 0;
  2055. if ( cl_on != NULL )
  2056. if ( pi->out_cnt == 0 || dcIsOutIllegal(pi, c_on) == 0 )
  2057. if ( dclAdd(pi, cl_on, c_on) < 0 )
  2058. return 0;
  2059. if ( cl_dc != NULL )
  2060. if ( dcIsOutIllegal(pi, c_dc) == 0 )
  2061. if ( dclAdd(pi, cl_dc, c_dc) < 0 )
  2062. return 0;
  2063. }
  2064. }
  2065. if ( cl_dc != NULL && cl_on != NULL )
  2066. if ( dclSubtract(pi, cl_on, cl_dc) == 0 )
  2067. return 0;
  2068. return 1;
  2069. }
  2070. /*-- dclReadPLAStr ----------------------------------------------------------*/
  2071. int dclReadPLAStr(pinfo *pi, dclist cl_on, dclist cl_dc, const char **t)
  2072. {
  2073. static char s[DCL_LINE_LEN];
  2074. dcube *c_on = &(pi->tmp[3]);
  2075. dcube *c_dc = &(pi->tmp[4]);
  2076. int is_init = 0;
  2077. if ( cl_on != NULL )
  2078. dclRealClear(cl_on);
  2079. if ( cl_dc != NULL )
  2080. dclRealClear(cl_dc);
  2081. for(;;)
  2082. {
  2083. if ( *t == NULL )
  2084. break;
  2085. strcpy(s, *t);
  2086. t++;
  2087. if ( s[0] == '#' )
  2088. {
  2089. /* nothing */
  2090. }
  2091. else if ( s[0] == '.' && is_init == 0 )
  2092. {
  2093. if ( strncmp(s, ".i ", 3) == 0 )
  2094. pinfoSetInCnt(pi, atoi(s+3));
  2095. else if ( strncmp(s, ".o ", 3) == 0 )
  2096. pinfoSetOutCnt(pi, atoi(s+3));
  2097. }
  2098. else if ( s[0] == '.' )
  2099. {
  2100. /* ignore */
  2101. }
  2102. else
  2103. {
  2104. is_init = 1;
  2105. if ( dcSetAllByStr(pi, pi->in_cnt, pi->out_cnt, c_on, c_dc, s) == 0 )
  2106. return 0;
  2107. if ( cl_on != NULL )
  2108. if ( dcIsOutIllegal(pi, c_on) == 0 )
  2109. if ( dclAdd(pi, cl_on, c_on) < 0 )
  2110. return 0;
  2111. if ( cl_dc != NULL )
  2112. if ( dcIsOutIllegal(pi, c_dc) == 0 )
  2113. if ( dclAdd(pi, cl_dc, c_dc) < 0 )
  2114. return 0;
  2115. }
  2116. }
  2117. if ( cl_dc != NULL && cl_on != NULL )
  2118. if ( dclSubtract(pi, cl_on, cl_dc) == 0 )
  2119. return 0;
  2120. return 1;
  2121. }
  2122. /*-- dclReadFile ------------------------------------------------------------*/
  2123. int dclReadFile(pinfo *pi, dclist cl, const char *filename)
  2124. {
  2125. FILE *fp;
  2126. int ret;
  2127. fp = b_fopen(filename, NULL, ".pla", "r");
  2128. if ( fp == NULL )
  2129. return 0;
  2130. ret = dclReadFP(pi, cl, fp);
  2131. fclose(fp);
  2132. return ret;
  2133. }
  2134. /*-- dclReadDichotomyFP -----------------------------------------------------*/
  2135. int dclReadDichotomyFP(pinfo *pi, dclist cl_on, FILE *fp)
  2136. {
  2137. static char buf[DCL_LINE_LEN];
  2138. static char *s = buf;
  2139. dcube *c_on = &(pi->tmp[3]);
  2140. int is_init = 0;
  2141. if ( cl_on != NULL )
  2142. dclRealClear(cl_on);
  2143. for(;;)
  2144. {
  2145. s = buf;
  2146. if ( fgets(s, DCL_LINE_LEN, fp) == NULL )
  2147. break;
  2148. while( *s == ' ' || *s == '\t' )
  2149. s++;
  2150. if ( s[0] == '#' )
  2151. {
  2152. /* nothing */
  2153. }
  2154. else if ( s[0] == '\0' )
  2155. {
  2156. /* nothing */
  2157. }
  2158. else
  2159. {
  2160. while( *s != '\0' && *s != ':' )
  2161. s++;
  2162. if ( *s == '\0' )
  2163. return 0;
  2164. s++;
  2165. while( (*s) > '\0' && (*s) <= ' ' )
  2166. s++;
  2167. if ( is_init == 0 )
  2168. {
  2169. int i = 0;
  2170. while( s[i] == '-' || s[i] == '1' || s[i] == '0' )
  2171. i++;
  2172. pinfoSetInCnt(pi, i);
  2173. pinfoSetOutCnt(pi, 0);
  2174. is_init = 1;
  2175. }
  2176. if ( dcSetByStr(pi, c_on, s) == 0 )
  2177. return 0;
  2178. if ( cl_on != NULL )
  2179. if ( dclAdd(pi, cl_on, c_on) < 0 )
  2180. return 0;
  2181. }
  2182. }
  2183. return 1;
  2184. }
  2185. /*-- dclReadDichotomy -------------------------------------------------------*/
  2186. int dclReadDichotomy(pinfo *pi, dclist cl_on, const char *filename)
  2187. {
  2188. FILE *fp;
  2189. int ret;
  2190. fp = b_fopen(filename, NULL, ".dichot", "r");
  2191. if ( fp == NULL )
  2192. return 0;
  2193. ret = dclReadDichotomyFP(pi, cl_on, fp);
  2194. fclose(fp);
  2195. return ret;
  2196. }
  2197. int IsValidDichotomyFile(const char *filename)
  2198. {
  2199. pinfo pi;
  2200. int ret;
  2201. if ( pinfoInit(&pi) == 0 )
  2202. return 0;
  2203. ret = dclReadDichotomy(&pi, NULL, filename);
  2204. pinfoDestroy(&pi);
  2205. return ret;
  2206. }
  2207. /*-- dclReadPLA -------------------------------------------------------------*/
  2208. int dclReadPLA(pinfo *pi, dclist cl_on, dclist cl_dc, const char *filename)
  2209. {
  2210. FILE *fp;
  2211. int ret;
  2212. fp = b_fopen(filename, NULL, ".pla", "r");
  2213. if ( fp == NULL )
  2214. return 0;
  2215. ret = dclReadPLAFP(pi, cl_on, cl_dc, fp);
  2216. fclose(fp);
  2217. return ret;
  2218. }
  2219. int IsValidPLAFile(const char *filename)
  2220. {
  2221. pinfo pi;
  2222. int ret;
  2223. if ( pinfoInit(&pi) == 0 )
  2224. return 0;
  2225. ret = dclReadPLA(&pi, NULL, NULL, filename);
  2226. pinfoDestroy(&pi);
  2227. return ret;
  2228. }
  2229. /*!
  2230. \ingroup dclist
  2231. The representation of a boolean function contains three parts:
  2232. -# The problem info structure.
  2233. -# The ON-set of the boolean function (\a cl).
  2234. -# An optional DC (don't care) Set of the boolean function (\a cl_dc).
  2235. This function reads the contents for these parts from a file
  2236. with the name \a filename. The argument \a cl_dc can be set to \c NULL,
  2237. if one does not care about the DC-set.
  2238. \param pi The problem info structure for the boolean functions.
  2239. This argument will be modified.
  2240. \param cl The ON-set of the boolean function.
  2241. This argument will be modified.
  2242. \param cl_dc The DC-set of the boolean function.
  2243. This argument will be modified. The value \c NULL is valid for this
  2244. argument.
  2245. \param filename A file with a description of boolean functions.
  2246. \return 0, if an error occured.
  2247. \warning This function may take a very long time for certain descriptions.
  2248. \see gnc_SynthDCL()
  2249. */
  2250. int dclImport(pinfo *pi, dclist cl_on, dclist cl_dc, const char *filename)
  2251. {
  2252. if ( IsValidPLAFile(filename) != 0 )
  2253. return dclReadPLA(pi, cl_on, cl_dc, filename);
  2254. if ( IsValidNEXFile(filename) != 0 ) /* nex.c */
  2255. return dclReadNEX(pi, cl_on, cl_dc, filename); /* nex.c */
  2256. if ( IsValidBEXFile(filename) != 0 ) /* dcex.c */
  2257. return dclReadBEX(pi, cl_on, cl_dc, filename); /* dcex.c */
  2258. if ( IsValidDichotomyFile(filename) != 0 )
  2259. return dclReadDichotomy(pi, cl_on, filename);
  2260. return 0;
  2261. }
  2262. int IsValidDCLFile(const char *filename)
  2263. {
  2264. if ( IsValidNEXFile(filename) != 0 )
  2265. return 1;
  2266. if ( IsValidPLAFile(filename) != 0 )
  2267. return 1;
  2268. if ( IsValidBEXFile(filename) != 0 ) /* dcex.c */
  2269. return 1;
  2270. if ( IsValidDichotomyFile(filename) != 0 )
  2271. return 1;
  2272. return 0;
  2273. }
  2274. /*-- dclWritePLA ------------------------------------------------------------*/
  2275. int dclWritePLA(pinfo *pi, dclist cl, const char *filename)
  2276. {
  2277. int i, cnt = dclCnt(cl);
  2278. FILE *fp;
  2279. fp = fopen(filename, "w");
  2280. if ( fp == NULL )
  2281. return 0;
  2282. if ( fprintf(fp, ".i %d\n", pi->in_cnt) < 0 ) return fclose(fp), 0;
  2283. if ( fprintf(fp, ".o %d\n", pi->out_cnt) < 0 ) return fclose(fp), 0;
  2284. if ( pi->in_sl != NULL )
  2285. {
  2286. if ( fprintf(fp, ".ilb ") < 0 ) return fclose(fp), 0;
  2287. if ( b_sl_ExportToFP(pi->in_sl, fp, " ", "\n") == 0 ) return fclose(fp), 0;
  2288. }
  2289. if ( pi->out_sl != NULL )
  2290. {
  2291. if ( fprintf(fp, ".ob ") < 0 ) return fclose(fp), 0;
  2292. if ( b_sl_ExportToFP(pi->out_sl, fp, " ", "\n") == 0 ) return fclose(fp), 0;
  2293. }
  2294. if ( fprintf(fp, ".p %d\n", cnt) < 0 ) return fclose(fp), 0;
  2295. for( i = 0; i < cnt; i++ )
  2296. if ( fprintf(fp, "%s\n", dcToStr(pi, dclGet(cl, i), " ", "")) < 0 )
  2297. return fclose(fp), 0;
  2298. return fclose(fp), 1;
  2299. }
  2300. /*-- dclShow ----------------------------------------------------------------*/
  2301. void dclShow(pinfo *pi, dclist cl)
  2302. {
  2303. int i, cnt = dclCnt(cl);
  2304. if ( pi->in_sl != NULL )
  2305. {
  2306. if ( fprintf(stdout, ".ilb ") < 0 ) return;
  2307. if ( b_sl_ExportToFP(pi->in_sl, stdout, " ", "\n") == 0 ) return;
  2308. }
  2309. if ( pi->out_sl != NULL )
  2310. {
  2311. if ( fprintf(stdout, ".ob ") < 0 ) return;
  2312. if ( b_sl_ExportToFP(pi->out_sl, stdout, " ", "\n") == 0 ) return;
  2313. }
  2314. for( i = 0; i < cnt; i++ )
  2315. puts(dcToStr(pi, dclGet(cl, i), " ", ""));
  2316. }
  2317. /*-- dclSetOutAll -----------------------------------------------------------*/
  2318. void dclSetOutAll(pinfo *pi, dclist cl, c_int v)
  2319. {
  2320. int i, cnt = dclCnt(cl);
  2321. for( i = 0; i < cnt; i++ )
  2322. dcOutSetAll(pi, dclGet(cl, i), v);
  2323. }
  2324. /*-- dclDontCareExpand ------------------------------------------------------*/
  2325. int dclDontCareExpand(pinfo *pi, dclist cl)
  2326. {
  2327. int i, j, k, cnt;
  2328. for( i = 0; i < pi->in_cnt; i++ )
  2329. {
  2330. cnt = dclCnt(cl);
  2331. for( j = 0; j < cnt; j++ )
  2332. {
  2333. if ( dcGetIn(dclGet(cl, j), i) == 3 )
  2334. {
  2335. k = dclAdd(pi, cl, dclGet(cl, j));
  2336. if ( k < 0 )
  2337. return 0;
  2338. dcSetIn(dclGet(cl, j), i, 1);
  2339. dcSetIn(dclGet(cl, k), i, 2);
  2340. }
  2341. }
  2342. }
  2343. return 1;
  2344. }
  2345. /*-- dclOutExpand -----------------------------------------------------------*/
  2346. int dclOutExpand(pinfo *pi, dclist cl)
  2347. {
  2348. int i, j, k, cnt;
  2349. int state;
  2350. cnt = dclCnt(cl);
  2351. for( j = 0; j < cnt; j++ )
  2352. {
  2353. state = 0;
  2354. for( i = 0; i < pi->out_cnt; i++ )
  2355. {
  2356. if ( dcGetOut(dclGet(cl, j), i) == 1 )
  2357. {
  2358. if ( state == 0 )
  2359. {
  2360. state = 1;
  2361. }
  2362. else
  2363. {
  2364. k = dclAdd(pi, cl, dclGet(cl, j));
  2365. if ( k < 0 )
  2366. return 0;
  2367. dcOutSetAll(pi, dclGet(cl, k), 0);
  2368. dcSetOut(dclGet(cl, k), i, 1);
  2369. dcSetOut(dclGet(cl, j), i, 0);
  2370. }
  2371. }
  2372. }
  2373. }
  2374. return 1;
  2375. }
  2376. /*-- dclExpand1 -------------------------------------------------------------*/
  2377. static void dclExpand1(pinfo *pi, dclist cl, dclist cl_off)
  2378. {
  2379. int i, cnt = dclCnt(cl);
  2380. for( i = 0; i < cnt; i++ )
  2381. dcExpand1(pi, dclGet(cl, i), cl_off);
  2382. }
  2383. /*-- dclIsSingleSubSet ------------------------------------------------------*/
  2384. int dclIsSingleSubSet(pinfo *pi, dclist cl, dcube *c)
  2385. {
  2386. int i, cnt = dclCnt(cl);
  2387. for( i = 0; i < cnt; i++ )
  2388. if ( dcIsSubSet(pi, dclGet(cl, i), c) != 0 )
  2389. return 1;
  2390. return 0;
  2391. }
  2392. /*-- dclGetOutput ------------------------------------------------------*/
  2393. /* input: input part of c */
  2394. /* result: output part of c */
  2395. void dclGetOutput(pinfo *pi, dclist cl, dcube *c)
  2396. {
  2397. int i, cnt = dclCnt(cl);
  2398. dcOutSetAll(pi, c, 0);
  2399. for( i = 0; i < cnt; i++ )
  2400. if ( dcIsInSubSet(pi, dclGet(cl, i), c) != 0 )
  2401. dcOrOut(pi, c, c, dclGet(cl, i));
  2402. }
  2403. /*-- dclSharp ---------------------------------------------------------------*/
  2404. /* Results are added to cl */
  2405. int dclSharp(pinfo *pi, dclist cl, dcube *a, dcube *b)
  2406. {
  2407. int k;
  2408. for( k = 0; k < pi->in_cnt; k++ )
  2409. if ( dcSharpIn(pi, &(pi->tmp[1]), a, b, k) != 0 )
  2410. if ( dclAdd(pi, cl, &(pi->tmp[1])) < 0 )
  2411. return 0;
  2412. if ( dcSharpOut(pi, &(pi->tmp[1]), a, b) != 0 )
  2413. if ( dclAdd(pi, cl, &(pi->tmp[1])) < 0 )
  2414. return 0;
  2415. return 1;
  2416. }
  2417. /*-- dclD1Sharp -------------------------------------------------------------*/
  2418. /* Die Ergebnisse werden an die Liste cl angehaengt */
  2419. int dclD1Sharp(pinfo *pi, dclist cl, dcube *a, dcube *b)
  2420. {
  2421. int k;
  2422. for( k = 0; k < pi->in_cnt; k++ )
  2423. if ( dcD1SharpIn(pi, &(pi->tmp[1]), a, b, k) != 0 )
  2424. if ( dclAdd(pi, cl, &(pi->tmp[1])) < 0 )
  2425. return 0;
  2426. if ( dcD1SharpOut(pi, &(pi->tmp[1]), a, b) != 0 )
  2427. if ( dclAdd(pi, cl, &(pi->tmp[1])) < 0 )
  2428. return 0;
  2429. return 1;
  2430. }
  2431. /*-- dclAddDistance1 --------------------------------------------------------*/
  2432. int dclAddDistance1(pinfo *pi, dclist dest, dclist src)
  2433. {
  2434. int i, cnt = dclCnt(src);
  2435. for( i = 0; i < cnt; i++ )
  2436. if ( dclD1Sharp(pi, dest, &(pi->tmp[0]), dclGet(src, i)) == 0 )
  2437. return 0;
  2438. dclSubtract(pi, dest, src);
  2439. return 1;
  2440. }
  2441. /*-- dclDistance1 -----------------------------------------------------------*/
  2442. int dclDistance1(pinfo *pi, dclist dest, dclist src)
  2443. {
  2444. dclClear(dest);
  2445. return dclAddDistance1(pi, dest, src);
  2446. }
  2447. /*-- dclDistance1Cube -------------------------------------------------------*/
  2448. int dclDistance1Cube(pinfo *pi, dclist dest, dcube *c)
  2449. {
  2450. dclClear(dest);
  2451. if ( dclD1Sharp(pi, dest, &(pi->tmp[0]), c) == 0 )
  2452. return 0;
  2453. if ( dclSubtractCube(pi, dest, c) == 0 )
  2454. return 0;
  2455. return 1;
  2456. }
  2457. /*-- dclRestrictByDistance1 -------------------------------------------------*/
  2458. /* a = intersection(a, distance1(b)) */
  2459. int dclRestrictByDistance1(pinfo *pi, dclist a, dclist b)
  2460. {
  2461. dclist d1, aa;
  2462. if ( dclInitCachedVA(pi,2, &d1, &aa) == 0 )
  2463. return 0;
  2464. if ( dclDistance1(pi, d1, b) == 0 )
  2465. return dclDestroyCachedVA(pi, 2, d1, aa), 0;
  2466. if ( dclCopy(pi, aa, a) == 0 )
  2467. return dclDestroyCachedVA(pi, 2, d1, aa), 0;
  2468. dclClear(a);
  2469. if ( dclIntersectionList(pi, a, aa, d1) == 0 )
  2470. return dclDestroyCachedVA(pi, 2, d1, aa), 0;
  2471. return dclDestroyCachedVA(pi, 2, d1, aa), 1;
  2472. }
  2473. /*-- dclSCCSharpAndSetFlag --------------------------------------------------*/
  2474. /* Die Ergebnisse werden an die Liste cl angehaengt */
  2475. int dclSCCSharpAndSetFlag(pinfo *pi, dclist cl, dcube *a, dcube *b)
  2476. {
  2477. int k;
  2478. for( k = 0; k < pi->in_cnt; k++ )
  2479. if ( dcSharpIn(pi, &(pi->tmp[1]), a, b, k) != 0 )
  2480. if ( dclSCCAddAndSetFlag(pi, cl, &(pi->tmp[1])) == 0 )
  2481. return 0;
  2482. if ( dcSharpOut(pi, &(pi->tmp[1]), a, b) != 0 )
  2483. if ( dclSCCAddAndSetFlag(pi, cl, &(pi->tmp[1])) == 0 )
  2484. return 0;
  2485. return 1;
  2486. }
  2487. /*-- dclComplementCube ------------------------------------------------------*/
  2488. /* Berechnet das complement eines cubes. */
  2489. /* Ergebnisse werden angehaengt. */
  2490. int dclComplementCube(pinfo *pi, dclist cl, dcube *c)
  2491. {
  2492. return dclSharp(pi, cl, &(pi->tmp[0]), c);
  2493. }
  2494. /*-- dclIntersection --------------------------------------------------------*/
  2495. /* nach dieser Operation ist die SCC eigenschaft NICHT mehr erfuellt */
  2496. int dclIntersection(pinfo *pi, dclist cl, dcube *c)
  2497. {
  2498. int i, cnt = dclCnt(cl);
  2499. if ( dclClearFlags(cl) == 0 )
  2500. return 0;
  2501. for( i = 0; i < cnt; i++ )
  2502. {
  2503. if ( dcIntersection(pi, dclGet(cl, i), dclGet(cl, i), c) == 0 )
  2504. dclSetFlag(cl, i);
  2505. }
  2506. dclDeleteCubesWithFlag(pi, cl);
  2507. return 1;
  2508. }
  2509. /*-- dclIntersectionCube ----------------------------------------------------*/
  2510. /* nach dieser Operation ist die SCC eigenschaft NICHT mehr erfuellt */
  2511. int dclIntersectionCube(pinfo *pi, dclist dest, dclist src, dcube *c)
  2512. {
  2513. int i, cnt = dclCnt(src);
  2514. dclClear(dest);
  2515. for( i = 0; i < cnt; i++ )
  2516. {
  2517. if ( dcIntersection(pi, &(pi->tmp[1]), dclGet(src, i), c) != 0 )
  2518. if ( dclAdd(pi, dest, &(pi->tmp[1])) < 0 )
  2519. return 0;
  2520. }
  2521. return 1;
  2522. }
  2523. /*-- dclSCCIntersectionCube -------------------------------------------------*/
  2524. /* nach dieser Operation ist die SCC eigenschaft erfuellt */
  2525. int dclSCCIntersectionCube(pinfo *pi, dclist dest, dclist src, dcube *c)
  2526. {
  2527. int i, cnt = dclCnt(src);
  2528. dclClear(dest);
  2529. if ( dclClearFlags(dest) == 0 )
  2530. return 0;
  2531. for( i = 0; i < cnt; i++ )
  2532. {
  2533. if ( dcIntersection(pi, &(pi->tmp[1]), dclGet(src, i), c) != 0 )
  2534. if ( dclSCCAddAndSetFlag(pi, dest, &(pi->tmp[1])) == 0 )
  2535. return 0;
  2536. }
  2537. dclDeleteCubesWithFlag(pi, dest);
  2538. return 1;
  2539. }
  2540. /*-- dclIntersectionList ----------------------------------------------------*/
  2541. int dclIntersectionList(pinfo *pi, dclist dest, dclist a, dclist b)
  2542. {
  2543. int a_pos, a_cnt = dclCnt(a);
  2544. int b_pos, b_cnt = dclCnt(b);
  2545. dclClear(dest);
  2546. if ( dclClearFlags(dest) == 0 )
  2547. return 0;
  2548. for ( a_pos = 0; a_pos < a_cnt; a_pos++ )
  2549. {
  2550. for ( b_pos = 0; b_pos < b_cnt; b_pos++ )
  2551. if ( dcIntersection(pi, &(pi->tmp[1]), dclGet(a, a_pos), dclGet(b, b_pos)) != 0 )
  2552. if ( dclSCCAddAndSetFlag(pi, dest, &(pi->tmp[1])) == 0 )
  2553. return 0;
  2554. }
  2555. dclDeleteCubesWithFlag(pi, dest);
  2556. return 1;
  2557. }
  2558. /*-- dclConvertFromPOS ------------------------------------------------------*/
  2559. /*
  2560. Interprets the cubes in 'src' as OR-Terms, so 'src' is assumed to
  2561. be a product of sums.
  2562. This function converts the product of sums to a sum of product form.
  2563. */
  2564. int dclConvertFromPOS(pinfo *pi, dclist cl)
  2565. {
  2566. int i, cnt = dclCnt(cl);
  2567. int j;
  2568. int v;
  2569. dcube *c;
  2570. dclist a, b, o;
  2571. if ( cnt == 0 )
  2572. return 1;
  2573. if ( dclInitVA(3, &a, &b, &o) == 0 )
  2574. return 0;
  2575. for( i = 0; i < cnt; i++ )
  2576. {
  2577. /* convert OR-cube into a cube list (stored in 'o') */
  2578. dclClear(o);
  2579. for( j = 0; j < pi->in_cnt; j++ )
  2580. {
  2581. v = dcGetIn(dclGet(cl, i), j);
  2582. if ( v != 3 )
  2583. {
  2584. c = dclAddEmptyCube(pi, o);
  2585. if ( c == NULL )
  2586. return dclDestroyVA(3, a, b, o), 0;
  2587. dcCopyOut(pi, c, dclGet(cl, i));
  2588. dcInSetAll(pi, c, CUBE_IN_MASK_DC);
  2589. dcSetIn(c, j, v);
  2590. }
  2591. }
  2592. /* calculate the intersection between 'o' and the result so far.. */
  2593. if ( i == 0 )
  2594. {
  2595. if ( dclCopy(pi, a, o) == 0 )
  2596. return dclDestroyVA(3, a, b, o), 0;
  2597. }
  2598. else
  2599. {
  2600. if ( dclCopy(pi, b, a) == 0 )
  2601. return dclDestroyVA(3, a, b, o), 0;
  2602. dclClear(a);
  2603. if ( dclIntersectionList(pi, a, b, o) == 0 )
  2604. return dclDestroyVA(3, a, b, o), 0;
  2605. }
  2606. }
  2607. if ( dclCopy(pi, cl, a) == 0 )
  2608. return dclDestroyVA(3, a, b, o), 0;
  2609. return dclDestroyVA(3, a, b, o), 1;
  2610. }
  2611. /*-- dclIsIntersection ------------------------------------------------------*/
  2612. int dclIsIntersection(pinfo *pi, dclist a, dclist b)
  2613. {
  2614. int a_pos, a_cnt = dclCnt(a);
  2615. int b_pos, b_cnt = dclCnt(b);
  2616. for ( a_pos = 0; a_pos < a_cnt; a_pos++ )
  2617. for ( b_pos = 0; b_pos < b_cnt; b_pos++ )
  2618. if ( dcIntersection(pi, &(pi->tmp[1]), dclGet(a, a_pos), dclGet(b, b_pos)) != 0 )
  2619. return 1;
  2620. return 0;
  2621. }
  2622. /*-- dclIsIntersectionCube --------------------------------------------------*/
  2623. int dclIsIntersectionCube(pinfo *pi, dclist cl, dcube *c)
  2624. {
  2625. int i, cnt = dclCnt(cl);
  2626. for ( i = 0; i < cnt; i++ )
  2627. if ( dcIntersection(pi, &(pi->tmp[1]), dclGet(cl, i), c) != 0 )
  2628. return 1;
  2629. return 0;
  2630. }
  2631. /*-- dclIntersectionListInv -------------------------------------------------*/
  2632. int dclIntersectionListInv(pinfo *pi, dclist dest, dclist a, dclist b)
  2633. {
  2634. int a_pos, a_cnt = dclCnt(a);
  2635. int b_pos, b_cnt = dclCnt(b);
  2636. dcube *tc = &(pi->tmp[1]);
  2637. dcube *ac;
  2638. dclClear(dest);
  2639. if ( dclClearFlags(dest) == 0 )
  2640. return 0;
  2641. for ( a_pos = 0; a_pos < a_cnt; a_pos++ )
  2642. {
  2643. ac = dclGet(a, a_pos);
  2644. for ( b_pos = 0; b_pos < b_cnt; b_pos++ )
  2645. if ( dcIntersection(pi, tc, ac, dclGet(b, b_pos)) != 0 )
  2646. if ( dclSCCInvAddAndSetFlag(pi, dest, tc) == 0 )
  2647. return 0;
  2648. }
  2649. dclDeleteCubesWithFlag(pi, dest);
  2650. return 1;
  2651. }
  2652. /*-- dclAndElements ---------------------------------------------------------*/
  2653. void dclAndElements(pinfo *pi, dcube *r, dclist cl)
  2654. {
  2655. int i, cnt = dclCnt(cl);
  2656. dcSetTautology(pi, r);
  2657. for( i = 0; i < cnt; i++ )
  2658. dcAnd(pi, r, r, dclGet(cl, i));
  2659. }
  2660. /*-- dclOrElements ----------------------------------------------------------*/
  2661. void dclOrElements(pinfo *pi, dcube *r, dclist cl)
  2662. {
  2663. int i, cnt = dclCnt(cl);
  2664. dcInSetAll(pi, r, 0);
  2665. dcOutSetAll(pi, r, 0);
  2666. for( i = 0; i < cnt; i++ )
  2667. dcOr(pi, r, r, dclGet(cl, i));
  2668. }
  2669. /*-- dclResult --------------------------------------------------------------*/
  2670. /* calculate the output of 'r' by checking the input of 'r' */
  2671. void dclResult(pinfo *pi, dcube *r, dclist cl)
  2672. {
  2673. int i, cnt = dclCnt(cl);
  2674. dcOutSetAll(pi, r, 0);
  2675. for( i = 0; i < cnt; i++ )
  2676. if ( dcIsInSubSet(pi, dclGet(cl, i), r) != 0 )
  2677. dcOrOut(pi, r, r, dclGet(cl, i));
  2678. }
  2679. /*-- dclResultList ----------------------------------------------------------*/
  2680. /* calculate the output of 'r' by checking the input of 'r' */
  2681. int dclResultList(pinfo *pi, dclist r, dclist cl)
  2682. {
  2683. int i, cnt;
  2684. cnt = dclCnt(r);
  2685. for( i = 0; i < cnt; i++ )
  2686. dcSetOutTautology(pi, dclGet(r, i));
  2687. if ( dclDontCareExpand(pi, r) == 0 )
  2688. return 0; /* memory error */
  2689. dclSCC(pi, r);
  2690. cnt = dclCnt(r);
  2691. for( i = 0; i < cnt; i++ )
  2692. dclResult(pi, dclGet(r, i), cl);
  2693. return 1; /* no memory error */
  2694. }
  2695. /*-- dclSuper2 --------------------------------------------------------------*/
  2696. void dclSuper2(pinfo *pi, dcube *r, dclist cl1, dclist cl2)
  2697. {
  2698. int i, cnt;
  2699. dcInSetAll(pi, r, 0);
  2700. dcOutSetAll(pi, r, 0);
  2701. cnt = dclCnt(cl1);
  2702. for( i = 0; i < cnt; i++ )
  2703. dcOr(pi, r, r, dclGet(cl1, i));
  2704. cnt = dclCnt(cl2);
  2705. for( i = 0; i < cnt; i++ )
  2706. dcOr(pi, r, r, dclGet(cl2, i));
  2707. }
  2708. /*-- dclAndLocalElements ----------------------------------------------------*/
  2709. /* obsolete? --> dcubehf */
  2710. int dclAndLocalElements(pinfo *pi, dcube *r, dcube *m, dclist cl)
  2711. {
  2712. int i, cnt = dclCnt(cl);
  2713. dcSetTautology(pi, r);
  2714. for( i = 0; i < cnt; i++ )
  2715. if ( dcIsDeltaNoneZero(pi, m, dclGet(cl, i)) == 0 )
  2716. dcAnd(pi, r, r, dclGet(cl, i));
  2717. return 1;
  2718. }
  2719. /*-- dclSCCCofactor ---------------------------------------------------------*/
  2720. int dclAddSubCofactor(pinfo *pi, dclist dest, dclist src, dcube *cofactor)
  2721. {
  2722. int i, cnt = dclCnt(src);
  2723. dcube *r = &(pi->tmp[1]);
  2724. for ( i = 0; i < cnt; i++ )
  2725. if ( dcSubCofactor(pi, r, dclGet(src, i), cofactor) != 0 )
  2726. if ( dclAdd(pi, dest, r) < 0 )
  2727. return 0;
  2728. return 1;
  2729. }
  2730. int dclAddRedCofactor(pinfo *pi, dclist dest, dclist src, dcube *cofactor)
  2731. {
  2732. int i, cnt = dclCnt(src);
  2733. dcube *r = &(pi->tmp[1]);
  2734. for ( i = 0; i < cnt; i++ )
  2735. if ( dcRedCofactor(pi, r, dclGet(src, i), cofactor) != 0 )
  2736. if ( dclAdd(pi, dest, r) < 0 )
  2737. return 0;
  2738. return 1;
  2739. }
  2740. int xdcl_Cofactor(pinfo *pi, dclist dest, dclist src, dcube *cofactor)
  2741. {
  2742. dclClear(dest);
  2743. if ( dclAddRedCofactor(pi, dest, src, cofactor) == 0 )
  2744. return 0;
  2745. if ( dclSCC(pi, dest) == 0 )
  2746. return 0;
  2747. if ( dclAddSubCofactor(pi, dest, src, cofactor) == 0 )
  2748. return 0;
  2749. return 1;
  2750. }
  2751. int dclSCCCofactor(pinfo *pi, dclist dest, dclist src, dcube *cofactor)
  2752. {
  2753. int i, cnt = dclCnt(src);
  2754. dcube *r = &(pi->tmp[1]);
  2755. dclClear(dest);
  2756. if ( dclClearFlags(dest) == 0 )
  2757. return 0;
  2758. for ( i = 0; i < cnt; i++ )
  2759. if ( dcCofactor(pi, r, dclGet(src, i), cofactor) != 0 )
  2760. if ( dclSCCAddAndSetFlag(pi, dest, r) == 0 )
  2761. return 0;
  2762. dclDeleteCubesWithFlag(pi, dest);
  2763. return 1;
  2764. }
  2765. int dclSCCCofactorExcept(pinfo *pi, dclist dest, dclist src, dcube *cofactor, int src_except_pos)
  2766. {
  2767. int i, cnt = dclCnt(src);
  2768. dcube *r = &(pi->tmp[1]);
  2769. dclClear(dest);
  2770. if ( dclClearFlags(dest) == 0 )
  2771. return 0;
  2772. for ( i = 0; i < src_except_pos; i++ )
  2773. if ( dcCofactor(pi, r, dclGet(src, i), cofactor) != 0 )
  2774. if ( dclSCCAddAndSetFlag(pi, dest, r) == 0 )
  2775. return 0;
  2776. for ( i = src_except_pos+1; i < cnt; i++ )
  2777. if ( dcCofactor(pi, r, dclGet(src, i), cofactor) != 0 )
  2778. if ( dclSCCAddAndSetFlag(pi, dest, r) == 0 )
  2779. return 0;
  2780. dclDeleteCubesWithFlag(pi, dest);
  2781. return 1;
  2782. }
  2783. /*-- dclSCCInvCofactor ------------------------------------------------------*/
  2784. int dclSCCInvCofactor(pinfo *pi, dclist dest, dclist src, dcube *cofactor)
  2785. {
  2786. int i, cnt = dclCnt(src);
  2787. dcube *r = &(pi->tmp[1]);
  2788. dclClear(dest);
  2789. if ( dclClearFlags(dest) == 0 )
  2790. return 0;
  2791. for ( i = 0; i < cnt; i++ )
  2792. if ( dcCofactor(pi, r, dclGet(src, i), cofactor) != 0 )
  2793. if ( dclSCCInvAddAndSetFlag(pi, dest, r) == 0 )
  2794. return 0;
  2795. dclDeleteCubesWithFlag(pi, dest);
  2796. return 1;
  2797. }
  2798. /*-- dclCofactor ------------------------------------------------------------*/
  2799. int dclCofactor(pinfo *pi, dclist dest, dclist src, dcube *cofactor)
  2800. {
  2801. int i, cnt = dclCnt(src);
  2802. dcube *r = &(pi->tmp[1]);
  2803. dclClear(dest);
  2804. for ( i = 0; i < cnt; i++ )
  2805. if ( dcCofactor(pi, r, dclGet(src, i), cofactor) != 0 )
  2806. if ( dclAdd(pi, dest, r) < 0 )
  2807. return 0;
  2808. return 1;
  2809. }
  2810. /*-- dclGetCofactorCnt ------------------------------------------------------*/
  2811. /* bestimmt die anzahl der elemente, die bei einer kofaktorierung */
  2812. /* entstehen wuerden */
  2813. int dclGetCofactorCnt(pinfo *pi, dclist src, dcube *cofactor)
  2814. {
  2815. int i, cnt = dclCnt(src);
  2816. int elements = 0;
  2817. for ( i = 0; i < cnt; i++ )
  2818. if ( dcIsDeltaNoneZero(pi, dclGet(src, i), cofactor) == 0 )
  2819. elements++;
  2820. return elements;
  2821. }
  2822. /*-- dclSCCAddAndSetFlag ----------------------------------------------------*/
  2823. /* Wenn c teilmenge eines elementes aus cl ist, wird c nicht angehaengt. */
  2824. /* Alle elemente die teilmenge von c sind, werden markiert. */
  2825. /* Die idee ist: Wenn vor dem aufruf gilt SCC(cl), dann gilt ist dies */
  2826. /* auch nach dem aufruf, wenn dclDeleteCubesWithFlag(pi, cl) aufgerufen */
  2827. /* wurde. */
  2828. /* Rueckgabe: 0 im Falle eines Fehlers. */
  2829. int dclSCCAddAndSetFlagReadable(pinfo *pi, dclist cl, dcube *c)
  2830. {
  2831. register int i, cnt = dclCnt(cl);
  2832. register int flags = 0;
  2833. for( i = cnt-1; i >= 0; i-- )
  2834. if ( dclIsFlag(cl, i) == 0 )
  2835. {
  2836. if ( dcIsSubSet(pi, dclGet(cl, i), c) != 0 )
  2837. return 1;
  2838. }
  2839. else
  2840. {
  2841. flags++;
  2842. }
  2843. for( i = 0; i < cnt; i++ )
  2844. if ( dclIsFlag(cl, i) == 0 )
  2845. if ( dcIsSubSet(pi, c, dclGet(cl, i)) != 0 )
  2846. dclSetFlag(cl, i);
  2847. if ( dclAdd(pi, cl, c) >= 0 )
  2848. return 1;
  2849. if ( flags > 30 )
  2850. dclDeleteCubesWithFlag(pi, cl);
  2851. return 0;
  2852. }
  2853. int dclSCCAddAndSetFlag(pinfo *pi, dclist cl, dcube *c)
  2854. {
  2855. register int i, cnt = dclCnt(cl);
  2856. register int flags = 0;
  2857. for( i = cnt-1; i >= 4; i-=4 )
  2858. if ( dcIsSubSet4(pi, dclGet(cl, i), dclGet(cl, i-1), dclGet(cl, i-2), dclGet(cl, i-3), c) != 0 )
  2859. return 1;
  2860. for( ; i >= 0; i-- )
  2861. if ( dcIsSubSet(pi, dclGet(cl, i), c) != 0 )
  2862. return 1;
  2863. for( i = 0; i < cnt-6; i+=6 )
  2864. if ( dcIsSubSet6n(pi, c, dclGet(cl, i), dclGet(cl, i+1), dclGet(cl, i+2), dclGet(cl, i+3), dclGet(cl, i+4), dclGet(cl, i+5)) != 0 )
  2865. {
  2866. if ( dcIsSubSet(pi, c, dclGet(cl, i)) != 0 )
  2867. {
  2868. dclSetFlag(cl, i);
  2869. flags++;
  2870. }
  2871. if ( dcIsSubSet(pi, c, dclGet(cl, i+1)) != 0 )
  2872. {
  2873. dclSetFlag(cl, i+1);
  2874. flags++;
  2875. }
  2876. if ( dcIsSubSet(pi, c, dclGet(cl, i+2)) != 0 )
  2877. {
  2878. dclSetFlag(cl, i+2);
  2879. flags++;
  2880. }
  2881. if ( dcIsSubSet(pi, c, dclGet(cl, i+3)) != 0 )
  2882. {
  2883. dclSetFlag(cl, i+3);
  2884. flags++;
  2885. }
  2886. if ( dcIsSubSet(pi, c, dclGet(cl, i+4)) != 0 )
  2887. {
  2888. dclSetFlag(cl, i+4);
  2889. flags++;
  2890. }
  2891. if ( dcIsSubSet(pi, c, dclGet(cl, i+5)) != 0 )
  2892. {
  2893. dclSetFlag(cl, i+5);
  2894. flags++;
  2895. }
  2896. }
  2897. for( ; i < cnt; i++ )
  2898. if ( dcIsSubSet(pi, c, dclGet(cl, i)) != 0 )
  2899. {
  2900. dclSetFlag(cl, i);
  2901. flags++;
  2902. }
  2903. if ( dclAdd(pi, cl, c) < 0 )
  2904. return 0;
  2905. if ( flags > 0 )
  2906. dclDeleteCubesWithFlag(pi, cl);
  2907. return 1;
  2908. }
  2909. /*-- dclSCCInvAddAndSetFlag -------------------------------------------------*/
  2910. /* Wenn c teilmenge eines elementes aus cl ist, wird c nicht angehaengt. */
  2911. /* Alle elemente die teilmenge von c sind, werden markiert. */
  2912. /* Die idee ist: Wenn vor dem aufruf gilt SCC(cl), dann gilt ist dies */
  2913. /* auch nach dem aufruf, wenn dclDeleteCubesWithFlag(pi, cl) aufgerufen */
  2914. /* wurde. */
  2915. /* Rueckgabe: 0 im Falle eines Fehlers. */
  2916. int dclSCCInvAddAndSetFlag(pinfo *pi, dclist cl, dcube *c)
  2917. {
  2918. register int i, cnt = dclCnt(cl);
  2919. register int flags = 0;
  2920. for( i = cnt-1; i >= 6; i-=6 )
  2921. if ( dcIsSubSet6n(pi, c, dclGet(cl, i), dclGet(cl, i-1), dclGet(cl, i-2), dclGet(cl, i-3), dclGet(cl, i-4), dclGet(cl, i-5)) != 0 )
  2922. return 1;
  2923. for( ; i >= 0; i-- )
  2924. if ( dcIsSubSet(pi, c, dclGet(cl, i)) != 0 )
  2925. return 1;
  2926. for( i = 0; i < cnt; i++ )
  2927. if ( dcIsSubSet(pi, dclGet(cl, i), c) != 0 )
  2928. {
  2929. dclSetFlag(cl, i);
  2930. flags++;
  2931. }
  2932. if ( dclAdd(pi, cl, c) < 0 )
  2933. return 0;
  2934. if ( flags > 0 )
  2935. dclDeleteCubesWithFlag(pi, cl);
  2936. return 1;
  2937. }
  2938. /*-- dclSCCConsensusCube ----------------------------------------------------*/
  2939. /* a = consensus(a,b) */
  2940. /* die flagliste von a wird benutzt */
  2941. /* wenn vorher SCC(a) galt, gilt das danach auch noch */
  2942. int dclSCCConsensusCube(pinfo *pi, dclist a, dcube *b)
  2943. {
  2944. int a_pos, a_cnt = dclCnt(a);
  2945. if ( dclClearFlags(a) == 0 )
  2946. return 0;
  2947. for ( a_pos = 0; a_pos < a_cnt; a_pos++ )
  2948. {
  2949. if ( dcConsensus(pi, &(pi->tmp[1]), dclGet(a, a_pos), b) != 0 )
  2950. {
  2951. if ( dclSCCAddAndSetFlag(pi, a, &(pi->tmp[1])) == 0 )
  2952. return 0;
  2953. }
  2954. else
  2955. {
  2956. dclSetFlag(a, a_pos);
  2957. }
  2958. }
  2959. dclDeleteCubesWithFlag(pi, a);
  2960. return 1;
  2961. }
  2962. /*-- dclConsensusCube -------------------------------------------------------*/
  2963. /* a = consensus(a,b) */
  2964. /* die flagliste von a wird benutzt */
  2965. int dclConsensusCube(pinfo *pi, dclist a, dcube *b)
  2966. {
  2967. int a_pos, a_cnt = dclCnt(a);
  2968. if ( dclClearFlags(a) == 0 )
  2969. return 0;
  2970. for ( a_pos = 0; a_pos < a_cnt; a_pos++ )
  2971. {
  2972. if ( dcConsensus(pi, &(pi->tmp[1]), dclGet(a, a_pos), b) != 0 )
  2973. {
  2974. if ( dclAdd(pi, a, &(pi->tmp[1])) <0 )
  2975. return 0;
  2976. }
  2977. else
  2978. {
  2979. dclSetFlag(a, a_pos);
  2980. }
  2981. }
  2982. dclDeleteCubesWithFlag(pi, a);
  2983. return 1;
  2984. }
  2985. /*-- dclConsensus -----------------------------------------------------------*/
  2986. /* consensus zwischen allen elementen */
  2987. /* aus a und allen elementen aus b. */
  2988. /* 'dest' enthaelt die neuen Cubes. */
  2989. /* 'dest' erfuellt die SCC Eigenschaft */
  2990. /* Rueckgabe: 0 im Falle eines Fehlers. */
  2991. int dclConsensus2(pinfo *pi, dclist dest, dclist a, dclist b)
  2992. {
  2993. int a_pos, a_cnt = dclCnt(a);
  2994. int b_pos, b_cnt = dclCnt(b);
  2995. dclClear(dest);
  2996. if ( dclClearFlags(dest) == 0 )
  2997. return 0;
  2998. for ( a_pos = 0; a_pos < a_cnt; a_pos++ )
  2999. {
  3000. /*
  3001. if ( pinfoProcedure(pi, "Consensus", a_pos, a_cnt) == 0 )
  3002. return 0;
  3003. */
  3004. for ( b_pos = 0; b_pos < b_cnt; b_pos++ )
  3005. if ( dcConsensus(pi, &(pi->tmp[1]), dclGet(a, a_pos), dclGet(b, b_pos)) != 0 )
  3006. if ( dclSCCAddAndSetFlag(pi, dest, &(pi->tmp[1])) == 0 )
  3007. return 0;
  3008. }
  3009. dclDeleteCubesWithFlag(pi, dest);
  3010. return 1;
  3011. }
  3012. #define DCL_CONS_STEP 8
  3013. int dclConsensus(pinfo *pi, dclist dest, dclist a, dclist b)
  3014. {
  3015. int a_pos, a_cnt = dclCnt(a);
  3016. int b_pos, b_cnt = dclCnt(b);
  3017. int start = 0, min_cnt;
  3018. int cnt = 0;
  3019. dclClear(dest);
  3020. if ( dclClearFlags(dest) == 0 )
  3021. return 0;
  3022. min_cnt = a_cnt > b_cnt ? b_cnt : a_cnt;
  3023. while( start+DCL_CONS_STEP < min_cnt )
  3024. {
  3025. /*
  3026. if ( pinfoProcedure(pi, "Consensus", start, min_cnt) == 0 )
  3027. return 0;
  3028. */
  3029. for ( a_pos = start; a_pos < start+DCL_CONS_STEP; a_pos++ )
  3030. for ( b_pos = 0; b_pos < start+DCL_CONS_STEP; b_pos++ )
  3031. if ( dcConsensus(pi, &(pi->tmp[1]), dclGet(a, a_pos), dclGet(b, b_pos)) != 0 )
  3032. if ( dclSCCAddAndSetFlag(pi, dest, &(pi->tmp[1])) == 0 )
  3033. return 0;
  3034. for ( b_pos = start; b_pos < start+DCL_CONS_STEP; b_pos++ )
  3035. for ( a_pos = 0; a_pos < start; a_pos++ )
  3036. if ( dcConsensus(pi, &(pi->tmp[1]), dclGet(a, a_pos), dclGet(b, b_pos)) != 0 )
  3037. if ( dclSCCAddAndSetFlag(pi, dest, &(pi->tmp[1])) == 0 )
  3038. return 0;
  3039. cnt += DCL_CONS_STEP*(start+DCL_CONS_STEP)+DCL_CONS_STEP*start;
  3040. start += DCL_CONS_STEP;
  3041. }
  3042. /*
  3043. if ( pinfoProcedure(pi, "Consensus", min_cnt, min_cnt) == 0 )
  3044. return 0;
  3045. */
  3046. for ( a_pos = start; a_pos < a_cnt; a_pos++ )
  3047. for ( b_pos = 0; b_pos < b_cnt; b_pos++ )
  3048. if ( dcConsensus(pi, &(pi->tmp[1]), dclGet(a, a_pos), dclGet(b, b_pos)) != 0 )
  3049. if ( dclSCCAddAndSetFlag(pi, dest, &(pi->tmp[1])) == 0 )
  3050. return 0;
  3051. for ( b_pos = start; b_pos < b_cnt; b_pos++ )
  3052. for ( a_pos = 0; a_pos < start; a_pos++ )
  3053. if ( dcConsensus(pi, &(pi->tmp[1]), dclGet(a, a_pos), dclGet(b, b_pos)) != 0 )
  3054. if ( dclSCCAddAndSetFlag(pi, dest, &(pi->tmp[1])) == 0 )
  3055. return 0;
  3056. cnt += (a_cnt-start)*b_cnt + (b_cnt-start)*start;
  3057. assert(a_cnt*b_cnt == cnt);
  3058. dclDeleteCubesWithFlag(pi, dest);
  3059. return 1;
  3060. }
  3061. /*-- dclIsBinateInVar -------------------------------------------------------*/
  3062. int dclIsBinateInVar(dclist cl, int var)
  3063. {
  3064. int is_one = 0, is_zero = 0;
  3065. int i, cnt = dclCnt(cl);
  3066. int code;
  3067. for( i = 0; i < cnt; i++ )
  3068. {
  3069. code = dcGetIn(dclGet(cl,i), var);
  3070. if ( code == 1 )
  3071. is_zero = 1;
  3072. else if ( code == 2 )
  3073. is_one = 1;
  3074. }
  3075. if ( is_zero != 0 && is_one != 0 )
  3076. return 1;
  3077. return 0;
  3078. }
  3079. /*-- dclIsDCInVar -------------------------------------------------------*/
  3080. int dclIsDCInVar(pinfo *pi, dclist cl, int var)
  3081. {
  3082. dcube *r = &(pi->tmp[5]);
  3083. dclAndElements(pi, r, cl);
  3084. if ( dcGetIn(r, var) == 3 )
  3085. return 1;
  3086. return 0;
  3087. }
  3088. /*-- dclCheckTautology ------------------------------------------------------*/
  3089. /* Rueckgabe: 1 Ja, 'cl' ist eine Tautologie */
  3090. /* 0 Nein, keine Tautologie */
  3091. /* -1 Unbekannt */
  3092. int dclCheckTautology(pinfo *pi, dclist cl)
  3093. {
  3094. int i, cnt = dclCnt(cl);
  3095. dcAllClear(pi, &(pi->tmp[8]));
  3096. for( i = 0; i < cnt; i++ )
  3097. {
  3098. if ( dcIsTautology(pi, dclGet(cl, i)) != 0 )
  3099. return 1;
  3100. dcOr(pi, &(pi->tmp[8]), &(pi->tmp[8]), dclGet(cl, i));
  3101. }
  3102. if ( dcIsTautology(pi, &(pi->tmp[8])) == 0 )
  3103. return 0;
  3104. #ifdef xxxx
  3105. for( i = 0; i < pi->in_cnt; i++ )
  3106. if ( dcGetIn(&pi->tmp[8], i) != 3 ) /* Input: No don't care -> no tautology */
  3107. return 0;
  3108. for( i = 0; i < pi->out_cnt; i++ )
  3109. if ( dcGetOut(&pi->tmp[8], i) == 0 ) /* Output: Somewhere Zero? */
  3110. return 0;
  3111. #endif
  3112. return -1;
  3113. }
  3114. /*-- dclTautology -----------------------------------------------------------*/
  3115. int dclTautologyCof(pinfo *pi, dclist cl, dcube *cof, int depth)
  3116. {
  3117. dclist cl_left, cl_right;
  3118. dcube *cofactor_left = &(pi->stack1[depth]);
  3119. dcube *cofactor_right = &(pi->stack2[depth]);
  3120. int check;
  3121. check = dclCheckTautology(pi, cl);
  3122. if ( check >= 0 )
  3123. {
  3124. return check;
  3125. }
  3126. if ( dcGetCofactorForSplit(pi, cofactor_left, cofactor_right, cl, cof) == 0 )
  3127. {
  3128. /* function ist unate, enthaelt aber keinen tautologie cube */
  3129. /* --> keine tautolgy */
  3130. return 0;
  3131. }
  3132. if ( dclInitCachedVA(pi, 2, &cl_left, &cl_right) == 0 )
  3133. return 0;
  3134. if (depth >= PINFO_STACK_CUBES)
  3135. return 0;
  3136. if ( dclSCCCofactor(pi, cl_left, cl, cofactor_left) == 0 )
  3137. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3138. if ( dclSCCCofactor(pi, cl_right, cl, cofactor_right) == 0 )
  3139. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3140. /*
  3141. * puts("cofactored lists (left)");
  3142. * dclShow(pi, cl_left);
  3143. * puts("cofactored lists (right)");
  3144. * dclShow(pi, cl_right);
  3145. */
  3146. /* pinfoBTreeStart(pi); */
  3147. if ( dclTautologyCof(pi, cl_left, cofactor_left, depth+1) == 0 )
  3148. return /*pinfoBTreeEnd(pi),*/ dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3149. if ( dclTautologyCof(pi, cl_right, cofactor_right, depth+1) == 0 )
  3150. return /*pinfoBTreeEnd(pi),*/ dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3151. return /*pinfoBTreeEnd(pi),*/ dclDestroyCachedVA(pi, 2, cl_left, cl_right), 1;
  3152. }
  3153. int dclTautology(pinfo *pi, dclist cl)
  3154. {
  3155. int result;
  3156. dcube *cof = &(pi->tmp[2]);
  3157. dcSetTautology(pi, cof);
  3158. /* pinfoBTreeInit(pi, "Tautology"); */
  3159. result = dclTautologyCof(pi, cl, cof, 0);
  3160. /* pinfoBTreeFinish(pi); */
  3161. return result;
  3162. }
  3163. /*-- dclIsSubSet ------------------------------------------------------------*/
  3164. /* gibt 0 zurueck, wenn 'cube' nicht von 'cl' ueberdeckt wird */
  3165. /* returns 0, if 'cube' is not covered by 'cl' */
  3166. int dclIsSubSet(pinfo *pi, dclist cl, dcube *cube)
  3167. {
  3168. dclist tmp;
  3169. int result;
  3170. if ( dclInitCached(pi, &tmp) == 0 )
  3171. return 0;
  3172. if ( dclSCCCofactor(pi, tmp, cl, cube) == 0 )
  3173. return dclDestroyCached(pi, tmp), 0;
  3174. result = dclTautology(pi, tmp);
  3175. dclDestroyCached(pi, tmp);
  3176. return result;
  3177. }
  3178. /*-- dclRemoveSubSet --------------------------------------------------------*/
  3179. /* entfernt aus cl alle elemente, die von cover ueberdeckt werden */
  3180. /* eventuell entfernte elemente werden an removed angehaengt!!! */
  3181. /* removed kann NULL sein */
  3182. int dclRemoveSubSet(pinfo *pi, dclist cl, dclist cover, dclist removed)
  3183. {
  3184. int i, cnt = dclCnt(cl);
  3185. if ( dclClearFlags(cl) == 0 )
  3186. return 0;
  3187. pinfoProcedureInit(pi, "RemoveSubSet", cnt);
  3188. for( i = 0; i < cnt; i++ )
  3189. {
  3190. if ( pinfoProcedureDo(pi, i) == 0 )
  3191. return pinfoProcedureFinish(pi), 0;
  3192. if ( dclIsSubSet(pi, cover, dclGet(cl, i)) != 0 )
  3193. {
  3194. if ( removed != NULL )
  3195. if ( dclAdd(pi, removed, dclGet(cl, i)) < 0 )
  3196. return pinfoProcedureFinish(pi), 0;
  3197. dclSetFlag(cl, i);
  3198. }
  3199. }
  3200. pinfoProcedureFinish(pi);
  3201. dclDeleteCubesWithFlag(pi, cl);
  3202. return 1;
  3203. }
  3204. /*-- dclSortOutput ----------------------------------------------------------*/
  3205. static pinfo *qsort_pinfo;
  3206. int dcl_compare_output(const void *ap, const void *bp)
  3207. {
  3208. dcube *a = (dcube *)ap;
  3209. dcube *b = (dcube *)bp;
  3210. int i;
  3211. for( i = 0; i < qsort_pinfo->out_words; i++ )
  3212. {
  3213. if ( a->out[i] < b->out[i] )
  3214. return -1;
  3215. if ( a->out[i] > b->out[i] )
  3216. return 1;
  3217. }
  3218. return 0;
  3219. }
  3220. void dclSortOutput(pinfo *pi, dclist cl)
  3221. {
  3222. qsort_pinfo = pi;
  3223. qsort(cl->list, cl->cnt, sizeof(dcube), dcl_compare_output);
  3224. }
  3225. /*-- dclSortOutSize ---------------------------------------------------------*/
  3226. int dcl_compare_n(const void *ap, const void *bp)
  3227. {
  3228. dcube *a = (dcube *)ap;
  3229. dcube *b = (dcube *)bp;
  3230. if ( a->n > b->n )
  3231. return -1;
  3232. if ( a->n < b->n )
  3233. return 1;
  3234. return 0;
  3235. }
  3236. void dclSortOutSize(pinfo *pi, dclist cl)
  3237. {
  3238. int i, cnt = dclCnt(cl);
  3239. for( i = 0; i < cnt; i++ )
  3240. dclGet(cl, i)->n = dcOutCnt(pi, dclGet(cl, i));
  3241. qsort(cl->list, cl->cnt, sizeof(dcube), dcl_compare_n);
  3242. }
  3243. /*-- dclSortInSize ----------------------------------------------------------*/
  3244. void dclSortInSize(pinfo *pi, dclist cl)
  3245. {
  3246. int i, cnt = dclCnt(cl);
  3247. for( i = 0; i < cnt; i++ )
  3248. dclGet(cl, i)->n = dcInDCCnt(pi, dclGet(cl, i));
  3249. qsort(cl->list, cl->cnt, sizeof(dcube), dcl_compare_n);
  3250. }
  3251. /*-- dclSCC -----------------------------------------------------------------*/
  3252. int dclSCC(pinfo *pi, dclist cl)
  3253. {
  3254. int i, j, cnt = dclCnt(cl);
  3255. int scc_cnt = 0;
  3256. /* dclSortSize(pi, cl); */
  3257. if ( dclClearFlags(cl) == 0 )
  3258. return 0;
  3259. pinfoProcedureInit(pi, "SCC", cnt);
  3260. for( i = 0; i < cnt; i++ )
  3261. {
  3262. if ( pinfoProcedureDo(pi, i) == 0 )
  3263. return 0;
  3264. if ( dclIsFlag(cl, i) == 0 )
  3265. for( j = i+1; j < cnt; j++ )
  3266. {
  3267. if ( dclIsFlag(cl, j) == 0 )
  3268. {
  3269. if ( dcIsSubSet(pi, dclGet(cl, j), dclGet(cl, i)) != 0 )
  3270. {
  3271. dclSetFlag(cl, i);
  3272. scc_cnt++;
  3273. }
  3274. else if ( dcIsEqual(pi, dclGet(cl, i), dclGet(cl, j)) == 0 )
  3275. if ( dcIsSubSet(pi, dclGet(cl, i), dclGet(cl, j)) != 0 )
  3276. {
  3277. dclSetFlag(cl, j);
  3278. scc_cnt++;
  3279. }
  3280. }
  3281. }
  3282. }
  3283. /* printf("SCC: %d \n", scc_cnt); */
  3284. dclDeleteCubesWithFlag(pi, cl);
  3285. pinfoProcedureFinish(pi);
  3286. return 1;
  3287. }
  3288. /*-- dclSCCInv --------------------------------------------------------------*/
  3289. int dclSCCInv(pinfo *pi, dclist cl)
  3290. {
  3291. int i, j, cnt = dclCnt(cl);
  3292. int scc_cnt = 0;
  3293. /* dclSortSize(pi, cl); */
  3294. if ( dclClearFlags(cl) == 0 )
  3295. return 0;
  3296. pinfoProcedureInit(pi, "SCCInv", cnt);
  3297. for( i = 0; i < cnt; i++ )
  3298. {
  3299. if ( pinfoProcedureDo(pi, i) == 0 )
  3300. return 0;
  3301. if ( dclIsFlag(cl, i) == 0 )
  3302. for( j = i+1; j < cnt; j++ )
  3303. {
  3304. if ( dclIsFlag(cl, j) == 0 )
  3305. {
  3306. if ( dcIsSubSet(pi, dclGet(cl, i), dclGet(cl, j)) != 0 )
  3307. {
  3308. dclSetFlag(cl, i);
  3309. scc_cnt++;
  3310. }
  3311. else if ( dcIsEqual(pi, dclGet(cl, i), dclGet(cl, j)) == 0 )
  3312. if ( dcIsSubSet(pi, dclGet(cl, j), dclGet(cl, i)) != 0 )
  3313. {
  3314. dclSetFlag(cl, j);
  3315. scc_cnt++;
  3316. }
  3317. }
  3318. }
  3319. }
  3320. /* printf("SCC: %d \n", scc_cnt); */
  3321. dclDeleteCubesWithFlag(pi, cl);
  3322. pinfoProcedureFinish(pi);
  3323. return 1;
  3324. }
  3325. /*-- dclRemoveEqual ---------------------------------------------------------*/
  3326. int dclRemoveEqual(pinfo *pi, dclist cl)
  3327. {
  3328. int i, j, cnt = dclCnt(cl);
  3329. /* dclSortSize(pi, cl); */
  3330. if ( dclClearFlags(cl) == 0 )
  3331. return 0;
  3332. pinfoProcedureInit(pi, "RemoveEqual", cnt);
  3333. for( i = 0; i < cnt; i++ )
  3334. {
  3335. if ( pinfoProcedureDo(pi, i) == 0 )
  3336. return 0;
  3337. if ( dclIsFlag(cl, i) == 0 )
  3338. for( j = i+1; j < cnt; j++ )
  3339. {
  3340. if ( dclIsFlag(cl, j) == 0 )
  3341. {
  3342. if ( dcIsEqual(pi, dclGet(cl, i), dclGet(cl, j)) != 0 )
  3343. {
  3344. dclSetFlag(cl, j);
  3345. }
  3346. }
  3347. }
  3348. }
  3349. dclDeleteCubesWithFlag(pi, cl);
  3350. pinfoProcedureFinish(pi);
  3351. return 1;
  3352. }
  3353. /*-- dclSCCUnionSubset ------------------------------------------------------*/
  3354. /* annahme: dest und src haben SCC eigenschaft */
  3355. /* dest = SCC(dest + src) */
  3356. /* src bleibt unveraendert */
  3357. /* Elemente von src nicht groesser als die elemente von dest */
  3358. int dclSCCUnionSubset(pinfo *pi, dclist dest, dclist src)
  3359. {
  3360. int dest_i, dest_cnt = dclCnt(dest);
  3361. int src_i, src_cnt = dclCnt(src);
  3362. for( src_i = 0; src_i < src_cnt; src_i++ )
  3363. {
  3364. for( dest_i = 0; dest_i < dest_cnt; dest_i++ )
  3365. if ( dcIsSubSet(pi, dclGet(dest, dest_i), dclGet(src, src_i)) != 0 )
  3366. break;
  3367. if ( dest_i >= dest_cnt )
  3368. if ( dclAdd(pi, dest, dclGet(src, src_i)) < 0 )
  3369. return 0;
  3370. }
  3371. return 1;
  3372. }
  3373. /*-- dclSCCUnion ------------------------------------------------------------*/
  3374. /* annahme: dest und src haben SCC eigenschaft */
  3375. /* dest = SCC(dest + src) */
  3376. /* src bleibt unveraendert */
  3377. int dclSCCUnion(pinfo *pi, dclist dest, dclist src)
  3378. {
  3379. int dest_i, dest_cnt = dclCnt(dest);
  3380. int src_i, src_cnt = dclCnt(src);
  3381. if ( dclClearFlags(dest) == 0 )
  3382. return 0;
  3383. for( dest_i = 0; dest_i < dest_cnt; dest_i++ )
  3384. {
  3385. for( src_i = 0; src_i < src_cnt; src_i++ )
  3386. {
  3387. if ( dcIsSubSet(pi, dclGet(src, src_i), dclGet(dest, dest_i)) != 0 )
  3388. {
  3389. dclSetFlag(dest, dest_i);
  3390. break;
  3391. }
  3392. }
  3393. }
  3394. dclDeleteCubesWithFlag(pi, dest);
  3395. dest_cnt = dclCnt(dest);
  3396. for( src_i = 0; src_i < src_cnt; src_i++ )
  3397. {
  3398. for( dest_i = 0; dest_i < dest_cnt; dest_i++ )
  3399. if ( dcIsSubSet(pi, dclGet(dest, dest_i), dclGet(src, src_i)) != 0 )
  3400. break;
  3401. if ( dest_i >= dest_cnt )
  3402. if ( dclAdd(pi, dest, dclGet(src, src_i)) < 0 )
  3403. return 0;
  3404. }
  3405. return 1;
  3406. }
  3407. /*-- dclSCCInvUnionSubset ----------------------------------------------------*/
  3408. /* annahme: dest und src haben SCC eigenschaft */
  3409. /* dest = SCC(dest + src) */
  3410. /* src bleibt unveraendert */
  3411. /* Elemente von src nicht groesser als die elemente von dest */
  3412. int dclSCCInvUnionSubset(pinfo *pi, dclist dest, dclist src)
  3413. {
  3414. int dest_i, dest_cnt = dclCnt(dest);
  3415. int src_i, src_cnt = dclCnt(src);
  3416. for( src_i = 0; src_i < src_cnt; src_i++ )
  3417. {
  3418. for( dest_i = 0; dest_i < dest_cnt; dest_i++ )
  3419. if ( dcIsSubSet(pi, dclGet(src, src_i), dclGet(dest, dest_i)) != 0 )
  3420. break;
  3421. if ( dest_i >= dest_cnt )
  3422. if ( dclAdd(pi, dest, dclGet(src, src_i)) < 0 )
  3423. return 0;
  3424. }
  3425. return 1;
  3426. }
  3427. /*-- dclSCCInvUnion ---------------------------------------------------------*/
  3428. int dclSCCInvUnion(pinfo *pi, dclist dest, dclist src)
  3429. {
  3430. int dest_i, dest_cnt = dclCnt(dest);
  3431. int src_i, src_cnt = dclCnt(src);
  3432. if ( dclClearFlags(dest) == 0 )
  3433. return 0;
  3434. for( dest_i = 0; dest_i < dest_cnt; dest_i++ )
  3435. {
  3436. for( src_i = 0; src_i < src_cnt; src_i++ )
  3437. {
  3438. if ( dcIsSubSet(pi, dclGet(dest, dest_i), dclGet(src, src_i)) != 0 )
  3439. {
  3440. dclSetFlag(dest, dest_i);
  3441. break;
  3442. }
  3443. }
  3444. }
  3445. dclDeleteCubesWithFlag(pi, dest);
  3446. dest_cnt = dclCnt(dest);
  3447. for( src_i = 0; src_i < src_cnt; src_i++ )
  3448. {
  3449. for( dest_i = 0; dest_i < dest_cnt; dest_i++ )
  3450. if ( dcIsSubSet(pi, dclGet(src, src_i), dclGet(dest, dest_i)) != 0 )
  3451. break;
  3452. if ( dest_i >= dest_cnt )
  3453. if ( dclAdd(pi, dest, dclGet(src, src_i)) < 0 )
  3454. return 0;
  3455. }
  3456. return 1;
  3457. }
  3458. /*-- dclSCCSubtractCube -----------------------------------------------------*/
  3459. /* a = a - b */
  3460. /* nach dieser operation erfuellt a die SCC eigenschaft */
  3461. int dclSCCSubtractCube(pinfo *pi, dclist a, dcube *b)
  3462. {
  3463. int a_i;
  3464. int a_cnt;
  3465. dclist result;
  3466. if ( dclClearFlags(a) == 0 )
  3467. return 0;
  3468. if ( dclInitCached(pi, &result) == 0 )
  3469. return 0;
  3470. if ( dclClearFlags(result) == 0 )
  3471. return dclDestroyCached(pi, result), 0;
  3472. a_cnt = dclCnt(a);
  3473. dclClear(result);
  3474. for( a_i = 0; a_i < a_cnt; a_i++ )
  3475. {
  3476. if ( dclSCCSharpAndSetFlag(pi, result, dclGet(a, a_i), b) == 0 )
  3477. return dclDestroyCached(pi, result), 0;
  3478. if ( (a_i % 32) == 0 )
  3479. dclDeleteCubesWithFlag(pi, result);
  3480. }
  3481. dclDeleteCubesWithFlag(pi, result);
  3482. if ( dclCopy(pi, a, result) == 0 )
  3483. return dclDestroyCached(pi, result), 0;
  3484. dclDestroyCached(pi, result);
  3485. return 1;
  3486. }
  3487. /*-- dclSubtractCube -------------------------------------------------------*/
  3488. /* a = a - b */
  3489. /* nach dieser operation erfuellt a die SCC eigenschaft nicht mehr */
  3490. int dclSubtractCube(pinfo *pi, dclist a, dcube *b)
  3491. {
  3492. int a_i;
  3493. int a_cnt;
  3494. dclist result;
  3495. if ( dclInit(&result) == 0 )
  3496. return 0;
  3497. a_cnt = dclCnt(a);
  3498. for( a_i = 0; a_i < a_cnt; a_i++ )
  3499. {
  3500. if ( dclSharp(pi, result, dclGet(a, a_i), b) < 0 )
  3501. return dclDestroy(result), 0;
  3502. }
  3503. if ( dclCopy(pi, a, result) == 0 )
  3504. return dclDestroy(result), 0;
  3505. dclDestroy(result);
  3506. return 1;
  3507. }
  3508. /*-- dclSubtract ------------------------------------------------------------*/
  3509. /* a = a - b */
  3510. int dclSubtract(pinfo *pi, dclist a, dclist b)
  3511. {
  3512. int a_i, b_i;
  3513. int b_cnt = dclCnt(b);
  3514. int a_cnt;
  3515. dclist result;
  3516. if ( dclInit(&result) == 0 )
  3517. return 0;
  3518. if ( dclClearFlags(a) == 0 )
  3519. return dclDestroy(result), 0;
  3520. if ( dclClearFlags(result) == 0 )
  3521. return dclDestroy(result), 0;
  3522. pinfoProcedureInit(pi, "Subtract", b_cnt);
  3523. for( b_i = 0; b_i < b_cnt; b_i++ )
  3524. {
  3525. a_cnt = dclCnt(a);
  3526. dclClear(result);
  3527. if ( pinfoProcedureDo(pi, b_i) == 0 )
  3528. return pinfoProcedureFinish(pi), 0;
  3529. for( a_i = 0; a_i < a_cnt; a_i++ )
  3530. {
  3531. if ( dclSCCSharpAndSetFlag(pi, result, dclGet(a, a_i), dclGet(b, b_i)) == 0 )
  3532. return pinfoProcedureFinish(pi), dclDestroy(result), 0;
  3533. if ( (a_i % 32) == 0 )
  3534. dclDeleteCubesWithFlag(pi, result);
  3535. }
  3536. dclDeleteCubesWithFlag(pi, result);
  3537. if ( dclCopy(pi, a, result) == 0 )
  3538. return pinfoProcedureFinish(pi), dclDestroy(result), 0;
  3539. }
  3540. pinfoProcedureFinish(pi);
  3541. dclDestroy(result);
  3542. return 1;
  3543. }
  3544. /*-- dclComplementOut -------------------------------------------------------*/
  3545. /* subtract from the cube -----...---- 0000..00100...000 */
  3546. /* result is stored in cl */
  3547. int dclComplementOut(pinfo *pi, int o, dclist cl)
  3548. {
  3549. dcube *c;
  3550. dclist a;
  3551. if ( dclInit(&a) == 0 )
  3552. return 0;
  3553. c = dclAddEmptyCube(pi, a);
  3554. dcInSetAll(pi, c, CUBE_IN_MASK_DC);
  3555. dcOutSetAll(pi, c, 0);
  3556. dcSetOut(c, o, 1);
  3557. if ( dclSubtract(pi, a, cl) == 0 )
  3558. return dclDestroy(cl), 0;
  3559. if ( dclCopy(pi, cl, a) == 0 )
  3560. return dclDestroy(cl), 0;
  3561. return dclDestroy(cl), 1;
  3562. }
  3563. /*-- dcl_calc_substract -----------------------------------------------------*/
  3564. /*
  3565. Calculates the difference a - b and b - a.
  3566. Information is returend, wether these differences is empty.
  3567. there are four cases:
  3568. a-b = empty and b-a = empty
  3569. --> 'a' is equal to 'b'
  3570. a-b != empty and b-a = empty
  3571. --> 'b' is a subset of 'a'
  3572. a-b = empty and b-a != empty
  3573. --> 'a' is a subset of 'b'
  3574. a-b != empty and b-a != empty
  3575. --> 'a' and 'b' are partially disjunct
  3576. */
  3577. /*
  3578. * static int dcl_calc_substract(pinfo *pi, dclist a, dclist b,
  3579. * int *is_ab_empty, int *is_ba_empty)
  3580. * {
  3581. * dclist aa, bb;
  3582. * if ( dclInitVA(2, &aa, &bb) == 0 )
  3583. * return 0;
  3584. * if ( dclCopy(pi, aa, a) == 0 )
  3585. * return dclDestroyVA(2, aa, bb), 0;
  3586. * if ( dclCopy(pi, bb, b) == 0 )
  3587. * return dclDestroyVA(2, aa, bb), 0;
  3588. * if( dclSubtract(pi, aa, bb) == 0 )
  3589. * return dclDestroyVA(2, aa, bb), 0;
  3590. *
  3591. * *is_ab_empty = 0;
  3592. * if ( dclCnt(aa) != 0 )
  3593. * *is_ab_empty = 1;
  3594. *
  3595. * if ( dclCopy(pi, aa, a) == 0 )
  3596. * return dclDestroyVA(2, aa, bb), 0;
  3597. * if ( dclCopy(pi, bb, b) == 0 )
  3598. * return dclDestroyVA(2, aa, bb), 0;
  3599. * if( dclSubtract(pi, bb, aa) == 0 )
  3600. * return dclDestroyVA(2, aa, bb), 0;
  3601. *
  3602. * *is_ba_empty = 0;
  3603. * if ( dclCnt(bb) != 0 )
  3604. * *is_ba_empty = 1;
  3605. *
  3606. * return dclDestroyVA(2, aa, bb), 1;
  3607. * }
  3608. */
  3609. /*-- dclIsEquivalent --------------------------------------------------------*/
  3610. /* Ist b Teilmenge von a? */
  3611. int dclIsSubsetList(pinfo *pi, dclist a, dclist b)
  3612. {
  3613. dclist aa, bb;
  3614. if ( dclInitCachedVA(pi, 2, &aa, &bb) == 0 )
  3615. return 0;
  3616. if ( dclCopy(pi, aa, a) == 0 )
  3617. return dclDestroyCachedVA(pi, 2, aa, bb), 0;
  3618. if ( dclCopy(pi, bb, b) == 0 )
  3619. return dclDestroyCachedVA(pi, 2, aa, bb), 0;
  3620. if( dclSubtract(pi, bb, aa) == 0 )
  3621. return dclDestroyCachedVA(pi, 2, aa, bb), 0;
  3622. if ( dclCnt(bb) != 0 )
  3623. return dclDestroyCachedVA(pi, 2, aa, bb), 0;
  3624. return dclDestroyCachedVA(pi, 2, aa, bb), 1;
  3625. }
  3626. /*-- dclIsEquivalent --------------------------------------------------------*/
  3627. /*
  3628. zwei moeglichkeiten:
  3629. 1. a-b = leer und b-a = leer
  3630. 2. Kein element von b (a) ist im complement von a (b) enthalten.
  3631. */
  3632. int dclIsEquivalent(pinfo *pi, dclist a, dclist b)
  3633. {
  3634. dclist aa, bb;
  3635. if ( dclInitVA(2, &aa, &bb) == 0 )
  3636. return 0;
  3637. if ( dclCopy(pi, aa, a) == 0 )
  3638. return dclDestroyVA(2, aa, bb), 0;
  3639. if ( dclCopy(pi, bb, b) == 0 )
  3640. return dclDestroyVA(2, aa, bb), 0;
  3641. if( dclSubtract(pi, aa, bb) == 0 )
  3642. return dclDestroyVA(2, aa, bb), 0;
  3643. if ( dclCnt(aa) != 0 ) /* nicht equivalent */
  3644. return dclDestroyVA(2, aa, bb), 0;
  3645. if ( dclCopy(pi, aa, a) == 0 )
  3646. return dclDestroyVA(2, aa, bb), 0;
  3647. if ( dclCopy(pi, bb, b) == 0 )
  3648. return dclDestroyVA(2, aa, bb), 0;
  3649. if( dclSubtract(pi, bb, aa) == 0 )
  3650. return dclDestroyVA(2, aa, bb), 0;
  3651. if ( dclCnt(bb) != 0 ) /* nicht equivalent */
  3652. return dclDestroyVA(2, aa, bb), 0;
  3653. return dclDestroyVA(2, aa, bb), 1;
  3654. }
  3655. /*-- dclIsEquivalentDC ------------------------------------------------------*/
  3656. /*
  3657. bedingungen:
  3658. 1. cl-cl_on-cl_dc = leere menge
  3659. 2. cl_on-cl = leere menge
  3660. */
  3661. int dclIsEquivalentDC(pinfo *pi, dclist cl, dclist cl_on, dclist cl_dc)
  3662. {
  3663. dclist aa, bb;
  3664. if ( dclInitVA(2, &aa, &bb) == 0 )
  3665. return 0;
  3666. if ( dclCopy(pi, aa, cl) == 0 )
  3667. return dclDestroyVA(2, aa, bb), 0;
  3668. if ( dclCopy(pi, bb, cl_on) == 0 )
  3669. return dclDestroyVA(2, aa, bb), 0;
  3670. if( dclSubtract(pi, aa, bb) == 0 )
  3671. return dclDestroyVA(2, aa, bb), 0;
  3672. if ( cl_dc != NULL )
  3673. {
  3674. if ( dclCopy(pi, bb, cl_dc) == 0 )
  3675. return dclDestroyVA(2, aa, bb), 0;
  3676. if( dclSubtract(pi, aa, bb) == 0 )
  3677. return dclDestroyVA(2, aa, bb), 0;
  3678. }
  3679. if ( dclCnt(aa) != 0 ) /* nicht equivalent */
  3680. return dclDestroyVA(2, aa, bb), 0;
  3681. if ( dclCopy(pi, aa, cl_on) == 0 )
  3682. return dclDestroyVA(2, aa, bb), 0;
  3683. if ( dclCopy(pi, bb, cl) == 0 )
  3684. return dclDestroyVA(2, aa, bb), 0;
  3685. if( dclSubtract(pi, aa, bb) == 0 )
  3686. return dclDestroyVA(2, aa, bb), 0;
  3687. if ( dclCnt(aa) != 0 ) /* nicht equivalent */
  3688. return dclDestroyVA(2, aa, bb), 0;
  3689. return dclDestroyVA(2, aa, bb), 1;
  3690. }
  3691. /*-- dclArea ----------------------------------------------------------------*/
  3692. /*
  3693. berechnet die groesste zusammenhaengende menge die teilmenge
  3694. von cl_area ist und cl_s enthaelt.
  3695. Das ergebnis wird in cl_s abgelegt
  3696. Bedingung:
  3697. cl_s ist teilmenge von cl_area
  3698. Das kann mit hilfe von dclIntersectionList erzwungen werden.
  3699. nunja: die bedingung spielt keine groessere rolle, aber
  3700. man kann dann natuerlich nicht sagen, dass das ergebnis
  3701. teilmenge von cl_area ist.
  3702. wenn cl_s und cl_area die SCC eigenschaft erfuellen, dann
  3703. erfuellt das ergebnis in cl_s ebenfalls die SCC eigenschaft
  3704. */
  3705. int dclAreaIsMaxLarge(pinfo *pi, dclist cl_s, dclist cl_area, int *is_max_large)
  3706. {
  3707. dclist cl_a, cl_t, cl_r;
  3708. int i, j;
  3709. int is_found;
  3710. if ( dclInitVA(3, &cl_a, &cl_t, &cl_r) == 0 )
  3711. return 0;
  3712. if ( dclCopy(pi, cl_a, cl_area) == 0 )
  3713. return dclDestroyVA(3, cl_a, cl_t, cl_r), 0;
  3714. do
  3715. {
  3716. is_found = 0;
  3717. for( i = 0; i < dclCnt(cl_s); i++ )
  3718. {
  3719. dclClear(cl_t);
  3720. if ( dclClearFlags(cl_t) == 0 )
  3721. return dclDestroyVA(3, cl_a, cl_t, cl_r), 0;
  3722. if ( dclClearFlags(cl_a) == 0 )
  3723. return dclDestroyVA(3, cl_a, cl_t, cl_r), 0;
  3724. for( j = 0; j < dclCnt(cl_a); j++ )
  3725. {
  3726. if ( dcDelta(pi, dclGet(cl_s, i), dclGet(cl_a, j)) <= 1 )
  3727. {
  3728. if ( dclSCCAddAndSetFlag(pi, cl_t, dclGet(cl_a, j)) == 0 )
  3729. return dclDestroyVA(3, cl_a, cl_t, cl_r), 0;
  3730. dclSetFlag(cl_a, j);
  3731. is_found = 1;
  3732. }
  3733. }
  3734. dclDeleteCubesWithFlag(pi, cl_a);
  3735. dclDeleteCubesWithFlag(pi, cl_t);
  3736. }
  3737. if ( dclSCCUnion(pi, cl_r, cl_s) == 0 )
  3738. return dclDestroyVA(3, cl_a, cl_t, cl_r), 0;
  3739. if ( dclCopy(pi, cl_s, cl_t) == 0 )
  3740. return dclDestroyVA(3, cl_a, cl_t, cl_r), 0;
  3741. } while( is_found != 0 && dclCnt(cl_a) > 0 );
  3742. /* cubes in cl_s are still part of the result */
  3743. if ( dclSCCUnion(pi, cl_s, cl_r) == 0 )
  3744. return dclDestroyVA(3, cl_a, cl_t, cl_r), 0;
  3745. if ( is_max_large != NULL )
  3746. {
  3747. if ( dclCnt(cl_a) == 0 )
  3748. *is_max_large = 1;
  3749. else
  3750. *is_max_large = 0;
  3751. }
  3752. return dclDestroyVA(3, cl_a, cl_t, cl_r), 1;
  3753. }
  3754. int dclArea(pinfo *pi, dclist cl_s, dclist cl_area)
  3755. {
  3756. return dclAreaIsMaxLarge(pi, cl_s, cl_area, NULL);
  3757. }
  3758. /*-- dclIsRelated -----------------------------------------------------------*/
  3759. int dclIsRelated(pinfo *pi, dclist cl)
  3760. {
  3761. dclist cl_s;
  3762. int is_max_large = 0;
  3763. if ( dclInit(&cl_s) == 0 )
  3764. return 0;
  3765. if ( dclAdd(pi, cl_s, dclGet(cl, 0)) < 0 )
  3766. return dclDestroy(cl_s), 0;
  3767. if ( dclAreaIsMaxLarge(pi, cl_s, cl, &is_max_large) == 0 )
  3768. return dclDestroy(cl_s), 0;
  3769. return is_max_large;
  3770. }
  3771. /*-- dclComplementWithSharp -------------------------------------------------*/
  3772. /* Das ergebnis erfuellt die SCC eigenschaft und enthaelt alle (!) */
  3773. /* primeimplikanten. */
  3774. int dclComplementWithSharp(pinfo *pi, dclist cl)
  3775. {
  3776. dclist ncl;
  3777. dcube *tautologie_cube = &(pi->tmp[0]);
  3778. if ( dclCnt(cl) == 0 )
  3779. {
  3780. if ( dclAdd(pi, cl, tautologie_cube) < 0 )
  3781. return 0;
  3782. return 1;
  3783. }
  3784. else if ( dclCnt(cl) == 1 )
  3785. {
  3786. dcube *c = &(pi->tmp[13]);
  3787. dcCopy(pi, c, dclGet(cl, 0));
  3788. dclClear(cl);
  3789. if ( dclComplementCube(pi, cl, c) == 0 )
  3790. return 0;
  3791. return 1;
  3792. }
  3793. if ( dclInit(&ncl) == 0 )
  3794. return 0;
  3795. if ( dclAdd(pi, ncl, tautologie_cube) < 0 )
  3796. return dclDestroy(ncl), 0;
  3797. if ( dclSubtract(pi, ncl, cl) == 0 )
  3798. return dclDestroy(ncl), 0;
  3799. if ( dclCopy(pi, cl, ncl) == 0 )
  3800. return dclDestroy(ncl), 0;
  3801. dclDestroy(ncl);
  3802. return 1;
  3803. }
  3804. /*-- dclComplementWithURP ---------------------------------------------------*/
  3805. /* Komplementiert eine function */
  3806. /* Das ergebnis erfuellt die SCC eigenschaft und enthaelt nur */
  3807. /* primeimplikanten (aber nicht alle!) */
  3808. int dclComplementCof(pinfo *pi, dclist cl, dcube *cof, int depth)
  3809. {
  3810. int i, j;
  3811. dclist cl_left, cl_right, cl_c;
  3812. dcube *cof_left = &(pi->stack1[depth]);
  3813. dcube *cof_right = &(pi->stack2[depth]);
  3814. int left_cnt, right_cnt;
  3815. if (depth >= PINFO_STACK_CUBES)
  3816. return 0;
  3817. /* wenn die liste leer ist, ist das ergebnis der universelle cube */
  3818. if ( dclCnt(cl) == 0 )
  3819. return dclAdd(pi, cl, &(pi->tmp[0])) < 0 ? 0 : 1;
  3820. /* wenn die liste nur ein element hat, complementiere dieses */
  3821. if ( dclCnt(cl) == 1 )
  3822. {
  3823. dcCopy(pi, &(pi->tmp[6]), dclGet(cl, 0));
  3824. dclClear(cl);
  3825. return dclComplementCube(pi, cl, &(pi->tmp[6]));
  3826. }
  3827. /* wenn die liste ein einer spalte 0 hat, gibt es einen specialfall: */
  3828. /* dann ist naemlich: F = a AND F_a */
  3829. /* dessen complement ist: F = a' OR F'_a */
  3830. /*
  3831. {
  3832. dclist cl_cof;
  3833. dclOrElements(pi, cof_left, cl);
  3834. {
  3835. for( i = 0; i < pi->out_cnt; i++ )
  3836. if ( dcGetOut(cof_left, i) == 0 && dcGetOut(cof, i) != 0 )
  3837. break;
  3838. if ( i < pi->out_cnt || dcIsInTautology(pi, cof_left) == 0 )
  3839. {
  3840. for( i = 0; i < pi->out_words; i++ )
  3841. cof_left->out[i] &= cof->out[i];
  3842. if ( dclInit(&cl_cof) == 0 )
  3843. return 0;
  3844. if ( dclSCCCofactor(pi, cl_cof, cl, cof_left) == 0 )
  3845. return dclDestroy(cl_cof), 0;
  3846. if ( dclComplementCof(pi, cl_cof, cof_left, depth+1) == 0 )
  3847. return dclDestroy(cl_cof), 0;
  3848. dclClear(cl);
  3849. if ( dclComplementCube(pi, cl, cof_left) == 0 )
  3850. return dclDestroy(cl_cof), 0;
  3851. if ( dclSCCUnion(pi, cl, cl_cof) == 0 )
  3852. return dclDestroy(cl_cof), 0;
  3853. return dclDestroy(cl_cof), 1;
  3854. }
  3855. }
  3856. }
  3857. */
  3858. if ( dcGetNoneDCCofactorForSplit(pi, cof_left, cof_right, cl, cof) == 0 )
  3859. return 0;
  3860. if ( dclInitVA(3, &cl_left, &cl_right, &cl_c) == 0 )
  3861. return 0;
  3862. if ( dclSCCCofactor(pi, cl_left, cl, cof_left) == 0 )
  3863. return dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3864. if ( dclSCCCofactor(pi, cl_right, cl, cof_right) == 0 )
  3865. return dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3866. left_cnt = dclCnt(cl_left);
  3867. right_cnt = dclCnt(cl_right);
  3868. pinfoBTreeStart(pi);
  3869. if ( dclComplementCof(pi, cl_left, cof_left, depth+1) == 0 )
  3870. return pinfoBTreeEnd(pi), dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3871. if ( dclComplementCof(pi, cl_right, cof_right, depth+1) == 0 )
  3872. return pinfoBTreeEnd(pi), dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3873. pinfoBTreeEnd(pi);
  3874. /* Multiple-Valued Minimization for PLA Optimization: p736*/
  3875. /* Die beiden teillisten werden vergroessert. Da fuer beide */
  3876. /* Haelften die SCC eigenschaft erfuellt ist, ist sie nach */
  3877. /* dem erweitern ebenfalls noch erfuellt. */
  3878. dclExpand1(pi, cl_left, cl);
  3879. dclExpand1(pi, cl_right, cl);
  3880. /* Multiple-Valued Minimization for PLA Optimization: p735*/
  3881. /* Durch die Bildung der Schnittmenge mit dem Kofaktor */
  3882. /* werden Terme in zwei Haelften zerschnitten, die man besser */
  3883. /* auch haette zusammenfassen koennen. Um diesen Split zu ver- */
  3884. /* meiden, werden die Terme in eine dritte Liste transferiert. */
  3885. /* Gleiches gilt auf fuer die Subsets der jeweiligen anderen */
  3886. /* Haelfte. Interessanterweise kann dadurch spaeter die SCC */
  3887. /* operation gespart werden, wenn die identischen Elemente */
  3888. /* korrekt behandelt werden. */
  3889. dclClearFlags(cl_left);
  3890. dclClearFlags(cl_right);
  3891. dclClearFlags(cl_c);
  3892. for( i = 0; i < dclCnt(cl_left); i++ )
  3893. for( j = 0; j < dclCnt(cl_right); j++ )
  3894. {
  3895. if ( dclIsFlag(cl_right, j) == 0 )
  3896. if ( dcIsSubSet(pi, dclGet(cl_left, i), dclGet(cl_right, j)) != 0 )
  3897. {
  3898. dclSetFlag(cl_right, j);
  3899. if ( dclSCCAddAndSetFlag(pi, cl_c, dclGet(cl_right, j)) == 0 )
  3900. return dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3901. if ( dcIsEqual(pi, dclGet(cl_left, i), dclGet(cl_right, j)) != 0 )
  3902. dclSetFlag(cl_left, i);
  3903. }
  3904. if ( dclIsFlag(cl_left, i) == 0 )
  3905. if ( dcIsSubSet(pi, dclGet(cl_right, j), dclGet(cl_left, i)) != 0 )
  3906. {
  3907. dclSetFlag(cl_left, i);
  3908. if ( dclSCCAddAndSetFlag(pi, cl_c, dclGet(cl_left, i)) == 0 )
  3909. return dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3910. if ( dcIsEqual(pi, dclGet(cl_left, i), dclGet(cl_right, j)) != 0 )
  3911. dclSetFlag(cl_right, j);
  3912. }
  3913. }
  3914. dclDeleteCubesWithFlag(pi, cl_left);
  3915. dclDeleteCubesWithFlag(pi, cl_right);
  3916. dclDeleteCubesWithFlag(pi, cl_c);
  3917. if ( dclIntersection(pi, cl_left, cof_left) == 0 )
  3918. return dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3919. if ( dclIntersection(pi, cl_right, cof_right) == 0 )
  3920. return dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3921. dclClear(cl);
  3922. if ( dclJoin(pi, cl, cl_left) == 0 )
  3923. return dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3924. if ( dclJoin(pi, cl, cl_right) == 0 )
  3925. return dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3926. if ( dclJoin(pi, cl, cl_c) == 0 )
  3927. return dclDestroyVA(3, cl_left, cl_right, cl_c), 0;
  3928. return dclDestroyVA(3, cl_left, cl_right, cl_c), 1;
  3929. }
  3930. int dclComplementWithURP(pinfo *pi, dclist cl)
  3931. {
  3932. int result;
  3933. dcube *cof = &(pi->tmp[2]);
  3934. if ( dclSCC(pi, cl) == 0 )
  3935. return 0;
  3936. dcSetTautology(pi, cof);
  3937. pinfoBTreeInit(pi, "Complement URP");
  3938. result = dclComplementCof(pi, cl, cof, 0);
  3939. pinfoBTreeFinish(pi);
  3940. return result;
  3941. }
  3942. int dclComplement(pinfo *pi, dclist cl)
  3943. {
  3944. return dclComplementWithURP(pi, cl);
  3945. }
  3946. /*-- dclPrimes --------------------------------------------------------------*/
  3947. int dclPrimesCof(pinfo *pi, dclist cl, dcube *cof, int depth)
  3948. {
  3949. dclist cl_left, cl_right;
  3950. dcube *cof_left = &(pi->stack1[depth]);
  3951. dcube *cof_right = &(pi->stack2[depth]);
  3952. int left_cnt, right_cnt;
  3953. if (depth >= PINFO_STACK_CUBES)
  3954. return 0;
  3955. if ( dclCnt(cl) <= 1 )
  3956. return 1;
  3957. if ( dcGetCofactorForSplit(pi, cof_left, cof_right, cl, cof) == 0 )
  3958. return 1;
  3959. if ( dclInitCachedVA(pi, 2, &cl_left, &cl_right) == 0 )
  3960. return 0;
  3961. if ( dclSCCCofactor(pi, cl_left, cl, cof_left) == 0 )
  3962. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3963. if ( dclSCCCofactor(pi, cl_right, cl, cof_right) == 0 )
  3964. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3965. left_cnt = dclCnt(cl_left);
  3966. right_cnt = dclCnt(cl_right);
  3967. pinfoBTreeStart(pi);
  3968. if ( dclPrimesCof(pi, cl_left, cof_left, depth+1) == 0 )
  3969. return pinfoBTreeEnd(pi), dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3970. if ( dclPrimesCof(pi, cl_right, cof_right, depth+1) == 0 )
  3971. return pinfoBTreeEnd(pi), dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3972. pinfoBTreeEnd(pi);
  3973. if ( dclIntersection(pi, cl_left, cof_left) == 0 )
  3974. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3975. if ( dclIntersection(pi, cl_right, cof_right) == 0 )
  3976. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3977. /*
  3978. printf("Split: %d -> %d + %d (%d + %d) \n", dclCnt(cl), left_cnt, right_cnt, dclCnt(cl_left), dclCnt(cl_right));
  3979. */
  3980. if ( dcIsInTautology(pi, cof_left) != 0 )
  3981. {
  3982. dclSortOutSize(pi, cl_left);
  3983. dclSortOutSize(pi, cl_right);
  3984. }
  3985. else
  3986. {
  3987. dclSortInSize(pi, cl_left);
  3988. dclSortInSize(pi, cl_right);
  3989. }
  3990. /* dclClear(cl); */
  3991. if ( dclConsensus(pi, cl, cl_left, cl_right) == 0 )
  3992. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3993. if ( dclSCCUnionSubset(pi, cl, cl_left) == 0 )
  3994. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3995. if ( dclSCCUnionSubset(pi, cl, cl_right) == 0 )
  3996. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  3997. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 1;
  3998. }
  3999. int dclAddNoneSubsetList(pinfo *pi, dclist dest, dclist src, dcube *l, dcube *r)
  4000. {
  4001. int i, cnt = dclCnt(src);
  4002. for( i = 0; i < cnt; i++ )
  4003. {
  4004. if ( dcIsSubSet(pi, l, dclGet(src, i)) != 0 )
  4005. continue;
  4006. if ( dcIsSubSet(pi, r, dclGet(src, i)) != 0 )
  4007. continue;
  4008. if ( dclAdd(pi, dest, dclGet(src, i)) < 0 )
  4009. return 0;
  4010. }
  4011. return 1;
  4012. }
  4013. int xdclPrimesCof(pinfo *pi, dclist cl, dcube *cof, int depth)
  4014. {
  4015. dclist cl_l_sub, cl_r_sub, cl_l_red, cl_r_red, cl_c;
  4016. dcube *cof_left = &(pi->stack1[depth]);
  4017. dcube *cof_right = &(pi->stack2[depth]);
  4018. int left_cnt, right_cnt;
  4019. if (depth >= PINFO_STACK_CUBES)
  4020. return 0;
  4021. if ( dclCnt(cl) <= 1 )
  4022. return 1;
  4023. if ( dcGetCofactorForSplit(pi, cof_left, cof_right, cl, cof) == 0 )
  4024. return 1;
  4025. dclInitVA(5, &cl_l_sub, &cl_r_sub, &cl_l_red, &cl_r_red, &cl_c);
  4026. pinfoDepth(pi, "Primes", depth);
  4027. if ( dcIsInTautology(pi, cof_left) != 0 )
  4028. {
  4029. /* output split */
  4030. dclAddSubCofactor(pi, cl_l_sub, cl, cof_left);
  4031. dclAddSubCofactor(pi, cl_r_sub, cl, cof_right);
  4032. dclAddRedCofactor(pi, cl_l_red, cl, cof_left);
  4033. dclAddRedCofactor(pi, cl_r_red, cl, cof_right);
  4034. left_cnt = dclCnt(cl_l_sub) + dclCnt(cl_l_red);
  4035. right_cnt = dclCnt(cl_r_sub) + dclCnt(cl_r_red);
  4036. dclSCC(pi, cl_l_red);
  4037. dclSCC(pi, cl_r_red);
  4038. dclJoin(pi, cl_l_red, cl_l_sub);
  4039. dclJoin(pi, cl_r_red, cl_r_sub);
  4040. dclPrimesCof(pi, cl_l_red, cof_left, depth+1);
  4041. dclPrimesCof(pi, cl_r_red, cof_right, depth+1);
  4042. dclIntersection(pi, cl_l_red, cof_left);
  4043. dclIntersection(pi, cl_r_red, cof_right);
  4044. printf("Split: %d -> %d + %d (%d + %d) \n", dclCnt(cl), left_cnt,
  4045. right_cnt, dclCnt(cl_l_red), dclCnt(cl_r_red));
  4046. dclClear(cl);
  4047. dclConsensus(pi, cl, cl_l_red, cl_r_red);
  4048. dclSCCUnionSubset(pi, cl, cl_l_red);
  4049. dclSCCUnionSubset(pi, cl, cl_r_red);
  4050. }
  4051. else
  4052. {
  4053. dclAddSubCofactor(pi, cl_l_sub, cl, cof_left);
  4054. dclAddSubCofactor(pi, cl_r_sub, cl, cof_right);
  4055. dclAddNoneSubsetList(pi, cl_c, cl, cof_left, cof_right);
  4056. assert(dclCnt(cl_l_sub)+dclCnt(cl_r_sub)+dclCnt(cl_c) == dclCnt(cl));
  4057. left_cnt = dclCnt(cl_l_sub) + dclCnt(cl_l_red);
  4058. right_cnt = dclCnt(cl_r_sub) + dclCnt(cl_r_red);
  4059. /* input split */
  4060. dclJoin(pi, cl_l_sub, cl_c);
  4061. dclJoin(pi, cl_r_sub, cl_c);
  4062. dclPrimesCof(pi, cl_l_sub, cof_left, depth+1);
  4063. dclPrimesCof(pi, cl_r_sub, cof_right, depth+1);
  4064. dclIntersection(pi, cl_l_sub, cof_left);
  4065. dclIntersection(pi, cl_r_sub, cof_right);
  4066. puts(dcToStr(pi, cof_left, " ", ""));
  4067. printf("Split: %d -> %d + %d (%d + %d) \n", dclCnt(cl), left_cnt,
  4068. right_cnt, dclCnt(cl_l_sub), dclCnt(cl_r_sub));
  4069. dclClear(cl);
  4070. dclConsensus(pi, cl, cl_l_sub, cl_r_sub);
  4071. dclSCCUnion(pi, cl, cl_c);
  4072. dclSCCUnionSubset(pi, cl, cl_l_sub);
  4073. dclSCCUnionSubset(pi, cl, cl_r_sub);
  4074. }
  4075. return dclDestroyVA(5, cl_l_sub, cl_r_sub, cl_l_red, cl_r_red, cl_c), 1;
  4076. }
  4077. int dclPrimes(pinfo *pi, dclist cl)
  4078. {
  4079. int result;
  4080. dcube *cof = &(pi->tmp[2]);
  4081. if ( dclSCC(pi, cl) == 0 )
  4082. return 0;
  4083. dcSetTautology(pi, cof);
  4084. pinfoBTreeInit(pi, "Primes");
  4085. result = dclPrimesCof(pi, cl, cof, 0);
  4086. pinfoBTreeFinish(pi);
  4087. return result;
  4088. }
  4089. /*-- dclPrimesDC ------------------------------------------------------------*/
  4090. int dclPrimesDC(pinfo *pi, dclist cl, dclist cl_dc)
  4091. {
  4092. if ( cl_dc != NULL )
  4093. if ( dclJoin(pi, cl, cl_dc) == 0 )
  4094. return 0;
  4095. if ( dclPrimes(pi, cl) == 0 )
  4096. return 0;
  4097. if ( cl_dc != NULL )
  4098. if ( dclRemoveSubSet(pi, cl, cl_dc, NULL) == 0 )
  4099. return 0;
  4100. return 1;
  4101. }
  4102. /*-- dclPrimesInv -----------------------------------------------------------*/
  4103. /*
  4104. A(L)
  4105. A(L) = SCCINV(x A(L_x) + !x (L_!x) + INTERSECTION(x A(L_x), !x (L_!x)))
  4106. */
  4107. static int dcPrimesInvGetCofactorForSplit(pinfo *pi, dcube *l, dcube *r, dclist cl, dcube *cof)
  4108. {
  4109. /* geht nicht --- may be, but why and... is this true???
  4110. if ( pinfoGetInVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  4111. return 1;
  4112. */
  4113. /*
  4114. if ( dcGetBinateInVarCofactor(pi, l, r, cl, cof) != 0 )
  4115. return 1;
  4116. if ( dcGetOutVarCofactor(pi, l, r, cl, cof) != 0 )
  4117. return 1;
  4118. if ( pinfoGetInVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  4119. return 1;
  4120. if ( dcGetBinateInVarCofactor(pi, l, r, cl, cof) != 0 )
  4121. return 1;
  4122. */
  4123. if ( pinfoGetInVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  4124. return 1;
  4125. /*
  4126. if ( dcGetBinateInVarCofactor(pi, l, r, cl, cof) != 0 )
  4127. return 1;
  4128. */
  4129. if ( pinfoGetOutVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  4130. return 1;
  4131. return 0;
  4132. }
  4133. int dclPrimesInvCof(pinfo *pi, dclist cl, dcube *cof, int depth)
  4134. {
  4135. dclist cl_left, cl_right;
  4136. dcube *cof_left = &(pi->stack1[depth]);
  4137. dcube *cof_right = &(pi->stack2[depth]);
  4138. int left_cnt, right_cnt;
  4139. if (depth >= PINFO_STACK_CUBES)
  4140. return 0;
  4141. if ( dclCnt(cl) <= 1 )
  4142. return 1;
  4143. if ( dcPrimesInvGetCofactorForSplit(pi, cof_left, cof_right, cl, cof) == 0 )
  4144. {
  4145. if ( dclCnt(cl) >= 2 )
  4146. {
  4147. int i;
  4148. for( i = 1; i < dclCnt(cl); i++ )
  4149. {
  4150. dcIntersection(pi, dclGet(cl, 0), dclGet(cl, 0), dclGet(cl, i));
  4151. dclSetFlag(cl, i);
  4152. }
  4153. dclDeleteCubesWithFlag(pi, cl);
  4154. if ( dcIsIllegal(pi, dclGet(cl, 0)) != 0 )
  4155. return 0; /* should never occur, but happend on 9 oct 2002 */
  4156. }
  4157. return 1;
  4158. }
  4159. if ( dclInitCachedVA(pi, 2, &cl_left, &cl_right) == 0 )
  4160. return 0;
  4161. if ( dclSCCInvCofactor(pi, cl_left, cl, cof_left) == 0 )
  4162. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4163. if ( dclSCCInvCofactor(pi, cl_right, cl, cof_right) == 0 )
  4164. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4165. left_cnt = dclCnt(cl_left);
  4166. right_cnt = dclCnt(cl_right);
  4167. pinfoBTreeStart(pi);
  4168. if ( dclPrimesInvCof(pi, cl_left, cof_left, depth+1) == 0 )
  4169. return pinfoBTreeEnd(pi), dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4170. if ( dclPrimesInvCof(pi, cl_right, cof_right, depth+1) == 0 )
  4171. return pinfoBTreeEnd(pi), dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4172. pinfoBTreeEnd(pi);
  4173. if ( dclIntersection(pi, cl_left, cof_left) == 0 )
  4174. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4175. if ( dclIntersection(pi, cl_right, cof_right) == 0 )
  4176. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4177. dclClear(cl);
  4178. /*
  4179. if ( dclIntersectionListInv(pi, cl, cl_left, cl_right) == 0 )
  4180. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4181. */
  4182. if ( dclSCCInvUnionSubset(pi, cl, cl_left) == 0 )
  4183. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4184. if ( dclSCCInvUnionSubset(pi, cl, cl_right) == 0 )
  4185. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4186. /*
  4187. printf("Split: %d -> %d + %d (%d + %d) \n", dclCnt(cl), left_cnt, right_cnt, dclCnt(cl_left), dclCnt(cl_right));
  4188. */
  4189. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 1;
  4190. }
  4191. int dclPrimesInv(pinfo *pi, dclist cl)
  4192. {
  4193. int result;
  4194. dcube *cof = &(pi->tmp[2]);
  4195. dclSCCInv(pi, cl);
  4196. dcSetTautology(pi, cof);
  4197. pinfoBTreeInit(pi, "PrimesInv");
  4198. result = dclPrimesInvCof(pi, cl, cof, 0);
  4199. pinfoBTreeFinish(pi);
  4200. return result;
  4201. }
  4202. /*-- dclIsEssential ---------------------------------------------------------*/
  4203. int dclIsEssential(pinfo *pi, dclist cl_on, dclist cl_dc, dcube *a)
  4204. {
  4205. dclist cl;
  4206. if ( dclInitCached(pi, &cl) == 0 )
  4207. return 0;
  4208. if ( dclCopy(pi, cl, cl_on) == 0 )
  4209. return dclDestroyCached(pi, cl), 0;
  4210. if ( cl_dc != NULL )
  4211. if ( dclJoin(pi, cl, cl_dc) == 0 )
  4212. return dclDestroyCached(pi, cl), 0;
  4213. if ( dclSubtractCube(pi, cl, a) == 0 )
  4214. return dclDestroyCached(pi, cl), 0;
  4215. if ( dclConsensusCube(pi, cl, a) == 0 )
  4216. return dclDestroyCached(pi, cl), 0;
  4217. if ( cl_dc != NULL )
  4218. if ( dclJoin(pi, cl, cl_dc) == 0 )
  4219. return dclDestroyCached(pi, cl), 0;
  4220. if ( dclIsSubSet(pi, cl, a) != 0 )
  4221. return dclDestroyCached(pi, cl), 0;
  4222. return dclDestroyCached(pi, cl), 1; /* essential prime */
  4223. }
  4224. /*-- dclIsRelativeEssential -------------------------------------------------*/
  4225. int dclIsRelativeEssential(pinfo *pi, dclist cl, int pos)
  4226. {
  4227. dclist cl_tmp;
  4228. if ( dclInitCached(pi, &cl_tmp) == 0 )
  4229. return 0;
  4230. if ( dclSCCCofactorExcept(pi, cl_tmp, cl, dclGet(cl, pos), pos) == 0 )
  4231. return dclDestroyCached(pi, cl_tmp), 0;
  4232. if ( dclTautology(pi, cl_tmp) != 0 )
  4233. return dclDestroyCached(pi, cl_tmp), 0;
  4234. return dclDestroyCached(pi, cl_tmp), 1;
  4235. }
  4236. int dclIsRelativeEssentialDC(pinfo *pi, dclist cl, int pos, dclist cl_dc)
  4237. {
  4238. dclist cl_tmp;
  4239. int i;
  4240. if ( dclInitCached(pi, &cl_tmp) == 0 )
  4241. return 0;
  4242. for( i = 0; i < dclCnt(cl); i++ )
  4243. if ( i != pos )
  4244. if ( dclAdd(pi, cl_tmp, dclGet(cl, i)) < 0 )
  4245. return dclDestroyCached(pi, cl_tmp), 0;
  4246. if ( cl_dc != NULL )
  4247. if ( dclJoin(pi, cl_tmp, cl_dc) == 0 )
  4248. return dclDestroyCached(pi, cl_tmp), 0;
  4249. if ( dclIsSubSet(pi, cl_tmp, dclGet(cl, pos)) != 0)
  4250. return dclDestroyCached(pi, cl_tmp), 0;
  4251. return dclDestroyCached(pi, cl_tmp), 1; /* relative essential */
  4252. }
  4253. /*-- dclEssential -----------------------------------------------------------*/
  4254. int dclEssential(pinfo *pi, dclist cl_es, dclist cl_nes, dclist cl, dclist cl_on, dclist cl_dc)
  4255. {
  4256. int i, cnt;
  4257. if ( cl_es != NULL )
  4258. dclClear(cl_es);
  4259. if ( cl_nes != NULL )
  4260. dclClear(cl_nes);
  4261. if ( cl_on == NULL && cl == NULL )
  4262. return 0;
  4263. if ( cl_on == NULL )
  4264. cl_on = cl;
  4265. if ( cl == NULL )
  4266. cl = cl_on;
  4267. cnt = dclCnt(cl);
  4268. pinfoProcedureInit(pi, "Essential", cnt);
  4269. for( i = 0; i < cnt; i++ )
  4270. {
  4271. if ( pinfoProcedureDo(pi, i) == 0 )
  4272. return pinfoProcedureFinish(pi), 0;
  4273. if ( dclIsEssential(pi, cl, cl_dc, dclGet(cl, i)) != 0 )
  4274. {
  4275. if ( cl_es != NULL )
  4276. if ( dclAdd(pi, cl_es, dclGet(cl, i)) < 0 )
  4277. return pinfoProcedureFinish(pi), 0;
  4278. }
  4279. else
  4280. {
  4281. if ( cl_nes != NULL )
  4282. if ( dclAdd(pi, cl_nes, dclGet(cl, i)) < 0 )
  4283. return pinfoProcedureFinish(pi), 0;
  4284. }
  4285. }
  4286. pinfoProcedureFinish(pi);
  4287. return 1;
  4288. }
  4289. /*-- dclRelativeEssential --------------------------------------------------*/
  4290. int dclRelativeEssential(pinfo *pi, dclist cl_es, dclist cl_nes, dclist cl, dclist cl_dc)
  4291. {
  4292. int i, cnt;
  4293. dclist cl_tmp;
  4294. if ( dclInitCached(pi, &cl_tmp) == 0 )
  4295. return 0;
  4296. if ( cl_es != NULL )
  4297. dclClear(cl_es);
  4298. if ( cl_nes != NULL )
  4299. dclClear(cl_nes);
  4300. if ( dclCopy(pi, cl_tmp, cl) == 0 )
  4301. return dclDestroyCached(pi, cl_tmp), 0;
  4302. if ( cl_dc != NULL )
  4303. if ( dclJoin(pi, cl_tmp, cl_dc) == 0 )
  4304. return dclDestroyCached(pi, cl_tmp), 0;
  4305. cnt = dclCnt(cl); /* !!! sic !!! */
  4306. pinfoProcedureInit(pi, "Essential", cnt);
  4307. for( i = 0; i < cnt; i++ )
  4308. {
  4309. if ( pinfoProcedureDo(pi, i) == 0 )
  4310. return dclDestroyCached(pi, cl_tmp), pinfoProcedureFinish(pi), 0;
  4311. if ( dclIsRelativeEssential(pi, cl_tmp, i) != 0 )
  4312. {
  4313. if ( cl_es != NULL )
  4314. if ( dclAdd(pi, cl_es, dclGet(cl, i)) < 0 )
  4315. return dclDestroyCached(pi, cl_tmp), pinfoProcedureFinish(pi), 0;
  4316. }
  4317. else
  4318. {
  4319. if ( cl_nes != NULL )
  4320. if ( dclAdd(pi, cl_nes, dclGet(cl, i)) < 0 )
  4321. return dclDestroyCached(pi, cl_tmp), pinfoProcedureFinish(pi), 0;
  4322. }
  4323. }
  4324. dclDestroyCached(pi, cl_tmp);
  4325. pinfoProcedureFinish(pi);
  4326. return 1;
  4327. }
  4328. /*-- dclGetEssential --------------------------------------------------------*/
  4329. int dclGetEssential(pinfo *pi, dclist cl, dclist cl_on, dclist cl_dc)
  4330. {
  4331. dclist cl_es;
  4332. if ( cl_on == NULL )
  4333. cl_on = cl;
  4334. if ( dclInit(&cl_es) == 0 )
  4335. return 0;
  4336. if ( dclEssential(pi, cl_es, NULL, cl, cl_on, cl_dc) == 0 )
  4337. return dclDestroy(cl_es), 0;
  4338. if ( dclCopy(pi, cl, cl_es) == 0 )
  4339. return dclDestroy(cl_es), 0;
  4340. return dclDestroy(cl_es), 1;
  4341. }
  4342. /*-- dclSplitEssential ------------------------------------------------------*/
  4343. /*
  4344. Spaltet eine function auf in:
  4345. - essentielle cubes [cl_es]
  4346. - vollkommen redundante cubes [cl_fr] (cl_vr wird von cl_es ueberdeckt)
  4347. - teilweise redundaten cubes [cl_pr] (die uebrigen cubes)
  4348. */
  4349. int dclSplitEssential(pinfo *pi, dclist cl_es, dclist cl_fr, dclist cl_pr, dclist cl, dclist cl_on, dclist cl_dc)
  4350. {
  4351. int i, cnt;
  4352. dclist cl_es_dc;
  4353. dclClear(cl_es);
  4354. dclClear(cl_pr);
  4355. if ( cl_fr != NULL )
  4356. dclClear(cl_fr);
  4357. /*
  4358. if ( dclEssential(pi, cl_es, cl_pr, cl, cl_on, cl_dc) == 0 )
  4359. return dclClear(cl_es), dclClear(cl_pr), 0;
  4360. */
  4361. if ( dclRelativeEssential(pi, cl_es, cl_pr, cl, cl_dc) == 0 )
  4362. return dclClear(cl_es), dclClear(cl_pr), 0;
  4363. if ( dclClearFlags(cl_pr) == 0 )
  4364. return dclClear(cl_es), dclClear(cl_pr), 0;
  4365. if ( dclInit(&cl_es_dc) == 0 )
  4366. return dclClear(cl_es), dclClear(cl_pr), 0;
  4367. if ( dclCopy(pi, cl_es_dc, cl_es) == 0 )
  4368. return dclClear(cl_es), dclClear(cl_pr), dclDestroy(cl_es_dc), 0;
  4369. if ( cl_dc != NULL )
  4370. if ( dclJoin(pi, cl_es_dc, cl_dc) == 0 )
  4371. return dclClear(cl_es), dclClear(cl_pr), dclDestroy(cl_es_dc), 0;
  4372. cnt = dclCnt(cl_pr);
  4373. pinfoProcedureInit(pi, "SplitEssential", cnt);
  4374. for( i = 0; i < cnt; i++ )
  4375. {
  4376. if ( pinfoProcedureDo(pi, i) == 0 )
  4377. return pinfoProcedureFinish(pi), 0;
  4378. if ( dclIsSubSet(pi, cl_es_dc, dclGet(cl_pr, i)) != 0 )
  4379. {
  4380. if ( cl_fr != NULL )
  4381. {
  4382. if ( dclAdd(pi, cl_fr, dclGet(cl_pr, i)) < 0 )
  4383. {
  4384. dclClear(cl_es);
  4385. dclClear(cl_pr);
  4386. dclDestroy(cl_es_dc);
  4387. pinfoProcedureFinish(pi);
  4388. return 0;
  4389. }
  4390. }
  4391. dclSetFlag(cl_pr, i);
  4392. }
  4393. }
  4394. pinfoProcedureFinish(pi);
  4395. dclDeleteCubesWithFlag(pi, cl_pr);
  4396. dclDestroy(cl_es_dc);
  4397. return 1;
  4398. }
  4399. /*-- dclSplitRelativEssential ----------------------------------------------*/
  4400. /*
  4401. Spaltet eine function auf in:
  4402. - essentielle cubes [cl_es]
  4403. - vollkommen redundante cubes [cl_fr] (cl_vr wird von cl_es ueberdeckt,
  4404. darf aber kein element aus cl_rc ueberdecken)
  4405. - teilweise redundaten cubes [cl_pr] (die uebrigen cubes)
  4406. */
  4407. int dclSplitRelativeEssential(pinfo *pi, dclist cl_es, dclist cl_fr, dclist cl_pr, dclist cl, dclist cl_dc, dclist cl_rc)
  4408. {
  4409. int i, cnt;
  4410. int j;
  4411. dclist cl_es_dc;
  4412. dclClear(cl_es);
  4413. dclClear(cl_pr);
  4414. if ( cl_fr != NULL )
  4415. dclClear(cl_fr);
  4416. if ( dclRelativeEssential(pi, cl_es, cl_pr, cl, cl_dc) == 0 )
  4417. return dclClear(cl_es), dclClear(cl_pr), 0;
  4418. if ( dclClearFlags(cl_pr) == 0 )
  4419. return dclClear(cl_es), dclClear(cl_pr), 0;
  4420. if ( dclInit(&cl_es_dc) == 0 )
  4421. return dclClear(cl_es), dclClear(cl_pr), 0;
  4422. if ( dclCopy(pi, cl_es_dc, cl_es) == 0 )
  4423. return dclClear(cl_es), dclClear(cl_pr), dclDestroy(cl_es_dc), 0;
  4424. if ( cl_dc != NULL )
  4425. if ( dclJoin(pi, cl_es_dc, cl_dc) == 0 )
  4426. return dclClear(cl_es), dclClear(cl_pr), dclDestroy(cl_es_dc), 0;
  4427. cnt = dclCnt(cl_pr);
  4428. pinfoProcedureInit(pi, "SplitRelativeEssential", cnt);
  4429. for( i = 0; i < cnt; i++ )
  4430. {
  4431. if ( pinfoProcedureDo(pi, i) == 0 )
  4432. return dclDestroy(cl_es_dc), pinfoProcedureFinish(pi), 0;
  4433. if ( cl_rc != NULL )
  4434. {
  4435. for( j = 0; j < dclCnt(cl_rc); j++ )
  4436. if ( dcIsSubSet(pi, dclGet(cl_pr, i), dclGet(cl_rc, j)) != 0 )
  4437. break;
  4438. if ( j < dclCnt(cl_rc) )
  4439. continue; /* do not remove the pr prime, go to next one */
  4440. }
  4441. if ( dclIsSubSet(pi, cl_es_dc, dclGet(cl_pr, i)) != 0 )
  4442. {
  4443. if ( cl_fr != NULL )
  4444. {
  4445. if ( dclAdd(pi, cl_fr, dclGet(cl_pr, i)) < 0 )
  4446. {
  4447. dclClear(cl_es);
  4448. dclClear(cl_pr);
  4449. dclDestroy(cl_es_dc);
  4450. pinfoProcedureFinish(pi);
  4451. return 0;
  4452. }
  4453. }
  4454. dclSetFlag(cl_pr, i);
  4455. }
  4456. }
  4457. pinfoProcedureFinish(pi);
  4458. dclDestroy(cl_es_dc);
  4459. dclDeleteCubesWithFlag(pi, cl_pr);
  4460. return 1;
  4461. }
  4462. /*-- dclIrredundantMark -----------------------------------------------------*/
  4463. /*
  4464. * static int dclIrredundantStoreList(pinfo *pi_m, dclist cl_m, pinfo *pi, int n, dclist cl)
  4465. * {
  4466. * dcube *c = pi_m->tmp+9;
  4467. * int i, cnt = dclCnt(cl);
  4468. *
  4469. * dcInSetAll(pi_m, c, CUBE_IN_MASK_DC);
  4470. * dcOutSetAll(pi_m, c, 0);
  4471. * dcSetOut(c, n, 1);
  4472. * for( i = 0; i < cnt; i++ )
  4473. * if ( dcIsTautology(pi, dclGet(cl, i)) != 0 )
  4474. * if ( dclGet(cl, i)->n >= 0 )
  4475. * dcSetOut(c, dclGet(cl, i)->n, 1);
  4476. * if ( dclSCCInvAddAndSetFlag(pi_m, cl_m, c) == 0 )
  4477. * return 0;
  4478. * return 1;
  4479. * }
  4480. */
  4481. static int dcIrredundantGetCofactorForSplit(pinfo *pi, dcube *l, dcube *r, dclist cl, dcube *cof)
  4482. {
  4483. if ( pinfoGetInVarDCubeCofactor(pi, l, r, cl, cof) != 0 )
  4484. return 1;
  4485. /*
  4486. if ( dcGetBinateInVarCofactor(pi, l, r, cl, cof) != 0 )
  4487. return 1;
  4488. */
  4489. if ( dcGetOutVarCofactor(pi, l, r, cl, cof) != 0 )
  4490. return 1;
  4491. return 0;
  4492. }
  4493. static int dclIrredundantCofactor(pinfo *pi, dclist dest, dclist src, dcube *cofactor, dcube *mark)
  4494. {
  4495. int i, cnt = dclCnt(src);
  4496. dcube *r = &(pi->tmp[1]);
  4497. dclClear(dest);
  4498. for ( i = 0; i < cnt; i++ )
  4499. if ( dcCofactor(pi, r, dclGet(src, i), cofactor) != 0 )
  4500. {
  4501. if ( dcIsTautology(pi, r) != 0 && r->n >= 0 )
  4502. {
  4503. dcSetOut(mark, r->n, 1);
  4504. }
  4505. else
  4506. {
  4507. if ( dclAdd(pi, dest, r) < 0 )
  4508. return 0;
  4509. }
  4510. }
  4511. return 1;
  4512. }
  4513. int dclIrredundantMarkTautCof(pinfo *pi_m, dclist cl_m, pinfo *pi, dclist cl, dcube *cof, dcube *mark, int depth)
  4514. {
  4515. dclist cl_left, cl_right;
  4516. dcube *cofactor_left = &(pi->stack1[depth]);
  4517. dcube *cofactor_right = &(pi->stack2[depth]);
  4518. dcube *mark_left = &(pi_m->stack1[depth]);
  4519. dcube *mark_right = &(pi_m->stack2[depth]);
  4520. int i, cnt;
  4521. cnt = dclCnt(cl);
  4522. if ( cnt == 0 )
  4523. {
  4524. if ( dclSCCInvAddAndSetFlag(pi_m, cl_m, mark) == 0 )
  4525. return 0;
  4526. return 1;
  4527. }
  4528. {
  4529. dcube *c;
  4530. for( i = 0; i < cnt; i++ )
  4531. {
  4532. c = dclGet(cl, i);
  4533. /* wenn tautologie und element der DC oder essentiell set, dann abbruch */
  4534. if ( dcIsTautology(pi, c) != 0 && c->n < 0 )
  4535. {
  4536. return 1;
  4537. }
  4538. }
  4539. /* ueberpruefen, ob ueberhaupt noch partiell redundante terme vorhanden sind */
  4540. /*
  4541. for( i = 0; i < cnt; i++ )
  4542. {
  4543. if ( dclGet(cl, i)->n >= 0 )
  4544. break;
  4545. }
  4546. if ( i == cnt )
  4547. return 1;
  4548. */
  4549. }
  4550. if ( dcIrredundantGetCofactorForSplit(pi, cofactor_left, cofactor_right, cl, cof) == 0 )
  4551. {
  4552. /* alle markierten elemente sind teil der loesung */
  4553. if ( dclSCCInvAddAndSetFlag(pi_m, cl_m, mark) == 0 )
  4554. return 0;
  4555. return 1;
  4556. }
  4557. dcCopy(pi_m, mark_left, mark);
  4558. dcCopy(pi_m, mark_right, mark);
  4559. if ( dclInitCachedVA(pi, 2, &cl_left, &cl_right) == 0 )
  4560. return 0;
  4561. if (depth >= PINFO_STACK_CUBES)
  4562. return 0;
  4563. if ( dclIrredundantCofactor(pi, cl_left, cl, cofactor_left, mark_left) == 0 )
  4564. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4565. if ( dclIrredundantCofactor(pi, cl_right, cl, cofactor_right, mark_right) == 0 )
  4566. return dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4567. /*pinfoBTreeStart(pi);*/
  4568. if ( dclIrredundantMarkTautCof(pi_m, cl_m, pi, cl_left, cofactor_left, mark_left, depth+1) == 0 )
  4569. return /*pinfoBTreeEnd(pi),*/ dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4570. if ( dclIrredundantMarkTautCof(pi_m, cl_m, pi, cl_right, cofactor_right, mark_right, depth+1) == 0 )
  4571. return /*pinfoBTreeEnd(pi),*/ dclDestroyCachedVA(pi, 2, cl_left, cl_right), 0;
  4572. return /*pinfoBTreeEnd(pi),*/ dclDestroyCachedVA(pi, 2, cl_left, cl_right), 1;
  4573. }
  4574. int dclIrredundantMarkTaut(pinfo *pi_m, dclist cl_m, pinfo *pi, int n, dclist cl)
  4575. {
  4576. int result;
  4577. dcube *cof = &(pi->tmp[2]);
  4578. dcube *mark = &(pi_m->tmp[2]);
  4579. dcSetTautology(pi, cof);
  4580. dcInSetAll(pi_m, mark, CUBE_IN_MASK_DC);
  4581. dcOutSetAll(pi_m, mark, 0);
  4582. dcSetOut(mark, n, 1);
  4583. /* pinfoBTreeInit(pi, "TautologyMark"); */
  4584. result = dclIrredundantMarkTautCof(pi_m, cl_m, pi, cl, cof, mark, 0);
  4585. /* pinfoBTreeFinish(pi); */
  4586. return result;
  4587. }
  4588. /*
  4589. Es wird der cube dclGet(cl_pr, pos) betrachtet.
  4590. Diese Routine markiert diejenige Elemente in cl_pr,
  4591. die in ihrer Vereinung dclGet(cl_pr, pos) ueberdecken.
  4592. cl_es und cl_dc koennen auch NULL sein.
  4593. */
  4594. int dclIrredundantMark(pinfo *pi_m, dclist cl_m, pinfo *pi, dclist cl_pr, int pos, dclist cl_es, dclist cl_dc)
  4595. {
  4596. dclist cl_u;
  4597. int i, added_index;
  4598. if ( dclInit(&cl_u) == 0 )
  4599. return 0;
  4600. /* Alle Elemente aus cl_pr, bis auf das Element 'pos' */
  4601. for( i = 0; i < dclCnt(cl_pr); i++ )
  4602. {
  4603. if ( i != pos )
  4604. {
  4605. added_index = dclAdd(pi, cl_u, dclGet(cl_pr, i));
  4606. if ( added_index < 0 )
  4607. return dclDestroy(cl_u), 0;
  4608. dclGet(cl_u, added_index)->n = i;
  4609. }
  4610. }
  4611. /* Alle Elemente aus cl_es */
  4612. if ( cl_es != NULL )
  4613. for( i = 0; i < dclCnt(cl_es); i++ )
  4614. {
  4615. added_index = dclAdd(pi, cl_u, dclGet(cl_es, i));
  4616. if ( added_index < 0 )
  4617. return dclDestroy(cl_u), 0;
  4618. dclGet(cl_u, added_index)->n = -1;
  4619. }
  4620. /* Alle Elemente aus cl_dc */
  4621. if ( cl_dc != NULL )
  4622. for( i = 0; i < dclCnt(cl_dc); i++ )
  4623. {
  4624. added_index = dclAdd(pi, cl_u, dclGet(cl_dc, i));
  4625. if ( added_index < 0 )
  4626. return dclDestroy(cl_u), 0;
  4627. dclGet(cl_u, added_index)->n = -1;
  4628. }
  4629. /* Berechne den Kofaktor (hmm... koennte man auch gleich machen) */
  4630. dclClearFlags(cl_u);
  4631. for ( i = 0; i < dclCnt(cl_u); i++ )
  4632. if ( dcCofactor(pi, dclGet(cl_u, i), dclGet(cl_u, i), dclGet(cl_pr, pos)) == 0 )
  4633. dclSetFlag(cl_u, i);
  4634. dclDeleteCubesWithFlag(pi, cl_u);
  4635. /* es wird auf jedenfall ein element drangehaengt */
  4636. /* Das problem ist, dass die liste nicht notwendigerweise */
  4637. /* frei von essentiellen oder relativ essentiellen cubes ist */
  4638. /*
  4639. if ( dclCnt(cl_u) == 0 || dclTautology(pi, cl_u) == 0 )
  4640. {
  4641. int p, t = 0;
  4642. dcube *c;
  4643. p = dclAddEmpty(pi_m, cl_m);
  4644. if ( p < 0 )
  4645. return 0;
  4646. c = dclGet(cl_m, p);
  4647. dcInSetAll(pi_m, c, CUBE_IN_MASK_DC);
  4648. dcOutSetAll(pi_m, c, 0);
  4649. dcSetOut(c, pos, 1);
  4650. }
  4651. else
  4652. */
  4653. {
  4654. dclClearFlags(cl_pr); /* obsolete? */
  4655. dclSetFlag(cl_pr, pos); /* obsolete? */
  4656. if ( dclIrredundantMarkTaut(pi_m, cl_m, pi, pos, cl_u) == 0 )
  4657. return dclDestroy(cl_u), 0;
  4658. }
  4659. return dclDestroy(cl_u), 1;
  4660. }
  4661. /*-- dclIrredundantMatrix ---------------------------------------------------*/
  4662. /*-- dclIrredundantDCubeMatrix ----------------------------------------------*/
  4663. /* diese routine erfordert vorher ZWINGEND den aufruf von dclSplitRelativeEssential */
  4664. /* cl_pr darf keine relativ essentiellen cubes enthalten */
  4665. int dclIrredundantDCubeMatrix(pinfo *pi_m, dclist cl_m, pinfo *pi, dclist cl_es, dclist cl_pr, dclist cl_dc)
  4666. {
  4667. int i, cnt = dclCnt(cl_pr);
  4668. if ( dclSetPinfoByLength(pi_m, cl_pr) == 0 )
  4669. return 0;
  4670. dclClear(cl_m);
  4671. dclClearFlags(cl_m);
  4672. pinfoProcedureInit(pi, "IrredundantDCubeMatrix", cnt);
  4673. for(i = 0; i < cnt; i++ )
  4674. {
  4675. if ( pinfoProcedureDo(pi, i) == 0 )
  4676. return pinfoProcedureFinish(pi), 0;
  4677. if ( dclIrredundantMark(pi_m, cl_m, pi, cl_pr, i, cl_es, cl_dc) == 0 )
  4678. return pinfoProcedureFinish(pi), 0;
  4679. }
  4680. /*
  4681. dclSCCInv(pi_m, cl_m);
  4682. */
  4683. dclDeleteCubesWithFlag(pi_m, cl_m);
  4684. return pinfoProcedureFinish(pi), 1;
  4685. }
  4686. /*-- dclMarkRequiredCube ----------------------------------------------------*/
  4687. int dclMarkRequiredCube(pinfo *pi_m, dclist cl_m, pinfo *pi, dclist cl_es, dclist cl_pr, dclist cl_rc)
  4688. {
  4689. int i, j;
  4690. int o;
  4691. dcube *mark = &(pi_m->tmp[2]);
  4692. dclClearFlags(cl_m);
  4693. for( i = 0; i < dclCnt(cl_rc); i++ )
  4694. {
  4695. /*
  4696. printf("--> required cube '%s'.\n", dcToStr(pi, dclGet(cl_rc, i), " ", ""));
  4697. */
  4698. for( o = 0; o < pi->out_cnt; o++ )
  4699. {
  4700. for( j = 0; j < dclCnt(cl_es); j++ )
  4701. if ( dcGetOut(dclGet(cl_es, j), o) != 0 )
  4702. if ( dcIsInSubSet(pi, dclGet(cl_es, j), dclGet(cl_rc, i)) != 0 )
  4703. break;
  4704. /* cube is part of an essential cube, consider next output */
  4705. if ( j < dclCnt(cl_es) )
  4706. {
  4707. /*
  4708. printf("----> %o: required cube '%s' is part of essential set.\n", o, dcToStr(pi, dclGet(cl_rc, i), " ", ""));
  4709. */
  4710. continue;
  4711. }
  4712. dcInSetAll(pi_m, mark, CUBE_IN_MASK_DC);
  4713. dcOutSetAll(pi_m, mark, 0);
  4714. for( j = 0; j < dclCnt(cl_pr); j++ )
  4715. {
  4716. if ( dcGetOut(dclGet(cl_pr, j), o) != 0 )
  4717. {
  4718. if ( dcIsInSubSet(pi, dclGet(cl_pr, j), dclGet(cl_rc, i)) != 0 )
  4719. {
  4720. dcSetOut(mark, j, 1);
  4721. /*
  4722. printf("----> %o: required cube '%s' is subset of '%s'.\n", o, dcToStr(pi, dclGet(cl_rc, i), " ", ""), dcToStr2(pi, dclGet(cl_pr, j), " ", ""));
  4723. */
  4724. }
  4725. }
  4726. }
  4727. if ( dcIsIllegal(pi_m, mark) == 0 )
  4728. {
  4729. /*
  4730. if ( o == 1 )
  4731. printf("--> %o: required cube '%s' added.\n", o, dcToStr(pi, dclGet(cl_rc, i), " ", ""));
  4732. */
  4733. if ( dclSCCInvAddAndSetFlag(pi_m, cl_m, mark) == 0 )
  4734. return 0;
  4735. }
  4736. else
  4737. {
  4738. /*
  4739. printf("----> %o: required cube '%s' NOT added.\n", o, dcToStr(pi, dclGet(cl_rc, i), " ", ""));
  4740. */
  4741. }
  4742. /*
  4743. if ( dcIsIllegal(pi_m, mark) == 0 )
  4744. if ( dclAdd(pi_m, cl_m, mark) < 0 )
  4745. return 0;
  4746. */
  4747. }
  4748. }
  4749. /* dclSCCInv(pi_m, cl_m); */
  4750. dclDeleteCubesWithFlag(pi_m, cl_m);
  4751. return 1;
  4752. }
  4753. int xxxdclMarkRequiredCube(pinfo *pi_m, dclist cl_m, pinfo *pi, dclist cl_es, dclist cl_pr, dclist cl_rc)
  4754. {
  4755. int i, j;
  4756. dcube *mark = &(pi_m->tmp[2]);
  4757. dclClearFlags(cl_m);
  4758. for( i = 0; i < dclCnt(cl_rc); i++ )
  4759. {
  4760. for( j = 0; j < dclCnt(cl_es); j++ )
  4761. if ( dcIsSubSet(pi, dclGet(cl_es, j), dclGet(cl_rc, i)) != 0 )
  4762. break;
  4763. /* cube is part of an essential cube, consider next output */
  4764. if ( j < dclCnt(cl_es) )
  4765. {
  4766. printf("--> required cube '%s' is part of essential set.\n", dcToStr(pi, dclGet(cl_rc, i), " ", ""));
  4767. continue;
  4768. }
  4769. dcInSetAll(pi_m, mark, CUBE_IN_MASK_DC);
  4770. dcOutSetAll(pi_m, mark, 0);
  4771. for( j = 0; j < dclCnt(cl_pr); j++ )
  4772. {
  4773. if ( dcIsSubSet(pi, dclGet(cl_pr, j), dclGet(cl_rc, i)) != 0 )
  4774. {
  4775. dcSetOut(mark, j, 1);
  4776. printf("--> required cube '%s' is subset of '%s'.\n", dcToStr(pi, dclGet(cl_rc, i), " ", ""), dcToStr2(pi, dclGet(cl_pr, j), " ", ""));
  4777. }
  4778. }
  4779. if ( dcIsIllegal(pi_m, mark) == 0 )
  4780. {
  4781. printf("--> required cube '%s' added.\n", dcToStr(pi, dclGet(cl_rc, i), " ", ""));
  4782. if ( dclSCCInvAddAndSetFlag(pi_m, cl_m, mark) == 0 )
  4783. return 0;
  4784. }
  4785. else
  4786. {
  4787. printf("--> required cube '%s' NOT added.\n", dcToStr(pi, dclGet(cl_rc, i), " ", ""));
  4788. }
  4789. }
  4790. dclDeleteCubesWithFlag(pi_m, cl_m);
  4791. return 1;
  4792. }
  4793. /*-- dclIrredundantDCubeTMatrix ---------------------------------------------*/
  4794. /* diese routine erfordert vorher ZWINGEND den aufruf von dclSplitRelativeEssential */
  4795. /* cl_pr darf keine relativ essentiellen cubes enthalten */
  4796. int dclIrredundantDCubeTMatrix(pinfo *pi_m, dclist cl_m, pinfo *pi, dclist cl_es, dclist cl_pr, dclist cl_dc)
  4797. {
  4798. pinfo local_pi;
  4799. dclist local_cl;
  4800. if ( pinfoInit(&local_pi) == 0 )
  4801. return 0;
  4802. if (pi->progress != NULL)
  4803. pinfoInitProgress(&local_pi);
  4804. if ( dclInit(&local_cl) == 0 )
  4805. return pinfoDestroy(&local_pi), 0;
  4806. if ( dclIrredundantDCubeMatrix(&local_pi, local_cl, pi, cl_es, cl_pr, cl_dc) == 0 )
  4807. return dclDestroy(local_cl), pinfoDestroy(&local_pi), 0;
  4808. if ( dclInvertOutMatrix(pi_m, cl_m, &local_pi, local_cl) == 0 )
  4809. return dclDestroy(local_cl), pinfoDestroy(&local_pi), 0;
  4810. /*
  4811. * puts("-- 111 ---");
  4812. * dclShow(&local_pi, local_cl);
  4813. * puts("-- 222 ---");
  4814. * dclShow(pi_m, cl_m);
  4815. */
  4816. return dclDestroy(local_cl), pinfoDestroy(&local_pi), 1;
  4817. }
  4818. int dclIrredundantDCubeTMatrixRC(pinfo *pi_m, dclist cl_m, pinfo *pi, dclist cl_es, dclist cl_pr, dclist cl_dc, dclist cl_rc)
  4819. {
  4820. pinfo local_pi;
  4821. dclist local_cl;
  4822. if ( pinfoInit(&local_pi) == 0 )
  4823. return 0;
  4824. if (pi->progress != NULL)
  4825. pinfoInitProgress(&local_pi);
  4826. if ( dclInit(&local_cl) == 0 )
  4827. return pinfoDestroy(&local_pi), 0;
  4828. if ( dclIrredundantDCubeMatrix(&local_pi, local_cl, pi, cl_es, cl_pr, cl_dc) == 0 )
  4829. return dclDestroy(local_cl), pinfoDestroy(&local_pi), 0;
  4830. if ( cl_rc != NULL )
  4831. if ( dclMarkRequiredCube(&local_pi, local_cl, pi, cl_es, cl_pr, cl_rc) == 0 )
  4832. return dclDestroy(local_cl), pinfoDestroy(&local_pi), 0;
  4833. if ( dclInvertOutMatrix(pi_m, cl_m, &local_pi, local_cl) == 0 )
  4834. return dclDestroy(local_cl), pinfoDestroy(&local_pi), 0;
  4835. /*
  4836. * puts("-- 111 ---");
  4837. * dclShow(&local_pi, local_cl);
  4838. * puts("-- 222 ---");
  4839. * dclShow(pi_m, cl_m);
  4840. */
  4841. return dclDestroy(local_cl), pinfoDestroy(&local_pi), 1;
  4842. }
  4843. /*-- dclReduceDCubeTMatrix --------------------------------------------------*/
  4844. /* diese routine entfernt nochmals einige spalten */
  4845. /* zu diesen zweck wird auch cl_pr angepasst und */
  4846. /* die entsprechenden cubes aus cl_pr entfernt. */
  4847. /* streng genommen muessten diese cubes in die */
  4848. /* fully redundant liste aufgenommen werden. */
  4849. int dclReduceDCubeTMatrix(pinfo *pi_m, dclist cl_m, pinfo *pi_pr, dclist cl_pr)
  4850. {
  4851. int i, cnt = dclCnt(cl_m);
  4852. int j;
  4853. if ( dclCnt(cl_m) != dclCnt(cl_pr) )
  4854. return 0;
  4855. /* clear empty lines */
  4856. if ( dclClearFlags(cl_m) == 0 )
  4857. return 0;
  4858. if ( dclClearFlags(cl_pr) == 0 )
  4859. return 0;
  4860. for( i = 0; i < cnt; i++ )
  4861. {
  4862. if ( dcOutCnt(pi_m, dclGet(cl_m, i)) == 0 )
  4863. {
  4864. dclSetFlag(cl_m, i);
  4865. dclSetFlag(cl_pr, i);
  4866. }
  4867. }
  4868. dclDeleteCubesWithFlag(pi_m, cl_m);
  4869. dclDeleteCubesWithFlag(pi_pr, cl_pr);
  4870. /* clear elements, which are subset and are more expensive */
  4871. if ( dclClearFlags(cl_m) == 0 )
  4872. return 0;
  4873. if ( dclClearFlags(cl_pr) == 0 )
  4874. return 0;
  4875. cnt = dclCnt(cl_pr);
  4876. for( i = 0; i < cnt; i++ )
  4877. dclGet(cl_pr, i)->n = pi_pr->in_cnt-dcInDCCnt(pi_pr, dclGet(cl_pr, i));
  4878. pinfoProcedureInit(pi_pr, "ReduceDCubeTMatrix", cnt);
  4879. for( i = 0; i < cnt; i++ )
  4880. {
  4881. pinfoProcedureDo(pi_pr, i);
  4882. for( j = i+1; j < cnt; j++ )
  4883. if ( dcIsSubSet(pi_m, dclGet(cl_m, i), dclGet(cl_m, j)) != 0 )
  4884. if ( dclGet(cl_pr, i)->n <= dclGet(cl_pr, j)->n )
  4885. {
  4886. dclSetFlag(cl_m, j);
  4887. dclSetFlag(cl_pr, j);
  4888. }
  4889. }
  4890. pinfoProcedureFinish(pi_pr);
  4891. dclDeleteCubesWithFlag(pi_m, cl_m);
  4892. dclDeleteCubesWithFlag(pi_pr, cl_pr);
  4893. return 1;
  4894. }
  4895. /*-- dclIrredundant ---------------------------------------------------------*/
  4896. int dclIrredundant(pinfo *pi, dclist cl, dclist cl_dc)
  4897. {
  4898. dclist cl_es, cl_pr;
  4899. if ( dclInitVA(2, &cl_es, &cl_pr) == 0 )
  4900. return 0;
  4901. if ( dclSplitRelativeEssential(pi, cl_es, NULL, cl_pr, cl, cl_dc, NULL) == 0 )
  4902. return dclDestroyVA(2, cl_es, cl_pr), 0;
  4903. if ( dclIrredundantGreedy(pi, cl_es, cl_pr, cl_dc, NULL) == 0 )
  4904. return dclDestroyVA(2, cl_es, cl_pr), 0;
  4905. if ( dclCopy(pi, cl, cl_es) == 0 )
  4906. return dclDestroyVA(2, cl_es, cl_pr), 0;
  4907. if ( dclJoin(pi, cl, cl_pr) == 0 )
  4908. return dclDestroyVA(2, cl_es, cl_pr), 0;
  4909. return dclDestroyVA(2, cl_es, cl_pr), 1;
  4910. }
  4911. /*-- dclRestrictOutput ------------------------------------------------------*/
  4912. /* Setzt Ausgaenge, die ueberfluessigerweise auf 1 gesetzt sind auf 0. */
  4913. /* Ueblicherweise wird diese Funktion aufgerufen nachdem eine minimale */
  4914. /* loesung gefunden wurde. */
  4915. void dclRestrictOutput(pinfo *pi, dclist cl)
  4916. {
  4917. int i, j, cnt = dclCnt(cl);
  4918. int out;
  4919. for( out = 0; out < pi->out_cnt; out++ )
  4920. {
  4921. for( i = 0; i < cnt; i++ )
  4922. {
  4923. if ( dcGetOut(dclGet(cl, i), out) != 0 )
  4924. {
  4925. for( j = i+1; j < cnt; j++ )
  4926. {
  4927. if ( dcGetOut(dclGet(cl, j), out) != 0 )
  4928. {
  4929. if ( dcIsInSubSet(pi, dclGet(cl, i), dclGet(cl, j)) != 0 )
  4930. {
  4931. dcSetOut(dclGet(cl, j), out, 0);
  4932. }
  4933. else if ( dcIsInSubSet(pi, dclGet(cl, j), dclGet(cl, i)) != 0 )
  4934. {
  4935. dcSetOut(dclGet(cl, i), out, 0);
  4936. break;
  4937. }
  4938. }
  4939. }
  4940. }
  4941. }
  4942. }
  4943. }
  4944. /*-- dclMinimize ------------------------------------------------------------*/
  4945. int dclMinimize(pinfo *pi, dclist cl)
  4946. {
  4947. dclist cl_es, cl_fr, cl_pr;
  4948. dclInitVA(3, &cl_es, &cl_fr, &cl_pr);
  4949. if ( dclPrimes(pi, cl) == 0 )
  4950. return dclDestroyVA(3, cl_es, cl_fr, cl_pr), 0;
  4951. if ( dclSplitRelativeEssential(pi, cl_es, cl_fr, cl_pr, cl, NULL, NULL) == 0 )
  4952. return dclDestroyVA(3, cl_es, cl_fr, cl_pr), 0;
  4953. if ( dclIrredundantGreedy(pi, cl_es, cl_pr, NULL, NULL) == 0 )
  4954. return dclDestroyVA(3, cl_es, cl_fr, cl_pr), 0;
  4955. if ( dclJoin(pi, cl_pr, cl_es) == 0 )
  4956. return dclDestroyVA(3, cl_es, cl_fr, cl_pr), 0;
  4957. dclRestrictOutput(pi, cl_pr);
  4958. if( dclIsEquivalent(pi, cl, cl_pr) == 0 )
  4959. return dclDestroyVA(3, cl_es, cl_fr, cl_pr), 0;
  4960. if ( dclCopy(pi, cl, cl_pr) == 0 )
  4961. return dclDestroyVA(3, cl_es, cl_fr, cl_pr), 0;
  4962. dclDestroyVA(3, cl_es, cl_fr, cl_pr);
  4963. return 1;
  4964. }
  4965. /*-- dclMinimizeDC ----------------------------------------------------------*/
  4966. /* result is stored in cl */
  4967. /* inputs are cl and cl_dc */
  4968. /* cl and cl_dc should be disjunct! */
  4969. /* dclSubtract(pi, cl, cl_dc) or dclSubtract(pi, cl_dc, cl) would force this */
  4970. /*!
  4971. \ingroup dclist
  4972. The representation of a boolean function contains three parts:
  4973. -# The problem info structure.
  4974. -# The ON-set of the boolean function (\a cl).
  4975. -# An optional DC (don't care) Set of the boolean function (\a cl_dc).
  4976. This function tries to reduce the number of cubes that are stored in \a cl.
  4977. One often assumes that the area of a hardware implementation of boolean
  4978. functions decreases with the number of cubes. So it is often
  4979. a good idea to call this function before hardware synthesis (gnc_SynthDCL()).
  4980. \pre \a cl and \a cl_dc must be disjunct, they must have
  4981. an empty intersection. This condition can be forced with
  4982. dclSubtract(pi, cl, cl_dc) or dclSubtract(pi, cl_dc, cl).
  4983. \post \a cl and \a cl_dc are not disjunct any more. After a successful
  4984. minimization, \a cl and \a cl_dc may have a nonempty intersection.
  4985. \param pi The problem info structure for the boolean functions.
  4986. \param cl The ON-set of the boolean function. This argument will be modified
  4987. by dclMinimizeDC().
  4988. \param cl_dc The DC-set of the boolean function. Set this argument to \c NULL if there
  4989. is not DC-set.
  4990. \param greedy Use the recommended value 0 for an exact minimzation.
  4991. \return 0, if an error occured.
  4992. \warning This function may take a very long time.
  4993. \see gnc_SynthDCL()
  4994. \see dclImport()
  4995. */
  4996. int dclMinimizeDC(pinfo *pi, dclist cl, dclist cl_dc, int greedy, int is_literal)
  4997. {
  4998. dclist cl_es, cl_fr, cl_pr, cl_on;
  4999. dclInitVA(4, &cl_es, &cl_fr, &cl_pr, &cl_on);
  5000. if ( dclCopy(pi, cl_on, cl) == 0 )
  5001. return dclDestroyVA(4, cl_es, cl_fr, cl_pr, cl_on), 0;
  5002. if ( dclPrimesDC(pi, cl, cl_dc) == 0 )
  5003. return dclDestroyVA(4, cl_es, cl_fr, cl_pr, cl_on), 0;
  5004. if ( dclSplitRelativeEssential(pi, cl_es, cl_fr, cl_pr, cl, cl_dc, NULL) == 0 )
  5005. return dclDestroyVA(4, cl_es, cl_fr, cl_pr, cl_on), 0;
  5006. /* ---- dclIrredundantGreedy has been disabled for the benefit of (the following function) maMatrixIrredundant:
  5007. if ( dclIrredundantGreedy(pi, cl_es, cl_pr, cl_dc, NULL) == 0 )
  5008. return dclDestroyVA(4, cl_es, cl_fr, cl_pr, cl_on), 0;
  5009. */
  5010. if ( is_literal != 0 )
  5011. {
  5012. if ( maMatrixIrredundant(pi, cl_es, cl_pr, cl_dc, NULL, greedy, MA_LIT_SOP) == 0 )
  5013. return dclDestroyVA(4, cl_es, cl_fr, cl_pr, cl_on), 0;
  5014. }
  5015. else
  5016. {
  5017. if ( maMatrixIrredundant(pi, cl_es, cl_pr, cl_dc, NULL, greedy, MA_LIT_NONE) == 0 )
  5018. return dclDestroyVA(4, cl_es, cl_fr, cl_pr, cl_on), 0;
  5019. }
  5020. if ( dclJoin(pi, cl_pr, cl_es) == 0 )
  5021. return dclDestroyVA(4, cl_es, cl_fr, cl_pr, cl_on), 0;
  5022. dclRestrictOutput(pi, cl_pr);
  5023. if( dclIsEquivalentDC(pi, cl_pr, cl_on, cl_dc) == 0 )
  5024. return dclDestroyVA(4, cl_es, cl_fr, cl_pr, cl_on), 0;
  5025. if ( dclCopy(pi, cl, cl_pr) == 0 )
  5026. return dclDestroyVA(4, cl_es, cl_fr, cl_pr, cl_on), 0;
  5027. dclDestroyVA(4, cl_es, cl_fr, cl_pr, cl_on);
  5028. return 1;
  5029. }
  5030. /*-- dclWriteBin ------------------------------------------------------------*/
  5031. int dclWriteBin(pinfo *pi, dclist cl, FILE *fp)
  5032. {
  5033. int i;
  5034. if ( cl == NULL )
  5035. {
  5036. if ( b_io_WriteInt(fp, 0) == 0 )
  5037. return 0;
  5038. return 1;
  5039. }
  5040. if ( b_io_WriteInt(fp, 1) == 0 )
  5041. return 0;
  5042. if ( b_io_WriteInt(fp, cl->cnt) == 0 )
  5043. return 0;
  5044. for( i = 0; i < cl->cnt; i++ )
  5045. if ( dcWriteBin(pi, cl->list+i, fp) == 0 )
  5046. return 0;
  5047. return 1;
  5048. }
  5049. /*-- dclReadBin -------------------------------------------------------------*/
  5050. int dclReadBin(pinfo *pi, dclist *cl, FILE *fp)
  5051. {
  5052. int i;
  5053. if ( *cl != NULL )
  5054. dclDestroy(*cl);
  5055. *cl = NULL;
  5056. if ( b_io_ReadInt(fp, &(i)) == 0 )
  5057. return 0;
  5058. if ( i == 0 )
  5059. return 1;
  5060. if ( dclInit(cl) == 0 )
  5061. {
  5062. *cl = NULL;
  5063. return 0;
  5064. }
  5065. if ( b_io_ReadInt(fp, &((*cl)->cnt)) == 0 )
  5066. return 0;
  5067. if ( dclExpandTo(pi, *cl, (*cl)->cnt) == 0 )
  5068. return 0;
  5069. for( i = 0; i < (*cl)->cnt; i++ )
  5070. if ( dcReadBin(pi, (*cl)->list+i, fp) == 0 )
  5071. return 0;
  5072. return 1;
  5073. }
  5074. /*-- dclGetLiteralCnt -------------------------------------------------------*/
  5075. int dclGetLiteralCnt(pinfo *pi, dclist cl)
  5076. {
  5077. int i, cnt = dclCnt(cl);
  5078. int lit_cnt = 0;
  5079. for( i = 0; i < cnt; i++ )
  5080. {
  5081. /*
  5082. lit_cnt += pi->in_cnt - dcInDCCnt(pi, dclGet(cl, i));
  5083. lit_cnt += dcOutCnt(pi, dclGet(cl, i));
  5084. */
  5085. lit_cnt += dcGetLiteralCnt(pi, dclGet(cl, i));
  5086. }
  5087. return lit_cnt;
  5088. }