home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / modula2 / library / queuem2 / deq3.txt < prev    next >
Text File  |  1989-12-15  |  4KB  |  117 lines

  1. (*====================================================================*)
  2. (*                  Peter M. Perchansky                               *)
  3. (*              412-1 Springside, Drive East                          *)
  4. (*                 Shillington, PA  19607                             *)
  5. (*                                                                    *)
  6. (*                  CompuServe 71301,344                              *)
  7. (*                  FidoNet    1:273/101                              *)
  8. (*                  Interlink                                         *)
  9. (*====================================================================*)
  10.  
  11. Generic Deque Notes:
  12.  
  13. Don't let the ARRAY OF BYTE, ADDRESS, or ARRAY OF WORD scare you when
  14. you look at generic procedures or modules.  Calling a generic procedure
  15. can be very simple.
  16.  
  17. Here's an example:
  18.  
  19. ----
  20. MODULE Person;
  21.  
  22. FROM PMPDeque IMPORT Deques (* type *), Enqueue, Dequeue, InQueue;
  23. FROM PMPDeque IMPORT CreateDeque, DestroyDeque;
  24.  
  25. TYPE
  26.      PersonRec = RECORD
  27.                    lastName      : ARRAY [0..24] OF CHAR;
  28.                    firstName     : ARRAY [0..14] OF CHAR;
  29.                    middleInitial : CHAR;
  30.                    age           : CARDINAL;
  31.                  END;
  32.  
  33. VAR
  34.      person : PersonRec;
  35.      queue  : Deques;
  36.      ok     : BOOLEAN;  (* used to check for queue errors *)
  37.  
  38. BEGIN
  39.      CreateDeque (queue); (* make queue NIL *)
  40.  
  41.      WITH person DO
  42.           lastName := "Fieldings";
  43.           firstName := "Robert;
  44.           middleInitial := "M";
  45.           age := 26;
  46.      END;
  47.  
  48.      Enqueue (person, queue, ok); (* place Robert in queue *)
  49.  
  50.      IF NOT ok THEN
  51.         (* handle queue overflow --- not enough memory --- error *)
  52.      END;
  53.  
  54.      WITH person DO
  55.           lastName := "Yoritomo";
  56.           firstName := "Shakari;
  57.           middleInitial := " ";
  58.           age := 37;
  59.      END;
  60.  
  61.      Enqueue (person, queue, ok); (* place Shakari in queue *)
  62.  
  63.      IF NOT ok THEN
  64.         (* handle queue overflow --- not enough memory --- error *)
  65.      END;
  66.  
  67.      WITH person DO
  68.           lastName := "Fieldings";
  69.           firstName := "Robert;
  70.           middleInitial := "M";
  71.           age := 26;
  72.      END;
  73.  
  74.      IF InQueue (person, queue) THEN
  75.         (* Robert is in queue *)
  76.      ELSE
  77.         (* Robert is not in queue *)
  78.      END;
  79.  
  80.      Dequeue (person, queue, ok); (* serve person in front of queue *)
  81.                                   (* Robert in this case            *)
  82.  
  83.      IF NOT ok THEN
  84.         (* handle queue underflow --- queue is empty ---  *)
  85.         (* possibility also exists that the type being    *)
  86.         (* retrieved is not the same type that was stored *)
  87.      END;
  88.  
  89.      ....
  90.      (* and so on *)
  91.      ...
  92.      DestroyDeque (queue);    (* remove queue from memory *)
  93. END Person;
  94.  
  95. The same module, or another, could have created a variable called stack
  96. of type Deques and used stack operations (push, pop, top, etc.).
  97.  
  98. As you can tell, you don't have to worry about ARRAYs OF BYTE, or
  99. ADDRESSes, or ARRAYs OF WORDs.  That's why using and writing generic
  100. procedures can be very helpful.  The main area to be careful when
  101. writing or using generic procedures or modules is you, the programmer,
  102. are responsible for keeping track of types you are storing or
  103. manipulating.
  104.  
  105. For instance, you can have used Enqueue (or Push) to place variables of
  106. type INTEGER (or whatever) in the same queue you placed variables of
  107. type PersonRec.  That's where the danger lies --- keeping careful track
  108. of what types you store.
  109.  
  110. Although you can mix types in a generic dequeue, it's highly advisable
  111. you set up a separate stack, queue, deque, etc. for each data type you
  112. plan on manipulating or storing.
  113.  
  114. Please feel free to contact me via postal mail, CompuServe, FidoNet,
  115. or Interlink (language or Pascal Conference <they are working on
  116. adding a Modula-2 Conference>) if you have any questions or comments.