Download presentation
Presentation is loading. Please wait.
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?
Membership Test Answer Apple in the set { Apple Pear Cherry } True Kumquat in the set { Apple Banana Lemon } False
5
Union, Intersection, and Difference
are three operations that form a new set from two existing sets. Union The union of two sets is a set consisting of those components that are in the first set or in the second set. The or used here is the inclusive or. Intersection The intersection of two sets is a set consisting of those components occurring in the first set and in the second set. Difference The difference between two sets is the set of components in the first set that are not in the second.
6
Examples Union, Intersection, and Difference
1st Set 2nd Set Union Intersection Difference {P Q R} {S} {P Q R S} {} {A B C D} {B D F} {A B C D F} {B D} {A C} {L N} {L M N O P} {X Y Z}
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 First Set Second Set First is subset of Second
First is proper subset of Second {A E I O U} {A B C D E F} False {A B C D} {A B C} {X Y Z} True {W X Y Z}
9
Adding and Removing Members
Set Element Addition of Element Removal of Element {A E I O U} B {A B E I O U} {A B C D} {A C D}
10
UML Diagram of Set
11
Bingo Numbers Column Number Range B 1 to 15 I 16 to 30 N 31 to 45 G
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 ; subtype I_Range is Bingo_Number range ; subtype N_Range is Bingo_Number range ; subtype G_Range is Bingo_Number range ; subtype O_Range is Bingo_Number range ; - - 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
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 Reuse Generic 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 ::= with package defining_identifier is new generic_package_name formal_package_actual_part;
24
Generic Selection Sort
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; 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."=");
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.