1 -- name : Jonathan Herman
  2 -- course: cs240
  3 -- due date: sept 26, 2007
  4 -- source: discrete_set.adb
  5 -- executable: na
  6 -- professor: adams
  7 -- compiler: gnat ada
  8 
  9 -- Purpose:  this program performs operations on discrete sets of anything
 10 -- Input: sets and elements of those sets
 11 -- Output: other sets and booleans
 12 
 13 ÕÖ×package body Discrete_Set is
 14  2 ÏÏ§
 15  3 ÏÏ§----------------------------------------------------------------------------
 16  4 ÏÏ§ÏÞßàfunction Is_Member (Set     : in Set_Type;  
 17  5 ÏÏ§ÏÏ§Ï                Element : in Element_Type) return Boolean is  
 18  6 ÏÏ§ÏÏ§begin
 19                -- checks to see if that element is set to true or false
 20  7 ÏÏ§Â¹Ä¹¹Ïreturn Set(Element);
 21  8 ÏÏ§ÏÏ©end Is_Member;
 22  9 ÏÏ§
 23 10 ÏÏ§----------------------------------------------------------------------------
 24 11 ÏÏ§ÏÞßàfunction "+" (Left : in Set_Type;  Right : in Set_Type) return Set_Type is
 25 12 ÏÏ§ÏÏ§begin
 26                -- returns a set filled with every element in the left or right set
 27                -- as a member.
 28                -- (as in, the or is a boolean, so it is looking for everything that
 29                --  is true in left or right, which is why it gets a set with all the
 30                --  true elements. this one is a pain to put in words.)
 31 13 ÏÏ§Â¹Ä¹¹Ïreturn  Left or Right;
 32 14 ÏÏ§ÏÏ©end "+";
 33 15 ÏÏ§
 34 16 ÏÏ§----------------------------------------------------------------------------
 35 17 ÏÏ§ÏÞßàfunction "+" (Left  : in Set_Type; 
 36 18 ÏÏ§ÏÏ§Ï          Right : in Element_Type) return Set_Type is
 37 19 ÏÏ§ÏÏ§ÏíÏResult : Set_Type;
 38 20 ÏÏ§ÏÏ§begin
 39 21 ÏÏ§ÏÏ¨¹¹ÏResult := Left; -- makes result the same set as left
 40              -- goes to the place that holds that element type and sets it to true
 41              -- which means that the set now has that element in it.
 42 22 ÏÏ§ÏÏ¨¹¹ÏResult(Right) := True; 
 43 23 ÏÏ§Â¹Ä¹¹Ïreturn Result;
 44 24 ÏÏ§ÏÏ©end "+";
 45 25 ÏÏ§
 46 26 ÏÏ§----------------------------------------------------------------------------
 47 27 ÏÏ§ÏÞßàfunction "+" (Left  : in Element_Type;
 48 28 ÏÏ§ÏÏ§Ï          Right : in Set_Type) return Set_Type is
 49 29 ÏÏ§ÏÏ§ÏíÏResult : Set_Type;
 50 30 ÏÏ§ÏÏ§begin
 51 31 ÏÏ§ÏÏ¨¹¹ÏResult := Right;
 52              -- goes to the place that holds that element type and sets it to true
 53              -- which means that the set now has that element in it.
 54 32 ÏÏ§ÏÏ¨¹¹ÏResult(Left) := True;   -- Add the new element to the set
 55 33 ÏÏ§Â¹Ä¹¹Ïreturn Result;
 56 34 ÏÏ§ÏÏ©end "+";
 57 35 ÏÏ§
 58 36 ÏÏ§----------------------------------------------------------------------------
 59 37 ÏÏ§ÏÞßàfunction "*" (Left : in Set_Type;  Right : in Set_Type) return Set_Type is
 60 38 ÏÏ§ÏÏ§begin
 61             -- because its using a boolean operation on sets of booleans, it goes through
 62             -- each element and if both have true for that element, the new set has true
 63             -- for that element
 64 39 ÏÏ§Â¹Ä¹¹Ïreturn  Left and Right;
 65 40 ÏÏ§ÏÏ©end "*";
 66 41 ÏÏ§
 67 42 ÏÏ§----------------------------------------------------------------------------
 68 43 ÏÏ§ÏÞßàfunction "-" (Left : in Set_Type;  Right : in Set_Type) return Set_Type is
 69 44 ÏÏ§ÏÏ§begin
 70             -- again, because it is using a boolean operation on sets of booleans, it
 71             -- goes through the entire set on the left and if the element is false
 72             -- for the one on the right, the new set that is being created has its
 73             -- corresponding element set to true
 74 45 ÏÏ§Â¹Ä¹¹Ïreturn Left and not Right;
 75 46 ÏÏ§ÏÏ©end "-";
 76 47 ÏÏ§
 77 48 ÏÏ§----------------------------------------------------------------------------
 78 
 79 49 ÏÏ§ÏÞßàfunction "-" (Left  : in Set_Type;
 80 50 ÏÏ§ÏÏ§Ï          Right : in Element_Type) return Set_Type is
 81 51 ÏÏ§ÏÏ§ÏíÏResult : Set_Type;
 82 52 ÏÏ§ÏÏ§begin
 83 53 ÏÏ§ÏÏ¨¹¹ÏResult := Left;
 84             -- finds the location that holds the boolean for that element
 85             -- and sets it to false
 86 54 ÏÏ§ÏÏ¨¹¹ÏResult(Right) := False;   -- Remove the element from the set
 87 55 ÏÏ§Â¹Ä¹¹Ïreturn Result;
 88 56 ÏÏ§ÏÏ©end "-";
 89 57 ÏÏ§
 90 58 ÏÏ§----------------------------------------------------------------------------
 91 59 ÏÏ§ÏÞßàfunction "<=" (Left : in Set_Type; Right : in Set_Type) return Boolean is
 92 60 ÏÏ§ÏÏ§ÏíÏIs_A_Subset : Boolean;
 93 61 ÏÏ§ÏÏ§begin
 94             -- left has less elements than right so if it shares all the same elements
 95             -- (all corresponding locations are set to true) it will switch every
 96             -- true in left to false and leave it all false, or the empty set.
 97             -- if it is, returns true.
 98 62 ÏÏ§ÏÏ¨¹¹ÏIs_A_Subset := (Left - Right) = Empty_Set;
 99 63 ÏÏ§Â¹Ä¹¹Ïreturn Is_A_Subset;
100 64 ÏÏ§ÏÏ©end "<=";
101 65 ÏÏ§
102 66 ÏÏ§----------------------------------------------------------------------------
103 67 ÏÏ§ÏÞßàfunction "<" (Left : in Set_Type; Right : in Set_Type) return Boolean is
104 68 ÏÏ§ÏÏ§ÏíÏResult : Boolean;
105 69 ÏÏ§ÏÏ§begin
106 70 ÏÏ§ÏÏ¨¹³´if Left = Right then  -- If the sets are equal, not a proper subset
107 71 ÏÏ§ÏÏ§Ï6¾¹¹ÏResult := False;
108 72 ÏÏ§ÏÏ§Ïö´else
109                -- basically, this is just the previous method, except it returns false
110                -- if the sets are equal
111 73 ÏÏ§ÏÏ§Ï¸¾¹¹ÏResult := Left <= Right;  -- If not equal, test for subset
112 74 ÏÏ§ÏÏ§ÏÈÏend if;                      -- using the function above
113 75 ÏÏ§Â¹Ä¹¹Ïreturn Result;
114 76 ÏÏ§ÏÏ©end "<";
115 77 ÏÏ§
116 78 ÏÏ§----------------------------------------------------------------------------
117 79 ÏÏ§ÏÞßàfunction ">=" (Left : in Set_Type; Right : in Set_Type) return Boolean is
118 80 ÏÏ§ÏÏ§begin
119             -- this is only calling the previous method, which i explained, in reverse
120             -- pretty self explanatory or i'm about to lose a point.
121 81 ÏÏ§Â¹Ä¹¹Ïreturn  Right <= Left;   -- Reverse the order of the 
122 82 ÏÏ§ÏÏ§                         -- parameters and use the operation 
123 83 ÏÏ§ÏÏ§                         -- we've already written
124 84 ÏÏ§ÏÏ©end ">=";
125 85 ÏÏ§
126 86 ÏÏ§----------------------------------------------------------------------------
127 87 ÏÏ§ÏÞßàfunction ">" (Left : in Set_Type; Right : in Set_Type) return Boolean is
128 88 ÏÏ§ÏÏ§begin
129             -- same as before, just takes switches left and right and calls the other
130             -- function. its basically cheating at programming.
131 89 ÏÏ§Â¹Ä¹¹Ïreturn  Right < Left;     -- Reverse the order of the 
132 90 ÏÏ§ÏÏ§                          -- parameters and use the operation 
133 91 ÏÏ§ÏÏ§                          -- we've already written
134 92 ÏÏ§ÏÏ©end ">";
135 93 ÏÏ§
136 94 ÏÏ©end Discrete_Set;
137 95 
138