vector.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: [email protected] (Jonathan Tang)
  16. #include "vector.h"
  17. #include <assert.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include "util.h"
  22. struct GumboInternalParser;
  23. const GumboVector kGumboEmptyVector = {NULL, 0, 0};
  24. void gumbo_vector_init(struct GumboInternalParser* parser,
  25. size_t initial_capacity, GumboVector* vector) {
  26. vector->length = 0;
  27. vector->capacity = initial_capacity;
  28. if (initial_capacity > 0) {
  29. vector->data =
  30. gumbo_parser_allocate(parser, sizeof(void*) * initial_capacity);
  31. } else {
  32. vector->data = NULL;
  33. }
  34. }
  35. void gumbo_vector_destroy(
  36. struct GumboInternalParser* parser, GumboVector* vector) {
  37. if (vector->capacity > 0) {
  38. gumbo_parser_deallocate(parser, vector->data);
  39. }
  40. }
  41. static void enlarge_vector_if_full(
  42. struct GumboInternalParser* parser, GumboVector* vector) {
  43. if (vector->length >= vector->capacity) {
  44. if (vector->capacity) {
  45. size_t old_num_bytes = sizeof(void*) * vector->capacity;
  46. vector->capacity *= 2;
  47. size_t num_bytes = sizeof(void*) * vector->capacity;
  48. void** temp = gumbo_parser_allocate(parser, num_bytes);
  49. memcpy(temp, vector->data, old_num_bytes);
  50. gumbo_parser_deallocate(parser, vector->data);
  51. vector->data = temp;
  52. } else {
  53. // 0-capacity vector; no previous array to deallocate.
  54. vector->capacity = 2;
  55. vector->data =
  56. gumbo_parser_allocate(parser, sizeof(void*) * vector->capacity);
  57. }
  58. }
  59. }
  60. void gumbo_vector_add(
  61. struct GumboInternalParser* parser, void* element, GumboVector* vector) {
  62. enlarge_vector_if_full(parser, vector);
  63. assert(vector->data);
  64. assert(vector->length < vector->capacity);
  65. vector->data[vector->length++] = element;
  66. }
  67. void* gumbo_vector_pop(
  68. struct GumboInternalParser* parser, GumboVector* vector) {
  69. if (vector->length == 0) {
  70. return NULL;
  71. }
  72. return vector->data[--vector->length];
  73. }
  74. int gumbo_vector_index_of(GumboVector* vector, const void* element) {
  75. for (unsigned int i = 0; i < vector->length; ++i) {
  76. if (vector->data[i] == element) {
  77. return i;
  78. }
  79. }
  80. return -1;
  81. }
  82. void gumbo_vector_insert_at(struct GumboInternalParser* parser, void* element,
  83. unsigned int index, GumboVector* vector) {
  84. assert(index >= 0);
  85. assert(index <= vector->length);
  86. enlarge_vector_if_full(parser, vector);
  87. ++vector->length;
  88. memmove(&vector->data[index + 1], &vector->data[index],
  89. sizeof(void*) * (vector->length - index - 1));
  90. vector->data[index] = element;
  91. }
  92. void gumbo_vector_remove(
  93. struct GumboInternalParser* parser, void* node, GumboVector* vector) {
  94. int index = gumbo_vector_index_of(vector, node);
  95. if (index == -1) {
  96. return;
  97. }
  98. gumbo_vector_remove_at(parser, index, vector);
  99. }
  100. void* gumbo_vector_remove_at(struct GumboInternalParser* parser,
  101. unsigned int index, GumboVector* vector) {
  102. assert(index >= 0);
  103. assert(index < vector->length);
  104. void* result = vector->data[index];
  105. memmove(&vector->data[index], &vector->data[index + 1],
  106. sizeof(void*) * (vector->length - index - 1));
  107. --vector->length;
  108. return result;
  109. }