stack.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* stack - a dynamically resizing stack
  2. * Copyright (c) 2001 Michael B. Allen <mballen@erols.com>
  3. *
  4. * The MIT License
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include "sd/stack.h"
  25. #include <stdlib.h>
  26. #include <limits.h>
  27. #include <assert.h>
  28. #include "sd/malloc.h"
  29. #define SD_STACK_INIT_SIZE 32
  30. struct __sd_stack {
  31. size_t max; // 堆栈的能分配的最大长度;
  32. size_t sp; // push的个数,实际堆栈元素;
  33. size_t size; // 当前堆栈分配的大小;
  34. size_t iter; // 迭代索引;
  35. void **array; // 堆栈区;
  36. };
  37. /************************************************************************/
  38. /* 函数:sd_stack_new[6/14/2016 ItCivilian];
  39. /* 描述:分配最大max长度的堆栈;
  40. /* 参数:;
  41. /* [IN] max:堆栈的最大值;
  42. /* 返回:返回堆栈指针;
  43. /* 注意:;
  44. /* 示例:;
  45. /************************************************************************/
  46. sd_stack_t* sd_stack_new(size_t max)
  47. {
  48. sd_stack_t* ptrThis;
  49. ptrThis = (sd_stack_t*)sd_calloc(1, sizeof(sd_stack_t));
  50. ptrThis->max = max == 0 ? INT_MAX : max;
  51. ptrThis->size = SD_STACK_INIT_SIZE;
  52. ptrThis->sp = 0;
  53. ptrThis->array = (void **)sd_calloc(ptrThis->size, sizeof(*ptrThis->array));
  54. return ptrThis;
  55. }
  56. /************************************************************************/
  57. /* 函数:sd_stack_delete[6/14/2016 ItCivilian];
  58. /* 描述:删除指定的堆栈;
  59. /* 参数:;
  60. /* [IN] ptrThis:要删除的堆栈指针;
  61. /* [IN] free_data_fn:回调函数指针;
  62. /* 返回:void;
  63. /* 注意:;
  64. /* 示例:;
  65. /************************************************************************/
  66. void sd_stack_delete(sd_stack_t* ptrThis, void (*free_data_fn)(void *))
  67. {
  68. if (!ptrThis)
  69. return;
  70. sd_stack_clear(ptrThis, free_data_fn);
  71. free(ptrThis->array);
  72. free(ptrThis);
  73. }
  74. /************************************************************************/
  75. /* 函数:[6/14/2016 ItCivilian];
  76. /* 描述:获取堆栈元素个数;
  77. /* 参数:;
  78. /* [IN] ptrThis:堆栈;
  79. /* 返回:void;
  80. /* 注意:;
  81. /* 示例:;
  82. /************************************************************************/
  83. size_t sd_stack_get_nelem(const sd_stack_t* ptrThis)
  84. {
  85. return ptrThis ? ptrThis->sp : -1;
  86. }
  87. /******************************************************************************/
  88. void sd_stack_clear(sd_stack_t* ptrThis, void (*free_data_fn)(void *))
  89. {
  90. if (!ptrThis)
  91. return;
  92. if (free_data_fn) {
  93. while (ptrThis->sp > 0) {
  94. free_data_fn(ptrThis->array[--(ptrThis->sp)]);
  95. }
  96. }
  97. }
  98. /******************************************************************************/
  99. void* sd_stack_begin(sd_stack_t* ptrThis)
  100. {
  101. if (!ptrThis)
  102. return NULL;
  103. ptrThis->iter = 0;
  104. return ptrThis->array[ptrThis->iter];
  105. }
  106. /******************************************************************************/
  107. void* sd_stack_next(sd_stack_t* ptrThis)
  108. {
  109. if (ptrThis && ptrThis->iter < ptrThis->sp)
  110. return ptrThis->array[ptrThis->iter++];
  111. return NULL;
  112. }
  113. /******************************************************************************/
  114. void* sd_stack_end(sd_stack_t* ptrThis)
  115. {
  116. return sd_stack_peek(ptrThis);
  117. }
  118. /******************************************************************************/
  119. void* sd_stack_peek(sd_stack_t* ptrThis)
  120. {
  121. if (!ptrThis || !ptrThis->sp)
  122. return NULL;
  123. return ptrThis->array[ptrThis->sp - 1];
  124. }
  125. /******************************************************************************/
  126. int sd_stack_push(sd_stack_t* ptrThis, void *data)
  127. {
  128. if (ptrThis == NULL)
  129. return -1;
  130. if (ptrThis->sp == ptrThis->size) {
  131. size_t new_size;
  132. if (ptrThis->size == ptrThis->max)
  133. return -1;
  134. if (ptrThis->size * 2 > ptrThis->max)
  135. {
  136. new_size = ptrThis->max;
  137. }
  138. else
  139. {
  140. new_size = ptrThis->size * 2;
  141. }
  142. ptrThis->size = new_size;
  143. ptrThis->array = (void **)sd_realloc(ptrThis->array, sizeof(*ptrThis->array) * ptrThis->size);
  144. }
  145. assert(ptrThis->sp <= ptrThis->size);
  146. ptrThis->array[ptrThis->sp++] = data;
  147. return 0;
  148. }
  149. /******************************************************************************/
  150. void* sd_stack_pop(sd_stack_t* ptrThis)
  151. {
  152. if (ptrThis == NULL || ptrThis->sp == 0)
  153. return NULL;
  154. if (ptrThis->size >= SD_STACK_INIT_SIZE * 4 && ptrThis->sp < ptrThis->size / 4) {
  155. size_t new_size = ptrThis->size / 2;
  156. ptrThis->size = new_size;
  157. ptrThis->array = (void **)sd_realloc(ptrThis->array, sizeof(*ptrThis->array) * ptrThis->size);
  158. }
  159. assert(ptrThis->sp > 0 && ptrThis->sp <= ptrThis->size);
  160. return ptrThis->array[--(ptrThis->sp)];
  161. }