c_slist.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. struct clib_slist*
  25. new_clib_slist(clib_destroy fn_d, clib_compare fn_c){
  26. struct clib_slist *pSlist = (struct clib_slist*)malloc(sizeof(struct clib_slist));
  27. pSlist->head = (struct clib_slist_node*)0;
  28. pSlist->destruct_fn = fn_d;
  29. pSlist->compare_fn = fn_c;
  30. pSlist->size = 0;
  31. return pSlist;
  32. }
  33. void
  34. delete_clib_slist( struct clib_slist *pSlist){
  35. while(pSlist->size != 0) {
  36. remove_clib_slist ( pSlist, 0 );
  37. }
  38. free ( pSlist );
  39. }
  40. clib_error
  41. push_back_clib_slist( struct clib_slist *pSlist, void *elem, size_t elem_size){
  42. struct clib_slist_node* current = (struct clib_slist_node*)0;
  43. struct clib_slist_node* new_node = (struct clib_slist_node*)0;
  44. new_node = (struct clib_slist_node*)malloc(sizeof(struct clib_slist_node));
  45. new_node->elem = new_clib_object ( elem, elem_size );
  46. if ( ! new_node->elem )
  47. return CLIB_SLIST_INSERT_FAILED;
  48. new_node->next = (struct clib_slist_node*)0;
  49. if ( pSlist->head == (struct clib_slist_node*)0 ) {
  50. pSlist->head = new_node;
  51. pSlist->size++;
  52. return CLIB_ERROR_SUCCESS;
  53. }
  54. current = pSlist->head;
  55. while ( current->next != (struct clib_slist_node*)0 )
  56. current = current->next;
  57. current->next = new_node;
  58. pSlist->size++;
  59. return CLIB_ERROR_SUCCESS;
  60. }
  61. static void
  62. pvt_remove_clib_list ( struct clib_slist *pSlist, struct clib_slist_node* pSlistNode ) {
  63. void *elem;
  64. get_raw_clib_object(pSlistNode->elem, &elem);
  65. if ( pSlist->destruct_fn) {
  66. (pSlist->destruct_fn)(elem);
  67. delete_clib_object ( pSlistNode->elem );
  68. }else {
  69. free ( elem );
  70. delete_clib_object ( pSlistNode->elem );
  71. }
  72. free ( pSlistNode);
  73. }
  74. void
  75. remove_clib_slist( struct clib_slist *pSlist, int pos ) {
  76. int i = 0;
  77. struct clib_slist_node* current = pSlist->head;
  78. struct clib_slist_node* temp = (struct clib_slist_node*)0;
  79. if ( pos > pSlist->size ) return;
  80. if ( pos == 0 ) {
  81. pSlist->head = current->next;
  82. pvt_remove_clib_list(pSlist, current);
  83. pSlist->size--;
  84. return;
  85. }
  86. for ( i = 1; i < pos - 1; i++)
  87. current = current->next;
  88. temp = current->next;
  89. current->next = current->next->next;
  90. pvt_remove_clib_list ( pSlist, temp );
  91. pSlist->size--;
  92. }
  93. clib_error
  94. insert_clib_slist(struct clib_slist *pSlist, int pos, void *elem, size_t elem_size) {
  95. int i = 0;
  96. struct clib_slist_node* current = pSlist->head;
  97. struct clib_slist_node* new_node = (struct clib_slist_node*)0;
  98. if ( pos == 1 ) {
  99. new_node = (struct clib_slist_node*)malloc(sizeof(struct clib_slist_node));
  100. new_node->elem = new_clib_object ( elem, elem_size );
  101. if ( ! new_node->elem ) {
  102. free ( new_node );
  103. return CLIB_SLIST_INSERT_FAILED;
  104. }
  105. new_node->next = pSlist->head;
  106. pSlist->head = new_node;
  107. pSlist->size++;
  108. return CLIB_ERROR_SUCCESS;
  109. }
  110. if ( pos >= pSlist->size + 1 ) {
  111. return push_back_clib_slist ( pSlist, elem, elem_size );
  112. }
  113. for ( i = 1; i < pos - 1; i++) {
  114. current = current->next;
  115. }
  116. new_node = (struct clib_slist_node*)malloc(sizeof(struct clib_slist_node));
  117. new_node->elem = new_clib_object ( elem, elem_size );
  118. if ( ! new_node->elem ) {
  119. free ( new_node );
  120. return CLIB_SLIST_INSERT_FAILED;
  121. }
  122. new_node->next = current->next;
  123. current->next = new_node;
  124. pSlist->size++;
  125. return CLIB_ERROR_SUCCESS;
  126. }
  127. void
  128. for_each_clib_slist (struct clib_slist *pSlist, void (*fn)(void* )) {
  129. void *elem;
  130. struct clib_slist_node* current = pSlist->head;
  131. while ( current != (struct clib_slist_node*)0 ) {
  132. get_raw_clib_object(current->elem, &elem);
  133. (fn)(elem);
  134. free ( elem );
  135. current = current->next;
  136. }
  137. }
  138. clib_bool
  139. find_clib_slist (struct clib_slist *pSlist, void* find_value, void**out_value) {
  140. struct clib_slist_node* current = pSlist->head;
  141. while ( current != (struct clib_slist_node*)0 ) {
  142. get_raw_clib_object(current->elem, out_value);
  143. if ((pSlist->compare_fn)(find_value,*out_value) != 0){
  144. break;
  145. }
  146. free ( *out_value );
  147. current = current->next;
  148. }
  149. if ( current ) {
  150. return clib_true;
  151. }
  152. return clib_false;
  153. }