home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / ptrvec.c < prev    next >
C/C++ Source or Header  |  1999-01-03  |  4KB  |  147 lines

  1. /* -*-C-*-
  2.  
  3. $Id: ptrvec.c,v 1.3 1999/01/03 05:33:56 cph Exp $
  4.  
  5. Copyright (C) 1990-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "outf.h"
  23. #include "dstack.h"
  24.  
  25. static PTR
  26. DEFUN (xmalloc, (length), unsigned int length)
  27. {
  28.   extern PTR EXFUN (malloc, (unsigned int length));
  29.   PTR result = (malloc (length));
  30.   if (result == 0)
  31.     {
  32.       outf_fatal ("malloc: memory allocation failed\n");
  33.       outf_flush_fatal ();
  34.       abort ();
  35.     }
  36.   return (result);
  37. }
  38.  
  39. static PTR
  40. DEFUN (xrealloc, (ptr, length), PTR ptr AND unsigned int length)
  41. {
  42.   extern PTR EXFUN (realloc, (PTR ptr, unsigned int length));
  43.   PTR result = (realloc (ptr, length));
  44.   if (result == 0)
  45.     {
  46.       outf_fatal ("realloc: memory allocation failed\n");
  47.       outf_flush_fatal ();
  48.       abort ();
  49.     }
  50.   return (result);
  51. }
  52.  
  53. Tptrvec
  54. DEFUN (ptrvec_allocate, (length), Tptrvec_length length)
  55. {
  56.   Tptrvec ptrvec = (xmalloc (sizeof (struct struct_ptrvec)));
  57.   (ptrvec -> length) = length;
  58.   (ptrvec -> elements) =
  59.     ((length > 0) ? (xmalloc (length * (sizeof (PTR)))) : 0);
  60.   return (ptrvec);
  61. }
  62.  
  63. void
  64. DEFUN (ptrvec_deallocate, (ptrvec), Tptrvec ptrvec)
  65. {
  66.   if ((ptrvec -> length) > 0)
  67.     free (ptrvec -> elements);
  68.   free (ptrvec);
  69. }
  70.  
  71. void
  72. DEFUN (ptrvec_set_length, (ptrvec, length),
  73.        Tptrvec ptrvec AND
  74.        Tptrvec_length length)
  75. {
  76.   (ptrvec -> length) = length;
  77.   (ptrvec -> elements) =
  78.     ((length > 0)
  79.      ? (xrealloc ((ptrvec -> elements), (length * (sizeof (PTR)))))
  80.      : 0);
  81. }
  82.  
  83. Tptrvec
  84. DEFUN (ptrvec_copy, (ptrvec), Tptrvec ptrvec)
  85. {
  86.   Tptrvec_length length = (PTRVEC_LENGTH (ptrvec));
  87.   Tptrvec result = (ptrvec_allocate (length));
  88.   PTR * scan_source = (PTRVEC_START (ptrvec));
  89.   PTR * end_source = (scan_source + length);
  90.   PTR * scan_result = (PTRVEC_START (result));
  91.   while (scan_source < end_source)
  92.     (*scan_result++) = (*scan_source++);
  93.   return (result);
  94. }
  95.  
  96. void
  97. DEFUN (ptrvec_adjoin, (ptrvec, element), Tptrvec ptrvec AND PTR element)
  98. {
  99.   Tptrvec_length length = (PTRVEC_LENGTH (ptrvec));
  100.   ptrvec_set_length (ptrvec, (length + 1));
  101.   (PTRVEC_REF (ptrvec, length)) = element;
  102. }
  103.  
  104. int
  105. DEFUN (ptrvec_memq, (ptrvec, element), Tptrvec ptrvec AND PTR element)
  106. {
  107.   PTR * scan = (PTRVEC_START (ptrvec));
  108.   PTR * end = (scan + (PTRVEC_LENGTH (ptrvec)));
  109.   while (scan < end)
  110.     if (element == (*scan++))
  111.       return (1);
  112.   return (0);
  113. }
  114.  
  115. void
  116. DEFUN (ptrvec_move_left,
  117.        (source, source_start, source_end, target, target_start),
  118.        Tptrvec source AND
  119.        Tptrvec_index source_start AND
  120.        Tptrvec_index source_end AND
  121.        Tptrvec target AND
  122.        Tptrvec_index target_start)
  123. {
  124.   PTR * scan_source = (PTRVEC_LOC (source, source_start));
  125.   PTR * end_source = (PTRVEC_LOC (source, source_end));
  126.   PTR * scan_target = (PTRVEC_LOC (target, target_start));
  127.   while (scan_source < end_source)
  128.     (*scan_target++) = (*scan_source++);
  129. }
  130.  
  131. void
  132. DEFUN (ptrvec_move_right,
  133.        (source, source_start, source_end, target, target_start),
  134.        Tptrvec source AND
  135.        Tptrvec_index source_start AND
  136.        Tptrvec_index source_end AND
  137.        Tptrvec target AND
  138.        Tptrvec_index target_start)
  139. {
  140.   PTR * end_source = (PTRVEC_LOC (source, source_start));
  141.   PTR * scan_source = (PTRVEC_LOC (source, source_end));
  142.   PTR * scan_target =
  143.     (PTRVEC_LOC (target, (target_start + (source_end - source_start))));
  144.   while (scan_source > end_source)
  145.     (*--scan_target) = (*--scan_source);
  146. }
  147.