list.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright (c) 2012 Martin Sustrik All rights reserved.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. and/or sell copies of the Software, and to permit persons to whom
  8. the Software is furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. */
  19. #include <stddef.h>
  20. #include "list.h"
  21. #include "err.h"
  22. #include "attr.h"
  23. void nn_list_init (struct nn_list *self)
  24. {
  25. self->first = NULL;
  26. self->last = NULL;
  27. }
  28. void nn_list_term (struct nn_list *self)
  29. {
  30. nn_assert (self->first == NULL);
  31. nn_assert (self->last == NULL);
  32. }
  33. int nn_list_empty (struct nn_list *self)
  34. {
  35. return self->first ? 0 : 1;
  36. }
  37. struct nn_list_item *nn_list_begin (struct nn_list *self)
  38. {
  39. return self->first;
  40. }
  41. struct nn_list_item *nn_list_end (NN_UNUSED struct nn_list *self)
  42. {
  43. return NULL;
  44. }
  45. struct nn_list_item *nn_list_prev (struct nn_list *self,
  46. struct nn_list_item *it)
  47. {
  48. if (!it)
  49. return self->last;
  50. nn_assert (it->prev != NN_LIST_NOTINLIST);
  51. return it->prev;
  52. }
  53. struct nn_list_item *nn_list_next (NN_UNUSED struct nn_list *self,
  54. struct nn_list_item *it)
  55. {
  56. nn_assert (it->next != NN_LIST_NOTINLIST);
  57. return it->next;
  58. }
  59. void nn_list_insert (struct nn_list *self, struct nn_list_item *item,
  60. struct nn_list_item *it)
  61. {
  62. nn_assert (!nn_list_item_isinlist (item));
  63. item->prev = it ? it->prev : self->last;
  64. item->next = it;
  65. if (item->prev)
  66. item->prev->next = item;
  67. if (item->next)
  68. item->next->prev = item;
  69. if (!self->first || self->first == it)
  70. self->first = item;
  71. if (!it)
  72. self->last = item;
  73. }
  74. struct nn_list_item *nn_list_erase (struct nn_list *self,
  75. struct nn_list_item *item)
  76. {
  77. struct nn_list_item *next;
  78. nn_assert (nn_list_item_isinlist (item));
  79. if (item->prev)
  80. item->prev->next = item->next;
  81. else
  82. self->first = item->next;
  83. if (item->next)
  84. item->next->prev = item->prev;
  85. else
  86. self->last = item->prev;
  87. next = item->next;
  88. item->prev = NN_LIST_NOTINLIST;
  89. item->next = NN_LIST_NOTINLIST;
  90. return next;
  91. }
  92. void nn_list_item_init (struct nn_list_item *self)
  93. {
  94. self->prev = NN_LIST_NOTINLIST;
  95. self->next = NN_LIST_NOTINLIST;
  96. }
  97. void nn_list_item_term (struct nn_list_item *self)
  98. {
  99. nn_assert (!nn_list_item_isinlist (self));
  100. }
  101. int nn_list_item_isinlist (struct nn_list_item *self)
  102. {
  103. return self->prev == NN_LIST_NOTINLIST ? 0 : 1;
  104. }