summaryrefslogtreecommitdiff
path: root/support/texlab/crates/bibutils_sys/src/vplist.c
blob: 7f4e2927c9f5f7bb6eee9f13f5a3dfc4bce5888e (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * vplist.c
 *
 * Version: 1/9/2017
 *
 * Copyright (c) Chris Putnam 2011-2019
 *
 * Source code released under the GPL version 2
 *
 * Implements a simple managed array of pointers to void
 *
 */
#include <stdlib.h>
#include "vplist.h"

/* Do not use asserts in VPLIST_NOASSERT defined */
#ifdef VPLIST_NOASSERT
#define NDEBUG
#endif
#include <assert.h>

#define VPLIST_MINALLOC (20)

#define VPLIST_EXACT_SIZE  (0)
#define VPLIST_DOUBLE_SIZE (1)

void
vplist_init( vplist *vpl )
{
	assert( vpl );
	vpl->data = NULL;
	vpl->n = vpl->max = 0;
}

vplist *
vplist_new( void )
{
	vplist *vpl;
	vpl = ( vplist * ) malloc( sizeof( vplist ) );
	if ( vpl ) vplist_init( vpl );
	return vpl;
}

static inline int
vplist_alloc( vplist *vpl, vplist_index alloc )
{
	vpl->data = ( void ** ) malloc( sizeof( void * ) * alloc );
	if ( !vpl->data ) return VPLIST_MEMERR;

	vpl->max = alloc;
	vpl->n = 0;

	return VPLIST_OK;
}

static inline int
vplist_realloc( vplist *vpl, vplist_index alloc )
{
	void **more;

	more = ( void ** ) realloc( vpl->data, sizeof( void * ) * alloc );
	if ( !more ) return VPLIST_MEMERR;

	vpl->data = more;
	vpl->max  = alloc;

	return VPLIST_OK;
}

/* vplist_ensure_space( vpl, n, mode )
 *
 *    Makes sure that vplist can hold at least n members, allocating memory if required.
 *
 *    mode
 *       - Can either be VPLIST_DOUBLE_SIZE or VPLIST_EXACT_SIZE.
 *       - If VPLIST_EXACT_SIZE and current size < n, size will be exactly n.
 *       - If VPLIST_DOUBLE_SIZE and current size < n, size will be doubled (or VPLIST_MINALLOC 
 *         if the vplist is empty) or n, whichever is bigger.
 *
 *    Returns VPLIST_OK or VPLIST_MEMERR.
 */
static int
vplist_ensure_space( vplist *vpl, vplist_index n, unsigned char mode )
{
	vplist_index alloc = n;
	int status = VPLIST_OK;

	if ( vpl->max == 0 ) {
		if ( mode == VPLIST_DOUBLE_SIZE && alloc < VPLIST_MINALLOC ) alloc = VPLIST_MINALLOC;
		status = vplist_alloc( vpl, alloc );
	}

	else if ( vpl->max < n ) {
		if ( mode == VPLIST_DOUBLE_SIZE && alloc < 2 * vpl->max ) alloc = 2 * vpl->max;
		status = vplist_realloc( vpl, alloc );
	}

	return status;
}

int
vplist_copy( vplist *to, vplist *from )
{
	vplist_index i;
	int status;

	assert( to );
	assert( from );

	status = vplist_ensure_space( to, from->n, VPLIST_EXACT_SIZE );

	if ( status == VPLIST_OK ) {

		for ( i=0; i<from->n; ++i )
			to->data[i] = from->data[i];
		to->n = from->n;

	}

	return status;
}

int
vplist_fill( vplist *vpl, vplist_index n, void *v )
{
	vplist_index i;
	int status;

	assert( vpl );

	status = vplist_ensure_space( vpl, n, VPLIST_EXACT_SIZE );

	if ( status == VPLIST_OK ) {

		for ( i=0; i<n; ++i )
			vpl->data[i] = v;
		vpl->n = n;

	}

	return status;
}

int
vplist_add( vplist *vpl, void *v )
{
	int status;

	assert( vpl );

	status = vplist_ensure_space( vpl, vpl->n + 1, VPLIST_DOUBLE_SIZE );

	if ( status == VPLIST_OK ) {

		vpl->data[vpl->n] = v;
		vpl->n++;

	}

	return status;
}

int
vplist_insert_list( vplist *vpl, vplist_index pos, vplist *add )
{
	vplist_index i;
	int status;

	assert( vpl );
	assert( add );
	assert( pos <= vpl->n );

	/* nothing to do here */
	if ( add->n < 1 ) return VPLIST_OK;

	status = vplist_ensure_space( vpl, vpl->n + add->n, VPLIST_DOUBLE_SIZE );

	if ( status == VPLIST_OK ) {

		for ( i=vpl->n-1; i>=pos; --i )
			vpl->data[i+add->n] = vpl->data[i];

		for ( i=0; i<add->n; ++i )
			vpl->data[pos+i] = add->data[i];

		vpl->n += add->n;
	}

	return status;
}

int
vplist_append( vplist *vpl, vplist *add )
{
	vplist_index i;
	int status;

	assert( vpl );
	assert( add );

	status = vplist_ensure_space( vpl, vpl->n + add->n, VPLIST_DOUBLE_SIZE );

	if ( status == VPLIST_OK ) {

		for ( i=0; i<add->n; ++i )
			vpl->data[ vpl->n + i ] = add->data[i];

		vpl->n += add->n;

	}

	return status;
}

static void
vplist_freemembers( vplist *vpl, vplist_ptrfree vpf )
{
	vplist_index i;
	void *v;
	for ( i=0; i<vpl->n; ++i ) {
		v = vplist_get( vpl, i );
		if ( v ) (*vpf)( v );
	}
}

void
vplist_emptyfn( vplist *vpl, vplist_ptrfree vpf )
{
	assert( vpl );
	if ( vpf ) vplist_freemembers( vpl, vpf );
	vpl->n = 0;
}

void
vplist_empty( vplist *vpl )
{
	vplist_emptyfn( vpl, NULL );
}

void
vplist_freefn( vplist *vpl, vplist_ptrfree vpf )
{
	assert( vpl );
	if ( vpf ) vplist_freemembers( vpl, vpf );
	if ( vpl->data ) free( vpl->data );
	vplist_init( vpl );
}

void
vplist_free( vplist *vpl )
{
	vplist_freefn( vpl, NULL );
}

void
vplist_deletefn( vplist **vpl, vplist_ptrfree vpf )
{
	vplist_freefn( *vpl, vpf );
	free( *vpl );
	*vpl = NULL;
}

void
vplist_delete( vplist **vpl )
{
	vplist_deletefn( vpl, NULL );
}

static inline int
vplist_validindex( vplist *vpl, vplist_index n )
{
	if ( n < 0 || n >= vpl->n ) return 0;
	return 1;
}

void *
vplist_get( vplist *vpl, vplist_index n )
{
	assert( vpl );
	if ( !vplist_validindex( vpl, n ) ) return NULL;
	return vpl->data[ n ];
}

void
vplist_set( vplist *vpl, vplist_index n, void *v )
{
	assert( vpl );
	assert( vplist_validindex( vpl, n ) );
	vpl->data[ n ] = v;
}

int
vplist_find( vplist *vpl, void *v )
{
	vplist_index i;
	assert( vpl );
	for ( i=0; i<vpl->n; ++i )
		if ( vpl->data[i]==v ) return i;
	return -1;
}

void
vplist_swap( vplist *vpl, vplist_index n1, vplist_index n2 )
{
	void *tmp;

	assert( vpl );
	assert( vplist_validindex( vpl, n1 ) );
	assert( vplist_validindex( vpl, n2 ) );

	tmp           = vpl->data[n1];
	vpl->data[n1] = vpl->data[n2];
	vpl->data[n2] = tmp;
}

int
vplist_removefn( vplist *vpl, vplist_index n, vplist_ptrfree vpf )
{
	vplist_index i;

	assert( vpl );
	assert( vplist_validindex( vpl, n ) );

	if ( vpf ) (*vpf)( vplist_get( vpl, n ) );

	for ( i=n+1; i<vpl->n; ++i )
		vpl->data[ i-1 ] = vpl->data[ i ];
	vpl->n -= 1;

	return 1;
}

int
vplist_remove( vplist *vpl, vplist_index n )
{
	return vplist_removefn( vpl, n, NULL );
}

int
vplist_removevpfn( vplist *vpl, void *v, vplist_ptrfree vpf )
{
	vplist_index n;
	int count = 0;

	assert( vpl );

	do {
		n = vplist_find( vpl, v );
		if ( vplist_found( vpl, n ) ) {
			vplist_removefn( vpl, n, vpf );
			count++;
		}
	} while ( vplist_found( vpl, n ) );

	return count;
}

int
vplist_removevp( vplist *vpl, void *v )
{
	return vplist_removevpfn( vpl, v, NULL );
}

void
vplist_remove_rangefn( vplist *vpl, vplist_index start, vplist_index endplusone, vplist_ptrfree vpf )
{
	vplist_index i, n;

	assert( endplusone <= vpl->n );
	assert( endplusone > start );

	n = endplusone - start;
	if ( vpf ) {
		for ( i=start; i<endplusone; ++i )
			(*vpf)( vplist_get( vpl, i ) );
	}
	for ( i=endplusone; i<vpl->n; ++i ) {
		vpl->data[i-n] = vpl->data[i];
	}
	vpl->n -= n;
}

void
vplist_remove_range( vplist *vpl, vplist_index start, vplist_index endplusone )
{
	vplist_remove_rangefn( vpl, start, endplusone, NULL );
}