You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.6 KiB

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