ijksdl_container.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2015 Bilibili
  3. * copyright (c) 2015 Zhang Rui <bbcallen@gmail.com>
  4. *
  5. * This file is part of ijkPlayer.
  6. *
  7. * ijkPlayer is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * ijkPlayer is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with ijkPlayer; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef IJKSDL__IJKSDL_CONTAINER_H
  22. #define IJKSDL__IJKSDL_CONTAINER_H
  23. #include <stdlib.h>
  24. typedef int isdl_error;
  25. /*
  26. * Array
  27. */
  28. typedef struct ISDL_Array
  29. {
  30. void **elements;
  31. size_t capacity;
  32. size_t size;
  33. } ISDL_Array;
  34. inline static isdl_error ISDL_Array__grow(ISDL_Array *array, size_t new_capacity)
  35. {
  36. assert(array);
  37. if (array->capacity >= new_capacity)
  38. return 0;
  39. void **new_elements = realloc(array->elements, sizeof(void *) * new_capacity);
  40. if (!new_elements)
  41. return -1;
  42. array->elements = new_elements;
  43. array->capacity = new_capacity;
  44. return 0;
  45. }
  46. inline static isdl_error ISDL_Array__init(ISDL_Array *array, size_t capacity)
  47. {
  48. assert(array);
  49. memset(array, 0, sizeof(ISDL_Array));
  50. if (ISDL_Array__grow(array, capacity))
  51. return -1;
  52. return 0;
  53. }
  54. inline static isdl_error ISDL_Array__push_back(ISDL_Array *array, void *val)
  55. {
  56. assert(array);
  57. if (array->size >= array->capacity) {
  58. if (ISDL_Array__grow(array, array->capacity * 2))
  59. return -1;
  60. }
  61. array->elements[array->size++] = val;
  62. return 0;
  63. }
  64. inline static void *ISDL_Array__pop_back(ISDL_Array *array)
  65. {
  66. assert(array);
  67. assert(array->size >= 1);
  68. return array->elements[--array->size];
  69. }
  70. inline static void ISDL_Array__clear(ISDL_Array *array)
  71. {
  72. assert(array);
  73. array->size = 0;
  74. }
  75. inline static void *ISDL_Array__at(ISDL_Array *array, int pos)
  76. {
  77. assert(array);
  78. assert(pos >= 0);
  79. assert(pos < array->size);
  80. return array->elements[pos];
  81. }
  82. inline static size_t ISDL_Array__size(ISDL_Array *array)
  83. {
  84. assert(array);
  85. return array->size;
  86. }
  87. inline static void **ISDL_Array__begin(ISDL_Array *array)
  88. {
  89. assert(array);
  90. return array->elements;
  91. }
  92. inline static void **ISDL_Array__end(ISDL_Array *array)
  93. {
  94. assert(array);
  95. return array->elements + array->size;
  96. }
  97. inline static void *ISDL_Array__back(ISDL_Array *array)
  98. {
  99. assert(array);
  100. assert(array->size >= 1);
  101. return array->elements[array->size - 1];
  102. }
  103. #endif