home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Aktief 1995 #6 / CDA_6.iso / shell / utils / disked29.arj / SOURCE.ZIP / ARRAYS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-08  |  4.7 KB  |  252 lines

  1. /***
  2. *arrays.c - array handling
  3. *
  4. *for Public Domain, 1995, Gregg Jennings.  All wrongs reserved.
  5. *   P O Box 200, Falmouth, MA 02541-0200
  6. *
  7. *Purpose:
  8. *   General array manipulation routines.
  9. *
  10. *Notice:
  11. *   This source file may be freely used and distributed without restriction.
  12. *******************************************************************************/
  13.  
  14. /*
  15.    Versions
  16.  
  17.    0.1   14-Mar-1995    added arraysub()
  18.    0.0   15-Dec-1994    These routines were written to replace some
  19.                         specific ones that in FILES.C.  They have
  20.                         are very general purpose though.
  21.  
  22.    Note: The use of _huge arrays are for  DISKED's clusters[] array.
  23.          I have not used these for other arrays yet but the huge-ness
  24.          should not be a cause of any problems nor any performance
  25.          decrease.
  26. */
  27.  
  28. #include <stdlib.h>           /* for itoa() */
  29.  
  30. #include "console.h"          /* for output() (you can use putch()) */
  31. #include "limits.h"           /* my additions to <limits.h> */
  32.  
  33.  
  34. /* NO globals referenced here */
  35.  
  36. /* NO globals defined here */
  37.  
  38.  
  39. /***
  40. *arraymax   -  get max number of array
  41. *
  42. ****/
  43.  
  44. extern int arraymax(int _huge *array, size_t max, int n)
  45. {
  46. size_t i;
  47.  
  48.    for (i = 0; i < max; i++)
  49.       if (array[i] > n)
  50.          n = array[i];
  51.    return n;
  52. }
  53.  
  54. /***
  55. *arraymin   -  get min number of array
  56. *
  57. ****/
  58.  
  59. extern int arraymin(int _huge *array, size_t max, int n)
  60. {
  61. size_t i;
  62.  
  63.    for (i = 0; i < max; i++)
  64.       if (array[i] < n)
  65.          n = array[i];
  66.    return n;
  67. }
  68.  
  69. /***
  70. *arraycnt   -  count occurences
  71. *
  72. ****/
  73.  
  74. extern int arraycnt(int _huge *array, size_t max, int n)
  75. {
  76. size_t i;
  77. int c;
  78.  
  79.    for (c = i = 0; i < max; i++)
  80.       if (array[i] == n)
  81.          ++c;
  82.    return c;
  83. }
  84.  
  85. /***
  86. *arrayfirst -  return offset of first occurance of n
  87. *              max if not found
  88. ****/
  89.  
  90. extern size_t arrayfirst(int _huge *array, size_t max, int n)
  91. {
  92. size_t i;
  93.  
  94.    for (i = 0; i < max; i++)
  95.       if (array[i] == n)
  96.          break;
  97.    return i;
  98. }
  99.  
  100. /***
  101. *arraylast  -  return offset of last occurance of n
  102. *              0 if not found
  103. ****/
  104.  
  105. extern size_t arraylast(int _huge *array, size_t max, int n)
  106. {
  107. size_t i;
  108.  
  109.    for (i = max-1; i > 0; i--)
  110.       if (array[i] == n)
  111.          break;
  112.    return i;
  113. }
  114.  
  115. /***
  116. *arrayfind  -  return offset of occurance of n
  117. *              max if not found
  118. ****/
  119.  
  120. extern size_t arrayfind(int _huge *array, size_t max, int n)
  121. {
  122. size_t i;
  123.  
  124.    for (i = 0; i < max; i++)
  125.       if (array[i] == n)
  126.          break;
  127.    return i;
  128. }
  129.  
  130. /***
  131. *arraytrav  -  find next/prev occurance of number at offset n
  132. *
  133. ****/
  134.  
  135. extern size_t arraytrav(int _huge *array, size_t max, size_t n, int dir)
  136. {
  137. int num;
  138.  
  139.    num = array[n];
  140.  
  141.    for (n += dir; ; n += dir)
  142.    {
  143.       if (dir == -1 && n == 0xFFFF)
  144.          n = max-1;
  145.       if (dir == 1 && n > max)
  146.          n = 0;
  147.       if (array[n] == num)
  148.          return n;
  149.    }
  150. }
  151.  
  152. /***
  153. *arraysub   -  retreive array offsets of number in array
  154. *
  155. ****/
  156.  
  157. extern size_t arraysub(int _huge *array, int *sub, size_t max, int n)
  158. {
  159. size_t i,j;
  160.  
  161.    for (i = j = 0; i < max; i++)
  162.    {
  163.       if (array[i] == n)
  164.          sub[j++] = i;
  165.    }
  166.    return j;
  167. }
  168.  
  169. /***
  170. *arraymap -
  171. *
  172. *     2 3 4 5 6 7 8 9 10 11 12 13 14 15
  173. *     2 2 2 2 2 0 0 3 3  0  0  0  0  4
  174. *
  175. *     7 8 11-14 ...
  176. *
  177. ****/
  178.  
  179. extern int arraymap(int _huge *array, size_t max, int n, int base)
  180. {
  181. size_t i;
  182. int c = 0;
  183. int count = 0;
  184. int indash = 0;
  185. char buf[INT_MAX_DIG+1];
  186.  
  187.    for (i = 2; i < max; i++)
  188.    {
  189.       if (array[i] != n)
  190.       {
  191.          c = 1;
  192.          if (count == 2)
  193.             output(' ');
  194.          if (count > 1)
  195.          {
  196.             print(itoa(i-1,buf,base));
  197.             output(' ');
  198.          }
  199.          if (count == 1)
  200.             output(' ');
  201.          count = indash = 0;
  202.       }
  203.       else
  204.       {
  205.          if (count == 1)
  206.             count++;
  207.          else if (count == 0)
  208.          {
  209.             print(itoa(i,buf,base));
  210.             count++;
  211.          }
  212.          else if (!indash)
  213.          {
  214.             output('-');
  215.             indash = 1;
  216.             count++;
  217.          }
  218.       }
  219.    }
  220.    return c;
  221. }
  222.  
  223. /* special for listing DISKED's tagged[] array */
  224.  
  225. extern void plist(long *array, size_t max, int base)
  226. {
  227. size_t i;
  228. char buf[LONG_MAX_DIG+1];
  229.  
  230.    for (i = 1; i < max; i++)
  231.       print("%d:%s ",i,ltoa(array[i],buf,base));
  232. }
  233.  
  234. #if 0
  235.  
  236. #include <stdio.h>
  237. #include <stdlib.h>
  238.  
  239. #define output putchar
  240. #define print printf
  241.  
  242. extern void arraymap(int *array, int max, int n, int base);
  243.  
  244. void main(void)
  245. {
  246. int array[] = { 0, 0, 1, 1, 0, 0, 2, 2, 2, 2, 0, 2, 0, -1 };
  247.  
  248.    arraymap(array,sizeof(array)/sizeof(int),2,10);
  249. }
  250.  
  251. #endif
  252.