home *** CD-ROM | disk | FTP | other *** search
/ Mega CD-ROM 1 / megacd_rom_1.zip / megacd_rom_1 / POSTSCRI / PSFIX.ZIP / VECTDEF.C < prev    next >
C/C++ Source or Header  |  1991-05-01  |  6KB  |  356 lines

  1. /*
  2.  *
  3.  *  vector allocation and deallocation
  4.  *
  5.  */
  6.  
  7. /*
  8.  * This software may be freely distributed under the following conditions:
  9.  *         1. It is distributed in its current form, without alterations.
  10.  *         2. No monetary profit is made, under any circumstances.
  11.  *
  12.  *             Marc A. Murison
  13.  *             Smithsonian Astrophysical Observatory
  14.  *             60 Garden Street, MS 63
  15.  *             Cambridge, MA  02138
  16.  *
  17.  *             (617) 495-7079      murison@cfacx2.harvard.edu
  18.  */
  19.  
  20.  
  21. #define        global        extern
  22. #define        VECTDEF_C
  23. #include    "vectdef.h"
  24.  
  25.  
  26. /*
  27.  *
  28.  * vector allocation
  29.  *
  30.  */
  31.  
  32.  
  33. /*
  34.  * allocate a float vector with index range n1 <= i <= n2 
  35.  */
  36. float    *fvector( int n1, int n2 )
  37. {
  38.     float    *v;
  39.  
  40.     v = (float *) malloc( (n2-n1+1)*sizeof(float) );
  41.     if( v == NULL )
  42.         verrout( ON, "\nfloat vector allocation failure!\n" );
  43.     return v-n1;
  44. }
  45.  
  46.  
  47. /*
  48.  * allocate a double vector with index range n1 <= i <= n2 
  49.  */
  50. double    *dvector( int n1, int n2 )
  51. {
  52.     double    *v;
  53.  
  54.     v = (double *) malloc( (n2-n1+1)*sizeof(double) );
  55.     if( v == NULL )
  56.         verrout( ON, "\ndouble vector allocation failure!\n" );
  57.     return v-n1;
  58. }
  59.  
  60.  
  61. /*
  62.  * allocate a character vector with index range n1 <= i <= n2 
  63.  */
  64. char    *cvector( int n1, int n2 )
  65. {
  66.     char    *v;
  67.  
  68.     v = (char *) malloc( (n2-n1+1)*sizeof(char) );
  69.     if( v == NULL )
  70.         verrout( ON, "\nchar vector allocation failure!\n" );
  71.     return v-n1;
  72. }
  73.  
  74.  
  75. /*
  76.  * allocate an unsigned character vector with index range n1 <= i <= n2 
  77.  */
  78. uchar    *ucvector( int n1, int n2 )
  79. {
  80.     uchar    *v;
  81.  
  82.     v = (uchar *) malloc( (n2-n1+1)*sizeof(uchar) );
  83.     if( v == NULL )
  84.         verrout( ON, "\nunsigned char vector allocation failure!\n" );
  85.     return v-n1;
  86. }
  87.  
  88.  
  89. /*
  90.  * allocate an integer vector with index range n1 <= i <= n2 
  91.  */
  92. int    *ivector( int n1, int n2 )
  93. {
  94.     int    *v;
  95.  
  96.     v = (int *) malloc( (n2-n1+1)*sizeof(int) );
  97.     if( v == NULL )
  98.         verrout( ON, "\ninteger vector allocation failure!\n" );
  99.     return v-n1;
  100. }
  101.  
  102.  
  103. /*
  104.  * allocate a short integer vector with index range n1 <= i <= n2 
  105.  */
  106. short int    *sivector( int n1, int n2 )
  107. {
  108.     short int    *v;
  109.  
  110.     v = (short int *) malloc( (n2-n1+1)*sizeof(short int) );
  111.     if( v == NULL )
  112.         verrout( ON, "\nshort integer vector allocation failure!\n" );
  113.     return v-n1;
  114. }
  115.  
  116.  
  117. /*
  118.  * allocate an unsigned short integer vector with index range n1 <= i <= n2 
  119.  */
  120. ushort    *usivector( int n1, int n2 )
  121. {
  122.     ushort    *v;
  123.  
  124.     v = (ushort *) malloc( (n2-n1+1)*sizeof(ushort) );
  125.     if( v == NULL )
  126.         verrout( ON, 
  127.             "\nunsigned short integer vector allocation failure!\n" );
  128.     return v-n1;
  129. }
  130.  
  131.  
  132. /*
  133.  * allocate a long integer vector with index range n1 <= i <= n2 
  134.  */
  135. long    *lvector( int n1, int n2 )
  136. {
  137.     long    *v;
  138.  
  139.     v = (long *) malloc( (n2-n1+1)*sizeof(long) );
  140.     if( v == NULL )
  141.         verrout( ON, "\nlong vector allocation failure!\n" );
  142.     return v-n1;
  143. }
  144.  
  145.  
  146. /*
  147.  * allocate an unsigned integer vector with index range n1 <= i <= n2 
  148.  */
  149. uint    *uvector( int n1, int n2 )
  150. {
  151.     uint    *v;
  152.  
  153.     v = (uint *) malloc( (n2-n1+1)*sizeof(uint) );
  154.     if( v == NULL )
  155.         verrout( ON, "\nunsigned vector allocation failure!\n" );
  156.     return v-n1;
  157. }
  158.  
  159.  
  160. /*
  161.  * allocate an unsigned long integer vector with index range n1 <= i <= n2 
  162.  */
  163. ulong    *ulvector( int n1, int n2 )
  164. {
  165.     ulong    *v;
  166.  
  167.     v = (ulong *) malloc( (n2-n1+1)*sizeof(ulong) );
  168.     if( v == NULL )
  169.         verrout( ON, "\nunsigned long vector allocation failure!\n" );
  170.     return v-n1;
  171. }
  172.  
  173.  
  174. /*
  175.  * allocate a Flag vector with index range n1 <= i <= n2 
  176.  */
  177. Flag *flagvector( int n1, int n2 )
  178. {
  179.     Flag    *v;
  180.  
  181.     v = (Flag *) malloc( (n2-n1+1)*sizeof(Flag) );
  182.     if( v == NULL )
  183.         verrout( ON, "\nFlag vector allocation failure!\n" );
  184.     return v-n1;
  185. }
  186.  
  187.  
  188. /*
  189.  * allocate a vector of pointers with index range n1 <= i <= n2 
  190.  */
  191. void **pvector( int n1, int n2 )
  192. {
  193.     void    **v;
  194.  
  195.     v = (void **) malloc( (n2-n1+1)*sizeof(char *) );
  196.     if( v == NULL )
  197.         verrout( ON, "\npointer vector allocation failure!\n" );
  198.     return v-n1;
  199. }
  200.  
  201.  
  202.  
  203.  
  204. /*
  205.  *
  206.  * vector deallocation
  207.  *
  208.  */
  209.  
  210.  
  211. /*
  212.  * deallocate a float vector with starting index n1 
  213.  */
  214. void free_fvector( float *v, int n1 )
  215. {
  216.     free( v + n1 );
  217.     return;
  218. }
  219.  
  220.  
  221. /*
  222.  * deallocate a double vector with starting index n1 
  223.  */
  224. void free_dvector( double *v, int n1 )
  225. {
  226.     free( v + n1 );
  227.     return;
  228. }
  229.  
  230.  
  231. /*
  232.  * deallocate a character vector with starting index n1 
  233.  */
  234. void free_cvector( char *v, int n1 )
  235. {
  236.     free( v + n1 );
  237.     return;
  238. }
  239.  
  240.  
  241. /*
  242.  * deallocate an unsigned character vector with starting index n1 
  243.  */
  244. void free_ucvector( uchar *v, int n1 )
  245. {
  246.     free( v + n1 );
  247.     return;
  248. }
  249.  
  250.  
  251. /*
  252.  * deallocate an integer vector with starting index n1 
  253.  */
  254. void free_ivector( int *v, int n1 )
  255. {
  256.     free( v + n1 );
  257.     return;
  258. }
  259.  
  260.  
  261. /*
  262.  * deallocate a short integer vector with starting index n1 
  263.  */
  264. void free_sivector( short int *v, int n1 )
  265. {
  266.     free( v + n1 );
  267.     return;
  268. }
  269.  
  270.  
  271. /*
  272.  * deallocate an unsigned short integer vector with starting index n1 
  273.  */
  274. void free_usivector( ushort *v, int n1 )
  275. {
  276.     free( v + n1 );
  277.     return;
  278. }
  279.  
  280.  
  281. /*
  282.  * deallocate a long integer vector with starting index n1 
  283.  */
  284. void free_lvector( long *v, int n1 )
  285. {
  286.     free( v + n1 );
  287.     return;
  288. }
  289.  
  290.  
  291. /*
  292.  * deallocate an unsigned vector with starting index n1 
  293.  */
  294. void free_uvector( uint *v, int n1 )
  295. {
  296.     free( v + n1 );
  297.     return;
  298. }
  299.  
  300.  
  301. /*
  302.  * deallocate an unsigned long vector with starting index n1 
  303.  */
  304. void free_ulvector( ulong *v, int n1 )
  305. {
  306.     free( v + n1 );
  307.     return;
  308. }
  309.  
  310.  
  311. /*
  312.  * deallocate a Flag vector with starting index n1 
  313.  */
  314. void free_flagvector( Flag *v, int n1 )
  315. {
  316.     free( v + n1 );
  317.     return;
  318. }
  319.  
  320.  
  321. /* 
  322.  * deallocate a vector of pointers with starting index n1
  323.  *
  324.  * caller must cast **v as type char before calling this routine 
  325.  */
  326. void free_pvector( char **v, int n1 )
  327. {
  328.     free( v + n1 );
  329.     return;
  330. }
  331.  
  332.  
  333.  
  334.  
  335. /*
  336.  *
  337.  * error output routine
  338.  *
  339.  */
  340.  
  341. /*-----------------------------------------------------------------------------
  342.  * void verrout( int bell, FILE *device, char *errstring )
  343.  *
  344.  * error output
  345.  *---------------------------------------------------------------------------*/
  346. void  verrout( Boolean bell, char *errstring )
  347. {
  348.     if( bell )  putchar( BELL );
  349.     printf( "%s", errstring );
  350.     return;
  351. }
  352.  
  353.  
  354.  
  355.  
  356.