1 / 27

Chapter 3

Chapter 3. Sets. Set Definitions. The abstract (logical) view of sets comes from discrete mathematics. A set a is an unordered collection of items called set members . An item is either in the set (is a member) or not in the set (is not a member).

Download Presentation

Chapter 3

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 Sets

  2. Set Definitions • The abstract (logical) view of sets comes from discrete mathematics. • A set a is an unordered collection of items called set members. • An item is either in the set (is a member) or not in the set (is not a member). • A set often is written by enclosing the group of items enclosed in braces. {Apple Pear Cherry} {Orange Cherry Pear Apple Banana}

  3. Special Sets • Empty set The set with no members. • Written as { } or  • Universal set The set containing all the values of a component type. • The universal set of fruits would contain all possible fruits

  4. Membership Operation • Membership - Is an item a member of a set?

  5. Union, Intersection, and Difference • are three operations that form a new set from two existing sets.

  6. Examples Union, Intersection, and Difference

  7. Subset • A subset is a set contained within another set. A set X is a subset of Y if each element of X is an element of Y. • If at least one element of Y is not in X, then X is called a proper subset of Y.

  8. Subset Examples

  9. Adding and Removing Members

  10. UML Diagram of Set

  11. Bingo Numbers

  12. Specification of Bingo Numbers • package Bingo_Numbers is • - This package defines Bingo numbers and their associated letters • - - The range of numbers on a Bingo Card • type Bingo_Number is range 0..75; • - - 0 can't be called, it is only for the Free Play square • subtype Callable_Number is Bingo_Number range 1..75; • - - Associations between Bingo numbers and letters • subtype B_Range is Bingo_Number range 1..15; • subtype I_Range is Bingo_Number range 16..30; • subtype N_Range is Bingo_Number range 31..45; • subtype G_Range is Bingo_Number range 46..60; • subtype O_Range is Bingo_Number range 61..75; • - - The 5 Bingo letters • type Bingo_Letter is (B, I, N, G, O); • end Bingo_Numbers;

  13. Specification of a Set of Bingo Numbers (1) with Bingo_Numbers; use Bingo_Numbers; package Bingo_Number_Set is subtype Element_Type is Bingo_Numbers.Callable_Number; type Set_Type is private; Empty_Set : constant Set_Type; Universal_Set : constant Set_Type; --------------------------------------------------------------------------- function Is_Member (Set : in Set_Type; Element : in Element_Type) return Boolean; ---------------------------------------------------------------------------- function "+" (Left : in Set_Type; Right : in Set_Type) return Set_Type; - - Returns the union of the two sets ---------------------------------------------------------------------------- function "+" (Left : in Set_Type; Right : in Element_Type) return Set_Type; - - Adds an element to a set

  14. Specification of a Set of Bingo Numbers (2) function "+" (Left : in Element_Type; Right : in Set_Type) return Set_Type; - - Adds an element to a set function "*" (Left : in Set_Type; Right : in Set_Type) return Set_Type; -- Returns the intersection of the two sets function "-" (Left : in Set_Type; Right : in Set_Type) return Set_Type; - - Returns the difference of the two sets function "-" (Left : in Set_Type; Right : in Element_Type) return Set_Type; - - Removes an element from the set

  15. Specification of a Set of Bingo Numbers (3) function "<=" (Left : in Set_Type; Right : in Set_Type) return Boolean; - - Returns True if Left is a subset of Right function "<" (Left : in Set_Type; Right : in Set_Type) return Boolean; - - Returns True if Left is a proper subset of Right function ">=" (Left : in Set_Type; Right : in Set_Type) return Boolean; - - Returns True if Right is a subset of Left function ">" (Left : in Set_Type; Right : in Set_Type) return Boolean; - - Returns True if Right is a proper subset of Left private - - We look at this part next end Bingo_Number_Set;

  16. A Set of Bingo Numbers Stored as an Array of Booleans Each of the 75 Bingo Numbers is either in My_Set or not in My_Set

  17. Examples of Set Operations

  18. Private Part of Bingo Number Specification private type Set_Type is array (Element_Type) of Boolean; - - Completion of deferred constants Empty_Set : constant Set_Type := (Element_Type => False); Universal_Set : constant Set_Type := (Element_Type => True); end Bingo_Number_Set;

  19. Programming for ReuseGeneric Units • Generic Unit A template for a package or subprogram. • Instance A package or subprogram created from a generic unit. • Generic Formal Parameter A parameter defined in a generic unit declaration. Used to customize a generic unit for a specific problem. • Instantiation A declaration that creates an instance of a generic unit.

  20. A Generic Set Class and Three Instances

  21. EBNF for Generic Units (1) generic_declaration ::= generic_subprogram_declaration | generic_package_declaration generic_subprogram_declaration ::= generic_formal_part subprogram_specification; generic_package_declaration ::= generic_formal_part package_specification; generic_formal_part ::= generic {generic_formal_parameter_declaration | use_clause} generic_formal_parameter_declaration ::= formal_object_declaration | formal_type_declaration | formal_subprogram_declaration | formal_package_declaration formal_type_declaration ::= type defining_identifier [discriminant_part] is formal_type_definition;

  22. EBNF for Generic Units (2) formal_type_declaration ::= type defining_identifier [discriminant_part] is formal_type_definition; formal_type_definition ::= formal_discrete_type_definition | formal_signed_integer_type_definition | formal_modular_type_definition | formal_floating_point_definition | formal_ordinary_fixed_point_definition | formal_decimal_fixed_point_definition | formal_array_type_definition | formal_private_type_definition | formal_access_type_definition | formal_interface_type_definition

  23. EBNF for Generic Units (3) formal_discrete_type_definition ::= (<>) formal_signed_integer_type_definition ::= range <> formal_modular_type_definition ::= mod <> formal_floating_point_definition ::= digits <> formal_ordinary_fixed_point_definition ::= delta <> formal_decimal_fixed_point_definition ::= delta <> digits <> formal_array_type_definition ::= array_type_definition formal_private_type_definition ::= [limited] private formal_subprogram_declaration ::= with subprogram_specification; formal_package_declaration ::= withpackage defining_identifier is newgeneric_package_name formal_package_actual_part;

  24. Generic Selection Sort generic type Element_Type is private; type Array_Type is array (Positive range <>) of Element_Type; with function "<" (Left : in Element_Type; Right : in Element_Type) return Boolean; procedure Selection_Sort (The_Array : in out Array_Type); -- Purpose : Sort an array of elements into ascending order

  25. Instantiation of Generic Selection Sort type Part_Rec is record Part_Number : Positive; Quantity : Natural; end record; -- The array type that we will sort type Part_Array is array (Positive range <>) of Part_Rec; -- A function to compare two inventory records function "<" (Left : in Part_Rec; Right : in Part_Rec) return Boolean is begin return Left.Part_Number < Right.Part_Number; end "<"; -- The inventory part sorting procedure procedure Part_Sort is new Selection_Sort (Element_Type => Part_Rec, Array_Type => Part_Array, "<" => "<");

  26. Generic Binary Search generic type Index_Type is (<>); type Element_Type is limited private; type Array_Type is array (Index_Type range <>) of Element_Type; with function "<" (Left : in Element_Type; Right : in Element_Type) return Boolean; with function "=" (Left : in Element_Type; Right : in Element_Type) return Boolean; procedure Binary_Search (List : in Array_Type; Value : in Element_Type; Found : out Boolean; Location : out Index_Type); -- Purpose : Search List for Value -- Preconditions : List is in ascending order -- Postconditions : Value is in List and Found and List(Location) = Value -- or -- Value is not in List and not Found

  27. Two Instantiations of Generic Binary Search type Month_Type is (January, February, March, April, May, June, July, August, September, October, November, December); type Nat_Array is array (Month_Type range <>) of Natural; type Int_Array is array (Positive range <>) of Integer; procedure Nat_Search is new Binary_Search (Element_Type => Natural, Index_Type => Month_Type, Array_Type => Nat_Array, "<" => "<", "=" => "="); procedure Int_Search is new Binary_Search (Element_Type => Integer, Index_Type => Positive, Array_Type => Int_Array, "<" => Standard."<", "=" => Standard."=");

More Related