vector.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. zv_vector_t * zv_vector_create(void) {
  8. return (zv_vector_t *) zv_malloc(sizeof(zv_vector_t));
  9. }
  10. void zv_vector_clear(zv_vector_t *self) {
  11. if (*self == NULL) {
  12. return;
  13. }
  14. }
  15. void zv_vector_remove(zv_vector_t *self) {
  16. zv_vector_clear(self);
  17. zv_free(self);
  18. }
  19. zv_vector_count_t zv_vector_length(zv_vector_t *self) {
  20. if (*self == NULL) {
  21. return 0;
  22. }
  23. zv_vector_container_t *first = *self;
  24. zv_vector_container_t *current = first;
  25. zv_vector_count_t size = 1;
  26. while (current->next != first) {
  27. ++size;
  28. current = current->next;
  29. }
  30. return size;
  31. }
  32. zv_vector_container_t * zv_vector_get_container(
  33. zv_vector_t *self,
  34. zv_vector_count_t index
  35. ) {
  36. if (*self == NULL) {
  37. return NULL;
  38. }
  39. zv_vector_container_t *current = *self;
  40. while (index > 0 && current != NULL) {
  41. current = current->next;
  42. --index;
  43. }
  44. while (index < 0 && current != NULL) {
  45. current = current->previous;
  46. ++index;
  47. }
  48. return current;
  49. }
  50. zv_universal_t zv_vector_pop(zv_vector_t *self) {
  51. return zv_vector_indexed_pop(self, -1);
  52. }
  53. zv_universal_t zv_vector_indexed_pop(
  54. zv_vector_t *self,
  55. zv_vector_count_t index
  56. ) {
  57. if (*self == NULL) {
  58. return ZV_UNIVERSAL_NULL;
  59. }
  60. zv_vector_container_t *current = *self;
  61. if (current->next == current) {
  62. *self = NULL;
  63. }
  64. return ZV_UNIVERSAL_NULL;
  65. }
  66. bool zv_vector_insert(
  67. zv_vector_t *self,
  68. zv_universal_t content,
  69. zv_vector_count_t index
  70. ) {
  71. zv_vector_container_t *target = zv_malloc(sizeof(zv_vector_container_t));
  72. if (target == NULL) {
  73. return false;
  74. }
  75. target->content = content;
  76. if (*self == NULL) {
  77. *self = target;
  78. target->next = target;
  79. target->previous = target;
  80. return true;
  81. }
  82. zv_vector_container_t *current = *self;
  83. bool overwrite_handler = (index == 0);
  84. while (index > 0 && current != NULL) {
  85. current = current->next;
  86. --index;
  87. }
  88. while (index < -1 && current != NULL) {
  89. current = current->previous;
  90. ++index;
  91. }
  92. if (current == NULL) {
  93. zv_free(target);
  94. return false;
  95. }
  96. target->next = current;
  97. target->previous = current->previous;
  98. current->previous->next = target;
  99. current->previous = target;
  100. if (overwrite_handler) {
  101. *self = target;
  102. }
  103. return true;
  104. }
  105. bool zv_vector_append(zv_vector_t *self, zv_universal_t content) {
  106. return zv_vector_insert(self, content, -1);
  107. }