|
| 1 | +=================== |
| 2 | +Extended Example |
| 3 | +=================== |
| 4 | + |
| 5 | +----------------------------- |
| 6 | +Implementing a Simple "Set" |
| 7 | +----------------------------- |
| 8 | + |
| 9 | +* We want to indicate which colors of the rainbow are in a **set** |
| 10 | + |
| 11 | + * If you remember from the *Basic Types* module, a type is made up of values and primitive operations |
| 12 | + |
| 13 | +* Our values will be |
| 14 | + |
| 15 | + * Type indicating colors of the rainbow |
| 16 | + * Type to group colors |
| 17 | + * Mechanism to indicate which color is in our set |
| 18 | + |
| 19 | +* Our primitive operations will be |
| 20 | + |
| 21 | + * Create a set |
| 22 | + * Add a color to the set |
| 23 | + * Remove a color from the set |
| 24 | + * Check if color is in set |
| 25 | + |
| 26 | +-------------------- |
| 27 | +Values for the Set |
| 28 | +-------------------- |
| 29 | + |
| 30 | +* Colors of the rainbow |
| 31 | + |
| 32 | + .. code:: Ada |
| 33 | +
|
| 34 | + type Color_T is (Red, Orange, Yellow, Green, |
| 35 | + Blue, Indigo, Violet, |
| 36 | + White, Black); |
| 37 | +
|
| 38 | +* Group of colors |
| 39 | + |
| 40 | + .. code:: Ada |
| 41 | +
|
| 42 | + type Group_Of_Colors_T is |
| 43 | + array (Positive range <>) of Color_T; |
| 44 | +
|
| 45 | +* Mechanism indicating which color is in the set |
| 46 | + |
| 47 | + .. code:: Ada |
| 48 | +
|
| 49 | + type Set_T is array (Color_T) of Boolean; |
| 50 | + -- if array component at Color is True, |
| 51 | + -- the color is in the set |
| 52 | +
|
| 53 | +---------------------------------- |
| 54 | +Primitive Operations for the Set |
| 55 | +---------------------------------- |
| 56 | + |
| 57 | +* Create a set |
| 58 | + |
| 59 | + .. code:: Ada |
| 60 | +
|
| 61 | + function Make (Colors : Group_Of_Colors_T) return Set_T; |
| 62 | +
|
| 63 | +* Add a color to the set |
| 64 | + |
| 65 | + .. code:: Ada |
| 66 | +
|
| 67 | + procedure Add (Set : in out Set_T; |
| 68 | + Color : Color_T); |
| 69 | +
|
| 70 | +* Remove a color from the set |
| 71 | + |
| 72 | + .. code:: Ada |
| 73 | +
|
| 74 | + procedure Remove (Set : in out Set_T; |
| 75 | + Color : Color_T); |
| 76 | +
|
| 77 | +* Check if color is in set |
| 78 | + |
| 79 | + .. code:: Ada |
| 80 | +
|
| 81 | + function Contains (Set : Set_T; |
| 82 | + Color : Color_T) |
| 83 | + return Boolean; |
| 84 | +
|
| 85 | +-------------------------------------------- |
| 86 | +Implementation of the Primitive Operations |
| 87 | +-------------------------------------------- |
| 88 | + |
| 89 | +* Implementation of the primitives is easy |
| 90 | + |
| 91 | + * We could do operations directly on :ada:`Set_T`, but that's not flexible |
| 92 | + |
| 93 | +.. code:: Ada |
| 94 | +
|
| 95 | + function Make (Colors : Group_Of_Colors_T) return Set_T is |
| 96 | + Set : Set_T := (others => False); |
| 97 | + begin |
| 98 | + for Color of Colors loop |
| 99 | + Set (Color) := True; |
| 100 | + end loop; |
| 101 | + return Set; |
| 102 | + end Make; |
| 103 | +
|
| 104 | + procedure Add (Set : in out Set_T; |
| 105 | + Color : Color_T) is |
| 106 | + begin |
| 107 | + Set (Color) := True; |
| 108 | + end Add; |
| 109 | +
|
| 110 | + procedure Remove (Set : in out Set_T; |
| 111 | + Color : Color_T) is |
| 112 | + begin |
| 113 | + Set (Color) := False; |
| 114 | + end Remove; |
| 115 | +
|
| 116 | + function Contains (Set : Set_T; |
| 117 | + Color : Color_T) |
| 118 | + return Boolean is |
| 119 | + (Set (Color)); |
| 120 | +
|
| 121 | +------------------------- |
| 122 | +Using our Set Construct |
| 123 | +------------------------- |
| 124 | + |
| 125 | +.. code:: Ada |
| 126 | +
|
| 127 | + Rgb : Set_T := Make ((Red, Green, Blue)); |
| 128 | + Light : Set_T := Make ((Red, Yellow, Green)); |
| 129 | +
|
| 130 | +.. code:: Ada |
| 131 | +
|
| 132 | + if Contains (Rgb, Black) then |
| 133 | + Remove (Rgb, Black); |
| 134 | + else |
| 135 | + Add (Rgb, Black); |
| 136 | + end if; |
| 137 | +
|
| 138 | +*In addition, because of the operations available to arrays of Boolean, we can easily implement set operations* |
| 139 | + |
| 140 | +.. code:: Ada |
| 141 | +
|
| 142 | + Union : Set_T := Rgb or Light; |
| 143 | + Intersection : Set_T := Rgb and Light; |
| 144 | + Difference : Set_T := Rgb xor Light; |
0 commit comments