queue.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2007, Novell Inc.
  3. *
  4. * This program is licensed under the BSD license, read LICENSE.BSD
  5. * for further information
  6. */
  7. /*
  8. * queue.h
  9. *
  10. */
  11. #ifndef LIBSOLV_QUEUE_H
  12. #define LIBSOLV_QUEUE_H
  13. #include "pooltypes.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef struct s_Queue {
  18. Id *elements; /* pointer to elements */
  19. int count; /* current number of elements in queue */
  20. Id *alloc; /* this is whats actually allocated, elements > alloc if shifted */
  21. int left; /* space left in alloc *after* elements+count */
  22. } Queue;
  23. extern void queue_alloc_one(Queue *q); /* internal */
  24. extern void queue_alloc_one_head(Queue *q); /* internal */
  25. /* clear queue */
  26. static inline void
  27. queue_empty(Queue *q)
  28. {
  29. if (q->alloc)
  30. {
  31. q->left += (q->elements - q->alloc) + q->count;
  32. q->elements = q->alloc;
  33. }
  34. else
  35. q->left += q->count;
  36. q->count = 0;
  37. }
  38. static inline Id
  39. queue_shift(Queue *q)
  40. {
  41. if (!q->count)
  42. return 0;
  43. q->count--;
  44. return *q->elements++;
  45. }
  46. static inline Id
  47. queue_pop(Queue *q)
  48. {
  49. if (!q->count)
  50. return 0;
  51. q->left++;
  52. return q->elements[--q->count];
  53. }
  54. static inline void
  55. queue_unshift(Queue *q, Id id)
  56. {
  57. if (!q->alloc || q->alloc == q->elements)
  58. queue_alloc_one_head(q);
  59. *--q->elements = id;
  60. q->count++;
  61. }
  62. static inline void
  63. queue_push(Queue *q, Id id)
  64. {
  65. if (!q->left)
  66. queue_alloc_one(q);
  67. q->elements[q->count++] = id;
  68. q->left--;
  69. }
  70. static inline void
  71. queue_pushunique(Queue *q, Id id)
  72. {
  73. int i;
  74. for (i = q->count; i > 0; )
  75. if (q->elements[--i] == id)
  76. return;
  77. queue_push(q, id);
  78. }
  79. static inline void
  80. queue_push2(Queue *q, Id id1, Id id2)
  81. {
  82. queue_push(q, id1);
  83. queue_push(q, id2);
  84. }
  85. static inline void
  86. queue_truncate(Queue *q, int n)
  87. {
  88. if (q->count > n)
  89. {
  90. q->left += q->count - n;
  91. q->count = n;
  92. }
  93. }
  94. extern void queue_init(Queue *q);
  95. extern void queue_init_buffer(Queue *q, Id *buf, int size);
  96. extern void queue_init_clone(Queue *target, const Queue *source);
  97. extern void queue_free(Queue *q);
  98. extern void queue_insert(Queue *q, int pos, Id id);
  99. extern void queue_insert2(Queue *q, int pos, Id id1, Id id2);
  100. extern void queue_insertn(Queue *q, int pos, int n, const Id *elements);
  101. extern void queue_delete(Queue *q, int pos);
  102. extern void queue_delete2(Queue *q, int pos);
  103. extern void queue_deleten(Queue *q, int pos, int n);
  104. extern void queue_prealloc(Queue *q, int n);
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif /* LIBSOLV_QUEUE_H */