• API Main Page
  • Documentation
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

src/array.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Matthias Ihrke                                  *
00003  *   ihrke@nld.ds.mpg.de
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   aint with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00150 #ifndef ARRAY_H
00151 # define ARRAY_H
00152 
00153 #include "definitions.h"
00154 #include "mathadd.h"
00155 
00156 #ifdef __cplusplus
00157 extern "C" {
00158 #endif
00159 
00160 #define DIM_ROWS 0
00161 #define DIM_COLS 1
00162 
00177 #define array_SIZEOF_DTYPE( size, dtype )                                       \
00178   switch( dtype ) {                                                                 \
00179   case CHAR:                                                                            \
00180     size=sizeof(char);break;                                                        \
00181   case UINT:                                                                            \
00182     size=sizeof(uint);break;                                                        \
00183   case INT:                                                                             \
00184     size=sizeof(int);break;                                                         \
00185   case LONG:                                                                            \
00186     size=sizeof(long);break;                                                        \
00187   case ULONG:                                                                           \
00188     size=sizeof(unsigned long); break;                                          \
00189   case FLOAT:                                                                           \
00190     size=sizeof(float); break;                                                      \
00191   case DOUBLE:                                                                          \
00192     size=sizeof(double); break;                                                 \
00193   default:                                                                              \
00194      warnprintf("Do not know this datatype: %i\n", dtype );             \
00195   }
00196 
00201 #define array_DTYPESTRING(dtstring,dtype)                                       \
00202   switch( dtype ){                                                                  \
00203   case CHAR:                                                                            \
00204      dtstring="char";break;                                                         \
00205   case UINT:                                                                            \
00206      dtstring="uint";break;                                                         \
00207   case INT:                                                                             \
00208      dtstring="int";break;                                                          \
00209   case LONG:                                                                            \
00210      dtstring="long";break;                                                         \
00211   case ULONG:                                                                           \
00212      dtstring="unsigned long"; break;                                           \
00213   case FLOAT:                                                                           \
00214      dtstring="float"; break;                                                       \
00215   case DOUBLE:                                                                          \
00216      dtstring="double"; break;                                                      \
00217   default:                                                                              \
00218      warnprintf("Do not know this datatype: %i\n", dtype );             \
00219   }
00220  
00224 #define array_NUMEL( a )                                \
00225   ( ((a)->nbytes)/((a)->dtype_size) )
00226 
00233 #define array_DTYPEPRINT( out, dtype, mem )                                 \
00234   switch( dtype ){                                                                  \
00235   case CHAR:                                                                            \
00236      fprintf( out, "%c", *(char*)(mem) ); break;                                \
00237   case UINT:                                                                            \
00238      fprintf( out, "%i", *(uint*)(mem) ); break;                                \
00239   case INT:                                                                             \
00240      fprintf( out, "%i", *(int*)(mem) ); break;                             \
00241   case LONG:                                                                            \
00242      fprintf( out, "%li", *(long*)(mem) ); break;                           \
00243   case ULONG:                                                                           \
00244      fprintf( out, "%li", *(ulong*)(mem) ); break;                          \
00245   case FLOAT:                                                                           \
00246      fprintf( out, "%.2f", *(float*)(mem) ); break;                         \
00247   case DOUBLE:                                                                          \
00248      fprintf( out, "%.2f", *(double*)(mem) ); break;                            \
00249   default:                                                                              \
00250      warnprintf("Do not know this datatype: %i\n", dtype );             \
00251   }
00252   
00259 #define array_MEMSET( mem, dtype, val )                                         \
00260   switch( dtype ){                                                                      \
00261   case CHAR:                                                                                \
00262      *((char*)(mem)) = (char)val;break;                                             \
00263   case UINT:                                                                                \
00264      *((uint*)(mem)) = (uint)val;break;                                             \
00265   case INT:                                                                                 \
00266      *((int*)(mem)) = (int)val;break;                                                   \
00267   case LONG:                                                                                \
00268      *((long*)(mem)) = (long)val;break;                                             \
00269   case ULONG:                                                                               \
00270      *((ulong*)(mem)) = (ulong)val;break;                                               \
00271   case FLOAT:                                                                               \
00272      *((float*)(mem)) = (float)val;break;                                               \
00273   case DOUBLE:                                                                              \
00274      *((double*)(mem)) = (double)val;break;                                         \
00275   default:                                                                                  \
00276      warnprintf("Do not know this datatype: %i\n", dtype );                 \
00277   }
00278 
00297 #define array_INDEX1( array, dtype, i1 )                            \
00298   (*((dtype*)( ((array)->data) +                                        \
00299                     ((i1)*((array)->dtype_size)) )))
00300 
00302 #define array_INDEX2( array, dtype, i1, i2 )                                        \
00303   (*((dtype*)( ((array)->data)+                                                         \
00304                     ((i1)*((array)->size[1])*((array)->dtype_size))+                    \
00305                     ((i2)*((array)->dtype_size)) )))
00306 
00308 #define array_INDEX3( array, dtype, i1, i2, i3 )                                \
00309   (*((dtype*)( ((array)->data)+                                                         \
00310                     ((i1)*((array)->size[1])*((array)->size[2])*((array)->dtype_size))+ \
00311                     ((i2)*((array)->size[2])*((array)->dtype_size))+                    \
00312                     ((i3)*((array)->dtype_size)) )))
00313 
00331 #define array_INDEXMEM1( array, i1 )                                    \
00332   ( ((array)->data) +                                                       \
00333      ((i1)*((array)->dtype_size)) )
00334 
00336 #define array_INDEXMEM2( array, i1, i2 )                                            \
00337   ( ((array)->data)+                                                                        \
00338      ((i1)*((array)->size[1])*((array)->dtype_size))+                               \
00339      ((i2)*((array)->dtype_size)) )
00340 
00342 #define array_INDEXMEM3( array, i1, i2, i3 )                                        \
00343   ( ((array)->data)+                                                                        \
00344      ((i1)*((array)->size[1])*((array)->size[2])*((array)->dtype_size))+            \
00345      ((i2)*((array)->size[2])*((array)->dtype_size))+                               \
00346      ((i3)*((array)->dtype_size)) )
00347 
00349   typedef enum {
00350      CHAR=0,
00351      UINT,
00352      INT,
00353      LONG,
00354      ULONG,
00355      FLOAT,
00356      DOUBLE
00357   } DType;
00358 
00360   typedef struct {
00361      void *data;       
00362      DType dtype;      
00363      uint  dtype_size; 
00364      uint  ndim;       
00365      ulong nbytes;     
00366      uint  *size;      
00367      bool  free_data;  
00368   } Array;
00369 
00370   /* -------------- FUNCTIONS ---------------- */
00371   Array *array_new ( DType dtype, uint ndim, const uint *dims );
00372   Array *array_new2( DType dtype, uint ndim, ... );
00373   Array *array_fromptr ( DType dtype, uint ndim, void *data, const uint *size );
00374   Array *array_fromptr2( DType dtype, uint ndim, void *data, ... );
00375   Array *array_new_dummy( DType dtype, uint ndim, ... );
00376   Array *array_randunif( unsigned long seed, uint ndim, ... );
00377   bool   array_comparable( const Array *a, const Array *b );
00378 
00379   Array* array_copy( const Array *in, bool allocdata );
00380   void   array_free( Array *a );
00381 
00382   void   array_shuffle( Array *a, unsigned long seed );
00383 
00384   void   array_typecast( Array *a, DType target_type );
00385 
00386   void*  array_index ( const Array *a, uint *idx );
00387   void*  array_index2( const Array *a, ... );
00388   Array* array_slice ( const Array *a, const char *slicedesc );
00389 
00390   Array* array_concatenate( const Array *a, const Array *b, int dim );
00391 
00392   int    array_scale (Array * a, double x);
00393 
00394   void   array_reverse( Array *a );
00395 
00396   int    array_dimred( Array *a );
00397 
00398   void*  array_max( const Array *a );
00399   void*  array_min( const Array *a );
00400 
00401 
00402   Array* array_convert_rowcolmajor( Array *a, bool alloc);
00403   void   array_calc_colindex( ulong idx, const uint *size, uint nsize, uint *index );
00404   void   array_calc_rowindex( ulong idx, const uint *size, uint nsize, uint *index );
00405 
00406   void   array_print ( Array *a, uint nel_per_dim, FILE *out );
00407   void   array_dtype_to_double( double *out, void *mem, DType dt );
00408 
00410 #ifdef __cplusplus
00411 }
00412 #endif
00413 
00414 #endif /* ARRAY_H */

Generated on Fri Jun 25 2010 14:10:16 for libeegtools by  doxygen 1.7.0