lpushpop.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2010 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * \file gorp/list/lpushpop.c
  36. * \brief Linked list push and pop functions.
  37. *
  38. * \verbatim
  39. * $Id$
  40. * \endverbatim
  41. */
  42. #include <sys/nutdebug.h>
  43. #include <gorp/lili.h>
  44. /*!
  45. * \addtogroup xgLili
  46. */
  47. /*@{*/
  48. /*!
  49. * \brief Find the node's location by a given item.
  50. *
  51. * Like LiLiFindItem(), but returns the first node of which the item is
  52. * larger than the given item. This function is useful for sorted lists
  53. * only.
  54. *
  55. * \param list Pointer to a list object, obtained from a previous call
  56. * to LiLiCreate().
  57. * \param ref Reference of the item to search for.
  58. *
  59. * \return Pointer to the node, or NULL if no node was found.
  60. */
  61. LILI_NODE *LiLiLocateItem(LILI *list, LILI_ITEMREF ref)
  62. {
  63. LILI_NODE *node;
  64. /* Sanity checks. */
  65. NUTASSERT(list != NULL);
  66. for (node = LiLiFirstNode(list); node; node = LiLiNextNode(node)) {
  67. if (list->lst_icompare(LiLiNodeItem(node), ref) >= 0) {
  68. break;
  69. }
  70. }
  71. return node;
  72. }
  73. /*!
  74. * \brief Find the node of a given item.
  75. *
  76. * Like LiLiLocateItem(), but searches for an exact match. If the list
  77. * is unsorted, all nodes will be scanned.
  78. *
  79. * \param list Pointer to a list object, obtained from a previous call
  80. * to LiLiCreate().
  81. * \param ref Reference of the item to search for.
  82. *
  83. * \return Pointer to the node, or NULL if no node was found.
  84. */
  85. LILI_NODE *LiLiFindItem(LILI *list, LILI_ITEMREF ref)
  86. {
  87. LILI_NODE *node;
  88. int rc;
  89. /* Sanity checks. */
  90. NUTASSERT(list != NULL);
  91. for (node = LiLiFirstNode(list); node; node = LiLiNextNode(node)) {
  92. rc = list->lst_icompare(LiLiNodeItem(node), ref);
  93. if (rc == 0) {
  94. break;
  95. }
  96. if (rc > 0 && LiLiIsSorted(list)) {
  97. return NULL;
  98. }
  99. }
  100. return node;
  101. }
  102. /*!
  103. * \brief Add an item to the list.
  104. *
  105. * In most cases this function will be used by applications to add new
  106. * items to a list.
  107. *
  108. * If the attribute LILI_F_SORT has been set when creating the list,
  109. * then a node will be inserted before the first node, of which the
  110. * item is larger or equal than the given one.
  111. *
  112. * If the attribute LILI_F_UNIQUE has been set and if a node with
  113. * the same item is already a member of the list, then no new node
  114. * will be added. This adds significant overhead to LIFO and FIFO
  115. * queues.
  116. *
  117. * \param list Pointer to a list object, obtained from a previous call
  118. * to LiLiCreate().
  119. * \param ref The item's reference. If a function for creating an item
  120. * has been specified, if will be called before inserting a
  121. * new node.
  122. *
  123. * \return 0 if a new node had been successfully added or of the given
  124. * item already exists in a unique list. On errors, -1 is returned.
  125. */
  126. int LiLiPushItem(LILI *list, LILI_ITEMREF ref)
  127. {
  128. LILI_NODE *node;
  129. int rc;
  130. /* Sanity checks. */
  131. NUTASSERT(list != NULL);
  132. node = LiLiLastNode(list);
  133. if (node) {
  134. if (LiLiIsSorted(list)) {
  135. /* A sorted list, compare the new with the last item. */
  136. rc = list->lst_icompare(LiLiNodeItem(node), ref);
  137. if (rc <= 0) {
  138. /* The new item is greater or equal, so we should append
  139. ** it. But only, if either the items are not equal or if
  140. ** this is not a list of unique items only. */
  141. if (rc || !LiLiHasUniqueItems(list)) {
  142. rc = LiLiInsertItemAfterNode(list, node, ref) ? 0 : -1;
  143. }
  144. return rc;
  145. }
  146. /* The new item is lower than the last one in the list. Search
  147. ** the first node with an item greater or equal than the new one. */
  148. node = LiLiLocateItem(list, ref);
  149. /* If the list has unique items only, then ignore an equal item. */
  150. if (LiLiHasUniqueItems(list) && list->lst_icompare(LiLiNodeItem(node), ref) == 0) {
  151. return 0;
  152. }
  153. } else {
  154. /* Do not insert an equal item into a unique list. */
  155. if (LiLiHasUniqueItems(list) && LiLiFindItem(list, ref)) {
  156. return 0;
  157. }
  158. /* If not a sorted list, add new items in front. */
  159. node = LiLiFirstNode(list);
  160. }
  161. }
  162. /* Add a new node. */
  163. return LiLiInsertItemBeforeNode(list, node, ref) ? 0 : -1;
  164. }
  165. /*!
  166. * \brief Remove the next item from the list.
  167. *
  168. * \param list Pointer to a list object, obtained from a previous call
  169. * to LiLiCreate().
  170. * \param refp Pointer that receives the item's reference of the removed
  171. * node. If a copy of the item object has been created during
  172. * list insertion, then the caller is responsible for destroying
  173. * it.
  174. *
  175. * \return -1 if the list is empty, otherwise 0 is returned.
  176. */
  177. int LiLiPopItem(LILI *list, LILI_ITEMREF *refp)
  178. {
  179. LILI_NODE *node;
  180. /* Sanity checks. */
  181. NUTASSERT(list != NULL);
  182. /* Get the first node, check if the list is empty. */
  183. node = LiLiFirstNode(list);
  184. if (node) {
  185. /* List is not empty. However, if this is a FIFO queue, remove
  186. ** the last node instead. */
  187. if (LiLiIsFifo(list)) {
  188. node = LiLiLastNode(list);
  189. }
  190. /* Pass the item reference to the caller and remove the node. */
  191. *refp = LiLiNodeItem(node);
  192. LiLiRemoveNode(list, node);
  193. }
  194. return node ? 0 : -1;
  195. }
  196. /*@}*/