• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * *****************************************************************************
3  *
4  * Copyright (c) 2018-2019 Gavin D. Howard and contributors.
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * * Redistributions of source code must retain the above copyright notice, this
12  *   list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright notice,
15  *   this list of conditions and the following disclaimer in the documentation
16  *   and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * *****************************************************************************
31  *
32  * Code to manipulate vectors (resizable arrays).
33  *
34  */
35 
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include <status.h>
41 #include <vector.h>
42 #include <lang.h>
43 #include <vm.h>
44 
bc_vec_grow(BcVec * restrict v,size_t n)45 static void bc_vec_grow(BcVec *restrict v, size_t n) {
46 
47 	size_t len, cap = v->cap;
48 
49 	len = bc_vm_growSize(v->len, n);
50 
51 	while (cap < len) cap = bc_vm_growSize(cap, cap);
52 
53 	v->v = bc_vm_realloc(v->v, bc_vm_arraySize(cap, v->size));
54 	v->cap = cap;
55 }
56 
bc_vec_init(BcVec * restrict v,size_t esize,BcVecFree dtor)57 void bc_vec_init(BcVec *restrict v, size_t esize, BcVecFree dtor) {
58 	assert(v != NULL && esize);
59 	v->size = esize;
60 	v->cap = BC_VEC_START_CAP;
61 	v->len = 0;
62 	v->dtor = dtor;
63 	v->v = bc_vm_malloc(bc_vm_arraySize(BC_VEC_START_CAP, esize));
64 }
65 
bc_vec_expand(BcVec * restrict v,size_t req)66 void bc_vec_expand(BcVec *restrict v, size_t req) {
67 	assert(v != NULL);
68 	if (v->cap < req) {
69 		v->v = bc_vm_realloc(v->v, bc_vm_arraySize(req, v->size));
70 		v->cap = req;
71 	}
72 }
73 
bc_vec_npop(BcVec * restrict v,size_t n)74 void bc_vec_npop(BcVec *restrict v, size_t n) {
75 	assert(v != NULL && n <= v->len);
76 	if (v->dtor == NULL) v->len -= n;
77 	else {
78 		size_t len = v->len - n;
79 		while (v->len > len) v->dtor(v->v + (v->size * --v->len));
80 	}
81 }
82 
bc_vec_npush(BcVec * restrict v,size_t n,const void * data)83 void bc_vec_npush(BcVec *restrict v, size_t n, const void *data) {
84 	assert(v != NULL && data != NULL);
85 	if (v->len + n > v->cap) bc_vec_grow(v, n);
86 	memcpy(v->v + (v->size * v->len), data, v->size * n);
87 	v->len += n;
88 }
89 
bc_vec_push(BcVec * restrict v,const void * data)90 void bc_vec_push(BcVec *restrict v, const void *data) {
91 	bc_vec_npush(v, 1, data);
92 }
93 
bc_vec_pushByte(BcVec * restrict v,uchar data)94 void bc_vec_pushByte(BcVec *restrict v, uchar data) {
95 	assert(v != NULL && v->size == sizeof(uchar));
96 	bc_vec_push(v, &data);
97 }
98 
bc_vec_pushIndex(BcVec * restrict v,size_t idx)99 void bc_vec_pushIndex(BcVec *restrict v, size_t idx) {
100 
101 	uchar amt, nums[sizeof(size_t)];
102 
103 	assert(v != NULL);
104 	assert(v->size == sizeof(uchar));
105 
106 	for (amt = 0; idx; ++amt) {
107 		nums[amt] = (uchar) idx;
108 		idx &= ((size_t) ~(UCHAR_MAX));
109 		idx >>= sizeof(uchar) * CHAR_BIT;
110 	}
111 
112 	bc_vec_push(v, &amt);
113 	bc_vec_npush(v, amt, nums);
114 }
115 
bc_vec_pushAt(BcVec * restrict v,const void * data,size_t idx)116 static void bc_vec_pushAt(BcVec *restrict v, const void *data, size_t idx) {
117 
118 	assert(v != NULL && data != NULL && idx <= v->len);
119 
120 	if (idx == v->len) bc_vec_push(v, data);
121 	else {
122 
123 		char *ptr;
124 
125 		if (v->len == v->cap) bc_vec_grow(v, 1);
126 
127 		ptr = v->v + v->size * idx;
128 
129 		memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
130 		memmove(ptr, data, v->size);
131 	}
132 }
133 
bc_vec_string(BcVec * restrict v,size_t len,const char * restrict str)134 void bc_vec_string(BcVec *restrict v, size_t len, const char *restrict str) {
135 
136 	assert(v != NULL && v->size == sizeof(char));
137 	assert(!v->len || !v->v[v->len - 1]);
138 	assert(v->v != str);
139 
140 	bc_vec_npop(v, v->len);
141 	bc_vec_expand(v, bc_vm_growSize(len, 1));
142 	memcpy(v->v, str, len);
143 	v->len = len;
144 
145 	bc_vec_pushByte(v, '\0');
146 }
147 
bc_vec_concat(BcVec * restrict v,const char * restrict str)148 void bc_vec_concat(BcVec *restrict v, const char *restrict str) {
149 
150 	assert(v != NULL && v->size == sizeof(char));
151 	assert(!v->len || !v->v[v->len - 1]);
152 	assert(v->v != str);
153 
154 	if (v->len) bc_vec_pop(v);
155 
156 	bc_vec_npush(v, strlen(str) + 1, str);
157 }
158 
bc_vec_empty(BcVec * restrict v)159 void bc_vec_empty(BcVec *restrict v) {
160 	assert(v != NULL && v->size == sizeof(char));
161 	bc_vec_npop(v, v->len);
162 	bc_vec_pushByte(v, '\0');
163 }
164 
165 #if BC_ENABLE_HISTORY
bc_vec_popAt(BcVec * restrict v,size_t idx)166 void bc_vec_popAt(BcVec *restrict v, size_t idx) {
167 
168 	char* ptr, *data;
169 
170 	assert(v != NULL);
171 	assert(idx + 1 < v->len);
172 
173 	ptr = bc_vec_item(v, idx);
174 	data = bc_vec_item(v, idx + 1);
175 
176 	if (v->dtor != NULL) v->dtor(ptr);
177 
178 	v->len -= 1;
179 	memmove(ptr, data, v->len * v->size);
180 }
181 
bc_vec_replaceAt(BcVec * restrict v,size_t idx,const void * data)182 void bc_vec_replaceAt(BcVec *restrict v, size_t idx, const void *data) {
183 
184 	char *ptr;
185 
186 	assert(v != NULL);
187 
188 	ptr = bc_vec_item(v, idx);
189 
190 	if (v->dtor != NULL) v->dtor(ptr);
191 	memcpy(ptr, data, v->size);
192 }
193 #endif // BC_ENABLE_HISTORY
194 
bc_vec_item(const BcVec * restrict v,size_t idx)195 void* bc_vec_item(const BcVec *restrict v, size_t idx) {
196 	assert(v != NULL && v->len && idx < v->len);
197 	return v->v + v->size * idx;
198 }
199 
bc_vec_item_rev(const BcVec * restrict v,size_t idx)200 void* bc_vec_item_rev(const BcVec *restrict v, size_t idx) {
201 	assert(v != NULL && v->len && idx < v->len);
202 	return v->v + v->size * (v->len - idx - 1);
203 }
204 
bc_vec_free(void * vec)205 void bc_vec_free(void *vec) {
206 	BcVec *v = (BcVec*) vec;
207 	bc_vec_npop(v, v->len);
208 	free(v->v);
209 }
210 
bc_map_find(const BcVec * restrict v,const BcId * restrict ptr)211 static size_t bc_map_find(const BcVec *restrict v, const BcId *restrict ptr) {
212 
213 	size_t low = 0, high = v->len;
214 
215 	while (low < high) {
216 
217 		size_t mid = (low + high) / 2;
218 		BcId *id = bc_vec_item(v, mid);
219 		int result = bc_id_cmp(ptr, id);
220 
221 		if (!result) return mid;
222 		else if (result < 0) high = mid;
223 		else low = mid + 1;
224 	}
225 
226 	return low;
227 }
228 
bc_map_insert(BcVec * restrict v,const BcId * restrict ptr,size_t * restrict i)229 bool bc_map_insert(BcVec *restrict v, const BcId *restrict ptr,
230                    size_t *restrict i)
231 {
232 	assert(v != NULL && ptr != NULL && i != NULL);
233 
234 	*i = bc_map_find(v, ptr);
235 
236 	assert(*i <= v->len);
237 
238 	if (*i == v->len) bc_vec_push(v, ptr);
239 	else if (!bc_id_cmp(ptr, bc_vec_item(v, *i))) return false;
240 	else bc_vec_pushAt(v, ptr, *i);
241 
242 	return true;
243 }
244 
bc_map_index(const BcVec * restrict v,const BcId * restrict ptr)245 size_t bc_map_index(const BcVec *restrict v, const BcId *restrict ptr) {
246 
247 	size_t i;
248 
249 	assert(v != NULL && ptr != NULL);
250 
251 	i = bc_map_find(v, ptr);
252 
253 	if (i >= v->len) return BC_VEC_INVALID_IDX;
254 
255 	return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
256 }
257