Matt Jenkins

 1 package body Discrete_Set is
 2 
 3 ----------------------------------------------------------------------------
-- this method checks to see if the element is a member of the set
 4 function Is_Member (Set     : in Set_Type;  
 5                 Element : in Element_Type) return Boolean is  
 6 begin
 7 return Set(Element);
 8  Is_Member;
 9 
10 ----------------------------------------------------------------------------
--this method takes the union of two sets which return a set of elements
-- that are in either or both sets
11 function "+" (Left : in Set_Type;  Right : in Set_Type) return Set_Type is
12 begin
13 return  Left or Right;
14 end "+";
15 
16 ----------------------------------------------------------------------------

--this method adds an element to a set but if the element is already
-- present then the set is left unchanged
17 function "+" (Left  : in Set_Type; 
18           Right : in Element_Type) return Set_Type is
19 Result : Set_Type;
20 begin
21 Result := Left;
22 Result(Right) := True;   -- Add the new element to the set
23 return Result;
24 end "+";
25 
26 ----------------------------------------------------------------------------
--this method adds an element to a set but if the element is already
-- present then the set is left unchanged 
27 function "+" (Left  : in Element_Type;
28           Right : in Set_Type) return Set_Type is
29 Result : Set_Type;
30 begin
31 Result := Right;
32 Result(Left) := True;   -- Add the new element to the set
33 return Result;
34 end "+";
35 
36 ----------------------------------------------------------------------------
--this method returns the intersection of two sets
-- only elements that occur in both sets are the intersection
37 function "*" (Left : in Set_Type;  Right : in Set_Type) return Set_Type is
38 begin
39 return  Left and Right;
40 end "*";
41 
42 ----------------------------------------------------------------------------
--this method will return a set containing the difference of the two sets

43 function "-" (Left : in Set_Type;  Right : in Set_Type) return Set_Type is
44 begin
45 return Left and not Right;
46 end "-";
47 
48 ----------------------------------------------------------------------------
--this method will remove an element from a set
49 function "-" (Left  : in Set_Type;
50           Right : in Element_Type) return Set_Type is
51 Result : Set_Type;
52 begin
53 Result := Left;
54 Result(Right) := False;   -- Remove the element from the set
55 return Result;
56 end "-";
57 
58 ----------------------------------------------------------------------------
--this method returns a boolean value if one set is a subset of the other
59 function "<=" (Left : in Set_Type; Right : in Set_Type) return Boolean is
60 Is_A_Subset : Boolean;
61 begin
62 Is_A_Subset := (Left - Right) = Empty_Set;
--Returns True if Left is a subset of Right
63 return Is_A_Subset;
64 end "<=";
65 
66 ----------------------------------------------------------------------------
-- returns a boolean value if the first set is a proper subset of the other
67 function "<" (Left : in Set_Type; Right : in Set_Type) return Boolean is
68 Result : Boolean;
69 begin
70 if Left = Right then  -- If the sets are equal, not a proper subset
71 Result := False;
72 else
73 Result := Left <= Right;  -- If not equal, test for subset
74 end if;                      -- using the function above
75 return Result;
76 end "<";
77 
78 ---------------------------------------------------------------------------
--returns a boolean value if the second set is a subset of the first
79 function ">=" (Left : in Set_Type; Right : in Set_Type) return Boolean is
80 begin
81 return  Right <= Left;   -- Reverse the order of the 
82                          -- parameters and use the operation 
83                          -- we've already written
84 end ">=";
85 
86 ----------------------------------------------------------------------------
--returns a boolean value if the second set is a proper subset of the first
87 function ">" (Left : in Set_Type; Right : in Set_Type) return Boolean is
88 begin
89 return  Right < Left;     -- Reverse the order of the 
90                           -- parameters and use the operation 
91                           -- we've already written
92 end ">";
93 
94  Discrete_Set;
95