2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

217 linhas
5.4 KiB

  1. #include <stdio.h>
  2. #include "IZ_list.h"
  3. void IZ_ListDoDeleteNode(IZ_ListNode* node) {
  4. if (!(node && node->list)) {
  5. // should we raise warnings here?
  6. return;
  7. }
  8. if (node->previous) {
  9. node->previous->next = node->next;
  10. } else {
  11. if (node->next) {
  12. node->next->previous = NULL;
  13. }
  14. node->list->root = node->next;
  15. }
  16. if (node->list->length > 0) {
  17. node->list->length -= 1;
  18. }
  19. IZ_free(node);
  20. }
  21. void IZ_ListDoAppendNode(IZ_List* list, void* node_value, IZ_ListNode** new_node_ref) {
  22. IZ_ListNode* new_node = IZ_malloc(sizeof(IZ_ListNode));
  23. new_node->value = node_value;
  24. new_node->next = NULL;
  25. new_node->list = list;
  26. new_node->list->length += 1;
  27. if (!(new_node->list->root)) {
  28. new_node->previous = NULL;
  29. new_node->list->root = new_node;
  30. if (new_node_ref) {
  31. *new_node_ref = new_node;
  32. }
  33. return;
  34. }
  35. IZ_ListNode** cursor_node = &new_node->list->root;
  36. while ((*cursor_node)->next) {
  37. cursor_node = &(*cursor_node)->next;
  38. }
  39. new_node->previous = *cursor_node;
  40. (*cursor_node)->next = new_node;
  41. if (new_node_ref) {
  42. *new_node_ref = new_node;
  43. }
  44. }
  45. #ifdef IZ_DEBUG
  46. void IZ_ListPrintNodeValues(IZ_List* list) {
  47. list->iterator = &list->root;
  48. u64 index = 0;
  49. printf("\nlist@%p\n", list);
  50. do {
  51. printf(" %lu@%p:%u\n", index, *list->iterator, *((unsigned int*)(*list->iterator)->value));
  52. index += 1;
  53. list->iterator = &(*list->iterator)->next;
  54. } while (*list->iterator);
  55. list->iterator = NULL;
  56. }
  57. #endif
  58. /**
  59. * Initializes a list.
  60. * @param list - The list to initialize.
  61. */
  62. void IZ_ListInitialize(IZ_List* list) {
  63. list->root = NULL;
  64. list->length = 0;
  65. list->iterator = NULL;
  66. }
  67. /**
  68. * Performs cleanup on a list.
  69. * @param list - The list to clean up.
  70. */
  71. void IZ_ListTeardown(IZ_List* list) {
  72. while (list->length > 0) {
  73. IZ_ListDoDeleteNode(list->root);
  74. }
  75. }
  76. /**
  77. * Appends a node to the end of the list.
  78. * @param list - The list to append to.
  79. * @param node_value - The value of the node to append.
  80. * @return Pointer to the newly created node.
  81. */
  82. void IZ_ListAppendNode(IZ_List* list, void* node_value, IZ_ListNode** new_node) {
  83. IZ_ListDoAppendNode(list, node_value, new_node);
  84. }
  85. /**
  86. * Deletes the first node in the list that matches the filter.
  87. * @param node - The node to delete.
  88. */
  89. void IZ_ListDeleteNode(IZ_ListNode* node) {
  90. IZ_ListDoDeleteNode(node);
  91. }
  92. /**
  93. * Finds the first node in the list that matches the filter.
  94. * @param list - The list to search.
  95. * @param filter - The filter to use to find the node.
  96. * @return Pointer to the node that matches the filter.
  97. * @see IZ_ListFindPredicate
  98. * @see IZ_ListFindAllNodes
  99. * @note This function will set the list's iterator to the node that was found. Ensure that the iterator is previously
  100. * set to an existing node in the list before calling this function to know where to begin the search.
  101. */
  102. void IZ_ListFindFirstNode(IZ_List* list, IZ_ListFindPredicate filter, IZ_ListNode** found_node) {
  103. if (!(list && list->root)) {
  104. return;
  105. }
  106. u64 index = 0;
  107. do {
  108. if (!filter(list->iterator, index, list)) {
  109. list->iterator = &((*list->iterator)->next);
  110. index += 1;
  111. continue;
  112. }
  113. *found_node = *list->iterator;
  114. return;
  115. } while (*list->iterator);
  116. }
  117. /**
  118. * Finds all nodes in the list that match the filter.
  119. * @param list - The list to search.
  120. * @param filter - The filter to use to find the node.
  121. * @param found_nodes - The list to append the found nodes to.
  122. * @return New list containing nodes that match the filter.
  123. * @see IZ_ListFindPredicate
  124. * @see IZ_ListFindFirstNode
  125. * @note This function will set the list's iterator to the node that was found. Ensure that the iterator is previously
  126. * set to an existing node in the list before calling this function to know where to begin the search.
  127. */
  128. void IZ_ListFindAllNodes(IZ_List* list, IZ_ListFindPredicate filter, IZ_List* found_nodes) {
  129. if (!(list && list->root)) {
  130. return;
  131. }
  132. list->iterator = &list->root;
  133. u64 index;
  134. for (index = 0; index < list->length; index += 1) {
  135. if (filter(list->iterator, index, list)) {
  136. IZ_ListDoAppendNode(found_nodes, (*list->iterator)->value, NULL);
  137. }
  138. list->iterator = &((*list->iterator)->next);
  139. }
  140. }
  141. /**
  142. * Inserts a node at the specified index.
  143. * @param list - The list to append to.
  144. * @param node_value - The value of the node to append.
  145. * @param dest_index - The index to insert the node at.
  146. * @return Pointer to the newly created node.
  147. */
  148. void IZ_ListInsertNodeAtIndex(IZ_List* list, void* node_value, u64 dest_index, IZ_ListNode** new_node_ref) {
  149. if (dest_index > list->length) {
  150. // to consumer: check your bounds first!
  151. return;
  152. }
  153. if (dest_index == list->length) {
  154. IZ_ListDoAppendNode(list, node_value, new_node_ref);
  155. return;
  156. }
  157. IZ_ListNode* new_node = IZ_malloc(sizeof(IZ_ListNode));
  158. new_node->value = node_value;
  159. new_node->list = list;
  160. if (!(new_node->list->root)) {
  161. new_node->previous = NULL;
  162. new_node->next = NULL;
  163. new_node->list->root = new_node;
  164. if (new_node_ref) {
  165. *new_node_ref = new_node;
  166. }
  167. return;
  168. }
  169. IZ_ListNode** cursor_node = NULL;
  170. u64 index;
  171. for (
  172. index = 0, cursor_node = &new_node->list->root;
  173. index < dest_index;
  174. index += 1, cursor_node = &((*cursor_node)->next)
  175. );
  176. new_node->next = *cursor_node;
  177. new_node->previous = (*cursor_node)->previous;
  178. if (dest_index == 0) {
  179. new_node->list->root = new_node;
  180. } else if (dest_index < list->length) {
  181. (*cursor_node)->previous->next = new_node;
  182. } else {
  183. (*cursor_node)->next = new_node;
  184. }
  185. new_node->list->length += 1;
  186. if (new_node_ref) {
  187. *new_node_ref = new_node;
  188. }
  189. }