gqueue.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * gqueue.c: Queue
  3. *
  4. * Author:
  5. * Duncan Mak ([email protected])
  6. * Gonzalo Paniagua Javier ([email protected])
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sublicense, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject to
  14. * the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. * Copyright (c) 2006-2009 Novell, Inc.
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <glib.h>
  32. gpointer
  33. g_queue_pop_head (GQueue *queue)
  34. {
  35. gpointer result;
  36. GList *old_head;
  37. if (!queue || queue->length == 0)
  38. return NULL;
  39. result = queue->head->data;
  40. old_head = queue->head;
  41. queue->head = old_head->next;
  42. g_list_free_1 (old_head);
  43. if (--queue->length)
  44. queue->head->prev = NULL;
  45. else
  46. queue->tail = NULL;
  47. return result;
  48. }
  49. gboolean
  50. g_queue_is_empty (GQueue *queue)
  51. {
  52. if (!queue)
  53. return TRUE;
  54. return queue->length == 0;
  55. }
  56. void
  57. g_queue_push_head (GQueue *queue, gpointer head)
  58. {
  59. if (!queue)
  60. return;
  61. queue->head = g_list_prepend (queue->head, head);
  62. if (!queue->tail)
  63. queue->tail = queue->head;
  64. queue->length ++;
  65. }
  66. void
  67. g_queue_push_tail (GQueue *queue, gpointer data)
  68. {
  69. if (!queue)
  70. return;
  71. queue->tail = g_list_append (queue->tail, data);
  72. if (queue->head == NULL)
  73. queue->head = queue->tail;
  74. else
  75. queue->tail = queue->tail->next;
  76. queue->length++;
  77. }
  78. GQueue *
  79. g_queue_new (void)
  80. {
  81. return g_new0 (GQueue, 1);
  82. }
  83. void
  84. g_queue_free (GQueue *queue)
  85. {
  86. if (!queue)
  87. return;
  88. g_list_free (queue->head);
  89. g_free (queue);
  90. }
  91. void
  92. g_queue_foreach (GQueue *queue, GFunc func, gpointer user_data)
  93. {
  94. g_list_foreach (queue->head, func, user_data);
  95. }