1 -- **********************************************************
  2 -- * Programmer: Michael Siltz
  3 -- * Course: CS 240
  4 -- * Date Due: September 26, 2007
  5 -- * Language: ADA
  6 -- * Compiler used: gnat
  7 -- * Source Filename: discrete_set.adb
  8 -- * Executable Filename (if any) or None: None
  9 -- * Professor: Dr. Adams
 10 -- **********************************************************
 11 
 12 ÏÕÖ×package body Discrete_Set is
 13 ÏÏ§
 14 ÏÏ§ ----------------------------------------------------------------------------
 15 ÏÏ§ 
 16 ÏÏ§	 -- Determines whether Element is in Set  
 17 ÏÏ§ÏÞßàfunction Is_Member (Set     : in Set_Type;  
 18 ÏÏ§ÏÏ§Ï                    Element : in Element_Type) return Boolean is  
 19 ÏÏ§ÏÏ§begin
 20 ÏÏ§ÏÏ§
 21 ÏÏ§ÏÏ§	-- Returns true if Element is found in Set, false if it isn't
 22 ÏÏ§Â¹Ä¹¹Ïreturn Set(Element);
 23 ÏÏ§ÏÏ©end Is_Member; -- Ends function
 24 ÏÏ§  
 25 ÏÏ§ -----------------------------------------------------------------------------
 26 ÏÏ§	 
 27 ÏÏ§	 -- Finds the union of the two sets    
 28 ÏÏ§ÏÞßàfunction "+" (Left : in Set_Type;  Right : in Set_Type) return Set_Type is
 29 ÏÏ§ÏÏ§begin
 30 ÏÏ§ÏÏ§
 31 ÏÏ§ÏÏ§   -- Returns the elements found in either or both sets
 32 ÏÏ§Â¹Ä¹¹Ïreturn  Left or Right;
 33 ÏÏ§ÏÏ©end "+"; -- Ends function
 34 ÏÏ§ 
 35 ÏÏ§ ----------------------------------------------------------------------------
 36 ÏÏ§	
 37 ÏÏ§	 -- Adds an element to a set, but the set doesn't change if
 38 ÏÏ§	 -- the function is already a member.
 39 ÏÏ§ÏÞßàfunction "+" (Left  : in Set_Type; 
 40 ÏÏ§ÏÏ§Ï              Right : in Element_Type) return Set_Type is
 41 ÏÏ§ÏÏ§ÏíÏResult : Set_Type;
 42 ÏÏ§ÏÏ§begin
 43 ÏÏ§ÏÏ¨¹¹ÏResult := Left;
 44 ÏÏ§ÏÏ¨¹¹ÏResult(Right) := True;   -- Add the new element to the set
 45 ÏÏ§Â¹Ä¹¹Ïreturn Result; -- Returns the set with the added element
 46 ÏÏ§ÏÏ©end "+"; -- Ends function
 47 ÏÏ§ 
 48 ÏÏ§ ----------------------------------------------------------------------------
 49 ÏÏ§   
 50 ÏÏ§	 -- Adds an element to a set, but the set doesn't change if
 51 ÏÏ§	 -- the function is already a member    
 52 ÏÏ§ÏÞßàfunction "+" (Left  : in Element_Type;
 53 ÏÏ§ÏÏ§Ï              Right : in Set_Type) return Set_Type is
 54 ÏÏ§ÏÏ§ÏíÏResult : Set_Type;
 55 ÏÏ§ÏÏ§begin
 56 ÏÏ§ÏÏ¨¹¹ÏResult := Right;
 57 ÏÏ§ÏÏ¨¹¹ÏResult(Left) := True;   -- Add the new element to the set
 58 ÏÏ§Â¹Ä¹¹Ïreturn Result; -- Returns the set with the added element
 59 ÏÏ§ÏÏ©end "+"; -- Ends function
 60 ÏÏ§ 
 61 ÏÏ§ ----------------------------------------------------------------------------
 62 ÏÏ§
 63 ÏÏ§	 -- Finds the intersection of the two sets    
 64 ÏÏ§ÏÞßàfunction "*" (Left : in Set_Type;  Right : in Set_Type) return Set_Type is
 65 ÏÏ§ÏÏ§begin
 66 ÏÏ§ÏÏ§
 67 ÏÏ§ÏÏ§	-- Returns only the elements found in both sets
 68 ÏÏ§Â¹Ä¹¹Ïreturn  Left and Right;
 69 ÏÏ§ÏÏ©end "*"; -- Ends function
 70 ÏÏ§ 
 71 ÏÏ§ ----------------------------------------------------------------------------
 72 ÏÏ§    
 73 ÏÏ§	 -- Finds the difference between the two sets
 74 ÏÏ§ÏÞßàfunction "-" (Left : in Set_Type;  Right : in Set_Type) return Set_Type is
 75 ÏÏ§ÏÏ§begin
 76 ÏÏ§ÏÏ§   
 77 ÏÏ§ÏÏ§	-- Returns only the elements in the left set that are not in the right set
 78 ÏÏ§Â¹Ä¹¹Ïreturn Left and not Right;
 79 ÏÏ§ÏÏ©end "-"; -- Ends function
 80 ÏÏ§ 
 81 ÏÏ§ ----------------------------------------------------------------------------
 82 ÏÏ§    
 83 ÏÏ§	 -- Removes an element from the set, but the set is not changed
 84 ÏÏ§	 -- if the element wasn't a member
 85 ÏÏ§ÏÞßàfunction "-" (Left  : in Set_Type;
 86 ÏÏ§ÏÏ§Ï              Right : in Element_Type) return Set_Type is
 87 ÏÏ§ÏÏ§ÏíÏResult : Set_Type;
 88 ÏÏ§ÏÏ§begin
 89 ÏÏ§ÏÏ¨¹¹ÏResult := Left;
 90 ÏÏ§ÏÏ¨¹¹ÏResult(Right) := False;   -- Remove the element from the set
 91 ÏÏ§Â¹Ä¹¹Ïreturn Result; -- Returns the set with the element removed
 92 ÏÏ§ÏÏ©end "-"; -- Ends function
 93 ÏÏ§ 
 94 ÏÏ§ ----------------------------------------------------------------------------
 95 ÏÏ§
 96 ÏÏ§	 -- Determines whether the left set is a subset of the right set    
 97 ÏÏ§ÏÞßàfunction "<=" (Left : in Set_Type; Right : in Set_Type) return Boolean is
 98 ÏÏ§ÏÏ§ÏíÏIs_A_Subset : Boolean;
 99 ÏÏ§ÏÏ§begin
100 ÏÏ§ÏÏ§
101 ÏÏ§ÏÏ§	-- If the difference between the left and right sets is equivalent to an
102 ÏÏ§ÏÏ§	-- empty set, then the left set is a subset of the right set and it
103 ÏÏ§ÏÏ§	-- returns true
104 ÏÏ§ÏÏ¨¹¹ÏIs_A_Subset := (Left - Right) = Empty_Set;
105 ÏÏ§Â¹Ä¹¹Ïreturn Is_A_Subset;
106 ÏÏ§ÏÏ©end "<="; -- Ends function
107 ÏÏ§ 
108 ÏÏ§ ----------------------------------------------------------------------------
109 ÏÏ§    
110 ÏÏ§	 -- Determines whether the left set is a proper subset of the right set
111 ÏÏ§ÏÞßàfunction "<" (Left : in Set_Type; Right : in Set_Type) return Boolean is
112 ÏÏ§ÏÏ§ÏíÏResult : Boolean;
113 ÏÏ§ÏÏ§begin
114 ÏÏ§ÏÏ¨¹³´if Left = Right then  -- If the sets are equal, not a proper subset
115 ÏÏ§ÏÏ§Ï6¾¹¹ÏResult := False;
116 ÏÏ§ÏÏ§Ïö´else
117 ÏÏ§ÏÏ§Ï¸¾¹¹ÏResult := Left <= Right;  -- If not equal, test for subset
118 ÏÏ§ÏÏ§ÏÈÏend if;                      -- using the function above
119 ÏÏ§Â¹Ä¹¹Ïreturn Result; -- Returns true if the left set is a proper subset of the
120 ÏÏ§ÏÏ§	               -- right set
121 ÏÏ§ÏÏ©end "<"; -- Ends function
122 ÏÏ§ 
123 ÏÏ§ ----------------------------------------------------------------------------
124 ÏÏ§    
125 ÏÏ§	 -- Determines whether the right set is a subset of the left set
126 ÏÏ§ÏÞßàfunction ">=" (Left : in Set_Type; Right : in Set_Type) return Boolean is
127 ÏÏ§ÏÏ§begin
128 ÏÏ§Â¹Ä¹¹Ïreturn  Right <= Left;   -- Reverse the order of the 
129 ÏÏ§ÏÏ§                            -- parameters and use the operation 
130 ÏÏ§ÏÏ§                            -- we've already written
131 ÏÏ§ÏÏ©end ">="; -- Ends function
132 ÏÏ§ 
133 ÏÏ§ ----------------------------------------------------------------------------
134 ÏÏ§    
135 ÏÏ§	 -- Determines whether the right set is a proper subset of the left set
136 ÏÏ§ÏÞßàfunction ">" (Left : in Set_Type; Right : in Set_Type) return Boolean is
137 ÏÏ§ÏÏ§begin
138 ÏÏ§Â¹Ä¹¹Ïreturn  Right < Left;     -- Reverse the order of the 
139 ÏÏ§ÏÏ§                             -- parameters and use the operation 
140 ÏÏ§ÏÏ§                             -- we've already written
141 ÏÏ§ÏÏ©end ">"; -- Ends function
142 ÏÏ§ 
143 ÏÏ©end Discrete_Set; -- Ends procedure