all repos — mgba @ 4c38f769565e8ddd7d3a8eef1a41975206c129a0

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 const* NAME ## GetConstPointer(const struct NAME* vector, size_t location); \
21	TYPE* NAME ## Append(struct NAME* vector); \
22	void NAME ## Clear(struct NAME* vector); \
23	void NAME ## Resize(struct NAME* vector, ssize_t change); \
24	void NAME ## Shift(struct NAME* vector, size_t location, size_t difference); \
25	void NAME ## Unshift(struct NAME* vector, size_t location, size_t difference); \
26	void NAME ## EnsureCapacity(struct NAME* vector, size_t capacity); \
27	size_t NAME ## Size(const struct NAME* vector); \
28	size_t NAME ## Index(const struct NAME* vector, const TYPE* member);
29
30#define DEFINE_VECTOR(NAME, TYPE) \
31	void NAME ## Init(struct NAME* vector, size_t capacity) { \
32		vector->size = 0; \
33		if (capacity == 0) { \
34			capacity = 4; \
35		} \
36		vector->capacity = capacity; \
37		vector->vector = malloc(sizeof(TYPE) * capacity); \
38	} \
39	void NAME ## Deinit(struct NAME* vector) { \
40		free(vector->vector); \
41		vector->vector = 0; \
42		vector->capacity = 0; \
43	} \
44	TYPE* NAME ## GetPointer(struct NAME* vector, size_t location) { \
45		return &vector->vector[location]; \
46	} \
47	TYPE const* NAME ## GetConstPointer(const struct NAME* vector, size_t location) { \
48		return &vector->vector[location]; \
49	} \
50	TYPE* NAME ## Append(struct NAME* vector) { \
51		NAME ## Resize(vector, 1); \
52		return &vector->vector[vector->size - 1]; \
53	} \
54	void NAME ## Resize(struct NAME* vector, ssize_t change) { \
55		if (change > 0) { \
56			NAME ## EnsureCapacity(vector, vector->size + change); \
57		} \
58		vector->size += change; \
59	} \
60	void NAME ## Clear(struct NAME* vector) { \
61		vector->size = 0; \
62	} \
63	void NAME ## EnsureCapacity(struct NAME* vector, size_t capacity) { \
64		if (capacity <= vector->capacity) { \
65			return; \
66		} \
67		while (capacity > vector->capacity) { \
68			vector->capacity <<= 1; \
69		} \
70		vector->vector = realloc(vector->vector, vector->capacity * sizeof(TYPE)); \
71	} \
72	void NAME ## Shift(struct NAME* vector, size_t location, size_t difference) { \
73		memmove(&vector->vector[location], &vector->vector[location + difference], (vector->size - location - difference) * sizeof(TYPE)); \
74		vector->size -= difference; \
75	} \
76	void NAME ## Unshift(struct NAME* vector, size_t location, size_t difference) { \
77		NAME ## Resize(vector, difference); \
78		memmove(&vector->vector[location + difference], &vector->vector[location], (vector->size - location - difference) * sizeof(TYPE)); \
79	} \
80	size_t NAME ## Size(const struct NAME* vector) { \
81		return vector->size; \
82	} \
83	size_t NAME ## Index(const struct NAME* vector, const TYPE* member) { \
84		return member - (const TYPE*) vector->vector; \
85	} \
86
87#endif