all repos — mgba @ f3ea4caf84ab2292e65b10245d9343b5ba31cdaf

mGBA Game Boy Advance Emulator

include/mgba-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 <mgba-util/common.h>
 10
 11CXX_GUARD_START
 12
 13#ifdef vector
 14#undef vector
 15#endif
 16
 17#define DECLARE_VECTOR(NAME, TYPE) \
 18	struct NAME { \
 19		TYPE* vector; \
 20		size_t size; \
 21		size_t capacity; \
 22	}; \
 23	void NAME ## Init(struct NAME* vector, size_t capacity); \
 24	void NAME ## Deinit(struct NAME* vector); \
 25	TYPE* NAME ## GetPointer(struct NAME* vector, size_t location); \
 26	TYPE const* NAME ## GetConstPointer(const struct NAME* vector, size_t location); \
 27	TYPE* NAME ## Append(struct NAME* vector); \
 28	void NAME ## Clear(struct NAME* vector); \
 29	void NAME ## Resize(struct NAME* vector, ssize_t change); \
 30	void NAME ## Shift(struct NAME* vector, size_t location, size_t difference); \
 31	void NAME ## Unshift(struct NAME* vector, size_t location, size_t difference); \
 32	void NAME ## EnsureCapacity(struct NAME* vector, size_t capacity); \
 33	size_t NAME ## Size(const struct NAME* vector); \
 34	size_t NAME ## Index(const struct NAME* vector, const TYPE* member); \
 35	void NAME ## Copy(struct NAME* dest, const struct NAME* src);
 36
 37#define DEFINE_VECTOR(NAME, TYPE) \
 38	void NAME ## Init(struct NAME* vector, size_t capacity) { \
 39		vector->size = 0; \
 40		if (capacity == 0) { \
 41			capacity = 4; \
 42		} \
 43		vector->capacity = capacity; \
 44		vector->vector = malloc(sizeof(TYPE) * capacity); \
 45	} \
 46	void NAME ## Deinit(struct NAME* vector) { \
 47		free(vector->vector); \
 48		vector->vector = 0; \
 49		vector->capacity = 0; \
 50	} \
 51	TYPE* NAME ## GetPointer(struct NAME* vector, size_t location) { \
 52		return &vector->vector[location]; \
 53	} \
 54	TYPE const* NAME ## GetConstPointer(const struct NAME* vector, size_t location) { \
 55		return &vector->vector[location]; \
 56	} \
 57	TYPE* NAME ## Append(struct NAME* vector) { \
 58		NAME ## Resize(vector, 1); \
 59		return &vector->vector[vector->size - 1]; \
 60	} \
 61	void NAME ## Resize(struct NAME* vector, ssize_t change) { \
 62		if (change > 0) { \
 63			NAME ## EnsureCapacity(vector, vector->size + change); \
 64		} \
 65		vector->size += change; \
 66	} \
 67	void NAME ## Clear(struct NAME* vector) { \
 68		vector->size = 0; \
 69	} \
 70	void NAME ## EnsureCapacity(struct NAME* vector, size_t capacity) { \
 71		if (capacity <= vector->capacity) { \
 72			return; \
 73		} \
 74		while (capacity > vector->capacity) { \
 75			vector->capacity <<= 1; \
 76		} \
 77		vector->vector = realloc(vector->vector, vector->capacity * sizeof(TYPE)); \
 78	} \
 79	void NAME ## Shift(struct NAME* vector, size_t location, size_t difference) { \
 80		memmove(&vector->vector[location], &vector->vector[location + difference], (vector->size - location - difference) * sizeof(TYPE)); \
 81		vector->size -= difference; \
 82	} \
 83	void NAME ## Unshift(struct NAME* vector, size_t location, size_t difference) { \
 84		NAME ## Resize(vector, difference); \
 85		memmove(&vector->vector[location + difference], &vector->vector[location], (vector->size - location - difference) * sizeof(TYPE)); \
 86	} \
 87	size_t NAME ## Size(const struct NAME* vector) { \
 88		return vector->size; \
 89	} \
 90	size_t NAME ## Index(const struct NAME* vector, const TYPE* member) { \
 91		return member - (const TYPE*) vector->vector; \
 92	} \
 93	void NAME ## Copy(struct NAME* dest, const struct NAME* src) { \
 94		NAME ## EnsureCapacity(dest, src->size); \
 95		memcpy(dest->vector, src->vector, src->size * sizeof(TYPE)); \
 96		dest->size = src->size; \
 97	} \
 98
 99DECLARE_VECTOR(StringList, char*);
100
101CXX_GUARD_END
102
103#endif