Array.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef SPINE_ARRAY_H
  30. #define SPINE_ARRAY_H
  31. #include <spine/dll.h>
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #define _SP_ARRAY_DECLARE_TYPE(name, itemType) \
  36. typedef struct name { int size; int capacity; itemType* items; } name; \
  37. SP_API name* name##_create(int initialCapacity); \
  38. SP_API void name##_dispose(name* self); \
  39. SP_API void name##_clear(name* self); \
  40. SP_API name* name##_setSize(name* self, int newSize); \
  41. SP_API void name##_ensureCapacity(name* self, int newCapacity); \
  42. SP_API void name##_add(name* self, itemType value); \
  43. SP_API void name##_addAll(name* self, name* other); \
  44. SP_API void name##_addAllValues(name* self, itemType* values, int offset, int count); \
  45. SP_API void name##_removeAt(name* self, int index); \
  46. SP_API int name##_contains(name* self, itemType value); \
  47. SP_API itemType name##_pop(name* self); \
  48. SP_API itemType name##_peek(name* self);
  49. #define _SP_ARRAY_IMPLEMENT_TYPE(name, itemType) \
  50. name* name##_create(int initialCapacity) { \
  51. name* array = CALLOC(name, 1); \
  52. array->size = 0; \
  53. array->capacity = initialCapacity; \
  54. array->items = CALLOC(itemType, initialCapacity); \
  55. return array; \
  56. } \
  57. void name##_dispose(name* self) { \
  58. FREE(self->items); \
  59. FREE(self); \
  60. } \
  61. void name##_clear(name* self) { \
  62. self->size = 0; \
  63. } \
  64. name* name##_setSize(name* self, int newSize) { \
  65. self->size = newSize; \
  66. if (self->capacity < newSize) { \
  67. self->capacity = MAX(8, (int)(self->size * 1.75f)); \
  68. self->items = REALLOC(self->items, itemType, self->capacity); \
  69. } \
  70. return self; \
  71. } \
  72. void name##_ensureCapacity(name* self, int newCapacity) { \
  73. if (self->capacity >= newCapacity) return; \
  74. self->capacity = newCapacity; \
  75. self->items = REALLOC(self->items, itemType, self->capacity); \
  76. } \
  77. void name##_add(name* self, itemType value) { \
  78. if (self->size == self->capacity) { \
  79. self->capacity = MAX(8, (int)(self->size * 1.75f)); \
  80. self->items = REALLOC(self->items, itemType, self->capacity); \
  81. } \
  82. self->items[self->size++] = value; \
  83. } \
  84. void name##_addAll(name* self, name* other) { \
  85. int i = 0; \
  86. for (; i < other->size; i++) { \
  87. name##_add(self, other->items[i]); \
  88. } \
  89. } \
  90. void name##_addAllValues(name* self, itemType* values, int offset, int count) { \
  91. int i = offset, n = offset + count; \
  92. for (; i < n; i++) { \
  93. name##_add(self, values[i]); \
  94. } \
  95. } \
  96. void name##_removeAt(name* self, int index) { \
  97. self->size--; \
  98. memmove(self->items + index, self->items + index + 1, sizeof(itemType) * (self->size - index)); \
  99. } \
  100. int name##_contains(name* self, itemType value) { \
  101. itemType* items = self->items; \
  102. int i, n; \
  103. for (i = 0, n = self->size; i < n; i++) { \
  104. if (items[i] == value) return -1; \
  105. } \
  106. return 0; \
  107. } \
  108. itemType name##_pop(name* self) { \
  109. itemType item = self->items[--self->size]; \
  110. return item; \
  111. } \
  112. itemType name##_peek(name* self) { \
  113. return self->items[self->size - 1]; \
  114. }
  115. _SP_ARRAY_DECLARE_TYPE(spFloatArray, float)
  116. _SP_ARRAY_DECLARE_TYPE(spIntArray, int)
  117. _SP_ARRAY_DECLARE_TYPE(spShortArray, short)
  118. _SP_ARRAY_DECLARE_TYPE(spUnsignedShortArray, unsigned short)
  119. _SP_ARRAY_DECLARE_TYPE(spArrayFloatArray, spFloatArray*)
  120. _SP_ARRAY_DECLARE_TYPE(spArrayShortArray, spShortArray*)
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. #endif /* SPINE_ARRAY_H */