vector.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <stdint.h>
  2. #include <stddef.h>
  3. #include <stdbool.h>
  4. #include "zalloc.h"
  5. #include "systypes.h"
  6. #include "vector.h"
  7. static zv_vector_container_t * zv__vector_container_create(void) {
  8. zv_vector_container_t *created
  9. = (zv_vector_container_t *) zv_malloc(sizeof(zv_vector_container_t));
  10. created->next = NULL;
  11. created->previous = NULL;
  12. return created;
  13. }
  14. static void zv__vector_container_drop(zv_vector_container_t *self) {
  15. zv_free(self);
  16. }
  17. static zv_vector_container_t * zv__vector_get_container(
  18. zv_vector_t *self,
  19. size_t index
  20. ) {
  21. if (self->length <= index) {
  22. return NULL;
  23. }
  24. if (index < self->length / 2) {
  25. zv_vector_container_t *current = self->first;
  26. while (index > 0) {
  27. current = current->next;
  28. --index;
  29. }
  30. return current;
  31. }
  32. zv_vector_container_t *current = self->last;
  33. index = self->length - index;
  34. while (index > 0) {
  35. current = current->previous;
  36. --index;
  37. }
  38. return current;
  39. }
  40. zv_vector_t * zv_vector_create(void) {
  41. zv_vector_t *self = (zv_vector_t *) zv_malloc(sizeof(zv_vector_t));
  42. if (self == NULL) {
  43. return NULL;
  44. }
  45. self->length = 0x00;
  46. self->first = NULL;
  47. self->last = NULL;
  48. return self;
  49. }
  50. bool zv_vector_append(zv_vector_t *self, zv_universal_t content) {
  51. return zv_vector_insert(self, content, self->length);
  52. }
  53. zv_universal_t zv_vector_pop(zv_vector_t *self) {
  54. return zv_vector_indexed_pop(self, self->length - 1);
  55. }
  56. bool zv_vector_insert(
  57. zv_vector_t *self,
  58. zv_universal_t content,
  59. size_t index
  60. ) {
  61. if (index > self->length) {
  62. return false;
  63. }
  64. zv_vector_container_t *target = zv__vector_container_create();
  65. if (target == NULL) {
  66. return false;
  67. }
  68. target->content = content;
  69. if (index == 0) {
  70. target->previous = NULL;
  71. target->next = self->first;
  72. self->first = target;
  73. ++self->length;
  74. return true;
  75. }
  76. if (index == self->length) {
  77. target->next = NULL;
  78. target->previous = self->last;
  79. self->last = target;
  80. ++self->length;
  81. return true;
  82. }
  83. target->previous = zv__vector_get_container(self, index - 1);
  84. target->next = target->previous->next;
  85. target->previous->next = target;
  86. target->next->previous = target;
  87. ++self->length;
  88. return true;
  89. }
  90. zv_universal_t zv_vector_indexed_pop(zv_vector_t *self, size_t index) {
  91. zv_vector_container_t *container = zv__vector_get_container(self, index);
  92. if (container == NULL) {
  93. return ZV_UNIVERSAL_NULL;
  94. }
  95. zv_universal_t content = container->content;
  96. if (index == 0) {
  97. self->first = container->next;
  98. self->first->previous = NULL;
  99. }
  100. if (index == self->length - 1) {
  101. self->last = container->previous;
  102. self->last->next = NULL;
  103. }
  104. if (index != 0 && index != self->length - 1) {
  105. container->next->previous = container->previous;
  106. container->previous->next = container->next;
  107. }
  108. --self->length;
  109. zv__vector_container_drop(container);
  110. return content;
  111. }
  112. void zv_vector_clear(zv_vector_t *self) {
  113. if (self->length == 0) {
  114. return;
  115. }
  116. zv_vector_container_t *current = self->first;
  117. while (current != NULL) {
  118. zv_vector_container_t *previous = current;
  119. current = current->next;
  120. zv__vector_container_drop(previous);
  121. }
  122. self->first = NULL;
  123. self->last = NULL;
  124. self->length = 0;
  125. }
  126. void zv_vector_remove(zv_vector_t *self) {
  127. zv_vector_clear(self);
  128. zv_free(self);
  129. }