all repos — mgba @ ca6b2e7b09c66ad14db5977a68c428f7ee4e7f7d

mGBA Game Boy Advance Emulator

src/util/vector.h (view raw)

 1/* Copyright (c) 2013-2015 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#ifndef VECTOR_H
 7#define VECTOR_H
 8
 9#include "util/common.h"
10
11#define DECLARE_VECTOR(NAME, TYPE) \
12	struct NAME { \
13		TYPE* vector; \
14		size_t size; \
15		size_t capacity; \
16	}; \
17	void NAME ## Init(struct NAME* vector, size_t capacity); \
18	void NAME ## Deinit(struct NAME* vector); \
19	TYPE* NAME ## GetPointer(struct NAME* vector, size_t location); \
20	TYPE* NAME ## Append(struct NAME* vector); \
21	void NAME ## Clear(struct NAME* vector); \
22	void NAME ## Resize(struct NAME* vector, ssize_t change); \
23	void NAME ## Shift(struct NAME* vector, size_t location, size_t difference); \
24	void NAME ## Unshift(struct NAME* vector, size_t location, size_t difference); \
25	void NAME ## EnsureCapacity(struct NAME* vector, size_t capacity); \
26	size_t NAME ## Size(const struct NAME* vector); \
27
28#define DEFINE_VECTOR(NAME, TYPE) \
29	void NAME ## Init(struct NAME* vector, size_t capacity) { \
30		vector->size = 0; \
31		if (capacity == 0) { \
32			capacity = 4; \
33		} \
34		vector->capacity = capacity; \
35		vector->vector = malloc(sizeof(TYPE) * capacity); \
36	} \
37	void NAME ## Deinit(struct NAME* vector) { \
38		free(vector->vector); \
39		vector->vector = 0; \
40		vector->capacity = 0; \
41	} \
42	TYPE* NAME ## GetPointer(struct NAME* vector, size_t location) { \
43		return &vector->vector[location]; \
44	} \
45	TYPE* NAME ## Append(struct NAME* vector) { \
46		NAME ## Resize(vector, 1); \
47		return &vector->vector[vector->size - 1]; \
48	} \
49	void NAME ## Resize(struct NAME* vector, ssize_t change) { \
50		if (change > 0) { \
51			NAME ## EnsureCapacity(vector, vector->size + change); \
52		} \
53		vector->size += change; \
54	} \
55	void NAME ## Clear(struct NAME* vector) { \
56		vector->size = 0; \
57	} \
58	void NAME ## EnsureCapacity(struct NAME* vector, size_t capacity) { \
59		if (capacity <= vector->capacity) { \
60			return; \
61		} \
62		while (capacity > vector->capacity) { \
63			vector->capacity <<= 1; \
64		} \
65		vector->vector = realloc(vector->vector, vector->capacity * sizeof(TYPE)); \
66	} \
67	void NAME ## Shift(struct NAME* vector, size_t location, size_t difference) { \
68		memmove(&vector->vector[location], &vector->vector[location + difference], (vector->size - location - difference) * sizeof(TYPE)); \
69		vector->size -= difference; \
70	} \
71	void NAME ## Unshift(struct NAME* vector, size_t location, size_t difference) { \
72		NAME ## Resize(vector, difference); \
73		memmove(&vector->vector[location + difference], &vector->vector[location], (vector->size - location - difference) * sizeof(TYPE)); \
74	} \
75	size_t NAME ## Size(const struct NAME* vector) { \
76		return vector->size; \
77	} \
78
79#endif