2015-02-04 08:43:37 +00:00
|
|
|
/* Copyright (c) 2013-2015 Jeffrey Pfau
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#ifndef VECTOR_H
|
|
|
|
#define VECTOR_H
|
|
|
|
|
2016-12-31 01:00:22 +00:00
|
|
|
#include <mgba-util/common.h>
|
2015-02-04 08:43:37 +00:00
|
|
|
|
2016-12-27 05:01:55 +00:00
|
|
|
CXX_GUARD_START
|
|
|
|
|
2017-12-23 23:37:57 +00:00
|
|
|
#ifdef vector
|
|
|
|
#undef vector
|
|
|
|
#endif
|
|
|
|
|
2015-02-04 08:43:37 +00:00
|
|
|
#define DECLARE_VECTOR(NAME, TYPE) \
|
|
|
|
struct NAME { \
|
|
|
|
TYPE* vector; \
|
|
|
|
size_t size; \
|
|
|
|
size_t capacity; \
|
|
|
|
}; \
|
|
|
|
void NAME ## Init(struct NAME* vector, size_t capacity); \
|
|
|
|
void NAME ## Deinit(struct NAME* vector); \
|
|
|
|
TYPE* NAME ## GetPointer(struct NAME* vector, size_t location); \
|
2016-08-19 12:05:30 +00:00
|
|
|
TYPE const* NAME ## GetConstPointer(const struct NAME* vector, size_t location); \
|
2015-02-04 10:34:59 +00:00
|
|
|
TYPE* NAME ## Append(struct NAME* vector); \
|
2015-02-15 02:04:01 +00:00
|
|
|
void NAME ## Clear(struct NAME* vector); \
|
2015-02-04 08:43:37 +00:00
|
|
|
void NAME ## Resize(struct NAME* vector, ssize_t change); \
|
|
|
|
void NAME ## Shift(struct NAME* vector, size_t location, size_t difference); \
|
|
|
|
void NAME ## Unshift(struct NAME* vector, size_t location, size_t difference); \
|
|
|
|
void NAME ## EnsureCapacity(struct NAME* vector, size_t capacity); \
|
2015-02-15 06:45:40 +00:00
|
|
|
size_t NAME ## Size(const struct NAME* vector); \
|
2017-02-07 21:52:05 +00:00
|
|
|
size_t NAME ## Index(const struct NAME* vector, const TYPE* member); \
|
|
|
|
void NAME ## Copy(struct NAME* dest, const struct NAME* src);
|
2015-02-04 08:43:37 +00:00
|
|
|
|
2023-01-11 10:24:54 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
#define VECTOR_BOUNDS_CHECK(NAME, V, L)
|
|
|
|
#else
|
|
|
|
#define VECTOR_BOUNDS_CHECK(NAME, V, L) \
|
|
|
|
if ((L) >= (V)->size) { \
|
|
|
|
fprintf(stderr, "Vector type %s invalid access of index %" PRIuPTR " into vector of size %" PRIuPTR "\n", #NAME, (L), (V)->size); \
|
|
|
|
abort(); \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-04 08:43:37 +00:00
|
|
|
#define DEFINE_VECTOR(NAME, TYPE) \
|
|
|
|
void NAME ## Init(struct NAME* vector, size_t capacity) { \
|
|
|
|
vector->size = 0; \
|
|
|
|
if (capacity == 0) { \
|
|
|
|
capacity = 4; \
|
|
|
|
} \
|
|
|
|
vector->capacity = capacity; \
|
2020-07-08 01:02:34 +00:00
|
|
|
vector->vector = calloc(capacity, sizeof(TYPE)); \
|
2015-02-04 08:43:37 +00:00
|
|
|
} \
|
|
|
|
void NAME ## Deinit(struct NAME* vector) { \
|
|
|
|
free(vector->vector); \
|
|
|
|
vector->vector = 0; \
|
|
|
|
vector->capacity = 0; \
|
2020-07-30 04:36:34 +00:00
|
|
|
vector->size = 0; \
|
2015-02-04 08:43:37 +00:00
|
|
|
} \
|
|
|
|
TYPE* NAME ## GetPointer(struct NAME* vector, size_t location) { \
|
2023-01-11 10:24:54 +00:00
|
|
|
VECTOR_BOUNDS_CHECK(NAME, vector, location); \
|
2015-02-04 08:43:37 +00:00
|
|
|
return &vector->vector[location]; \
|
|
|
|
} \
|
2016-08-19 12:05:30 +00:00
|
|
|
TYPE const* NAME ## GetConstPointer(const struct NAME* vector, size_t location) { \
|
2023-01-11 10:24:54 +00:00
|
|
|
VECTOR_BOUNDS_CHECK(NAME, vector, location); \
|
2016-08-19 12:05:30 +00:00
|
|
|
return &vector->vector[location]; \
|
|
|
|
} \
|
2015-02-04 10:34:59 +00:00
|
|
|
TYPE* NAME ## Append(struct NAME* vector) { \
|
|
|
|
NAME ## Resize(vector, 1); \
|
|
|
|
return &vector->vector[vector->size - 1]; \
|
|
|
|
} \
|
2015-02-04 08:43:37 +00:00
|
|
|
void NAME ## Resize(struct NAME* vector, ssize_t change) { \
|
|
|
|
if (change > 0) { \
|
|
|
|
NAME ## EnsureCapacity(vector, vector->size + change); \
|
|
|
|
} \
|
|
|
|
vector->size += change; \
|
|
|
|
} \
|
2015-02-15 02:04:01 +00:00
|
|
|
void NAME ## Clear(struct NAME* vector) { \
|
|
|
|
vector->size = 0; \
|
|
|
|
} \
|
2015-02-04 08:43:37 +00:00
|
|
|
void NAME ## EnsureCapacity(struct NAME* vector, size_t capacity) { \
|
|
|
|
if (capacity <= vector->capacity) { \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
while (capacity > vector->capacity) { \
|
|
|
|
vector->capacity <<= 1; \
|
|
|
|
} \
|
|
|
|
vector->vector = realloc(vector->vector, vector->capacity * sizeof(TYPE)); \
|
|
|
|
} \
|
|
|
|
void NAME ## Shift(struct NAME* vector, size_t location, size_t difference) { \
|
2023-01-11 10:24:54 +00:00
|
|
|
VECTOR_BOUNDS_CHECK(NAME, vector, location); \
|
2015-02-04 08:43:37 +00:00
|
|
|
memmove(&vector->vector[location], &vector->vector[location + difference], (vector->size - location - difference) * sizeof(TYPE)); \
|
|
|
|
vector->size -= difference; \
|
|
|
|
} \
|
|
|
|
void NAME ## Unshift(struct NAME* vector, size_t location, size_t difference) { \
|
2023-01-11 10:24:54 +00:00
|
|
|
VECTOR_BOUNDS_CHECK(NAME, vector, location); \
|
2015-02-04 08:43:37 +00:00
|
|
|
NAME ## Resize(vector, difference); \
|
|
|
|
memmove(&vector->vector[location + difference], &vector->vector[location], (vector->size - location - difference) * sizeof(TYPE)); \
|
|
|
|
} \
|
2015-02-15 06:45:40 +00:00
|
|
|
size_t NAME ## Size(const struct NAME* vector) { \
|
2015-02-04 08:43:37 +00:00
|
|
|
return vector->size; \
|
|
|
|
} \
|
2015-03-29 11:38:13 +00:00
|
|
|
size_t NAME ## Index(const struct NAME* vector, const TYPE* member) { \
|
|
|
|
return member - (const TYPE*) vector->vector; \
|
|
|
|
} \
|
2017-02-07 21:52:05 +00:00
|
|
|
void NAME ## Copy(struct NAME* dest, const struct NAME* src) { \
|
|
|
|
NAME ## EnsureCapacity(dest, src->size); \
|
|
|
|
memcpy(dest->vector, src->vector, src->size * sizeof(TYPE)); \
|
|
|
|
dest->size = src->size; \
|
|
|
|
} \
|
2015-02-04 08:43:37 +00:00
|
|
|
|
2022-06-29 01:43:15 +00:00
|
|
|
DECLARE_VECTOR(IntList, int);
|
2023-01-11 07:13:58 +00:00
|
|
|
DECLARE_VECTOR(SInt8List, int8_t);
|
|
|
|
DECLARE_VECTOR(SInt16List, int16_t);
|
|
|
|
DECLARE_VECTOR(SInt32List, int32_t);
|
|
|
|
DECLARE_VECTOR(SIntPtrList, intptr_t);
|
|
|
|
DECLARE_VECTOR(UInt8List, uint8_t);
|
|
|
|
DECLARE_VECTOR(UInt16List, uint16_t);
|
|
|
|
DECLARE_VECTOR(UInt32List, uint32_t);
|
|
|
|
DECLARE_VECTOR(UIntPtrList, uintptr_t);
|
|
|
|
DECLARE_VECTOR(StringList, char*);
|
2017-04-12 04:41:53 +00:00
|
|
|
|
2016-12-27 05:01:55 +00:00
|
|
|
CXX_GUARD_END
|
|
|
|
|
2015-02-04 08:43:37 +00:00
|
|
|
#endif
|