lili.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef GORP_LILI_LILI_H
  2. #define GORP_LILI_LILI_H
  3. /*
  4. * Copyright 2010 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*
  37. * \file include/gorp/lili.h
  38. * \brief Linked list definitions.
  39. *
  40. * \verbatim
  41. * $Id$
  42. * \endverbatim
  43. */
  44. #include <compiler.h>
  45. #include <stdint.h>
  46. /*!
  47. * \addtogroup xgLili
  48. */
  49. /*@{*/
  50. /*! \name List attribute flags */
  51. /*@{*/
  52. /*! \brief Last in, first out queue. */
  53. #define LILI_F_LIFO 0x00
  54. /*! \brief First in, first out queue. */
  55. #define LILI_F_FIFO 0x01
  56. /*! \brief Sorted list. */
  57. #define LILI_F_SORT 0x02
  58. /*! \brief List order mask. */
  59. #define LILI_F_ORDER 0x03
  60. /*! \brief Allow unique items only. */
  61. #define LILI_F_UNIQUE 0x80
  62. /*@}*/
  63. /*! \brief Type of an item reference. */
  64. typedef intptr_t LILI_ITEMREF;
  65. typedef LILI_ITEMREF (*LiLiItemCreateFunc) (LILI_ITEMREF);
  66. typedef void (*LiLiItemDestroyFunc) (LILI_ITEMREF);
  67. typedef int (*LiLiItemCompareFunc) (LILI_ITEMREF, LILI_ITEMREF);
  68. /*! \brief Node object type. */
  69. typedef struct _LILI_NODE LILI_NODE;
  70. /*! \brief Node object structure. */
  71. struct _LILI_NODE {
  72. LILI_NODE *nod_nxt;
  73. LILI_NODE *nod_prv;
  74. LILI_ITEMREF nod_itm;
  75. };
  76. /*! \brief List object type. */
  77. typedef struct _LILI LILI;
  78. /*! \brief List object structure. */
  79. struct _LILI {
  80. LILI_NODE *lst_head;
  81. LILI_NODE *lst_tail;
  82. uint_fast8_t lst_flags;
  83. LiLiItemCreateFunc lst_icreate;
  84. LiLiItemDestroyFunc lst_idestroy;
  85. LiLiItemCompareFunc lst_icompare;
  86. };
  87. /*!
  88. * \brief Return true if the given list is a last in, first out queue.
  89. */
  90. #define LiLiIsLifo(list) (((list)->lst_flags & LILI_F_ORDER) == LILI_F_LIFO)
  91. /*!
  92. * \brief Return true if the given list is a first in, first out queue.
  93. */
  94. #define LiLiIsFifo(list) (((list)->lst_flags & LILI_F_ORDER) == LILI_F_FIFO)
  95. /*!
  96. * \brief Return true if the given list is sorted.
  97. */
  98. #define LiLiIsSorted(list) (((list)->lst_flags & LILI_F_ORDER) == LILI_F_SORT)
  99. /*!
  100. * \brief Return true if the given list is empty.
  101. */
  102. #define LiLiIsEmpty(list) ((list)->lst_head == NULL)
  103. /*!
  104. * \brief Return true if the given list has unique items only.
  105. */
  106. #define LiLiHasUniqueItems(list) (((list)->lst_flags & LILI_F_UNIQUE) == LILI_F_UNIQUE)
  107. /*!
  108. * \brief Return the first node object of a given list.
  109. */
  110. #define LiLiFirstNode(list) ((list)->lst_head)
  111. /*!
  112. * \brief Return the last node object of a given list.
  113. */
  114. #define LiLiLastNode(list) ((list)->lst_tail)
  115. /*!
  116. * \brief Return the next node object of a given node.
  117. */
  118. #define LiLiNextNode(node) ((node)->nod_nxt)
  119. /*!
  120. * \brief Return the previous node object of a given node.
  121. */
  122. #define LiLiPreviousNode(node) ((node)->nod_prv)
  123. /*!
  124. * \brief Return the item reference of a given node.
  125. */
  126. #define LiLiNodeItem(node) ((node)->nod_itm)
  127. /*@}*/
  128. extern LILI *LiLiCreate(uint8_t flags, LiLiItemCreateFunc cre, LiLiItemDestroyFunc des, LiLiItemCompareFunc cmp);
  129. extern void LiLiClean(LILI *list);
  130. extern void LiLiDestroy(LILI *list);
  131. extern int LiLiNodes(LILI *list);
  132. extern int LiLiPushItem(LILI *list, LILI_ITEMREF ref);
  133. extern int LiLiPopItem(LILI *list, LILI_ITEMREF *refp);
  134. extern LILI_NODE *LiLiFindItem(LILI *list, LILI_ITEMREF ref);
  135. extern LILI_NODE *LiLiLocateItem(LILI *list, LILI_ITEMREF ref);
  136. extern LILI_NODE *LiLiInsertItemAfterNode(LILI *list, LILI_NODE *node, LILI_ITEMREF ref);
  137. extern LILI_NODE *LiLiInsertItemBeforeNode(LILI *list, LILI_NODE *node, LILI_ITEMREF ref);
  138. extern void LiLiRemoveNode(LILI *list, LILI_NODE *node);
  139. extern LILI_ITEMREF LiLiCreateStringItemCopy(LILI_ITEMREF ref);
  140. extern void LiLiDestroyStringItemCopy(LILI_ITEMREF ref);
  141. extern int LiLiCompareStringItems(LILI_ITEMREF ref1, LILI_ITEMREF ref2);
  142. #endif