stack.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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;
  33. size_t size;
  34. size_t iter;
  35. void **array;
  36. };
  37. /******************************************************************************/
  38. sd_stack_t* sd_stack_new(size_t max)
  39. {
  40. sd_stack_t* ptrThis;
  41. ptrThis = (sd_stack_t*)sd_calloc(1, sizeof(sd_stack_t));
  42. ptrThis->max = max == 0 ? INT_MAX : max;
  43. ptrThis->size = SD_STACK_INIT_SIZE;
  44. ptrThis->sp = 0;
  45. ptrThis->array = (void **)sd_calloc(ptrThis->size, sizeof(*ptrThis->array));
  46. return ptrThis;
  47. }
  48. /******************************************************************************/
  49. void sd_stack_delete(sd_stack_t* ptrThis, void (*free_data_fn)(void *))
  50. {
  51. if (!ptrThis)
  52. return;
  53. sd_stack_clear(ptrThis, free_data_fn);
  54. free(ptrThis->array);
  55. free(ptrThis);
  56. }
  57. /******************************************************************************/
  58. size_t sd_stack_get_nelem(const sd_stack_t* ptrThis)
  59. {
  60. return ptrThis ? ptrThis->sp : -1;
  61. }
  62. /******************************************************************************/
  63. void sd_stack_clear(sd_stack_t* ptrThis, void (*free_data_fn)(void *))
  64. {
  65. if (!ptrThis)
  66. return;
  67. if (free_data_fn) {
  68. while (ptrThis->sp > 0) {
  69. free_data_fn(ptrThis->array[--(ptrThis->sp)]);
  70. }
  71. }
  72. }
  73. /******************************************************************************/
  74. void* sd_stack_begin(sd_stack_t* ptrThis)
  75. {
  76. if (!ptrThis)
  77. return NULL;
  78. ptrThis->iter = 0;
  79. return ptrThis->array[ptrThis->iter];
  80. }
  81. /******************************************************************************/
  82. void* sd_stack_next(sd_stack_t* ptrThis)
  83. {
  84. if (ptrThis && ptrThis->iter < ptrThis->sp)
  85. return ptrThis->array[ptrThis->iter++];
  86. return NULL;
  87. }
  88. /******************************************************************************/
  89. void* sd_stack_end(sd_stack_t* ptrThis)
  90. {
  91. return sd_stack_peek(ptrThis);
  92. }
  93. /******************************************************************************/
  94. void* sd_stack_peek(sd_stack_t* ptrThis)
  95. {
  96. if (!ptrThis || !ptrThis->sp)
  97. return NULL;
  98. return ptrThis->array[ptrThis->sp - 1];
  99. }
  100. /******************************************************************************/
  101. int sd_stack_push(sd_stack_t* ptrThis, void *data)
  102. {
  103. if (ptrThis == NULL)
  104. return -1;
  105. if (ptrThis->sp == ptrThis->size) {
  106. size_t new_size;
  107. if (ptrThis->size == ptrThis->max)
  108. return -1;
  109. if (ptrThis->size * 2 > ptrThis->max) {
  110. new_size = ptrThis->max;
  111. } else {
  112. new_size = ptrThis->size * 2;
  113. }
  114. ptrThis->size = new_size;
  115. ptrThis->array = (void **)sd_realloc(ptrThis->array, sizeof(*ptrThis->array) * ptrThis->size);
  116. }
  117. assert(ptrThis->sp <= ptrThis->size);
  118. ptrThis->array[ptrThis->sp++] = data;
  119. return 0;
  120. }
  121. /******************************************************************************/
  122. void* sd_stack_pop(sd_stack_t* ptrThis)
  123. {
  124. if (ptrThis == NULL || ptrThis->sp == 0)
  125. return NULL;
  126. if (ptrThis->size >= SD_STACK_INIT_SIZE * 4 && ptrThis->sp < ptrThis->size / 4) {
  127. size_t new_size = ptrThis->size / 2;
  128. ptrThis->size = new_size;
  129. ptrThis->array = (void **)sd_realloc(ptrThis->array, sizeof(*ptrThis->array) * ptrThis->size);
  130. }
  131. assert(ptrThis->sp > 0 && ptrThis->sp <= ptrThis->size);
  132. return ptrThis->array[--(ptrThis->sp)];
  133. }