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.

1054 lines
17 KiB

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