1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/** @file array.h
** @brief Array
** @author Andrea Vedaldi
**/
/*
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
All rights reserved.
This file is part of the VLFeat library and is made available under
the terms of the BSD license (see the COPYING file).
*/
#include "array.h"
#include <string.h>
/** @brief Get number of elements in array
** @param self array.
** @return number of elements.
**/
VL_EXPORT vl_size
vl_array_get_num_elements (VlArray const * self)
{
vl_size numElements = 1 ;
vl_uindex k ;
if (self->numDimensions == 0) {
return 0 ;
}
for (k = 0 ; k < self->numDimensions ; ++k) {
numElements *= self->dimensions[k] ;
}
return numElements ;
}
/* ---------------------------------------------------------------- */
/* init & dealloc */
/* ---------------------------------------------------------------- */
/** @brief New numeric array
** @param self array to initialize.
** @param type data type.
** @param numDimensions number of dimensions.
** @param dimensions dimensions.
**
** The function initializes the specified array and allocates
** the necessary memory for storage.
**/
VL_EXPORT VlArray *
vl_array_init (VlArray* self, vl_type type,
vl_size numDimensions, vl_size const * dimensions)
{
assert (numDimensions <= VL_ARRAY_MAX_NUM_DIMENSIONS) ;
self->type = type ;
self->numDimensions = numDimensions ;
memcpy(self->dimensions, dimensions, sizeof(vl_size) * numDimensions) ;
self->data = vl_malloc(vl_get_type_size(type) * vl_array_get_num_elements (self)) ;
self->isEnvelope = VL_FALSE ;
self->isSparse = VL_FALSE ;
return self ;
}
/** @brief New numeric array envelope
** @param self array to initialize.
** @param data data to envelople.
** @param type data type.
** @param numDimensions number of dimensions.
** @param dimensions dimensions.
**
** The function initializes the specified array wrapping the
** specified buffer.
**/
VL_EXPORT VlArray *
vl_array_init_envelope (VlArray * self, void * data, vl_type type,
vl_size numDimensions, vl_size const * dimensions)
{
assert (numDimensions <= VL_ARRAY_MAX_NUM_DIMENSIONS) ;
self->type = type ;
self->numDimensions = numDimensions ;
memcpy(self->dimensions, dimensions, sizeof(vl_size) * numDimensions) ;
self->data = data ;
self->isEnvelope = VL_TRUE ;
self->isSparse = VL_FALSE ;
return self ;
}
/** @brief New numeric array with matrix shape
** @param self array to initialize.
** @param type type.
** @param numRows number of rows.
** @param numColumns number of columns.
**/
VL_EXPORT VlArray *
vl_array_init_matrix (VlArray * self, vl_type type, vl_size numRows, vl_size numColumns)
{
vl_size dimensions [2] = {numRows, numColumns} ;
return vl_array_init (self, type, 2, dimensions) ;
}
/** @brief New numeric array envelpe with matrix shape
** @param self array to initialize.
** @param data data to envelope.
** @param type type.
** @param numRows number of rows.
** @param numColumns number of columns.
**/
VL_EXPORT VlArray *
vl_array_init_matrix_envelope (VlArray * self, void * data,
vl_type type, vl_size numRows, vl_size numColumns)
{
vl_size dimensions [2] = {numRows, numColumns} ;
return vl_array_init_envelope (self, data, type, 2, dimensions) ;
}
/** @brief Delete array
** @param self array.
**/
VL_EXPORT void
vl_array_dealloc (VlArray * self)
{
if (! self->isEnvelope) {
if (self->data) {
vl_free(self->data) ;
self->data = NULL ;
}
}
}
/* ---------------------------------------------------------------- */
/* new & delete */
/* ---------------------------------------------------------------- */
/** @brief New numeric array
** @param type data type.
** @param numDimensions number of dimensions.
** @param dimensions dimensions.
**
** The function creates a new VLArray instance and allocates
** the necessary memory for storage.
**/
VL_EXPORT VlArray *
vl_array_new (vl_type type, vl_size numDimensions, vl_size const * dimensions)
{
VlArray * self = vl_malloc(sizeof(VlArray)) ;
return vl_array_init(self, type, numDimensions, dimensions) ;
}
/** @brief New numeric array with matrix shape
** @param type type.
** @param numRows number of rows.
** @param numColumns number of columns.
**/
VL_EXPORT VlArray *
vl_array_new_matrix (vl_type type, vl_size numRows, vl_size numColumns)
{
vl_size dimensions [2] = {numRows, numColumns} ;
return vl_array_new (type, 2, dimensions) ;
}
/** @brief New numeric array envelope
** @param data data to envelople.
** @param type data type.
** @param numDimensions number of dimensions.
** @param dimensions dimensions.
**/
VL_EXPORT VlArray *
vl_array_new_envelope (void * data, vl_type type,
vl_size numDimensions, vl_size const * dimensions)
{
VlArray * self = vl_malloc(sizeof(VlArray)) ;
return vl_array_init_envelope(self, data, type, numDimensions, dimensions) ;
}
/** @brief New numeric array envelpe with matrix shape
** @param data data to envelope.
** @param type type.
** @param numRows number of rows.
** @param numColumns number of columns.
**/
VL_EXPORT VlArray *
vl_array_new_matrix_envelope (void * data, vl_type type, vl_size numRows, vl_size numColumns)
{
vl_size dimensions [2] = {numRows, numColumns} ;
return vl_array_new_envelope (data, type, 2, dimensions) ;
}
/** @brief Delete array
** @param self array.
**/
VL_EXPORT void
vl_array_delete (VlArray * self)
{
vl_array_dealloc(self) ;
vl_free(self) ;
}
|