list.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef __LIST_H
  2. #define __LIST_H
  3. //*****************************************************************************
  4. //
  5. // list.h
  6. //
  7. // the interface for a generic list structure. This list isn't a list in the
  8. // typical way; it can only hold one of any one data at a time (i.e. character
  9. // bob can only exist in the list once). I cannot think of any time in a MUD
  10. // setting where someone would exist in a list more than once, so this list was
  11. // designed with this in mind. If you want to use it otherwise, it should be
  12. // pretty easy to comment out the checks :)
  13. //
  14. //*****************************************************************************
  15. typedef struct list LIST;
  16. typedef struct list_iterator LIST_ITERATOR;
  17. //
  18. // Create a new list
  19. //
  20. LIST *newList();
  21. //
  22. // Delete an existing list
  23. //
  24. void deleteList(LIST *L);
  25. //
  26. // Delete the list. Also delete all of its contents with the function
  27. // that is passed in. The function should take one argument, and should be
  28. // compatible with the type of data in the list
  29. //
  30. void deleteListWith(LIST *L, void *func);
  31. //
  32. // Add an element to the list
  33. //
  34. void listPut(LIST *L, void *elem);
  35. //
  36. // Add an element to the end of the list
  37. //
  38. void listQueue(LIST *L, void *elem);
  39. //
  40. // Return true if the element is in the list. False otherwise
  41. //
  42. int listIn(LIST *L, const void *elem);
  43. //
  44. // Remove all instances of the elem from the list. Return
  45. // true if successful, and false otherwise.
  46. //
  47. int listRemove(LIST *L, const void *elem);
  48. //
  49. // Remove the element in the list at the specified place,
  50. // and return it
  51. //
  52. void *listRemoveNum(LIST *L, unsigned int num);
  53. //
  54. // remove the first element in the list, and return it
  55. //
  56. void *listPop(LIST *L);
  57. //
  58. // add the item to the head of the list
  59. //
  60. void listPush(LIST *L, void *elem);
  61. //
  62. // How many elements does the list have?
  63. //
  64. int listSize(LIST *L);
  65. //
  66. // Is the list empty?
  67. //
  68. int isListEmpty(LIST *L);
  69. //
  70. // get the element with the specific number
  71. //
  72. void *listGet(LIST *L, unsigned int num);
  73. //
  74. // Return the head of the list
  75. //
  76. void *listHead(LIST *L);
  77. //
  78. // Return the tail of the list
  79. //
  80. void *listTail(LIST *L);
  81. //
  82. // Put the element in the list in an ascending order, based on
  83. // what the comparator, func, tells us is the order.
  84. //
  85. void listPutWith(LIST *L, void *elem, void *func);
  86. //
  87. // get an element... use a function that is passed in to find the right element
  88. // the function should return a boolean value (TRUE if we have a match) and
  89. // only take one argument. cmpto must be the thing we are compared against
  90. // in func.
  91. //
  92. void *listGetWith(LIST *L, const void *cmpto, void *func);
  93. //
  94. // Similar to listGetWith, but removes the element permenantly. Returns
  95. // TRUE if the item was found and removed. FALSE other wise. Cmpto must
  96. // be the thing we are compared against in func.
  97. //
  98. void *listRemoveWith(LIST *L, const void *cmpto, void *func);
  99. //
  100. // Sorts the element in a list with the specified comparator function.
  101. // Func takes two arguments, and returns 0 if the two match. -1 is returned
  102. // if the first is less than the second, and 1 otherwise.
  103. //
  104. void listSortWith(LIST *L, void *func);
  105. //
  106. // Make a copy of the list. func is a function that takes one argument (the
  107. // data that is in the list) and returns a copy of that data.
  108. //
  109. LIST *listCopyWith(LIST *L, void *func);
  110. //
  111. // Parses out the first n arguments of the list, and assigns them to the
  112. // pointers supplied as arguments in the elipsis. Assumes n <= listSize(L)
  113. void listParse(LIST *L, int n, ...);
  114. //*****************************************************************************
  115. // list iterator function prototypes
  116. //*****************************************************************************
  117. // iterate across all the elements in a list
  118. #define ITERATE_LIST(val, it) \
  119. for(val = listIteratorCurrent(it); val != NULL; val = listIteratorNext(it))
  120. //
  121. // Create an iterator to go over the list
  122. //
  123. LIST_ITERATOR *newListIterator(LIST *L);
  124. //
  125. // Delete the list iterator (but not the contents)
  126. //
  127. void deleteListIterator(LIST_ITERATOR *I);
  128. //
  129. // Point the list iterator back at the head of the list
  130. //
  131. void listIteratorReset(LIST_ITERATOR *I);
  132. //
  133. // Skip to the next element in the list. Return the next element
  134. // if one exists, and NULL otherwise.
  135. //
  136. void *listIteratorNext(LIST_ITERATOR *I);
  137. //
  138. // return a pointer to the current list element
  139. //
  140. void *listIteratorCurrent(LIST_ITERATOR *I);
  141. #endif // __LIST_H