c_array.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  2. * This file is part of clib library
  3. * Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
  23. #include "c_lib.h"
  24. #include <string.h>
  25. #include <stdio.h>
  26. static struct clib_array*
  27. array_check_and_grow ( struct clib_array* pArray) {
  28. if ( pArray->no_of_elements >= pArray->no_max_elements ) {
  29. pArray->no_max_elements = 2 * pArray->no_max_elements;
  30. pArray->pElements = (struct clib_object**) realloc ( pArray->pElements,
  31. pArray->no_max_elements * sizeof ( struct clib_object*));
  32. }
  33. return pArray;
  34. }
  35. struct clib_array*
  36. new_clib_array(int array_size, clib_compare fn_c, clib_destroy fn_d) {
  37. struct clib_array* pArray = (struct clib_array*)malloc(sizeof(struct clib_array));
  38. if ( ! pArray )
  39. return (struct clib_array*)0;
  40. pArray->no_max_elements = array_size < 8 ? 8 : array_size;
  41. pArray->pElements = (struct clib_object**) malloc(pArray->no_max_elements * sizeof(struct clib_object*));
  42. if ( ! pArray->pElements ){
  43. free ( pArray );
  44. return (struct clib_array*)0;
  45. }
  46. pArray->compare_fn = fn_c;
  47. pArray->destruct_fn = fn_d;
  48. pArray->no_of_elements = 0;
  49. return pArray;
  50. }
  51. static clib_error
  52. insert_clib_array ( struct clib_array* pArray, int index, void *elem, size_t elem_size) {
  53. clib_error rc = CLIB_ERROR_SUCCESS;
  54. struct clib_object* pObject = new_clib_object ( elem, elem_size );
  55. if ( ! pObject )
  56. return CLIB_ARRAY_INSERT_FAILED;
  57. pArray->pElements[index] = pObject;
  58. pArray->no_of_elements++;
  59. return rc;
  60. }
  61. clib_error
  62. push_back_clib_array (struct clib_array* pArray, void *elem, size_t elem_size) {
  63. clib_error rc = CLIB_ERROR_SUCCESS;
  64. if ( ! pArray)
  65. return CLIB_ARRAY_NOT_INITIALIZED;
  66. array_check_and_grow ( pArray);
  67. rc = insert_clib_array( pArray, pArray->no_of_elements, elem, elem_size);
  68. return rc;
  69. }
  70. clib_error
  71. element_at_clib_array (struct clib_array* pArray, int index, void** elem) {
  72. clib_error rc = CLIB_ERROR_SUCCESS;
  73. if ( ! pArray )
  74. return CLIB_ARRAY_NOT_INITIALIZED;
  75. if ( index < 0 || index >= pArray->no_of_elements )
  76. return CLIB_ARRAY_INDEX_OUT_OF_BOUND;
  77. get_raw_clib_object ( pArray->pElements[index], elem );
  78. return rc;
  79. }
  80. int
  81. size_clib_array ( struct clib_array* pArray ) {
  82. if ( pArray == (struct clib_array*)0 )
  83. return 0;
  84. return pArray->no_of_elements;
  85. }
  86. int
  87. capacity_clib_array ( struct clib_array* pArray ) {
  88. if ( pArray == (struct clib_array*)0 )
  89. return 0;
  90. return pArray->no_max_elements;
  91. }
  92. clib_bool
  93. empty_clib_array ( struct clib_array* pArray) {
  94. if ( pArray == (struct clib_array*)0 )
  95. return 0;
  96. if ( pArray->no_of_elements == 0 )
  97. return clib_true;
  98. else
  99. return clib_false;
  100. }
  101. clib_error
  102. reserve_clib_array ( struct clib_array* pArray, int new_size) {
  103. if ( pArray == (struct clib_array*)0 )
  104. return CLIB_ARRAY_NOT_INITIALIZED;
  105. if ( new_size <= pArray->no_max_elements )
  106. return CLIB_ERROR_SUCCESS;
  107. array_check_and_grow ( pArray);
  108. return CLIB_ERROR_SUCCESS;
  109. }
  110. clib_error
  111. front_clib_array ( struct clib_array* pArray,void *elem) {
  112. return element_at_clib_array ( pArray, 0, elem );
  113. }
  114. clib_error
  115. back_clib_array ( struct clib_array* pArray,void *elem) {
  116. return element_at_clib_array ( pArray, pArray->no_of_elements - 1, elem );
  117. }
  118. clib_error
  119. insert_at_clib_array ( struct clib_array* pArray, int index, void *elem, size_t elem_size) {
  120. clib_error rc = CLIB_ERROR_SUCCESS;
  121. if ( ! pArray )
  122. return CLIB_ARRAY_NOT_INITIALIZED;
  123. if ( index < 0 || index > pArray->no_max_elements )
  124. return CLIB_ARRAY_INDEX_OUT_OF_BOUND;
  125. array_check_and_grow ( pArray);
  126. memmove ( &(pArray->pElements[index + 1]),
  127. &pArray->pElements[index],
  128. (pArray->no_of_elements - index ) * sizeof(struct clib_object*));
  129. rc = insert_clib_array ( pArray, index, elem , elem_size);
  130. return rc;
  131. }
  132. clib_error
  133. remove_clib_array ( struct clib_array* pArray, int index) {
  134. clib_error rc = CLIB_ERROR_SUCCESS;
  135. if ( ! pArray )
  136. return rc;
  137. if ( index < 0 || index >= pArray->no_of_elements )
  138. return CLIB_ARRAY_INDEX_OUT_OF_BOUND;
  139. if ( pArray->destruct_fn ) {
  140. void *elem;
  141. if ( CLIB_ERROR_SUCCESS == element_at_clib_array ( pArray, index , &elem ) ) {
  142. pArray->destruct_fn(elem);
  143. }
  144. }
  145. delete_clib_object ( pArray->pElements[index]);
  146. if ( index != pArray->no_of_elements - 1 ) {
  147. memmove ( &(pArray->pElements[index]),
  148. &pArray->pElements[index + 1],
  149. (pArray->no_of_elements - index ) * sizeof(struct clib_object*));
  150. }
  151. pArray->no_of_elements--;
  152. return rc;
  153. }
  154. clib_error
  155. delete_clib_array( struct clib_array* pArray) {
  156. clib_error rc = CLIB_ERROR_SUCCESS;
  157. int i = 0;
  158. if ( pArray == (struct clib_array*)0 )
  159. return rc;
  160. if ( pArray->destruct_fn ) {
  161. for ( i = 0; i < pArray->no_of_elements; i++) {
  162. void *elem;
  163. if ( CLIB_ERROR_SUCCESS == element_at_clib_array ( pArray, i , &elem ) )
  164. pArray->destruct_fn(elem);
  165. }
  166. }
  167. for ( i = 0; i < pArray->no_of_elements; i++)
  168. delete_clib_object ( pArray->pElements[i]);
  169. free ( pArray->pElements);
  170. free ( pArray );
  171. return rc;
  172. }
  173. void
  174. swap_element_clib_array ( struct clib_array *pArray, int left, int right) {
  175. struct clib_object *temp = pArray->pElements[left];
  176. pArray->pElements[left] = pArray->pElements[right];
  177. pArray->pElements[right] = temp;
  178. }
  179. void for_each_clib_array ( struct clib_array *pArray, void (*fn)(void*)) {
  180. int size = pArray->no_of_elements;
  181. int i = 0;
  182. for ( i = 0; i < size; i++ ) {
  183. void *elem;
  184. element_at_clib_array ( pArray, i , &elem);
  185. (fn)(elem);
  186. free ( elem );
  187. }
  188. }