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

  1.  
  2. -------- SIMTEL20 Ada Software Repository Prologue ------------
  3. --                                                           -*
  4. -- Unit name    : generic package LIMITED_PRIORITIZED_QUEUE
  5. -- Version      : 1.0
  6. -- Author       : John A. Anderson
  7. --              : TEXAS INSTRUMENTS MS 8006
  8. --              : P.O. BOX 801
  9. --              : MCKINNEY, TEXAS   75069
  10. -- DDN Address  : ANDERSON%TI-EG@CSNET-RELAY
  11. -- Copyright    : (c) 1984 John A. Anderson
  12. -- Date created :  OCTOBER  2, 1984
  13. -- Release date :  NOVEMBER 27, 1984
  14. -- Last update  :  ANDERSON Wed Nov 27, 1984
  15. --                                                           -*
  16. ---------------------------------------------------------------
  17. --                                                           -*
  18. -- Keywords     :  QUEUE
  19. ----------------:  PRIORITIZED QUEUE
  20. --
  21. -- Abstract     :  This generic package creates a Prioritized
  22. ----------------:  Queue of a User-defined Limited number of
  23. ----------------:  objects.  The Queue is First-In, First-Out
  24. ----------------:  except where overridden by the priority.
  25. ----------------:  The priority may be any discrete type.
  26. ----------------:  It is assumed that the priorities are from
  27. ----------------:  lowest to highest.  The type of data structure
  28. ----------------:  to be instantiated for the queue may be any
  29. ----------------:  type having assignment and equality.  Other
  30. ----------------:  types may be enqueued by using access types.
  31. ----------------:  (i.e. Access variable pointing to a task.)
  32. --                                                           -*
  33. ------------------ Revision history ---------------------------
  34. --                                                           -*
  35. -- DATE         VERSION    AUTHOR                  HISTORY
  36. -- 11/27/84      1.0    Anderson        Initial Release
  37. --                                                           -*
  38. ------------------ Distribution and Copyright -----------------
  39. --                                                           -*
  40. -- This prologue must be included in all copies of this software.
  41. --
  42. -- This software is copyright by the author.
  43. --
  44. -- This software is released to the Ada community.
  45. -- This software is released to the Public Domain (note:
  46. --   software released to the Public Domain is not subject
  47. --   to copyright protection).
  48. -- Restrictions on use or distribution:  NONE
  49. --                                                           -*
  50. ------------------ Disclaimer ---------------------------------
  51. --                                                           -*
  52. -- This software and its documentation are provided "AS IS" and
  53. -- without any expressed or implied warranties whatsoever.
  54. -- No warranties as to performance, merchantability, or fitness
  55. -- for a particular purpose exist.
  56. --
  57. -- Because of the diversity of conditions and hardware under
  58. -- which this software may be used, no warranty of fitness for
  59. -- a particular purpose is offered.  The user is advised to
  60. -- test the software thoroughly before relying on it.  The user
  61. -- must assume the entire risk and liability of using this
  62. -- software.
  63. --
  64. -- In no event shall any person or organization of people be
  65. -- held responsible for any direct, indirect, consequential
  66. -- or inconsequential damages or lost profits.
  67. --                                                           -*
  68. -------------------END-PROLOGUE--------------------------------
  69.  
  70. generic
  71.     SIZE : INTEGER;
  72.  
  73.     type ELEMENT_TYPE is private;
  74.  
  75.     type PRIORITY_TYPE is (<>);
  76.  
  77. package LIMITED_PRIORITIZED_QUEUE is
  78.  
  79.     procedure ADD (ELEMENT  : ELEMENT_TYPE;
  80.                    PRIORITY : PRIORITY_TYPE := PRIORITY_TYPE'FIRST);
  81.  
  82.     procedure REMOVE (ELEMENT : out ELEMENT_TYPE);
  83.  
  84.     function IS_EMPTY return BOOLEAN;
  85.  
  86.     function IS_FULL return BOOLEAN;
  87.  
  88.     UNDERFLOW : exception;
  89.  
  90.     OVERFLOW  : exception;
  91.  
  92. end LIMITED_PRIORITIZED_QUEUE;
  93.  
  94. package body LIMITED_PRIORITIZED_QUEUE is
  95.  
  96.     type NODE;
  97.  
  98.     type LINK is access NODE;
  99.  
  100.     type NODE is
  101.         record
  102.             VALUE : ELEMENT_TYPE;
  103.             NEXT  : LINK;
  104.         end record;
  105.  
  106.     type PRIORITY_ARRAY_TYPE is array (PRIORITY_TYPE
  107.                                          range PRIORITY_TYPE'FIRST ..
  108.                                                PRIORITY_TYPE'LAST) of LINK;
  109.  
  110.     LIST_HEADS   : PRIORITY_ARRAY_TYPE;
  111.  
  112.     LIST_TAILS   : PRIORITY_ARRAY_TYPE;
  113.  
  114.     POOL_HEAD    : LINK;
  115.  
  116.     POOL_ELEMENT : LINK;
  117.  
  118.     function IS_EMPTY return BOOLEAN is
  119.         EMPTY_HEADS : PRIORITY_ARRAY_TYPE;
  120.     begin
  121.  
  122. -- EMPTY_HEADS was initialized to all null
  123.         return (LIST_HEADS = EMPTY_HEADS);
  124.  
  125.     end IS_EMPTY;
  126.  
  127.  
  128.     function IS_FULL return BOOLEAN is
  129.     begin
  130.  
  131. -- if the POOL_HEAD is null all
  132. --  available resources are in queue
  133.         return POOL_HEAD = null;
  134.  
  135.     end IS_FULL;
  136.  
  137.     procedure ADD (ELEMENT  : ELEMENT_TYPE;
  138.                    PRIORITY : PRIORITY_TYPE := PRIORITY_TYPE'FIRST) is
  139.  
  140.         POINTER : LINK;
  141.  
  142.     begin
  143.         if IS_FULL then
  144.             raise OVERFLOW;
  145.         end if;
  146.  
  147. ---------
  148. -- obtain record from pool
  149. ---------
  150.         POINTER := POOL_HEAD;
  151.         -- set POINTER to next available cell
  152.         POOL_HEAD := POINTER.NEXT;
  153.         -- reset POOL_HEAD to next available cell
  154.  
  155. ---------
  156. -- assign values to record
  157. ---------
  158.         POINTER.VALUE := ELEMENT;
  159.         POINTER.NEXT := null;
  160.  
  161. ---------
  162. -- link to proper priority list of queue
  163. ---------
  164.         if LIST_TAILS (PRIORITY) /= null then
  165.             LIST_TAILS (PRIORITY).NEXT := POINTER;
  166.             -- link onto tail of queue
  167.         else
  168.             -- this priority has nothing in it, so
  169.             LIST_HEADS (PRIORITY) := POINTER;
  170.             --   link it to the front
  171.         end if;
  172.         LIST_TAILS (PRIORITY) := POINTER;
  173.         -- set this item to be last in queue
  174.     end ADD;
  175.  
  176.     procedure REMOVE (ELEMENT : out ELEMENT_TYPE) is
  177.         POINTER      : LINK;
  178.         TEMP_ELEMENT : ELEMENT_TYPE;
  179.         PRIORITY     : PRIORITY_TYPE;
  180.     begin
  181.  
  182.         if IS_EMPTY then
  183.             raise UNDERFLOW;
  184.         end if;
  185.  
  186. ---------
  187. -- find highest priority with element to be removed
  188. ---------
  189.         PRIORITY := PRIORITY_TYPE'LAST;
  190.         while LIST_HEADS (PRIORITY) = null loop
  191.             PRIORITY := PRIORITY_TYPE'PRED (PRIORITY);
  192.         end loop;
  193.  
  194. ---------
  195. -- load ELEMENT with value
  196. ---------
  197.         ELEMENT := LIST_HEADS (PRIORITY).VALUE;
  198.  
  199. ---------
  200. -- remove ELEMENT from queue
  201. ---------
  202.         POINTER := LIST_HEADS (PRIORITY);
  203.         -- set POINTER to cell to be released
  204.         LIST_HEADS (PRIORITY) := LIST_HEADS (PRIORITY).NEXT;
  205.         -- reset queue
  206.         if LIST_HEADS (PRIORITY) = null then
  207.             LIST_TAILS (PRIORITY) := null;
  208.         end if;
  209. ---------
  210. -- return cell to resource pool
  211. ---------
  212.         POINTER.NEXT := POOL_HEAD; -- link POINTER to Pool
  213.         POOL_HEAD := POINTER; -- reset POOL_HEAD
  214.  
  215.     end REMOVE;
  216.  
  217. begin
  218.     for COUNT in 1 .. SIZE loop
  219.         POOL_ELEMENT := new NODE;      -- allocate memory
  220.         POOL_ELEMENT.NEXT := POOL_HEAD; -- link to old head
  221.         POOL_HEAD := POOL_ELEMENT;     -- make this new
  222.                                        -- header
  223.     end loop;
  224. end LIMITED_PRIORITIZED_QUEUE;
  225.  
  226.