PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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