You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1040 lines
17 KiB

  1. /*
  2. * Copyright 2011-2015 Formal Methods and Tools, University of Twente
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <sylvan_obj.hpp>
  17. using namespace sylvan;
  18. /***
  19. * Implementation of class Bdd
  20. */
  21. int
  22. Bdd::operator==(const Bdd& other) const
  23. {
  24. return bdd == other.bdd;
  25. }
  26. int
  27. Bdd::operator!=(const Bdd& other) const
  28. {
  29. return bdd != other.bdd;
  30. }
  31. Bdd
  32. Bdd::operator=(const Bdd& right)
  33. {
  34. bdd = right.bdd;
  35. return *this;
  36. }
  37. int
  38. Bdd::operator<=(const Bdd& other) const
  39. {
  40. // TODO: better implementation, since we are not interested in the BDD result
  41. LACE_ME;
  42. BDD r = sylvan_ite(this->bdd, sylvan_not(other.bdd), sylvan_false);
  43. return r == sylvan_false;
  44. }
  45. int
  46. Bdd::operator>=(const Bdd& other) const
  47. {
  48. // TODO: better implementation, since we are not interested in the BDD result
  49. return other <= *this;
  50. }
  51. int
  52. Bdd::operator<(const Bdd& other) const
  53. {
  54. return bdd != other.bdd && *this <= other;
  55. }
  56. int
  57. Bdd::operator>(const Bdd& other) const
  58. {
  59. return bdd != other.bdd && *this >= other;
  60. }
  61. Bdd
  62. Bdd::operator!() const
  63. {
  64. return Bdd(sylvan_not(bdd));
  65. }
  66. Bdd
  67. Bdd::operator~() const
  68. {
  69. return Bdd(sylvan_not(bdd));
  70. }
  71. Bdd
  72. Bdd::operator*(const Bdd& other) const
  73. {
  74. LACE_ME;
  75. return Bdd(sylvan_and(bdd, other.bdd));
  76. }
  77. Bdd
  78. Bdd::operator*=(const Bdd& other)
  79. {
  80. LACE_ME;
  81. bdd = sylvan_and(bdd, other.bdd);
  82. return *this;
  83. }
  84. Bdd
  85. Bdd::operator&(const Bdd& other) const
  86. {
  87. LACE_ME;
  88. return Bdd(sylvan_and(bdd, other.bdd));
  89. }
  90. Bdd
  91. Bdd::operator&=(const Bdd& other)
  92. {
  93. LACE_ME;
  94. bdd = sylvan_and(bdd, other.bdd);
  95. return *this;
  96. }
  97. Bdd
  98. Bdd::operator+(const Bdd& other) const
  99. {
  100. LACE_ME;
  101. return Bdd(sylvan_or(bdd, other.bdd));
  102. }
  103. Bdd
  104. Bdd::operator+=(const Bdd& other)
  105. {
  106. LACE_ME;
  107. bdd = sylvan_or(bdd, other.bdd);
  108. return *this;
  109. }
  110. Bdd
  111. Bdd::operator|(const Bdd& other) const
  112. {
  113. LACE_ME;
  114. return Bdd(sylvan_or(bdd, other.bdd));
  115. }
  116. Bdd
  117. Bdd::operator|=(const Bdd& other)
  118. {
  119. LACE_ME;
  120. bdd = sylvan_or(bdd, other.bdd);
  121. return *this;
  122. }
  123. Bdd
  124. Bdd::operator^(const Bdd& other) const
  125. {
  126. LACE_ME;
  127. return Bdd(sylvan_xor(bdd, other.bdd));
  128. }
  129. Bdd
  130. Bdd::operator^=(const Bdd& other)
  131. {
  132. LACE_ME;
  133. bdd = sylvan_xor(bdd, other.bdd);
  134. return *this;
  135. }
  136. Bdd
  137. Bdd::operator-(const Bdd& other) const
  138. {
  139. LACE_ME;
  140. return Bdd(sylvan_and(bdd, sylvan_not(other.bdd)));
  141. }
  142. Bdd
  143. Bdd::operator-=(const Bdd& other)
  144. {
  145. LACE_ME;
  146. bdd = sylvan_and(bdd, sylvan_not(other.bdd));
  147. return *this;
  148. }
  149. Bdd
  150. Bdd::AndAbstract(const Bdd &g, const BddSet &cube) const
  151. {
  152. LACE_ME;
  153. return sylvan_and_exists(bdd, g.bdd, cube.set.bdd);
  154. }
  155. Bdd
  156. Bdd::ExistAbstract(const BddSet &cube) const
  157. {
  158. LACE_ME;
  159. return sylvan_exists(bdd, cube.set.bdd);
  160. }
  161. Bdd
  162. Bdd::UnivAbstract(const BddSet &cube) const
  163. {
  164. LACE_ME;
  165. return sylvan_forall(bdd, cube.set.bdd);
  166. }
  167. Bdd
  168. Bdd::Ite(const Bdd &g, const Bdd &h) const
  169. {
  170. LACE_ME;
  171. return sylvan_ite(bdd, g.bdd, h.bdd);
  172. }
  173. Bdd
  174. Bdd::And(const Bdd &g) const
  175. {
  176. LACE_ME;
  177. return sylvan_and(bdd, g.bdd);
  178. }
  179. Bdd
  180. Bdd::Or(const Bdd &g) const
  181. {
  182. LACE_ME;
  183. return sylvan_or(bdd, g.bdd);
  184. }
  185. Bdd
  186. Bdd::Nand(const Bdd &g) const
  187. {
  188. LACE_ME;
  189. return sylvan_nand(bdd, g.bdd);
  190. }
  191. Bdd
  192. Bdd::Nor(const Bdd &g) const
  193. {
  194. LACE_ME;
  195. return sylvan_nor(bdd, g.bdd);
  196. }
  197. Bdd
  198. Bdd::Xor(const Bdd &g) const
  199. {
  200. LACE_ME;
  201. return sylvan_xor(bdd, g.bdd);
  202. }
  203. Bdd
  204. Bdd::Xnor(const Bdd &g) const
  205. {
  206. LACE_ME;
  207. return sylvan_equiv(bdd, g.bdd);
  208. }
  209. int
  210. Bdd::Leq(const Bdd &g) const
  211. {
  212. // TODO: better implementation, since we are not interested in the BDD result
  213. LACE_ME;
  214. BDD r = sylvan_ite(bdd, sylvan_not(g.bdd), sylvan_false);
  215. return r == sylvan_false;
  216. }
  217. Bdd
  218. Bdd::RelPrev(const Bdd& relation, const BddSet& cube) const
  219. {
  220. LACE_ME;
  221. return sylvan_relprev(relation.bdd, bdd, cube.set.bdd);
  222. }
  223. Bdd
  224. Bdd::RelNext(const Bdd &relation, const BddSet &cube) const
  225. {
  226. LACE_ME;
  227. return sylvan_relnext(bdd, relation.bdd, cube.set.bdd);
  228. }
  229. Bdd
  230. Bdd::Closure() const
  231. {
  232. LACE_ME;
  233. return sylvan_closure(bdd);
  234. }
  235. Bdd
  236. Bdd::Constrain(const Bdd &c) const
  237. {
  238. LACE_ME;
  239. return sylvan_constrain(bdd, c.bdd);
  240. }
  241. Bdd
  242. Bdd::Restrict(const Bdd &c) const
  243. {
  244. LACE_ME;
  245. return sylvan_restrict(bdd, c.bdd);
  246. }
  247. Bdd
  248. Bdd::Compose(const BddMap &m) const
  249. {
  250. LACE_ME;
  251. return sylvan_compose(bdd, m.bdd);
  252. }
  253. Bdd
  254. Bdd::Permute(const std::vector<uint32_t>& from, const std::vector<uint32_t>& to) const
  255. {
  256. LACE_ME;
  257. /* Create a map */
  258. BddMap map;
  259. for (int i=from.size()-1; i>=0; i--) {
  260. map.put(from[i], Bdd::bddVar(to[i]));
  261. }
  262. return sylvan_compose(bdd, map.bdd);
  263. }
  264. Bdd
  265. Bdd::Support() const
  266. {
  267. LACE_ME;
  268. return sylvan_support(bdd);
  269. }
  270. BDD
  271. Bdd::GetBDD() const
  272. {
  273. return bdd;
  274. }
  275. void
  276. Bdd::PrintDot(FILE *out) const
  277. {
  278. sylvan_fprintdot(out, bdd);
  279. }
  280. void
  281. Bdd::GetShaHash(char *string) const
  282. {
  283. sylvan_getsha(bdd, string);
  284. }
  285. std::string
  286. Bdd::GetShaHash() const
  287. {
  288. char buf[65];
  289. sylvan_getsha(bdd, buf);
  290. return std::string(buf);
  291. }
  292. double
  293. Bdd::SatCount(const BddSet &variables) const
  294. {
  295. LACE_ME;
  296. return sylvan_satcount(bdd, variables.set.bdd);
  297. }
  298. double
  299. Bdd::SatCount(size_t nvars) const
  300. {
  301. LACE_ME;
  302. // Note: the mtbdd_satcount can be called without initializing the MTBDD module.
  303. return mtbdd_satcount(bdd, nvars);
  304. }
  305. void
  306. Bdd::PickOneCube(const BddSet &variables, uint8_t *values) const
  307. {
  308. LACE_ME;
  309. sylvan_sat_one(bdd, variables.set.bdd, values);
  310. }
  311. std::vector<bool>
  312. Bdd::PickOneCube(const BddSet &variables) const
  313. {
  314. std::vector<bool> result = std::vector<bool>();
  315. BDD bdd = this->bdd;
  316. BDD vars = variables.set.bdd;
  317. if (bdd == sylvan_false) return result;
  318. for (; !sylvan_set_isempty(vars); vars = sylvan_set_next(vars)) {
  319. uint32_t var = sylvan_set_var(vars);
  320. if (bdd == sylvan_true) {
  321. // pick 0
  322. result.push_back(false);
  323. } else {
  324. if (sylvan_var(bdd) != var) {
  325. // pick 0
  326. result.push_back(false);
  327. } else {
  328. if (sylvan_low(bdd) == sylvan_false) {
  329. // pick 1
  330. result.push_back(true);
  331. bdd = sylvan_high(bdd);
  332. } else {
  333. // pick 0
  334. result.push_back(false);
  335. bdd = sylvan_low(bdd);
  336. }
  337. }
  338. }
  339. }
  340. return result;
  341. }
  342. Bdd
  343. Bdd::PickOneCube() const
  344. {
  345. LACE_ME;
  346. return Bdd(sylvan_sat_one_bdd(bdd));
  347. }
  348. Bdd
  349. Bdd::UnionCube(const BddSet &variables, uint8_t *values) const
  350. {
  351. LACE_ME;
  352. return sylvan_union_cube(bdd, variables.set.bdd, values);
  353. }
  354. Bdd
  355. Bdd::UnionCube(const BddSet &variables, std::vector<uint8_t> values) const
  356. {
  357. LACE_ME;
  358. uint8_t *data = values.data();
  359. return sylvan_union_cube(bdd, variables.set.bdd, data);
  360. }
  361. /**
  362. * @brief Generate a cube representing a set of variables
  363. */
  364. Bdd
  365. Bdd::VectorCube(const std::vector<Bdd> variables)
  366. {
  367. Bdd result = Bdd::bddOne();
  368. for (int i=variables.size()-1; i>=0; i--) {
  369. result *= variables[i];
  370. }
  371. return result;
  372. }
  373. /**
  374. * @brief Generate a cube representing a set of variables
  375. */
  376. Bdd
  377. Bdd::VariablesCube(std::vector<uint32_t> variables)
  378. {
  379. BDD result = sylvan_true;
  380. for (int i=variables.size()-1; i>=0; i--) {
  381. result = sylvan_makenode(variables[i], sylvan_false, result);
  382. }
  383. return result;
  384. }
  385. size_t
  386. Bdd::NodeCount() const
  387. {
  388. return sylvan_nodecount(bdd);
  389. }
  390. Bdd
  391. Bdd::bddOne()
  392. {
  393. return sylvan_true;
  394. }
  395. Bdd
  396. Bdd::bddZero()
  397. {
  398. return sylvan_false;
  399. }
  400. Bdd
  401. Bdd::bddVar(uint32_t index)
  402. {
  403. LACE_ME;
  404. return sylvan_ithvar(index);
  405. }
  406. Bdd
  407. Bdd::bddCube(const BddSet &variables, uint8_t *values)
  408. {
  409. LACE_ME;
  410. return sylvan_cube(variables.set.bdd, values);
  411. }
  412. Bdd
  413. Bdd::bddCube(const BddSet &variables, std::vector<uint8_t> values)
  414. {
  415. LACE_ME;
  416. uint8_t *data = values.data();
  417. return sylvan_cube(variables.set.bdd, data);
  418. }
  419. int
  420. Bdd::isConstant() const
  421. {
  422. return bdd == sylvan_true || bdd == sylvan_false;
  423. }
  424. int
  425. Bdd::isTerminal() const
  426. {
  427. return bdd == sylvan_true || bdd == sylvan_false;
  428. }
  429. int
  430. Bdd::isOne() const
  431. {
  432. return bdd == sylvan_true;
  433. }
  434. int
  435. Bdd::isZero() const
  436. {
  437. return bdd == sylvan_false;
  438. }
  439. uint32_t
  440. Bdd::TopVar() const
  441. {
  442. return sylvan_var(bdd);
  443. }
  444. Bdd
  445. Bdd::Then() const
  446. {
  447. return Bdd(sylvan_high(bdd));
  448. }
  449. Bdd
  450. Bdd::Else() const
  451. {
  452. return Bdd(sylvan_low(bdd));
  453. }
  454. /***
  455. * Implementation of class BddMap
  456. */
  457. BddMap::BddMap(uint32_t key_variable, const Bdd value)
  458. {
  459. bdd = sylvan_map_add(sylvan_map_empty(), key_variable, value.bdd);
  460. }
  461. BddMap
  462. BddMap::operator+(const Bdd& other) const
  463. {
  464. return BddMap(sylvan_map_addall(bdd, other.bdd));
  465. }
  466. BddMap
  467. BddMap::operator+=(const Bdd& other)
  468. {
  469. bdd = sylvan_map_addall(bdd, other.bdd);
  470. return *this;
  471. }
  472. BddMap
  473. BddMap::operator-(const Bdd& other) const
  474. {
  475. return BddMap(sylvan_map_removeall(bdd, other.bdd));
  476. }
  477. BddMap
  478. BddMap::operator-=(const Bdd& other)
  479. {
  480. bdd = sylvan_map_removeall(bdd, other.bdd);
  481. return *this;
  482. }
  483. void
  484. BddMap::put(uint32_t key, Bdd value)
  485. {
  486. bdd = sylvan_map_add(bdd, key, value.bdd);
  487. }
  488. void
  489. BddMap::removeKey(uint32_t key)
  490. {
  491. bdd = sylvan_map_remove(bdd, key);
  492. }
  493. size_t
  494. BddMap::size() const
  495. {
  496. return sylvan_map_count(bdd);
  497. }
  498. int
  499. BddMap::isEmpty() const
  500. {
  501. return sylvan_map_isempty(bdd);
  502. }
  503. /***
  504. * Implementation of class Mtbdd
  505. */
  506. Mtbdd
  507. Mtbdd::int64Terminal(int64_t value)
  508. {
  509. return mtbdd_int64(value);
  510. }
  511. Mtbdd
  512. Mtbdd::doubleTerminal(double value)
  513. {
  514. return mtbdd_double(value);
  515. }
  516. Mtbdd
  517. Mtbdd::fractionTerminal(int64_t nominator, uint64_t denominator)
  518. {
  519. return mtbdd_fraction(nominator, denominator);
  520. }
  521. Mtbdd
  522. Mtbdd::terminal(uint32_t type, uint64_t value)
  523. {
  524. return mtbdd_makeleaf(type, value);
  525. }
  526. Mtbdd
  527. Mtbdd::mtbddVar(uint32_t variable)
  528. {
  529. return mtbdd_makenode(variable, mtbdd_false, mtbdd_true);
  530. }
  531. Mtbdd
  532. Mtbdd::mtbddOne()
  533. {
  534. return mtbdd_true;
  535. }
  536. Mtbdd
  537. Mtbdd::mtbddZero()
  538. {
  539. return mtbdd_false;
  540. }
  541. Mtbdd
  542. Mtbdd::mtbddCube(const BddSet &variables, uint8_t *values, const Mtbdd &terminal)
  543. {
  544. LACE_ME;
  545. return mtbdd_cube(variables.set.bdd, values, terminal.mtbdd);
  546. }
  547. Mtbdd
  548. Mtbdd::mtbddCube(const BddSet &variables, std::vector<uint8_t> values, const Mtbdd &terminal)
  549. {
  550. LACE_ME;
  551. uint8_t *data = values.data();
  552. return mtbdd_cube(variables.set.bdd, data, terminal.mtbdd);
  553. }
  554. int
  555. Mtbdd::isTerminal() const
  556. {
  557. return mtbdd_isleaf(mtbdd);
  558. }
  559. int
  560. Mtbdd::isLeaf() const
  561. {
  562. return mtbdd_isleaf(mtbdd);
  563. }
  564. int
  565. Mtbdd::isOne() const
  566. {
  567. return mtbdd == mtbdd_true;
  568. }
  569. int
  570. Mtbdd::isZero() const
  571. {
  572. return mtbdd == mtbdd_false;
  573. }
  574. uint32_t
  575. Mtbdd::TopVar() const
  576. {
  577. return mtbdd_getvar(mtbdd);
  578. }
  579. Mtbdd
  580. Mtbdd::Then() const
  581. {
  582. return mtbdd_isnode(mtbdd) ? mtbdd_gethigh(mtbdd) : mtbdd;
  583. }
  584. Mtbdd
  585. Mtbdd::Else() const
  586. {
  587. return mtbdd_isnode(mtbdd) ? mtbdd_getlow(mtbdd) : mtbdd;
  588. }
  589. Mtbdd
  590. Mtbdd::Negate() const
  591. {
  592. LACE_ME;
  593. return mtbdd_negate(mtbdd);
  594. }
  595. Mtbdd
  596. Mtbdd::Apply(const Mtbdd &other, mtbdd_apply_op op) const
  597. {
  598. LACE_ME;
  599. return mtbdd_apply(mtbdd, other.mtbdd, op);
  600. }
  601. Mtbdd
  602. Mtbdd::UApply(mtbdd_uapply_op op, size_t param) const
  603. {
  604. LACE_ME;
  605. return mtbdd_uapply(mtbdd, op, param);
  606. }
  607. Mtbdd
  608. Mtbdd::Abstract(const BddSet &variables, mtbdd_abstract_op op) const
  609. {
  610. LACE_ME;
  611. return mtbdd_abstract(mtbdd, variables.set.bdd, op);
  612. }
  613. Mtbdd
  614. Mtbdd::Ite(const Mtbdd &g, const Mtbdd &h) const
  615. {
  616. LACE_ME;
  617. return mtbdd_ite(mtbdd, g.mtbdd, h.mtbdd);
  618. }
  619. Mtbdd
  620. Mtbdd::Plus(const Mtbdd &other) const
  621. {
  622. LACE_ME;
  623. return mtbdd_plus(mtbdd, other.mtbdd);
  624. }
  625. Mtbdd
  626. Mtbdd::Times(const Mtbdd &other) const
  627. {
  628. LACE_ME;
  629. return mtbdd_times(mtbdd, other.mtbdd);
  630. }
  631. Mtbdd
  632. Mtbdd::Min(const Mtbdd &other) const
  633. {
  634. LACE_ME;
  635. return mtbdd_min(mtbdd, other.mtbdd);
  636. }
  637. Mtbdd
  638. Mtbdd::Max(const Mtbdd &other) const
  639. {
  640. LACE_ME;
  641. return mtbdd_max(mtbdd, other.mtbdd);
  642. }
  643. Mtbdd
  644. Mtbdd::AbstractPlus(const BddSet &variables) const
  645. {
  646. LACE_ME;
  647. return mtbdd_abstract_plus(mtbdd, variables.set.bdd);
  648. }
  649. Mtbdd
  650. Mtbdd::AbstractTimes(const BddSet &variables) const
  651. {
  652. LACE_ME;
  653. return mtbdd_abstract_times(mtbdd, variables.set.bdd);
  654. }
  655. Mtbdd
  656. Mtbdd::AbstractMin(const BddSet &variables) const
  657. {
  658. LACE_ME;
  659. return mtbdd_abstract_min(mtbdd, variables.set.bdd);
  660. }
  661. Mtbdd
  662. Mtbdd::AbstractMax(const BddSet &variables) const
  663. {
  664. LACE_ME;
  665. return mtbdd_abstract_max(mtbdd, variables.set.bdd);
  666. }
  667. Mtbdd
  668. Mtbdd::AndExists(const Mtbdd &other, const BddSet &variables) const
  669. {
  670. LACE_ME;
  671. return mtbdd_and_exists(mtbdd, other.mtbdd, variables.set.bdd);
  672. }
  673. int
  674. Mtbdd::operator==(const Mtbdd& other) const
  675. {
  676. return mtbdd == other.mtbdd;
  677. }
  678. int
  679. Mtbdd::operator!=(const Mtbdd& other) const
  680. {
  681. return mtbdd != other.mtbdd;
  682. }
  683. Mtbdd
  684. Mtbdd::operator=(const Mtbdd& right)
  685. {
  686. mtbdd = right.mtbdd;
  687. return *this;
  688. }
  689. Mtbdd
  690. Mtbdd::operator!() const
  691. {
  692. return mtbdd_not(mtbdd);
  693. }
  694. Mtbdd
  695. Mtbdd::operator~() const
  696. {
  697. return mtbdd_not(mtbdd);
  698. }
  699. Mtbdd
  700. Mtbdd::operator*(const Mtbdd& other) const
  701. {
  702. LACE_ME;
  703. return mtbdd_times(mtbdd, other.mtbdd);
  704. }
  705. Mtbdd
  706. Mtbdd::operator*=(const Mtbdd& other)
  707. {
  708. LACE_ME;
  709. mtbdd = mtbdd_times(mtbdd, other.mtbdd);
  710. return *this;
  711. }
  712. Mtbdd
  713. Mtbdd::operator+(const Mtbdd& other) const
  714. {
  715. LACE_ME;
  716. return mtbdd_plus(mtbdd, other.mtbdd);
  717. }
  718. Mtbdd
  719. Mtbdd::operator+=(const Mtbdd& other)
  720. {
  721. LACE_ME;
  722. mtbdd = mtbdd_plus(mtbdd, other.mtbdd);
  723. return *this;
  724. }
  725. Mtbdd
  726. Mtbdd::operator-(const Mtbdd& other) const
  727. {
  728. LACE_ME;
  729. return mtbdd_minus(mtbdd, other.mtbdd);
  730. }
  731. Mtbdd
  732. Mtbdd::operator-=(const Mtbdd& other)
  733. {
  734. LACE_ME;
  735. mtbdd = mtbdd_minus(mtbdd, other.mtbdd);
  736. return *this;
  737. }
  738. Mtbdd
  739. Mtbdd::MtbddThreshold(double value) const
  740. {
  741. LACE_ME;
  742. return mtbdd_threshold_double(mtbdd, value);
  743. }
  744. Mtbdd
  745. Mtbdd::MtbddStrictThreshold(double value) const
  746. {
  747. LACE_ME;
  748. return mtbdd_strict_threshold_double(mtbdd, value);
  749. }
  750. Bdd
  751. Mtbdd::BddThreshold(double value) const
  752. {
  753. LACE_ME;
  754. return mtbdd_threshold_double(mtbdd, value);
  755. }
  756. Bdd
  757. Mtbdd::BddStrictThreshold(double value) const
  758. {
  759. LACE_ME;
  760. return mtbdd_strict_threshold_double(mtbdd, value);
  761. }
  762. Mtbdd
  763. Mtbdd::Support() const
  764. {
  765. LACE_ME;
  766. return mtbdd_support(mtbdd);
  767. }
  768. MTBDD
  769. Mtbdd::GetMTBDD() const
  770. {
  771. return mtbdd;
  772. }
  773. Mtbdd
  774. Mtbdd::Compose(MtbddMap &m) const
  775. {
  776. LACE_ME;
  777. return mtbdd_compose(mtbdd, m.mtbdd);
  778. }
  779. Mtbdd
  780. Mtbdd::Permute(const std::vector<uint32_t>& from, const std::vector<uint32_t>& to) const
  781. {
  782. LACE_ME;
  783. /* Create a map */
  784. MtbddMap map;
  785. for (int i=from.size()-1; i>=0; i--) {
  786. map.put(from[i], Bdd::bddVar(to[i]));
  787. }
  788. return mtbdd_compose(mtbdd, map.mtbdd);
  789. }
  790. double
  791. Mtbdd::SatCount(size_t nvars) const
  792. {
  793. LACE_ME;
  794. return mtbdd_satcount(mtbdd, nvars);
  795. }
  796. double
  797. Mtbdd::SatCount(const BddSet &variables) const
  798. {
  799. return SatCount(sylvan_set_count(variables.set.bdd));
  800. }
  801. size_t
  802. Mtbdd::NodeCount() const
  803. {
  804. LACE_ME;
  805. return mtbdd_nodecount(mtbdd);
  806. }
  807. /***
  808. * Implementation of class MtbddMap
  809. */
  810. MtbddMap::MtbddMap(uint32_t key_variable, Mtbdd value)
  811. {
  812. mtbdd = mtbdd_map_add(mtbdd_map_empty(), key_variable, value.mtbdd);
  813. }
  814. MtbddMap
  815. MtbddMap::operator+(const Mtbdd& other) const
  816. {
  817. return MtbddMap(mtbdd_map_addall(mtbdd, other.mtbdd));
  818. }
  819. MtbddMap
  820. MtbddMap::operator+=(const Mtbdd& other)
  821. {
  822. mtbdd = mtbdd_map_addall(mtbdd, other.mtbdd);
  823. return *this;
  824. }
  825. MtbddMap
  826. MtbddMap::operator-(const Mtbdd& other) const
  827. {
  828. return MtbddMap(mtbdd_map_removeall(mtbdd, other.mtbdd));
  829. }
  830. MtbddMap
  831. MtbddMap::operator-=(const Mtbdd& other)
  832. {
  833. mtbdd = mtbdd_map_removeall(mtbdd, other.mtbdd);
  834. return *this;
  835. }
  836. void
  837. MtbddMap::put(uint32_t key, Mtbdd value)
  838. {
  839. mtbdd = mtbdd_map_add(mtbdd, key, value.mtbdd);
  840. }
  841. void
  842. MtbddMap::removeKey(uint32_t key)
  843. {
  844. mtbdd = mtbdd_map_remove(mtbdd, key);
  845. }
  846. size_t
  847. MtbddMap::size()
  848. {
  849. return mtbdd_map_count(mtbdd);
  850. }
  851. int
  852. MtbddMap::isEmpty()
  853. {
  854. return mtbdd_map_isempty(mtbdd);
  855. }
  856. /***
  857. * Implementation of class Sylvan
  858. */
  859. void
  860. Sylvan::initPackage(size_t initialTableSize, size_t maxTableSize, size_t initialCacheSize, size_t maxCacheSize)
  861. {
  862. sylvan_init_package(initialTableSize, maxTableSize, initialCacheSize, maxCacheSize);
  863. }
  864. void
  865. Sylvan::initBdd(int granularity)
  866. {
  867. sylvan_init_bdd(granularity);
  868. }
  869. void
  870. Sylvan::initMtbdd()
  871. {
  872. sylvan_init_mtbdd();
  873. }
  874. void
  875. Sylvan::quitPackage()
  876. {
  877. sylvan_quit();
  878. }
  879. #include "sylvan_obj_storm.cpp"