00001
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "array.h"
00015 #include <string.h>
00016
00022 VL_EXPORT vl_size
00023 vl_array_get_num_elements (VlArray const * self)
00024 {
00025 vl_size numElements = 1 ;
00026 vl_uindex k ;
00027 if (self->numDimensions == 0) {
00028 return 0 ;
00029 }
00030 for (k = 0 ; k < self->numDimensions ; ++k) {
00031 numElements *= self->dimensions[k] ;
00032 }
00033 return numElements ;
00034 }
00035
00036
00037
00038
00039
00050 VL_EXPORT VlArray *
00051 vl_array_init (VlArray* self, vl_type type,
00052 vl_size numDimensions, vl_size const * dimensions)
00053 {
00054 assert (numDimensions <= VL_ARRAY_MAX_NUM_DIMENSIONS) ;
00055 self->type = type ;
00056 self->numDimensions = numDimensions ;
00057 memcpy(self->dimensions, dimensions, sizeof(vl_size) * numDimensions) ;
00058 self->data = vl_malloc(vl_get_type_size(type) * vl_array_get_num_elements (self)) ;
00059 self->isEnvelope = VL_FALSE ;
00060 self->isSparse = VL_FALSE ;
00061 return self ;
00062 }
00063
00075 VL_EXPORT VlArray *
00076 vl_array_init_envelope (VlArray * self, void * data, vl_type type,
00077 vl_size numDimensions, vl_size const * dimensions)
00078 {
00079 assert (numDimensions <= VL_ARRAY_MAX_NUM_DIMENSIONS) ;
00080 self->type = type ;
00081 self->numDimensions = numDimensions ;
00082 memcpy(self->dimensions, dimensions, sizeof(vl_size) * numDimensions) ;
00083 self->data = data ;
00084 self->isEnvelope = VL_TRUE ;
00085 self->isSparse = VL_FALSE ;
00086 return self ;
00087 }
00088
00096 VL_EXPORT VlArray *
00097 vl_array_init_matrix (VlArray * self, vl_type type, vl_size numRows, vl_size numColumns)
00098 {
00099 vl_size dimensions [2] = {numRows, numColumns} ;
00100 return vl_array_init (self, type, 2, dimensions) ;
00101 }
00102
00111 VL_EXPORT VlArray *
00112 vl_array_init_matrix_envelope (VlArray * self, void * data,
00113 vl_type type, vl_size numRows, vl_size numColumns)
00114 {
00115 vl_size dimensions [2] = {numRows, numColumns} ;
00116 return vl_array_init_envelope (self, data, type, 2, dimensions) ;
00117 }
00118
00123 VL_EXPORT void
00124 vl_array_dealloc (VlArray * self)
00125 {
00126 if (! self->isEnvelope) {
00127 if (self->data) {
00128 vl_free(self->data) ;
00129 self->data = NULL ;
00130 }
00131 }
00132 }
00133
00134
00135
00136
00137
00138
00148 VL_EXPORT VlArray *
00149 vl_array_new (vl_type type, vl_size numDimensions, vl_size const * dimensions)
00150 {
00151 VlArray * self = vl_malloc(sizeof(VlArray)) ;
00152 return vl_array_init(self, type, numDimensions, dimensions) ;
00153 }
00154
00161 VL_EXPORT VlArray *
00162 vl_array_new_matrix (vl_type type, vl_size numRows, vl_size numColumns)
00163 {
00164 vl_size dimensions [2] = {numRows, numColumns} ;
00165 return vl_array_new (type, 2, dimensions) ;
00166 }
00167
00175 VL_EXPORT VlArray *
00176 vl_array_new_envelope (void * data, vl_type type,
00177 vl_size numDimensions, vl_size const * dimensions)
00178 {
00179 VlArray * self = vl_malloc(sizeof(VlArray)) ;
00180 return vl_array_init_envelope(self, data, type, numDimensions, dimensions) ;
00181 }
00182
00190 VL_EXPORT VlArray *
00191 vl_array_new_matrix_envelope (void * data, vl_type type, vl_size numRows, vl_size numColumns)
00192 {
00193 vl_size dimensions [2] = {numRows, numColumns} ;
00194 return vl_array_new_envelope (data, type, 2, dimensions) ;
00195 }
00196
00201 VL_EXPORT void
00202 vl_array_delete (VlArray * self)
00203 {
00204 vl_array_dealloc(self) ;
00205 vl_free(self) ;
00206 }