MODULE grep0; (*--------------------------------------------------------- File name: grep0.pm Author: AG Date: 10/06/05 Problem: Find a pattern of characters on a chain. Pattern is originally stored in an array. Example of Execution: Pattern: abcd L3ch : abcd ... L2ch : cddabcdbbedddddadaedacabacbeadeaddcbe...eee Total found: 5 Cycles: N selections + N MOVE + N select + 2N assignments IMPORTANT: N is the size of the pattern, not the size of the chain. *) (*---------------------------------------------------------- CONFIGURATION ----------------------------------------------------------*) CONST MAXCELLS=1024; (* MAXCELLS cells in one line *) CONST N=4; CONFIGURATION chain[0..(MAXCELLS - 1)]; CONNECTION left: chain [i] -> chain[(i-1) MOD MAXCELLS]; right: chain[i] -> chain[(i+1) MOD MAXCELLS]; (*---------------------------------------------------------- VARIABLES -----------------------------------------------------------*) VAR L1bool: chain OF INTEGER; Achar: ARRAY[0..(N-1)] OF CHAR; L2ch, L3ch: chain OF CHAR; i, RightLimit, Tot: INTEGER; StartChar, nChar: INTEGER; (*-------------------------------------------------------- MAIN -----------------------------------------------------------*) BEGIN (* initializing data *) RightLimit:= 100; StartChar:= 97; (* ascii code for where the lower case alphabet starts *) nChar:= 5; (* number of characters (total = 25) *) L2ch := CHR((RandomInt(chain) MOD nChar) + StartChar); Achar[0] := 'a'; Achar[1] := 'b'; Achar[2] := 'c'; Achar[3] := 'd'; (* ==================start algorithm======================= *) L1bool:= 1; (* initialize chain to test all *) FOR i:= 0 TO (N - 1) DO IF L1bool = 1 THEN (* if selected *) IF L2ch # Achar[i] THEN (* if not there*) L1bool := 0; (* deselect *) END; (* IF *) END; (* IF *) L1bool := MOVE.right(L1bool); (* shift for next item *) END; (* for *) Tot:= REDUCE.SUM(L1bool); (* get total found *) (* to get the result *) L1bool:= MOVE.left(L1bool); FOR i:= 0 TO (N-1) DO IF L1bool = 1 THEN L3ch:= L2ch; (* pass on to chek line *) SEND.left(L1bool, L1bool); (* send the marker left *) END; (* IF *) END; (* FOR *) (* count 1's= how many times found*) (*================== printing========================== *) WriteString("Pattern: "); FOR i:= 0 TO (N - 1) DO Write(Achar[i]); END; (* FOR *) WriteLn; WriteString( " L3ch : ");Write( L3ch); WriteLn; WriteString( " L2ch : ");Write( L2ch); WriteLn; WriteString( " Total found: ");WriteInt(Tot , 2); WriteLn; END grep0.