~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina5/tina-libs/tina/math/mathVec_vector.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  *
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  *
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/math/mathVec_vector.c,v $
 37  * Date    :  $Date: 2008/12/07 02:27:36 $
 38  * Version :  $Revision: 1.5 $
 39  * CVS Id  :  $Id: mathVec_vector.c,v 1.5 2008/12/07 02:27:36 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Vector (1 dimensional array) allocation
 44  *
 45  *********
 46 */
 47 /** 
 48  *  @file
 49  *  @brief Vector (1 dimensional array) memory management.      
 50  *
 51  *  Includes allocating, freeing, casting, expanding functions.
 52  * 
 53 */
 54 
 55 #include "mathVec_vector.h"
 56 
 57 #if HAVE_CONFIG_H
 58 #include <config.h>
 59 #endif
 60 
 61 #if HAVE_STDARG_H
 62 #include <stdarg.h>
 63 #define VA_START(a, f)          va_start(a, f)
 64 #else
 65 #if HAVE_VARARGS_H
 66 #include <varargs.h>
 67 #define VA_START(a, f)  va_start(a)
 68 #endif
 69 #endif                          /* HAVE_STDARG_H */
 70 #ifndef VA_START
 71 error no variadic api available
 72 #endif
 73 
 74 #include <stdio.h>
 75 #include <tina/sys/sysDef.h>
 76 #include <tina/sys/sysPro.h>
 77 #include <tina/math/math_VecDef.h>
 78 
 79 
 80                 Vector * vector_alloc(int n, Vartype vtype)
 81 {
 82         Vector         *v;
 83         unsigned int    vsize;
 84 
 85         if (n <= 0)
 86                 return (NULL);
 87         if ((vsize = var_size(vtype)) == -1)
 88                 return (NULL);
 89 
 90         v = ts_ralloc(Vector);
 91         v->n = n;
 92         v->vtype = vtype;
 93         if (vtype == complex_v)
 94         {
 95                 v->data = zvector_alloc(0, n);
 96         } else
 97         {
 98                 v->data = nvector_alloc(0, n, vsize);
 99         }
100         return (v);
101 }
102 
103 void            vector_free(Vector * v)
104 {
105         if (v == NULL)
106                 return;
107         rfree((void *) v->data);
108         rfree((void *) v);
109 }
110 
111 /**
112 wrap a vector structure round already allocated memory
113 **/
114 
115 Vector         *vector_build(int n, Vartype vtype, void *el)
116 {
117         Vector         *v;
118 
119         if (n <= 0)
120                 return (NULL);
121         v = ts_ralloc(Vector);
122         v->n = n;
123         v->vtype = vtype;
124         v->data = el;
125         return (v);
126 }
127 
128 void            vector_copy_inplace(Vector * w, Vector * v)
129 {
130         int             i, n;
131 
132         if (w == NULL || v == NULL)
133                 return;
134 
135         if (v->n <= w->n && v->vtype == w->vtype)
136         {                       /** can use memcpy **/
137                 nvector_copy_inplace((char *) w->data, (char *) v->data, 0, v->n,
138                                      var_size(w->vtype));
139                 if (w->vtype == complex_v)
140                 {
141                         ts_nvector_zero_inplace((char *) w->data, v->n, w->n, Complex);
142                 } else
143                 {
144                         nvector_zero_inplace((char *) w->data, v->n, w->n, var_size(w->vtype));
145                 }
146                 return;
147         }
148         n = MIN(v->n, w->n);
149         switch (v->vtype)
150         {
151         case char_v:
152         case uchar_v:
153         case short_v:
154         case ushort_v:
155         case int_v:
156         case uint_v:
157         case ptr_v:
158                 for (i = 0; i < n; i++)
159                 {
160                         double          vi;
161 
162                         VECTOR_GET(v, i, vi);
163                         VECTOR_SET(w, i, vi);
164                 }
165                 break;
166         case float_v:
167         case double_v:
168                 for (i = 0; i < n; i++)
169                 {
170                         double          vi;
171 
172                         VECTOR_GET(v, i, vi);
173                         VECTOR_SET(w, i, vi);
174                 }
175                 break;
176         case complex_v:
177                 for (i = 0; i < n; i++)
178                 {
179                         Complex         vi = {Complex_id};
180 
181                         VECTOR_GETZ(v, i, vi);
182                         VECTOR_SETZ(w, i, vi);
183                 }
184                 break;
185         default:
186                 error("vector_copy_inplace: unsupported type", non_fatal);
187                 break;
188         }
189         if (w->vtype == complex_v)
190         {
191                 ts_nvector_zero_inplace((char *) w->data, n, w->n, Complex);
192         } else
193         {
194                 nvector_zero_inplace((char *) w->data, n, w->n, var_size(w->vtype));
195         }
196 }
197 
198 Vector         *vector_copy(Vector * v)
199 {
200         Vector         *copy;
201 
202         copy = vector_alloc((int) v->n, (Vartype) v->vtype);    /* ICCBUG argument and
203                                                                  * old-style */
204         /* parameter mismatch: 'vector_alloc'  */
205         vector_copy_inplace(copy, v);
206         return (copy);
207 }
208 
209 Vector         *vector_cast(Vector * v, Vartype vtype)
210 {
211         Vector         *copy;
212 
213         copy = vector_alloc(v->n, vtype);
214         vector_copy_inplace(copy, v);
215         return (copy);
216 }
217 
218 
219 Vector         *vector_expand(Vector * v, int n)
220 {
221         Vector         *copy;
222 
223         copy = vector_alloc(n, v->vtype);
224         vector_copy_inplace(copy, v);
225         vector_free(v);
226 
227         return (copy);
228 }
229 
230 
231 Vector         *vector_extend(Vector * v, int n, Vartype vtype)
232 {
233         Vector         *copy;
234 
235         copy = vector_alloc(n, vtype);  /* ICCBUG argument and old-style
236                                          * parameter */
237         /* mismatch: 'vector_alloc' */
238         vector_copy_inplace(copy, v);
239 
240         return (copy);
241 }
242 
243 Vartype         vector_sup_vtype(Vartype vtype1, Vartype vtype2)
244 {
245         if (vtype1 == complex_v || vtype2 == complex_v)
246                 return (complex_v);
247         if (vtype1 == double_v || vtype2 == double_v)
248                 return (double_v);
249         if (vtype1 == float_v || vtype2 == float_v)
250                 return (float_v);
251         return (int_v);
252 }
253 
254 Vector
255 #if HAVE_STDARG_H
256 * vector(int n, Vartype vtype,...)
257 #else
258 * vector(n, vtype, va_alist)
259         int             n;
260         Vartype         vtype;
261 va_dcl
262 #endif                          /* HAVE_STDARG_H */
263 {
264         va_list         ap;
265         int             i;
266         Vector         *v;
267 
268         if (n < 1)
269                 return (NULL);
270         v = vector_alloc(n, vtype);     /* ICCBUG argument and old-style
271                                          * parameter */
272         /* mismatch: 'vector_alloc' */
273         VA_START(ap, vtype);
274 
275         switch (vtype)
276         {
277         case ptr_v:
278                 for (i = 0; i < n; i++)
279                 {
280                         void           *vi = (void *) va_arg(ap, void *);
281 
282                         VECTOR_SETP(v, i, vi);
283                 }
284                 break;
285         case uchar_v:
286         case char_v:
287         case short_v:
288         case int_v:
289                 for (i = 0; i < n; i++)
290                 {
291                         int             vi = (int) va_arg(ap, int);
292 
293                         VECTOR_SET(v, i, vi);
294                 }
295                 break;
296         case float_v:
297         case double_v:
298                 for (i = 0; i < n; i++)
299                 {
300                         double          vi = (double) va_arg(ap, double);
301 
302                         VECTOR_SET(v, i, vi);
303                 }
304                 break;
305         default:
306                 error("vector: unsupported type", non_fatal);
307                 break;
308         }
309         va_end(ap);
310 
311         return (v);
312 }
313 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.