summaryrefslogtreecommitdiff
path: root/vector.h
blob: 9caf9d807bcb8cb8d2361831bc69e3009c9635a8 (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
/*
 * vector.h:
 * simple vectors
 *
 * Copyright (c) 2001 Chris Lightfoot. All rights reserved.
 *
 * $Id$
 *
 */

#ifndef __VECTOR_H_ /* include guard */
#define __VECTOR_H_

typedef union _item {
    void *v;
    long l;
} item;

#define _inline inline

static _inline item item_long(const long l) { item u; u.l = l; return u; }
static _inline item item_ptr(void *const v) { item u; u.v = v; return u; }

typedef struct _vector{
    item *ary;
    size_t n, n_used;
} *vector;

vector vector_new(void);
void vector_delete(vector);
void vector_delete_free(vector);

void  vector_push_back(vector, const item);
void  vector_pop_back(vector);
item vector_back(const vector);

item *vector_remove(vector, item *t);

void  vector_reallocate(vector, const size_t n);

/* A macro to iterate over a vector */
#define vector_iterate(_v, _t)  for ((_t) = (_v)->ary; (_t) < (_v)->ary + (_v)->n_used; ++(_t))

#endif /* __VECTOR_H_ */