list.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* $Id$
  2. *
  3. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  4. *
  5. * See the COPYING file for the terms of usage and distribution.
  6. */
  7. #ifndef __sd_list_h
  8. #define __sd_list_h
  9. /**
  10. * @file list.h @ingroup sd
  11. *
  12. * @brief Generic list object. It is implemented as an array of doubly-linked
  13. * lists of iterators. The index within the array is computed by a efficient
  14. * list function.
  15. */
  16. #include <stddef.h>
  17. #include <sd/defs.h>
  18. __SD_BEGIN_DECLS
  19. /**
  20. * This is the list object.
  21. */
  22. typedef struct __sd_list sd_list_t;
  23. struct __sd_list_iter {
  24. void* data;
  25. struct __sd_list* list;
  26. struct __sd_list_iter* __next;
  27. struct __sd_list_iter* __prev;
  28. int __foreach;
  29. };
  30. /**
  31. * This is the elementary container for storing data into the list object.
  32. */
  33. typedef struct __sd_list_iter sd_list_iter_t;
  34. /**
  35. * Signature of a "foreach" function.
  36. */
  37. typedef unsigned int (*sd_list_func_t)(void* a_data, void* a_userdata);
  38. /**
  39. * Creates a list.
  40. * @param a_capacity initial number of preallocated iterators
  41. * @return the list object.
  42. */
  43. extern sd_list_t* sd_list_new(size_t a_capacity);
  44. /**
  45. * Destroys the list object.
  46. * @todo need a function parameter to destroy list elements.
  47. */
  48. extern void sd_list_delete(sd_list_t* a_this);
  49. /**
  50. * Adds the given_data at the head of the list.
  51. */
  52. extern sd_list_iter_t* sd_list_prepend(sd_list_t* a_this, void* a_data);
  53. /**
  54. * Adds the given data at the tail of the list.
  55. */
  56. extern sd_list_iter_t* sd_list_append(sd_list_t* a_this, void* a_data);
  57. /**
  58. * Looks for the iterator associated to the given data in the list object.
  59. * @param a_data the data to find
  60. * @return a pointer to the found iterator or NULL.
  61. */
  62. extern sd_list_iter_t* sd_list_lookup(sd_list_t* a_this, void* a_data);
  63. /**
  64. * Looks for the iterator associated to the given data in the list object and
  65. * creates it if doesn't exist, using @c sd_list_add().
  66. * @param a_data the data to find/add
  67. * @return a pointer to the found iterator or NULL.
  68. */
  69. extern sd_list_iter_t* sd_list_lookadd(sd_list_t* a_this, void* a_data);
  70. /**
  71. * Adds the given data into the list object. If the data already exists,
  72. * the associated iterator is returned.
  73. * @warning the element is added at the begining of the list.
  74. * @param a_data the data to add
  75. * @return a pointer to the created or found iterator.
  76. */
  77. extern sd_list_iter_t* sd_list_add(sd_list_t* a_this, void* a_data);
  78. /**
  79. * Applies the given function to all list elements, starting from the
  80. * first one. As soon as the function returns a non-null value, the
  81. * given data is inserted in the list (before the element).
  82. * @param a_func the "sort" function.
  83. * @param a_data the data to add
  84. * @return a pointer to the created iterator.
  85. */
  86. extern sd_list_iter_t* sd_list_sortadd(sd_list_t* a_this,
  87. sd_list_func_t a_func,
  88. void* a_data);
  89. /**
  90. * Removes an iterator from the list object.
  91. * @param a_data the data associated to the iterator.
  92. */
  93. extern int sd_list_del(sd_list_t* a_this, void* a_data);
  94. /**
  95. * clears the list object.
  96. */
  97. extern void sd_list_clear(sd_list_t* a_this);
  98. /**
  99. * Calls \a a_func for each element of the list object, as long as \a a_func
  100. * returns 0.
  101. * @param a_func the "foreach" function.
  102. * @param a_data the user data passed to \a a_func.
  103. */
  104. extern void sd_list_foreach(sd_list_t* a_this, sd_list_func_t a_func,
  105. void* a_userdata);
  106. /**
  107. * Calls \a a_func for each element of the list object, as long as \a a_func
  108. * returns 0.
  109. * Same as sd_list_foreach but from tail to head of list.
  110. * @param a_func the "foreach" function.
  111. * @param a_data the user data passed to \a a_func.
  112. */
  113. extern void sd_list_rforeach(sd_list_t* a_this, sd_list_func_t a_func,
  114. void* a_userdata);
  115. /**
  116. * Gets the number of iterators.
  117. */
  118. extern size_t sd_list_get_nelem(sd_list_t* a_this);
  119. /**
  120. * Gets the iterator pointing to the first element of the list.
  121. */
  122. extern sd_list_iter_t* sd_list_begin(sd_list_t* a_this);
  123. /**
  124. * Gets the past-the-last-element iterator of the list.
  125. */
  126. extern sd_list_iter_t* sd_list_end(sd_list_t* a_this);
  127. /**
  128. * Gets the iterator pointing to the last element of the list.
  129. */
  130. extern sd_list_iter_t* sd_list_rbegin(sd_list_t* a_this);
  131. /**
  132. * Gets the before-the-first-element iterator of the list.
  133. */
  134. extern sd_list_iter_t* sd_list_rend(sd_list_t* a_this);
  135. /**
  136. * Gets a pointer to the next iterator.
  137. */
  138. extern sd_list_iter_t* sd_list_iter_next(sd_list_iter_t* a_this);
  139. /**
  140. * Gets a pointer to the previous iterator.
  141. */
  142. extern sd_list_iter_t* sd_list_iter_prev(sd_list_iter_t* a_this);
  143. /**
  144. * Deletes the iterator from the list.
  145. */
  146. extern void sd_list_iter_del(sd_list_iter_t* a_this);
  147. /**
  148. * Deletes the iterator from the list.
  149. */
  150. extern void sd_list_iter_del(sd_list_iter_t* a_this);
  151. /**
  152. * Creates a new iterator and inserts it before @a a_this.
  153. * @param a_data the data associated to the iterator.
  154. */
  155. extern sd_list_iter_t* sd_list_iter_insert(sd_list_iter_t* a_this,
  156. void* a_data);
  157. __SD_END_DECLS
  158. #endif