/tutorial/backtracking/logigram/mask_builder.e

http://github.com/tybor/Liberty · Specman e · 141 lines · 91 code · 14 blank · 36 comment · 4 complexity · 8eb2b5843652ef3fccfe40234e35a973 MD5 · raw file

  1. -- See the Copyright notice at the end of this file.
  2. --
  3. class MASK_BUILDER
  4. --
  5. -- manage mask on permutations and create corresponding backtracking nodes
  6. creation {ANY}
  7. make
  8. feature {ANY}
  9. make (situ: SITUATION) is
  10. -- creation
  11. do
  12. permut := situ.permut
  13. create mask_array.make(situ.wdim)
  14. create mask.make(permut.count)
  15. end
  16. clear is
  17. -- reset the state
  18. do
  19. mask_array.clear_all
  20. mask := Void
  21. is_ok := True
  22. end
  23. and_yes is
  24. -- make the previous association mandatory
  25. do
  26. change(False)
  27. end
  28. and_no is
  29. -- make the previous association forbidden
  30. do
  31. change(True)
  32. end
  33. is_ok: BOOLEAN
  34. -- is possible (has no impossibility)
  35. goto (it1, it2: ITEM) is
  36. -- select the association of two items
  37. local
  38. g1, g2: INTEGER
  39. do
  40. g1 := it1.group.index
  41. g2 := it2.group.index
  42. if g1 < g2 then
  43. index := g1 + (g2 * (g2 - 1)) // 2
  44. i1 := it1.index
  45. i2 := it2.index
  46. else
  47. index := g2 + (g1 * (g1 - 1)) // 2
  48. i2 := it1.index
  49. i1 := it2.index
  50. end
  51. mask := mask_array.item(index)
  52. if mask = Void then
  53. create mask.make(permut.count)
  54. mask.set_all
  55. mask_array.put(mask, index)
  56. end
  57. ensure
  58. mask /= Void
  59. end
  60. get_node: BACKTRACKING_NODE_AND_LIST is
  61. -- create the backtracking node corresponding to
  62. -- the currently built masks
  63. require
  64. is_ok
  65. local
  66. idx: INTEGER; node: NODE_MASK
  67. do
  68. from
  69. idx := mask_array.lower
  70. until
  71. idx > mask_array.upper
  72. loop
  73. mask := mask_array.item(idx)
  74. if mask /= Void then
  75. create node.make(idx, mask)
  76. Result := create {BACKTRACKING_NODE_AND_LIST}.make(node, Result)
  77. end
  78. idx := idx + 1
  79. end
  80. mask := mask_array.item(index)
  81. end
  82. feature {}
  83. permut: ARRAY[PERMUT]
  84. -- array of permutations
  85. mask_array: FAST_ARRAY[BIT_STRING]
  86. -- array of masks
  87. index, i1, i2: INTEGER
  88. -- selected
  89. mask: BIT_STRING
  90. -- selected mask
  91. change (test: BOOLEAN) is
  92. -- update the mask
  93. local
  94. i: INTEGER
  95. do
  96. -- enumerate permutations
  97. from
  98. i := permut.lower
  99. until
  100. i > permut.upper
  101. loop
  102. -- disable permutations that are not allowed
  103. if (permut.item(i).item(i1) = i2) = test then
  104. mask.put_0(i)
  105. end
  106. i := i + 1
  107. end
  108. -- update is_ok
  109. is_ok := is_ok and then not mask.all_cleared
  110. end
  111. end -- class MASK_BUILDER
  112. --
  113. -- ------------------------------------------------------------------------------------------------------------------------------
  114. -- Copyright notice below. Please read.
  115. --
  116. -- This file is free software, which comes along with SmartEiffel. This software is distributed in the hope that it will be
  117. -- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  118. -- You can modify it as you want, provided this footer is kept unaltered, and a notification of the changes is added.
  119. -- You are allowed to redistribute it and sell it, alone or as a part of another product.
  120. --
  121. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  122. -- Copyright(C) 2003-2005: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  123. --
  124. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  125. --
  126. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  127. -- ------------------------------------------------------------------------------------------------------------------------------