MODULE findseq; (*--------------------------------------------------------- File name: findseq.pm Author: MM, AG Date: 6/07/05 Problem: Finds a pattern on a chain of integers. Pattern is stored on an ARRAY of size N. Example of Execution: Pattern: 0 2 1 2 0 Sequence: 2 2 0 0 1 2 1 0 0 1 0 1 0 2 0 1 0 2 0 2 2 2 1 0 0 2 0 2 0 1 0 2 0 1 2 0 1 0 0 0 0 1 1 2 0 0 1 1 0 0 1 0 1 2 1 2 1 2 0 2 1 2 2 0 1 1 2 2 1 0 1 1 0 1 0 1 1 0 2 1 1 0 0 1 2 2 1 2 0 0 0 1 2 2 2 2 1 1 2 2 0 0 2 2 1 2 0 2 1 2 1 0 0 0 [0 2 1 2 0] 0 0 2 0 0 1 2 2 0 0 2 2 1 0 2 1 [0 2 1 2 0] 1 2 2 1 2 1 2 1 1 2 1 2 1 0 2 0 1 2 Total found: 6 Result: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Cycles: N = length of pattern. Does not depend on the length of the chain searched (N + 1) assign + (2N) test + N shift + 1 Reduce. *) (*---------------------------------------------------------- CONFIGURATION ----------------------------------------------------------*) CONST MAXCELLS=1024; (* MAXCELLS cells in one line *) CONFIGURATION chain[0..(MAXCELLS - 1)]; CONNECTION left: chain [i] -> chain[(i-1) MOD MAXCELLS]; right: chain[i] -> chain[(i+1) MOD MAXCELLS]; (*---------------------------------------------------------- VARIABLES ----------------------------------------------------------*) VAR L0seq, L1Res: chain OF INTEGER; i, N, Tot:INTEGER; A: ARRAY[0..(N-1)] OF INTEGER; (* pattern of ints *) (*---------------------------------------------------------- MAIN ----------------------------------------------------------*) BEGIN (* initialize data *) L0seq:= RandomInt(chain) MOD 3; A[0] := 0; A[1] := 2; A[2] := 1; A[3] := 2; A[4] := 0; N:=5; (* pattern length *) L1Res:= 1; (* initialize chain to test all *) FOR i:= 0 TO (N - 1) DO IF L1Res = 1 THEN (* if selected *) IF L0seq # A[i] THEN (* if not there*) L1Res := 0; (* deselect *) END; (* IF *) END; (* IF *) L1Res := MOVE.right(L1Res); (* shift for next item *) END; (* for *) Tot:= REDUCE.SUM(L1Res); (* count 1's= how many times found*) (* Printing *) IF ID(chain) < 160 THEN WriteString("Pattern: "); FOR i:= 0 TO (N - 1) DO WriteInt(A[i], 2); END; (* FOR *) WriteLn; WriteString("Sequence: "); WriteInt(L0seq, 2); WriteLn; WriteString("Total found: "); WriteInt(Tot, 2); WriteLn; WriteString("Result: "); WriteInt(L1Res, 2); WriteLn; END; (* display *) END findseq.