Debian Codegraph @

src/thirdparty/VLFeat/array.c

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