c_deque.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #define CLIB_DEQUE_INDEX(x) ((char *)(pDeq)->pElements + (sizeof(struct clib_object) * (x)))
  26. static clib_error
  27. insert_clib_deque ( struct clib_deque *pDeq, int index, void *elem,size_t elem_size) {
  28. clib_error rc = CLIB_ERROR_SUCCESS;
  29. struct clib_object* pObject = new_clib_object ( elem, elem_size );
  30. if ( ! pObject )
  31. return CLIB_ARRAY_INSERT_FAILED;
  32. pDeq->pElements[index] = pObject;
  33. pDeq->no_of_elements++;
  34. return rc;
  35. }
  36. static struct clib_deque*
  37. grow_deque ( struct clib_deque *pDeq ) {
  38. pDeq->no_max_elements = pDeq->no_max_elements * 2;
  39. pDeq->pElements = (struct clib_object**) realloc ( pDeq->pElements,
  40. pDeq->no_max_elements * sizeof ( struct clib_object*));
  41. return pDeq;
  42. }
  43. struct clib_deque*
  44. new_clib_deque( int deq_size , clib_compare fn_c, clib_destroy fn_d) {
  45. struct clib_deque *pDeq = (struct clib_deque*)malloc(sizeof(struct clib_deque));
  46. if ( pDeq == (struct clib_deque*)0 )
  47. return (struct clib_deque*)0;
  48. pDeq->no_max_elements = deq_size < 8 ? 8 : deq_size;
  49. pDeq->pElements = (struct clib_object**) malloc(pDeq->no_max_elements * sizeof(struct clib_object*));
  50. if ( pDeq == (struct clib_deque*)0 )
  51. return (struct clib_deque*)0;
  52. pDeq->compare_fn = fn_c;
  53. pDeq->destruct_fn = fn_d;
  54. pDeq->head = (int)pDeq->no_max_elements / 2;
  55. pDeq->tail = pDeq->head + 1;
  56. pDeq->no_of_elements = 0;
  57. return pDeq;
  58. }
  59. clib_error
  60. push_back_clib_deque(struct clib_deque *pDeq, void *elem, size_t elem_size) {
  61. if ( pDeq == (struct clib_deque*)0 )
  62. return CLIB_DEQUE_NOT_INITIALIZED;
  63. if ( pDeq->tail == pDeq->no_max_elements )
  64. pDeq = grow_deque(pDeq);
  65. insert_clib_deque(pDeq, pDeq->tail, elem, elem_size);
  66. pDeq->tail++;
  67. return CLIB_ERROR_SUCCESS;
  68. }
  69. clib_error
  70. push_front_clib_deque(struct clib_deque *pDeq, void *elem,size_t elem_size) {
  71. clib_error rc = CLIB_ERROR_SUCCESS;
  72. int to = 0;
  73. int from = 0;
  74. int count = 0;
  75. if ( pDeq->head == 0 ) {
  76. pDeq = grow_deque(pDeq);
  77. to = (pDeq->no_max_elements - pDeq->no_of_elements)/2;
  78. from = pDeq->head + 1;
  79. count = pDeq->tail - from + 1;
  80. memmove (&(pDeq->pElements[to]), &(pDeq->pElements[from]), count * sizeof (struct clib_object*));
  81. pDeq->head = to - 1;
  82. pDeq->tail = pDeq->head + count;
  83. }
  84. insert_clib_deque(pDeq, pDeq->head, elem, elem_size);
  85. pDeq->head--;
  86. return rc;
  87. }
  88. clib_error
  89. front_clib_deque (struct clib_deque *pDeq, void *elem) {
  90. if ( pDeq == (struct clib_deque*)0 )
  91. return CLIB_DEQUE_NOT_INITIALIZED;
  92. element_at_clib_deque ( pDeq, pDeq->head + 1, elem );
  93. return CLIB_ERROR_SUCCESS;
  94. }
  95. clib_error
  96. back_clib_deque (struct clib_deque *pDeq, void *elem) {
  97. if ( pDeq == (struct clib_deque*)0 )
  98. return CLIB_DEQUE_NOT_INITIALIZED;
  99. element_at_clib_deque ( pDeq, pDeq->tail - 1, elem );
  100. return CLIB_ERROR_SUCCESS;
  101. }
  102. clib_error
  103. pop_back_clib_deque (struct clib_deque *pDeq) {
  104. if ( pDeq == (struct clib_deque*)0 )
  105. return CLIB_DEQUE_NOT_INITIALIZED;
  106. if ( pDeq->destruct_fn ) {
  107. void *elem;
  108. if ( element_at_clib_deque( pDeq, pDeq->tail - 1, &elem ) == CLIB_ERROR_SUCCESS )
  109. pDeq->destruct_fn(elem);
  110. }
  111. delete_clib_object(pDeq->pElements[pDeq->tail - 1]);
  112. pDeq->tail--;
  113. pDeq->no_of_elements--;
  114. return CLIB_ERROR_SUCCESS;
  115. }
  116. clib_error
  117. pop_front_clib_deque(struct clib_deque *pDeq) {
  118. if ( pDeq == (struct clib_deque*)0 )
  119. return CLIB_DEQUE_NOT_INITIALIZED;
  120. if ( pDeq->destruct_fn ) {
  121. void *elem;
  122. if ( element_at_clib_deque( pDeq, pDeq->head + 1, &elem ) == CLIB_ERROR_SUCCESS )
  123. pDeq->destruct_fn(elem);
  124. }
  125. delete_clib_object(pDeq->pElements[pDeq->head + 1]);
  126. pDeq->head++;
  127. pDeq->no_of_elements--;
  128. return CLIB_ERROR_SUCCESS;
  129. }
  130. clib_bool
  131. empty_clib_deque (struct clib_deque *pDeq) {
  132. if ( pDeq == (struct clib_deque*)0 )
  133. return clib_true;
  134. return pDeq->no_of_elements == 0 ? clib_true : clib_false;
  135. }
  136. int
  137. size_clib_deque( struct clib_deque *pDeq ) {
  138. if ( pDeq == (struct clib_deque*)0 )
  139. return clib_true;
  140. return pDeq->no_of_elements - 1;
  141. }
  142. clib_error
  143. element_at_clib_deque (struct clib_deque *pDeq, int index, void**elem) {
  144. clib_error rc = CLIB_ERROR_SUCCESS;
  145. if ( ! pDeq )
  146. return CLIB_DEQUE_NOT_INITIALIZED;
  147. get_raw_clib_object ( pDeq->pElements[index], elem );
  148. return rc;
  149. }
  150. clib_error
  151. delete_clib_deque ( struct clib_deque *pDeq ) {
  152. int i = 0;
  153. if ( pDeq == (struct clib_deque*)0 )
  154. return CLIB_ERROR_SUCCESS;
  155. if ( pDeq->destruct_fn ) {
  156. for ( i = pDeq->head + 1; i < pDeq->tail; i++ ){
  157. void *elem;
  158. if ( element_at_clib_deque( pDeq, i, &elem ) == CLIB_ERROR_SUCCESS ) {
  159. pDeq->destruct_fn(elem);
  160. }
  161. }
  162. }
  163. for ( i = pDeq->head + 1; i < pDeq->tail; i++ ){
  164. delete_clib_object(pDeq->pElements[i]);
  165. }
  166. free ( pDeq->pElements);
  167. free ( pDeq );
  168. return CLIB_ERROR_SUCCESS;
  169. }
  170. void
  171. for_each_clib_deque ( struct clib_deque *pDeq, void (*fn)(void*)) {
  172. int index = 0;
  173. for ( index = pDeq->head + 1; index < pDeq->tail; index++ ){
  174. void *elem;
  175. if ( element_at_clib_deque( pDeq, index, &elem ) == CLIB_ERROR_SUCCESS ) {
  176. (fn)(elem);
  177. free ( elem );
  178. }
  179. }
  180. }