1 / 46

Chapter 3.1

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.

khan
Download Presentation

Chapter 3.1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3.1 Modules, Abstract Data Types and Objects

  2. 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

  3. 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

  4. 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.

  5. Global Variablesare unavoidable but dangerous ... MODULE MainModule VAR LineCount : CARDINAL PrintNicely; PrintNicely; PROCEDURE PrintNicely; BEGIN …. END PrintNicely; PrintNicely; PrintNicely; PrintNicely; END MainModule.

  6. Constant Declarations Type Declarations Variable Declarations Procedure Declarations Local Module Declarations BEGIN END Statement ; Block

  7. 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.

  8. Instance of the object : MyJack Possible actions : Fetch Inspect Store Lift a car Lower a car Repairing a flat tireObject-oriented style Object Jack =

  9. 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

  10. 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

  11. 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

  12. ORANGE YELLOW YELLOW YELLOW BLUE BLUE BLUE BLUE GREEN YELLOW YELLOW BLUE BLUE BLUE Last-in, First-out Stack

  13. 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;

  14. 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;

  15. 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

  16. 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;

  17. “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;

  18. 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

  19. External Modules DEFINITION MODULE ABC DEFINITION MODULE UVW IMPLEMENTATION MODULE ABC IMPLEMENTATION MODULE UVW

  20. 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

  21. Separate Compilation DEFINITION MODULE ABC DEFINITION MODULE UVW IMPLEMENTATION MODULE ABC IMPLEMENTATION MODULE UVW

  22. 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

  23. 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.

  24. 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

  25. 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

  26. A Sequential File Model File, in peripheral memory Write Buffer: User program Read Buffer: User program

  27. 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.

  28. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort File Sorting Merge-Sort Natural Merge-Sort Polyphase Sort Sort Algorithms

  29. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort File Sorting Merge-Sort Natural Merge-Sort Polyphase Sort Sort Algorithms

  30. Merge Distribute time Merge-Sort Algorithm qwertyuiopasdfghjklzxcvbnm qwertyuiopasdfghjklzxcvbnm qwertyiuopasdfghjklzcxbvmn qwertyiuopasdfghjklzcxbvmn

  31. Merge-Sort Algorithm (2) qwertyiuopasdfghjklzcxbvmn eqrwituyaopsdfghjklzbcxvmn eqrwituyaopsdfghjklzbcxvmn eiqrtuwyadfghopsbcjklxvzmn eiqrtuwyadfghopsbcjklxvzmn adefghiopqrstuwybcjklmnxvz adefghiopqrstuwybcjklmnxvz abcdefghijklmnopqrstuvwxyz

  32. 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

  33. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort File Sorting Merge-Sort Natural Merge-Sort Polyphase Sort Sort Algorithms

  34. Natural Merge-Sort qwertyuiopasdfghjklzxcvbnm eqrtwyiopuadfghjklsxzbcnvm eqrtwyiopuadfghjklsxzbcnvm eiopqrtuwyabcdfghjklnsvxzm eiopqrtuwyabcdfghjklnsvxzm abcdefghijklnopqrstuvwxyzm abcdefghijklnopqrstuvwxyzm abcdefghijklmnopqrstuvwxyz

  35. 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.

  36. Natural Merge-SortDistribute PROCEDURE Distribute; BEGIN REPEAT CopyRun(C,A); IFNOT C.eof THEN CopyRun(C,B) END UNTIL C.eof END Distribute;

  37. 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;

  38. 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;

  39. Natural Merge-SortCopyRun PROCEDURE CopyRun(VAR X,Y : FileType); BEGIN REPEAT CopyItem(X,Y) UNTIL X.eor END CopyRun;

  40. 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.

  41. 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;

  42. 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

  43. Natural Merge-SortRewriteFile implementation PROCEDURE RewriteFile(VAR X:FileType); BEGIN WITH X DO Seek(F,0); Truncate(F); END END RewriteFile;

  44. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort File Sorting Merge-Sort Natural Merge-Sort Polyphase Sort Sort Algorithms

  45. Four tape Merge-Sort Merge/distribute Distribute time X T = 2nt log2 n

  46. Polyphase Sort T = nt log(m-1) n

More Related