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