460 likes | 475 Views
Chapter 3.1. Modules, Abstract Data Types and Objects. Local Modules Abstract data types and objects Example : An explicit stack for Quicksort External Modules Definition and Implementation Modules Separate Compilation of Modules Libraries Example : Files and File Sorting. Summary.
E N D
Chapter 3.1 Modules, Abstract Data Types and Objects
Local Modules Abstract data types and objects Example : An explicit stack for Quicksort External Modules Definition and Implementation Modules Separate Compilation of Modules Libraries Example : Files and File Sorting Summary
Local Modules Abstract data types and objects Example : An explicit stack for Quicksort External Modules Definition and Implementation Modules Separate Compilation of Modules Libraries Example : Files and File Sorting Summary
Local Variablesdo not keep their value between activations MODULE MainModule PrintNicely; PrintNicely; PROCEDURE PrintNicely; VAR LineCount : CARDINAL BEGIN …. END PrintNicely; PrintNicely; PrintNicely; PrintNicely; END MainModule.
Global Variablesare unavoidable but dangerous ... MODULE MainModule VAR LineCount : CARDINAL PrintNicely; PrintNicely; PROCEDURE PrintNicely; BEGIN …. END PrintNicely; PrintNicely; PrintNicely; PrintNicely; END MainModule.
Constant Declarations Type Declarations Variable Declarations Procedure Declarations Local Module Declarations BEGIN END Statement ; Block
Local Modulesto control accessibility of variables MODULE MainModule PrintNicely; PrintNicely; MODULE Print; EXPORT PrintNicely; VAR LineCount : CARDINAL PROCEDURE PrintNicely; …. END PrintNicely; PrintNicely; BEGIN (* initialisations *) …. END Print. PrintNicely; PrintNicely; END MainModule.
Instance of the object : MyJack Possible actions : Fetch Inspect Store Lift a car Lower a car Repairing a flat tireObject-oriented style Object Jack =
Problem : If a data item is slightly changed, the entire program needs to be checked. Solution : Group data with description of all actions that can be performed upon such data Access data exclusively through the actions predefined for these data Object = data + actions to access it. Object oriented Design
Module = Abstract data type Object = Abstract data type + Goodies Goodies = Classes : templates to make several instances of one Abstract Data Type Inheritance : technique to derive one class from another class by adding features. None of these goodies is available in Modula 2 Abstract Data Types& Objects
Local Modules Abstract data types and objects Example : An explicit stack for Quicksort External Modules Definition and Implementation Modules Separate Compilation of Modules Libraries Example : Files and File Sorting Summary
ORANGE YELLOW YELLOW YELLOW BLUE BLUE BLUE BLUE GREEN YELLOW YELLOW BLUE BLUE BLUE Last-in, First-out Stack
Stack(1) MODULE Stack; IMPORT StackItem; EXPORT Push, Pop, Full, Empty; CONST Size = 30; VAR Store : ARRAY[0..Size] OF StackItem; Full,Empty : BOOLEAN; n : [0..Size]; PROCEDURE Push ... END; PROCEDURE Pop ... END; BEGIN n := 0; Empty := TRUE; Full := FALSE; END Stack;
Stack(2) PROCEDURE Push(x : StackItem); BEGIN IF n < Size THEN n:=n+1;Store[n]:=x END; IF n = Size THEN Full := TRUE END; Empty := FALSE END Push; PROCEDURE Pop(VAR x : StackItem); BEGIN IF n > 0 THEN x:=Store[n]; n:=n-1 END; IF n = 0 THEN Empty := TRUE; Full := FALSE END Pop;
Quicksort (without explicit recursion) Push limits of the array to be sorted on stack Get limits of an array to be sorted from stack Partition array into two parts A1 and A2 in such a way that all keys in A1 are < Pivot and all keys in A2 are > Pivot (Pivot has a value close to the median of key values) More than 1 element in A1 ? Yes Push limits of A1 on stack More than 1 element in A2 ? Yes Push limits of A2 on stack UNTIL Stack empty
QuickSort(Without Explicit Recursion) • PROCEDURE QuickSort(VARAARRAYOFItem); • TYPE StackItem : RECORD • L,H : CARDINAL • END; • VAR Task : StackItem; • ... • MODULE Stack .. . END Stack; • BEGIN • WITH Task DO L:=1;H:=High(A) END; • Push(Task); • REPEAT • Pop(Task);Perform the sorting task • UNTILEmpty • END QuickSort;
“Perform the sorting task” i := L; j := H; Pivot := A[(L+H) DIV 2].key; REPEAT WHILEA[i].key < Pivot DO INC(i) END; WHILEA[j].key > Pivot DO DEC(j) END; IF i<=j THEN Swap(A[i],A[j]); INC(i); DEC(j) END; UNTIL i > j; IFL < j THENPush(L,j)END; IFi < HTHENPush(i,H)END;
Local Modules Abstract data types and objects Example : An explicit stack for Quicksort External Modules Definition and Implementation Modules Separate Compilation of Modules Libraries Example : Files and File Sorting Summary
External Modules DEFINITION MODULE ABC DEFINITION MODULE UVW IMPLEMENTATION MODULE ABC IMPLEMENTATION MODULE UVW
Local Modules Abstract data types and objects Example : An explicit stack for Quicksort External Modules Definition and Implementation Modules Separate Compilation of Modules Libraries Example : Files and File Sorting Summary
Separate Compilation DEFINITION MODULE ABC DEFINITION MODULE UVW IMPLEMENTATION MODULE ABC IMPLEMENTATION MODULE UVW
Local Modules Abstract data types and objects Example : An explicit stack for Quicksort External Modules Definition and Implementation Modules Separate Compilation of Modules Libraries Example : Files and File Sorting Summary
InOut Library DEFINITION MODULE InOut; CONST EOL = 15C; (* hardware dependant--could also be 36C *) VAR Done: BOOLEAN; termCH: CHAR; PROCEDURE OpenInput (defext: ARRAY OF CHAR); PROCEDURE OpenOutput (defext: ARRAY OF CHAR); PROCEDURE CloseInput; PROCEDURE CloseOutput; PROCEDURE Read (VAR ch: CHAR); PROCEDURE ReadString (VAR s: ARRAY OF CHAR); PROCEDURE ReadInt (VAR x: INTEGER); PROCEDURE ReadCard (VAR x: CARDINAL); PROCEDURE Write (ch: CHAR); PROCEDURE WriteLn; PROCEDURE WriteString (s: ARRAY OF CHAR); PROCEDURE WriteInt (x: INTEGER; n: CARDINAL); PROCEDURE WriteCard (x, n: CARDINAL); PROCEDURE WriteOct (x, n: CARDINAL); PROCEDURE WriteHex (x, n: CARDINAL); END InOut.
Local Modules Abstract data types and objects Example : An explicit stack for Quicksort External Modules Definition and Implementation Modules Separate Compilation of Modules Libraries Example : Files and File Sorting Summary
Files are data structures that reside in peripheral memories, Files are managed by the operating system. Modula 2 doesn't have predefined files All Modula 2 implementations offer one or more libraries to access the file system. The programmer should design his/her own abstract data type implementing files, so that only the implementation module should be adapted to other environments. Files in Modula 2
A Sequential File Model File, in peripheral memory Write Buffer: User program Read Buffer: User program
Natural Merge-SortFiles definition DEFINITION MODULE MyFiles; FROMFileSystemIMPORTFile,Create,Rename,Close, SetRead,SetWrite,Eof; FROM TextIO IMPORT WriteCard,ReadCard; TYPE FileType = RECORD Buffer : CARDINAL; eof,last : BOOLEAN; F : File; END; VAR A,B,C : FileType; PROCEDURE ResetFile(VAR X:FileType); PROCEDURE RewriteFile(VAR X:FileType); PROCEDURE CopyItem(VAR X,Y:FileType); END MyFiles.
Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort File Sorting Merge-Sort Natural Merge-Sort Polyphase Sort Sort Algorithms
Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort File Sorting Merge-Sort Natural Merge-Sort Polyphase Sort Sort Algorithms
Merge Distribute time Merge-Sort Algorithm qwertyuiopasdfghjklzxcvbnm qwertyuiopasdfghjklzxcvbnm qwertyiuopasdfghjklzcxbvmn qwertyiuopasdfghjklzcxbvmn
Merge-Sort Algorithm (2) qwertyiuopasdfghjklzcxbvmn eqrwituyaopsdfghjklzbcxvmn eqrwituyaopsdfghjklzbcxvmn eiqrtuwyadfghopsbcjklxvzmn eiqrtuwyadfghopsbcjklxvzmn adefghiopqrstuwybcjklmnxvz adefghiopqrstuwybcjklmnxvz abcdefghijklmnopqrstuvwxyz
Time to copy one record : t duration of one distribute-merge cycle : 2.n.t Length of ordered sequence (run) after k distribue-merge cycles : 2 k Number of runs after k cycles : n / 2 k Number of cycles required to have one run : log2n Total duration of merge-sort : T = 2nt log2 n Merge-Sort Performance
Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort File Sorting Merge-Sort Natural Merge-Sort Polyphase Sort Sort Algorithms
Natural Merge-Sort qwertyuiopasdfghjklzxcvbnm eqrtwyiopuadfghjklsxzbcnvm eqrtwyiopuadfghjklsxzbcnvm eiopqrtuwyabcdfghjklnsvxzm eiopqrtuwyabcdfghjklnsvxzm abcdefghijklnopqrstuvwxyzm abcdefghijklnopqrstuvwxyzm abcdefghijklmnopqrstuvwxyz
Natural Merge-SortMain Program BEGIN (* Merge-Sort *) REPEAT RewriteFile(A); RewriteFile(B); ResetFile(C); Distribute; ResetFile(A); ResetFile(B); RewriteFile(C); Runs := 0; Merge; UNTIL Runs = 1; END MergeSort.
Natural Merge-SortDistribute PROCEDURE Distribute; BEGIN REPEAT CopyRun(C,A); IFNOT C.eof THEN CopyRun(C,B) END UNTIL C.eof END Distribute;
Natural Merge-SortMerge (1) PROCEDURE Merge; BEGIN REPEAT LOOP IF A.Buffer.Key < B.Buffer.Key THEN CopyItem(A,C); IF A.eor THEN CopyRun(B,C) EXIT END ELSE CopyItem(B,C); IF B.eor THEN CopyRun(A,C) EXIT END END (* IF *) END; (* LOOP *) Runs := Runs + 1; UNTIL A.eof OR B.eof;
Natural Merge-SortMerge (2) WHILE NOT A.eof DO (* copy any possible tail of A *) CopyRun(A,C); Runs := Runs + 1; END; WHILE NOT B.eof DO (* copy any possible tail of B *) CopyRun(B,C); Runs := Runs + 1; END; END Merge;
Natural Merge-SortCopyRun PROCEDURE CopyRun(VAR X,Y : FileType); BEGIN REPEAT CopyItem(X,Y) UNTIL X.eor END CopyRun;
Natural Merge-SortFiles definition DEFINITION MODULE MSFiles; FROMFileSystemIMPORTFile,Create,Rename,Close, SetRead,SetWrite,Eof; WriteWord,ReadWord; TYPE FileType = RECORD Buffer : Item; eor,eof : BOOLEAN; F : File; END; VAR A,B,C : FileType; PROCEDURE ResetFile(VAR X:FileType); PROCEDURE RewriteFile(VAR X:FileType); PROCEDURE CopyItem(VAR X,Y:FileType); END MSFiles.
Natural Merge-SortCopyItem implementation PROCEDURE CopyItem(VAR X,Y:FileType); BEGIN Y.Buffer := X.Buffer; X.Buffer := ReadWord(X.F); X.eof := X.Eof; X.eor := X.eof OR (Y.Buffer.Key > X.Buffer.Key); WriteWord(Y.F,Y.Buffer); END CopyItem;
PROCEDURE ResetFile(VAR X:FileType); BEGIN WITH X DO Seek(F,0); Buffer := ReadWord(F); Last := EOF; eof := FALSE; eor := FALSE; END END ResetFile; Natural Merge-SortResetFile implementation
Natural Merge-SortRewriteFile implementation PROCEDURE RewriteFile(VAR X:FileType); BEGIN WITH X DO Seek(F,0); Truncate(F); END END RewriteFile;
Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort File Sorting Merge-Sort Natural Merge-Sort Polyphase Sort Sort Algorithms
Four tape Merge-Sort Merge/distribute Distribute time X T = 2nt log2 n
Polyphase Sort T = nt log(m-1) n