-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraylist.h
More file actions
42 lines (34 loc) · 863 Bytes
/
arraylist.h
File metadata and controls
42 lines (34 loc) · 863 Bytes
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
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define INITIAL_SIZE 1024
typedef struct ArrayListBuf {
char* buff;
int capacity;
int N;
} ArrayListBuf;
void ArrayListBuf_init(struct ArrayListBuf* b) {
b->buff = (char*)malloc(INITIAL_SIZE);
b->capacity = INITIAL_SIZE;
b->N = 0;
}
void ArrayListBuf_free(struct ArrayListBuf* b) {
free(b->buff);
}
void ArrayListBuf_doubleCapacity(struct ArrayListBuf* b) {
char* newBuff = (char*)malloc(b->capacity*2);
memcpy(newBuff, b->buff, b->capacity);
free(b->buff);
b->buff = newBuff;
b->capacity *= 2;
}
void ArrayListBuf_push(struct ArrayListBuf* b, char* s, int len) {
while (b->N + len > b->capacity) {
ArrayListBuf_doubleCapacity(b);
}
memcpy(b->buff+b->N, s, len);
b->N += len;
}
#endif