comparison mupdf-source/thirdparty/gumbo-parser/src/vector.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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: jdtang@google.com (Jonathan Tang)
16
17 #include "vector.h"
18
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <strings.h>
23
24 #include "util.h"
25
26 struct GumboInternalParser;
27
28 const GumboVector kGumboEmptyVector = {NULL, 0, 0};
29
30 void gumbo_vector_init(struct GumboInternalParser* parser,
31 size_t initial_capacity, GumboVector* vector) {
32 vector->length = 0;
33 vector->capacity = initial_capacity;
34 if (initial_capacity > 0) {
35 vector->data =
36 gumbo_parser_allocate(parser, sizeof(void*) * initial_capacity);
37 } else {
38 vector->data = NULL;
39 }
40 }
41
42 void gumbo_vector_destroy(
43 struct GumboInternalParser* parser, GumboVector* vector) {
44 if (vector->capacity > 0) {
45 gumbo_parser_deallocate(parser, vector->data);
46 }
47 }
48
49 static void enlarge_vector_if_full(
50 struct GumboInternalParser* parser, GumboVector* vector) {
51 if (vector->length >= vector->capacity) {
52 if (vector->capacity) {
53 size_t old_num_bytes = sizeof(void*) * vector->capacity;
54 vector->capacity *= 2;
55 size_t num_bytes = sizeof(void*) * vector->capacity;
56 void** temp = gumbo_parser_allocate(parser, num_bytes);
57 memcpy(temp, vector->data, old_num_bytes);
58 gumbo_parser_deallocate(parser, vector->data);
59 vector->data = temp;
60 } else {
61 // 0-capacity vector; no previous array to deallocate.
62 vector->capacity = 2;
63 vector->data =
64 gumbo_parser_allocate(parser, sizeof(void*) * vector->capacity);
65 }
66 }
67 }
68
69 void gumbo_vector_add(
70 struct GumboInternalParser* parser, void* element, GumboVector* vector) {
71 enlarge_vector_if_full(parser, vector);
72 assert(vector->data);
73 assert(vector->length < vector->capacity);
74 vector->data[vector->length++] = element;
75 }
76
77 void* gumbo_vector_pop(
78 struct GumboInternalParser* parser, GumboVector* vector) {
79 if (vector->length == 0) {
80 return NULL;
81 }
82 return vector->data[--vector->length];
83 }
84
85 int gumbo_vector_index_of(GumboVector* vector, const void* element) {
86 for (unsigned int i = 0; i < vector->length; ++i) {
87 if (vector->data[i] == element) {
88 return i;
89 }
90 }
91 return -1;
92 }
93
94 void gumbo_vector_insert_at(struct GumboInternalParser* parser, void* element,
95 unsigned int index, GumboVector* vector) {
96 assert(index >= 0);
97 assert(index <= vector->length);
98 enlarge_vector_if_full(parser, vector);
99 ++vector->length;
100 memmove(&vector->data[index + 1], &vector->data[index],
101 sizeof(void*) * (vector->length - index - 1));
102 vector->data[index] = element;
103 }
104
105 void gumbo_vector_remove(
106 struct GumboInternalParser* parser, void* node, GumboVector* vector) {
107 int index = gumbo_vector_index_of(vector, node);
108 if (index == -1) {
109 return;
110 }
111 gumbo_vector_remove_at(parser, index, vector);
112 }
113
114 void* gumbo_vector_remove_at(struct GumboInternalParser* parser,
115 unsigned int index, GumboVector* vector) {
116 assert(index >= 0);
117 assert(index < vector->length);
118 void* result = vector->data[index];
119 memmove(&vector->data[index], &vector->data[index + 1],
120 sizeof(void*) * (vector->length - index - 1));
121 --vector->length;
122 return result;
123 }