home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / math / gset.ada < prev    next >
Encoding:
Text File  |  1988-05-03  |  7.0 KB  |  192 lines

  1.  
  2. -------- SIMTEL20 Ada Software Repository Prologue ------------
  3. --                                                           -*
  4. -- Unit name    : SET_PACKAGE
  5. -- Version      : 1.0
  6. -- Author       : Mike Linnig
  7. --              : Texas Instruments Ada Technology Branch
  8. --              : PO Box 801, MS 8007
  9. --              : McKinney, TX  75069
  10. -- DDN Address  : linnig%ti-eg at csnet-relay
  11. -- Copyright    : (c) 
  12. -- Date created :  27 June 85
  13. -- Release date :  27 June 85
  14. -- Last update  :  27 June 85
  15. -- Machine/System Compiled/Run on : DG MV 10000 with ROLM ADE
  16. --                    DEC VAX 11/780 with DEC Ada
  17. --                                                           -*
  18. ---------------------------------------------------------------
  19. --                                                           -*
  20. -- Keywords     :  SET, SET MANIPULATION
  21. ----------------:
  22. --
  23. -- Abstract     :  Set_Package contains a series of generic
  24. ----------------:  routines which can be instantiated to create
  25. -- routines which provide a series of set manipulation functions
  26. -- for sets of enumeration or numeric objects.  The functions in
  27. -- Set_Package include:
  28. --    set intersection
  29. --    set union
  30. --    set membership
  31. --    set element count
  32. -- and others
  33. --
  34. -- The code in this package was extracted from Chapter 15, Section 3
  35. -- (15.3) of Grady Booch's Software Engineering with Ada book.
  36. -- See 15.3 for further documentation on the functions.
  37. --
  38. --                                                           -*
  39. ------------------ Revision history ---------------------------
  40. --                                                           -*
  41. -- DATE         VERSION    AUTHOR                  HISTORY
  42. -- 19850627    1.0    Mike Linnig        Initial Release
  43. --                                                           -*
  44. ------------------ Distribution and Copyright -----------------
  45. --                                                           -*
  46. -- This prologue must be included in all copies of this software.
  47. --
  48. -- This software is released to the Ada community.
  49. -- This software is released to the Public Domain (note:
  50. --   software released to the Public Domain is not subject
  51. --   to copyright protection).
  52. -- Restrictions on use or distribution:  NONE
  53. --                                                           -*
  54. ------------------ Disclaimer ---------------------------------
  55. --                                                           -*
  56. -- This software and its documentation are provided "AS IS" and
  57. -- without any expressed or implied warranties whatsoever.
  58. -- No warranties as to performance, merchantability, or fitness
  59. -- for a particular purpose exist.
  60. --
  61. -- Because of the diversity of conditions and hardware under
  62. -- which this software may be used, no warranty of fitness for
  63. -- a particular purpose is offered.  The user is advised to
  64. -- test the software thoroughly before relying on it.  The user
  65. -- must assume the entire risk and liability of using this
  66. -- software.
  67. --
  68. -- In no event shall any person or organization of people be
  69. -- held responsible for any direct, indirect, consequential
  70. -- or inconsequential damages or lost profits.
  71. --                                                           -*
  72. -------------------END-PROLOGUE--------------------------------
  73.  
  74. GENERIC
  75.   TYPE Universe IS (<>);
  76. PACKAGE Set_Package IS
  77.  
  78.   TYPE Set IS PRIVATE;
  79.   NULL_SET: constant SET;
  80.  
  81.   FUNCTION "*"  (Set_1 : Set; Set_2 : Set) RETURN Set;
  82.   FUNCTION "+"  (Element : Universe; Set_1 : Set) RETURN Set;
  83.   FUNCTION "+"  (Set_1 : Set; Set_2 : Set) RETURN Set;
  84.   FUNCTION "+"  (Set_1 : Set; Element : Universe) RETURN Set;
  85.   FUNCTION "-"  (Set_1 : Set; Set_2 : Set) RETURN Set;
  86.   FUNCTION "-"  (Set_1 : Set; Element : Universe) RETURN Set;
  87.   FUNCTION "<"  (Set_1 : Set; Set_2 : Set) RETURN Boolean;
  88.   FUNCTION "<=" (Set_1 : Set; Set_2 : Set) RETURN Boolean;
  89.  
  90.   FUNCTION Is_A_Member (Element : Universe; Of_Set : Set) RETURN Boolean;
  91.   FUNCTION Is_Empty    (Set_1 : Set) RETURN Boolean;
  92.  
  93.   SUBTYPE Number IS Integer
  94.                       RANGE 0 .. (Universe'Pos (Universe'Last) -
  95.                                   Universe'Pos (Universe'First) + 1);
  96.  
  97.   FUNCTION Number_In (Set_1 : Set) RETURN Number;
  98.  
  99. PRIVATE
  100.   TYPE Set IS ARRAY (Universe) OF Boolean;
  101.  
  102.   Null_Set : CONSTANT Set := Set'(OTHERS => False);
  103.  
  104. END Set_Package;
  105.  
  106. --=====================================================================
  107. PRAGMA PAGE;
  108.  
  109. Package body set_package is
  110.  
  111.   FUNCTION "*"  (Set_1 : Set; Set_2 : Set) RETURN Set is
  112.         -- intersection
  113.         begin
  114.                 return(set_1 and set_2);
  115.         end"*";
  116. -------------------------------------------------------------------------
  117.  
  118.  
  119.   FUNCTION "+"  (Element : Universe; Set_1 : Set) RETURN Set is
  120.         value_set: set := set_1;
  121.   BEGIN
  122.         VALUE_SET(ELEMENT) := TRUE;
  123.         RETURN VALUE_SET;
  124.   END "+";
  125. -------------------------------------------------------------------------
  126.   FUNCTION "+"  (Set_1 : Set; Set_2 : Set) RETURN Set is
  127.   BEGIN
  128.         RETURN (SET_1 OR SET_2);
  129.   END "+";
  130.  
  131. -------------------------------------------------------------------------
  132.   FUNCTION "+"  (Set_1 : Set; Element : Universe) RETURN Set is
  133.         VALUE_SET: SET:= SET_1;
  134.  
  135.   BEGIN
  136.         VALUE_SET(ELEMENT) := TRUE;
  137.         RETURN VALUE_SET;
  138.   END "+";
  139.  
  140. -------------------------------------------------------------------------
  141.   FUNCTION "-"  (Set_1 : Set; Set_2 : Set) RETURN Set is
  142.   BEGIN
  143.         RETURN (SET_1 AND (NOT SET_2));
  144.   END "-";
  145. -------------------------------------------------------------------------
  146.   FUNCTION "-"  (Set_1 : Set; Element : Universe) RETURN Set is
  147.         VALUE_SET: SET:= SET_1;
  148.  
  149.   BEGIN
  150.         VALUE_SET(ELEMENT) := FALSE;
  151.         RETURN VALUE_SET;
  152.   END "-";
  153. -------------------------------------------------------------------------
  154.   FUNCTION "<=" (Set_1 : Set; Set_2 : Set) RETURN Boolean is
  155.         VALUE_SET:SET:= (set_1 and set_2);
  156.   BEGIN
  157.         RETURN (value_set = set_1);
  158.   END "<=";
  159. -------------------------------------------------------------------------
  160.   FUNCTION "<" (Set_1 : Set; Set_2 : Set) RETURN Boolean is
  161.         VALUE_SET:SET:= (set_1 and set_2);
  162.   BEGIN
  163.         RETURN ((value_set = set_1) and (value_set/= set_2));
  164.   END "<";
  165. -------------------------------------------------------------------------
  166.   FUNCTION Is_A_Member (Element : Universe; Of_Set : Set) RETURN Boolean
  167.  is
  168.   BEGIN
  169.         return of_set(element);
  170.   end is_a_member;
  171. -------------------------------------------------------------------------
  172.   FUNCTION Is_Empty    (Set_1 : Set) RETURN Boolean is
  173.  
  174.   begin
  175.         return (set_1 = null_set);
  176.   end is_empty;
  177. -------------------------------------------------------------------------
  178.   FUNCTION Number_In (Set_1 : Set) RETURN Number is
  179.  
  180.         count: integer:= 0;
  181.  
  182.   begin
  183.         for index in universe
  184.           loop
  185.             if set_1(index) then
  186.               count:= count +1;
  187.             end if;
  188.           end loop;
  189.         return count;
  190.   end number_in;
  191. END SET_PACKAGE;
  192.