list.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*====================================================================*
  2. - Copyright (C) 2001 Leptonica. All rights reserved.
  3. -
  4. - Redistribution and use in source and binary forms, with or without
  5. - modification, are permitted provided that the following conditions
  6. - are met:
  7. - 1. Redistributions of source code must retain the above copyright
  8. - notice, this list of conditions and the following disclaimer.
  9. - 2. Redistributions in binary form must reproduce the above
  10. - copyright notice, this list of conditions and the following
  11. - disclaimer in the documentation and/or other materials
  12. - provided with the distribution.
  13. -
  14. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
  18. - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *====================================================================*/
  26. #ifndef LEPTONICA_LIST_H
  27. #define LEPTONICA_LIST_H
  28. /*
  29. * list.h
  30. *
  31. * Cell for double-linked lists
  32. *
  33. * This allows composition of a list of cells with
  34. * prev, next and data pointers. Generic data
  35. * structures hang on the list cell data pointers.
  36. *
  37. * The list is not circular because that would add much
  38. * complexity in traversing the list under general
  39. * conditions where list cells can be added and removed.
  40. * The only disadvantage of not having the head point to
  41. * the last cell is that the list must be traversed to
  42. * find its tail. However, this traversal is fast, and
  43. * the listRemoveFromTail() function updates the tail
  44. * so there is no searching overhead with repeated use.
  45. *
  46. * The list macros are used to run through a list, and their
  47. * use is encouraged. They are invoked, e.g., as
  48. *
  49. * DLLIST *head, *elem;
  50. * ...
  51. * L_BEGIN_LIST_FORWARD(head, elem)
  52. * <do something with elem and/or elem->data >
  53. * L_END_LIST
  54. *
  55. */
  56. struct DoubleLinkedList
  57. {
  58. struct DoubleLinkedList *prev;
  59. struct DoubleLinkedList *next;
  60. void *data;
  61. };
  62. typedef struct DoubleLinkedList DLLIST;
  63. /* Simple list traverse macros */
  64. #define L_BEGIN_LIST_FORWARD(head, element) \
  65. { \
  66. DLLIST *_leptvar_nextelem_; \
  67. for ((element) = (head); (element); (element) = _leptvar_nextelem_) { \
  68. _leptvar_nextelem_ = (element)->next;
  69. #define L_BEGIN_LIST_REVERSE(tail, element) \
  70. { \
  71. DLLIST *_leptvar_prevelem_; \
  72. for ((element) = (tail); (element); (element) = _leptvar_prevelem_) { \
  73. _leptvar_prevelem_ = (element)->prev;
  74. #define L_END_LIST }}
  75. #endif /* LEPTONICA_LIST_H */