-
Notifications
You must be signed in to change notification settings - Fork 2
/
alist.h
138 lines (133 loc) · 3.12 KB
/
alist.h
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
/* alist.h: a CPP-based template implementation of array lists (resizable array) */
#ifndef ALIST_H_INCLUDED
#define ALIST_H_INCLUDED
#include <stdlib.h>
#define ALIST_PROTO(T, N) \
typedef struct N N; \
typedef int N##_iterator; \
N *N##_new(void); \
N *N##_new_cap(int size); \
void N##_free(N *s); \
int N##_size(const N *s); \
int N##_insert(N *s, T item, int pos); \
T N##_pop(N *s, int pos); \
T N##_get(const N *s, int pos); \
void N##_set(N *s, T item, int pos); \
int N##_resize(N *s, int size); \
N##_iterator N##_iterate(const N *s); \
int N##_next(const N *s, N##_iterator *iter); \
T N##_get_at(const N *s, N##_iterator iter); \
void N##_set_at(N *s, T item, N##_iterator iter); \
int N##_insert_at(N *s, T item, N##_iterator iter); \
T N##_pop_at(N *s, N##_iterator iter)
/* defines functions for an arraylist with elements of type T named N */
#define ALIST(T, N) \
struct N { int cap; int len; T *arr; }; \
const int N##_sizeof_element = sizeof(T); \
N *N##_new(void) \
{ \
return N##_new_cap(8); \
} \
N *N##_new_cap(int size) \
{ \
N *s; \
s = malloc(sizeof(struct N)); \
if (!s) return NULL; \
s->cap = size; \
s->len = 0; \
s->arr = malloc(size * N##_sizeof_element); \
if (!s->arr) { free(s); return NULL; } \
return s; \
} \
void N##_free(N *s) \
{ \
free(s->arr); \
free(s); \
} \
int N##_size(const N *s) \
{ \
return s->len; \
} \
int N##_insert(N *s, T item, int pos) \
{ \
T *temp; \
int i; \
if (s->len >= s->cap) { \
temp = realloc(s->arr, s->cap*1.5*N##_sizeof_element); \
if (!temp) return 0; \
s->arr = temp; \
s->cap *= 1.5; \
} \
if (pos >= 0 && pos != s->len) { \
for (i=s->len; i>pos; --i) { \
s->arr[i] = s->arr[i-1]; \
} \
} \
s->arr[pos<0?s->len:pos] = item; \
s->len++; \
return 1; \
} \
T N##_pop(N *s, int pos) \
{ \
T temp; \
int i; \
if (pos < 0) return s->arr[s->len-1]; \
temp = s->arr[pos]; \
for (i=pos; i<s->len-1; ++i) { \
s->arr[i] = s->arr[i+1]; \
} \
--s->len; \
return temp; \
} \
T N##_get(const N *s, int pos) \
{ \
return s->arr[pos<0||pos>=s->len?s->len-1:pos]; \
} \
void N##_set(N *s, T item, int pos) \
{ \
s->arr[pos<0||pos>=s->len?s->len-1:pos] = item; \
} \
int N##_resize(N *s, int size) \
{ \
T *temp; \
temp = realloc(s->arr, size*N##_sizeof_element); \
if (!temp) return 0; \
s->arr = temp; \
if (size < s->len) s->len = size; \
s->cap = size; \
return 1; \
} \
N##_iterator N##_iterate(const N *s) \
{ \
return -1; \
} \
int N##_next(const N *s, N##_iterator *iter) \
{ \
if (*iter < 0) { \
++*iter; \
return s->len > 0; \
} \
if (*iter >= s->len-1) { \
return 0; \
} \
++*iter; \
return 1; \
} \
T N##_get_at(const N *s, N##_iterator iter) \
{ \
return s->arr[iter]; \
} \
void N##_set_at(N *s, T item, N##_iterator iter) \
{ \
s->arr[iter] = item; \
} \
int N##_insert_at(N *s, T item, N##_iterator iter) \
{ \
return N##_insert(s, item, iter); \
} \
T N##_pop_at(N *s, N##_iterator iter) \
{ \
return N##_pop(s, iter); \
} \
struct N /* to avoid extra semicolon outside of a function */
#endif /* ifndef ALIST_H_INCLUDED */