gqueue.c 20.1 KB
Newer Older
1
/* GLIB - Library of useful routines for C programming
2 3 4 5
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * GQueue: Double ended queue implementation, piggy backed on GList.
 * Copyright (C) 1998 Tim Janik
6 7
 *
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9 10 11 12 13 14
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 19 20 21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
22

23 24 25
/*
 * MT safe
 */
26

27
#include "config.h"
28

29
#include "glib.h"
30
#include "galias.h"
31

32 33 34 35 36 37 38
/**
 * g_queue_new:
 *
 * Creates a new #GQueue. 
 *
 * Returns: a new #GQueue.
 **/
39
GQueue*
40
g_queue_new (void)
41
{
42
  return g_slice_new0 (GQueue);
43 44
}

45 46 47 48
/**
 * g_queue_free:
 * @queue: a #GQueue.
 *
49 50 51
 * Frees the memory allocated for the #GQueue. Only call this function if
 * @queue was created with g_queue_new(). If queue elements contain
 * dynamically-allocated memory, they should be freed first.
52
 **/
53
void
54
g_queue_free (GQueue *queue)
55
{
56
  g_return_if_fail (queue != NULL);
57

58
  g_list_free (queue->head);
59
  g_slice_free (GQueue, queue);
60 61
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/**
 * g_queue_init:
 * @queue: an uninitialized #GQueue
 *
 * A statically-allocated #GQueue must be initialized with this function
 * before it can be used. Alternatively you can initialize it with
 * #G_QUEUE_INIT. It is not necessary to initialize queues created with
 * g_queue_new().
 *
 * Since: 2.14
 **/
void
g_queue_init (GQueue *queue)
{
  g_return_if_fail (queue != NULL);

  queue->head = queue->tail = NULL;
  queue->length = 0;
}

/**
 * g_queue_clear:
 * @queue: a #GQueue
 *
 * Removes all the elements in @queue. If queue elements contain
 * dynamically-allocated memory, they should be freed first.
 *
 * Since: 2.14
 */
void
g_queue_clear (GQueue *queue)
{
  g_return_if_fail (queue != NULL);

  g_list_free (queue->head);
  g_queue_init (queue);
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/**
 * g_queue_is_empty:
 * @queue: a #GQueue.
 *
 * Returns %TRUE if the queue is empty.
 *
 * Returns: %TRUE if the queue is empty.
 **/
gboolean
g_queue_is_empty (GQueue *queue)
{
  g_return_val_if_fail (queue != NULL, TRUE);

  return queue->head == NULL;
}

/**
 * g_queue_get_length:
 * @queue: a #GQueue
 * 
 * Returns the number of items in @queue.
 * 
 * Return value: The number of items in @queue.
 * 
 * Since: 2.4
 **/
guint
g_queue_get_length (GQueue *queue)
{
  g_return_val_if_fail (queue != NULL, 0);

  return queue->length;
}

/**
 * g_queue_reverse:
 * @queue: a #GQueue
 * 
 * Reverses the order of the items in @queue.
 * 
 * Since: 2.4
 **/
void
g_queue_reverse (GQueue *queue)
{
  g_return_if_fail (queue != NULL);

  queue->tail = queue->head;
  queue->head = g_list_reverse (queue->head);
}

/**
 * g_queue_copy:
 * @queue: a #GQueue
 * 
Matthias Clasen's avatar
Matthias Clasen committed
155
 * Copies a @queue. Note that is a shallow copy. If the elements in the
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
 * queue consist of pointers to data, the pointers are copied, but the
 * actual data is not.
 * 
 * Return value: A copy of @queue
 * 
 * Since: 2.4
 **/
GQueue *
g_queue_copy (GQueue *queue)
{
  GQueue *result;
  GList *list;

  g_return_val_if_fail (queue != NULL, NULL);

  result = g_queue_new ();

  for (list = queue->head; list != NULL; list = list->next)
    g_queue_push_tail (result, list->data);

  return result;
}

/**
 * g_queue_foreach:
 * @queue: a #GQueue
 * @func: the function to call for each element's data
 * @user_data: user data to pass to @func
 * 
 * Calls @func for each element in the queue passing @user_data to the
 * function.
 * 
 * Since: 2.4
 **/
void
g_queue_foreach (GQueue   *queue,
		 GFunc     func,
		 gpointer  user_data)
{
  GList *list;

  g_return_if_fail (queue != NULL);
  g_return_if_fail (func != NULL);
  
  list = queue->head;
  while (list)
    {
      GList *next = list->next;
      func (list->data, user_data);
      list = next;
    }
}

/**
 * g_queue_find:
 * @queue: a #GQueue
 * @data: data to find
 * 
 * Finds the first link in @queue which contains @data.
 * 
 * Return value: The first link in @queue which contains @data.
 * 
 * Since: 2.4
 **/
GList *
g_queue_find (GQueue        *queue,
	      gconstpointer  data)
{
  g_return_val_if_fail (queue != NULL, NULL);

  return g_list_find (queue->head, data);
}

/**
 * g_queue_find_custom:
 * @queue: a #GQueue
 * @data: user data passed to @func
 * @func: a #GCompareFunc to call for each element. It should return 0
 * when the desired element is found
 *
 * Finds an element in a #GQueue, using a supplied function to find the
 * desired element. It iterates over the queue, calling the given function
 * which should return 0 when the desired element is found. The function
239 240
 * takes two gconstpointer arguments, the #GQueue element's data as the
 * first argument and the given user data as the second argument.
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
 * 
 * Return value: The found link, or %NULL if it wasn't found
 * 
 * Since: 2.4
 **/
GList *
g_queue_find_custom    (GQueue        *queue,
			gconstpointer  data,
			GCompareFunc   func)
{
  g_return_val_if_fail (queue != NULL, NULL);
  g_return_val_if_fail (func != NULL, NULL);

  return g_list_find_custom (queue->head, data, func);
}

/**
 * g_queue_sort:
 * @queue: a #GQueue
 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
 *     is passed two elements of the queue and should return 0 if they are
 *     equal, a negative value if the first comes before the second, and
 *     a positive value if the second comes before the first.
 * @user_data: user data passed to @compare_func
 * 
 * Sorts @queue using @compare_func. 
 * 
 * Since: 2.4
 **/
void
g_queue_sort (GQueue           *queue,
	      GCompareDataFunc  compare_func,
	      gpointer          user_data)
{
  g_return_if_fail (queue != NULL);
  g_return_if_fail (compare_func != NULL);

  queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
  queue->tail = g_list_last (queue->head);
}

282 283 284 285 286 287 288
/**
 * g_queue_push_head:
 * @queue: a #GQueue.
 * @data: the data for the new element.
 *
 * Adds a new element at the head of the queue.
 **/
289
void
290 291
g_queue_push_head (GQueue  *queue,
		   gpointer data)
292
{
293
  g_return_if_fail (queue != NULL);
294

295 296 297 298 299
  queue->head = g_list_prepend (queue->head, data);
  if (!queue->tail)
    queue->tail = queue->head;
  queue->length++;
}
300

301 302 303 304 305 306 307 308
/**
 * g_queue_push_nth:
 * @queue: a #GQueue
 * @data: the data for the new element
 * @n: the position to insert the new element. If @n is negative or
 *     larger than the number of elements in the @queue, the element is
 *     added to the end of the queue.
 * 
Matthias Clasen's avatar
Matthias Clasen committed
309
 * Inserts a new element into @queue at the given position
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
 * 
 * Since: 2.4
 **/
void
g_queue_push_nth (GQueue   *queue,
		  gpointer  data,
		  gint      n)
{
  g_return_if_fail (queue != NULL);

  if (n < 0 || n >= queue->length)
    {
      g_queue_push_tail (queue, data);
      return;
    }

  g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
}

329 330 331
/**
 * g_queue_push_head_link:
 * @queue: a #GQueue.
332
 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
333
 *     more than one element.
334 335 336
 *
 * Adds a new element at the head of the queue.
 **/
337 338 339 340 341 342
void
g_queue_push_head_link (GQueue *queue,
			GList  *link)
{
  g_return_if_fail (queue != NULL);
  g_return_if_fail (link != NULL);
343 344
  g_return_if_fail (link->prev == NULL);
  g_return_if_fail (link->next == NULL);
345 346 347 348 349 350 351 352

  link->next = queue->head;
  if (queue->head)
    queue->head->prev = link;
  else
    queue->tail = link;
  queue->head = link;
  queue->length++;
353 354
}

355 356 357 358 359 360 361
/**
 * g_queue_push_tail:
 * @queue: a #GQueue.
 * @data: the data for the new element.
 *
 * Adds a new element at the tail of the queue.
 **/
362 363 364 365 366 367 368 369 370 371 372 373 374
void
g_queue_push_tail (GQueue  *queue,
		   gpointer data)
{
  g_return_if_fail (queue != NULL);

  queue->tail = g_list_append (queue->tail, data);
  if (queue->tail->next)
    queue->tail = queue->tail->next;
  else
    queue->head = queue->tail;
  queue->length++;
}
375

376 377 378
/**
 * g_queue_push_tail_link:
 * @queue: a #GQueue.
379
 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
380 381 382 383
 *   more than one element.
 *
 * Adds a new element at the tail of the queue.
 **/
384
void
385 386
g_queue_push_tail_link (GQueue *queue,
			GList  *link)
387
{
388 389
  g_return_if_fail (queue != NULL);
  g_return_if_fail (link != NULL);
390 391
  g_return_if_fail (link->prev == NULL);
  g_return_if_fail (link->next == NULL);
392 393 394 395 396 397 398 399 400 401

  link->prev = queue->tail;
  if (queue->tail)
    queue->tail->next = link;
  else
    queue->head = link;
  queue->tail = link;
  queue->length++;
}

402 403 404 405 406 407 408 409
/**
 * g_queue_push_nth_link:
 * @queue: a #GQueue
 * @n: the position to insert the link. If this is negative or larger than
 *     the number of elements in @queue, the link is added to the end of
 *     @queue.
 * @link_: the link to add to @queue
 * 
Matthias Clasen's avatar
Matthias Clasen committed
410
 * Inserts @link into @queue at the given position.
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
 * 
 * Since: 2.4
 **/
void
g_queue_push_nth_link  (GQueue  *queue,
			gint     n,
			GList   *link_)
{
  GList *next;
  GList *prev;
  
  g_return_if_fail (queue != NULL);
  g_return_if_fail (link_ != NULL);

  if (n < 0 || n >= queue->length)
    {
      g_queue_push_tail_link (queue, link_);
      return;
    }

  g_assert (queue->head);
  g_assert (queue->tail);

  next = g_queue_peek_nth_link (queue, n);
  prev = next->prev;

  if (prev)
    prev->next = link_;
  next->prev = link_;

  link_->next = next;
  link_->prev = prev;

  if (queue->head->prev)
    queue->head = queue->head->prev;

  if (queue->tail->next)
    queue->tail = queue->tail->next;
  
  queue->length++;
}

453 454 455 456 457 458 459 460 461
/**
 * g_queue_pop_head:
 * @queue: a #GQueue.
 *
 * Removes the first element of the queue.
 *
 * Returns: the data of the first element in the queue, or %NULL if the queue
 *   is empty.
 **/
462 463 464 465 466 467
gpointer
g_queue_pop_head (GQueue *queue)
{
  g_return_val_if_fail (queue != NULL, NULL);

  if (queue->head)
468
    {
469 470
      GList *node = queue->head;
      gpointer data = node->data;
471

472 473 474
      queue->head = node->next;
      if (queue->head)
	queue->head->prev = NULL;
475
      else
476 477 478
	queue->tail = NULL;
      g_list_free_1 (node);
      queue->length--;
479

480
      return data;
481
    }
482

483 484
  return NULL;
}
485

486 487 488 489 490 491 492 493 494
/**
 * g_queue_pop_head_link:
 * @queue: a #GQueue.
 *
 * Removes the first element of the queue.
 *
 * Returns: the #GList element at the head of the queue, or %NULL if the queue
 *   is empty.
 **/
495 496
GList*
g_queue_pop_head_link (GQueue *queue)
497
{
498
  g_return_val_if_fail (queue != NULL, NULL);
499

500
  if (queue->head)
501
    {
502
      GList *node = queue->head;
503

504 505 506 507 508 509
      queue->head = node->next;
      if (queue->head)
	{
	  queue->head->prev = NULL;
	  node->next = NULL;
	}
510
      else
511 512
	queue->tail = NULL;
      queue->length--;
513

514
      return node;
515 516
    }

517
  return NULL;
518 519
}

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
/**
 * g_queue_peek_head_link:
 * @queue: a #GQueue
 * 
 * Returns the first link in @queue
 * 
 * Return value: the first link in @queue, or %NULL if @queue is empty
 * 
 * Since: 2.4
 **/
GList*
g_queue_peek_head_link (GQueue *queue)
{
  g_return_val_if_fail (queue != NULL, NULL);

  return queue->head;
}

/**
 * g_queue_peek_tail_link:
 * @queue: a #GQueue
 * 
 * Returns the last link @queue.
 * 
 * Return value: the last link in @queue, or %NULL if @queue is empty
 * 
 * Since: 2.4
 **/
GList*
g_queue_peek_tail_link (GQueue *queue)
{
  g_return_val_if_fail (queue != NULL, NULL);

  return queue->tail;
}

556 557 558 559 560 561 562 563 564
/**
 * g_queue_pop_tail:
 * @queue: a #GQueue.
 *
 * Removes the last element of the queue.
 *
 * Returns: the data of the last element in the queue, or %NULL if the queue
 *   is empty.
 **/
565
gpointer
566
g_queue_pop_tail (GQueue *queue)
567
{
568
  g_return_val_if_fail (queue != NULL, NULL);
569

570
  if (queue->tail)
571
    {
572 573 574 575 576 577 578 579 580 581
      GList *node = queue->tail;
      gpointer data = node->data;

      queue->tail = node->prev;
      if (queue->tail)
	queue->tail->next = NULL;
      else
	queue->head = NULL;
      queue->length--;
      g_list_free_1 (node);
582

583 584 585 586 587
      return data;
    }
  
  return NULL;
}
588

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
/**
 * g_queue_pop_nth:
 * @queue: a #GQueue
 * @n: the position of the element.
 * 
 * Removes the @n'th element of @queue.
 * 
 * Return value: the element's data, or %NULL if @n is off the end of @queue.
 * 
 * Since: 2.4
 **/
gpointer
g_queue_pop_nth (GQueue *queue,
		 guint   n)
{
  GList *nth_link;
  gpointer result;
  
  g_return_val_if_fail (queue != NULL, NULL);

  if (n >= queue->length)
    return NULL;
  
  nth_link = g_queue_peek_nth_link (queue, n);
  result = nth_link->data;

  g_queue_delete_link (queue, nth_link);

  return result;
}

620 621 622 623 624 625 626 627 628
/**
 * g_queue_pop_tail_link:
 * @queue: a #GQueue.
 *
 * Removes the last element of the queue.
 *
 * Returns: the #GList element at the tail of the queue, or %NULL if the queue
 *   is empty.
 **/
629 630 631 632 633 634 635 636 637 638 639
GList*
g_queue_pop_tail_link (GQueue *queue)
{
  g_return_val_if_fail (queue != NULL, NULL);
  
  if (queue->tail)
    {
      GList *node = queue->tail;
      
      queue->tail = node->prev;
      if (queue->tail)
640
	{
641 642 643
	  queue->tail->next = NULL;
	  node->prev = NULL;
	}
644
      else
645 646 647 648
	queue->head = NULL;
      queue->length--;
      
      return node;
649
    }
650 651 652
  
  return NULL;
}
653

654
/**
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
 * g_queue_pop_nth_link:
 * @queue: a #GQueue
 * @n: the link's position
 * 
 * Removes and returns the link at the given position.
 * 
 * Return value: The @n'th link, or %NULL if @n is off the end of @queue.
 * 
 * Since: 2.4
 **/
GList*
g_queue_pop_nth_link (GQueue *queue,
		      guint   n)
{
  GList *link;
  
  g_return_val_if_fail (queue != NULL, NULL);

673
  if (n >= queue->length)
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
    return NULL;
  
  link = g_queue_peek_nth_link (queue, n);
  g_queue_unlink (queue, link);

  return link;
}

/**
 * g_queue_peek_nth_link:
 * @queue: a #GQueue
 * @n: the position of the link
 * 
 * Returns the link at the given position
 * 
 * Return value: The link at the @n'th position, or %NULL if @n is off the
 * end of the list
 * 
 * Since: 2.4
 **/
GList *
g_queue_peek_nth_link (GQueue *queue,
		       guint   n)
{
  GList *link;
  gint i;
  
  g_return_val_if_fail (queue != NULL, NULL);

  if (n >= queue->length)
    return NULL;
  
  if (n > queue->length / 2)
    {
      n = queue->length - n - 1;

      link = queue->tail;
      for (i = 0; i < n; ++i)
	link = link->prev;
    }
  else
    {
      link = queue->head;
      for (i = 0; i < n; ++i)
	link = link->next;
    }

  return link;
}

/**
 * g_queue_link_index:
 * @queue: a #Gqueue
 * @link_: A #GList link
 * 
 * Returns the position of @link_ in @queue.
 * 
 * Return value: The position of @link_, or -1 if the link is
 * not part of @queue
 * 
 * Since: 2.4
 **/
gint
g_queue_link_index (GQueue *queue,
		    GList  *link_)
{
740
  g_return_val_if_fail (queue != NULL, -1);
741 742 743 744 745 746 747 748

  return g_list_position (queue->head, link_);
}

/**
 * g_queue_unlink
 * @queue: a #GQueue
 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
749
 *
750 751
 * Unlinks @link_ so that it will no longer be part of @queue. The link is
 * not freed.
752
 *
753 754 755
 * @link_ must be part of @queue,
 * 
 * Since: 2.4
756
 **/
757 758 759
void
g_queue_unlink (GQueue *queue,
		GList  *link_)
760
{
761 762
  g_return_if_fail (queue != NULL);
  g_return_if_fail (link_ != NULL);
763

764 765 766 767 768 769 770 771 772 773 774 775 776
  if (link_ == queue->tail)
    queue->tail = queue->tail->prev;
  
  queue->head = g_list_remove_link (queue->head, link_);
  queue->length--;
}

/**
 * g_queue_delete_link:
 * @queue: a #GQueue
 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
 *
 * Removes @link_ from @queue and frees it.
Soeren Sandmann's avatar
Soeren Sandmann committed
777 778
 *
 * @link_ must be part of @queue.
779 780 781 782 783 784 785 786 787 788 789 790
 * 
 * Since: 2.4
 **/
void
g_queue_delete_link (GQueue *queue,
		     GList  *link_)
{
  g_return_if_fail (queue != NULL);
  g_return_if_fail (link_ != NULL);

  g_queue_unlink (queue, link_);
  g_list_free (link_);
791 792
}

793 794 795 796 797 798 799 800 801
/**
 * g_queue_peek_head:
 * @queue: a #GQueue.
 *
 * Returns the first element of the queue.
 *
 * Returns: the data of the first element in the queue, or %NULL if the queue
 *   is empty.
 **/
802 803 804 805
gpointer
g_queue_peek_head (GQueue *queue)
{
  g_return_val_if_fail (queue != NULL, NULL);
806

807 808 809
  return queue->head ? queue->head->data : NULL;
}

810 811 812 813 814 815 816 817 818
/**
 * g_queue_peek_tail:
 * @queue: a #GQueue.
 *
 * Returns the last element of the queue.
 *
 * Returns: the data of the last element in the queue, or %NULL if the queue
 *   is empty.
 **/
819 820 821 822 823 824 825
gpointer
g_queue_peek_tail (GQueue *queue)
{
  g_return_val_if_fail (queue != NULL, NULL);

  return queue->tail ? queue->tail->data : NULL;
}
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933

/**
 * g_queue_peek_nth:
 * @queue: a #GQueue
 * @n: the position of the element.
 * 
 * Returns the @n'th element of @queue. 
 * 
 * Return value: The data for the @n'th element of @queue, or %NULL if @n is
 *   off the end of @queue.
 * 
 * Since: 2.4
 **/
gpointer
g_queue_peek_nth (GQueue *queue,
		  guint   n)
{
  GList *link;
  
  g_return_val_if_fail (queue != NULL, NULL);

  link = g_queue_peek_nth_link (queue, n);

  if (link)
    return link->data;

  return NULL;
}

/**
 * g_queue_index:
 * @queue: a #GQueue
 * @data: the data to find.
 * 
 * Returns the position of the first element in @queue which contains @data.
 * 
 * Return value: The position of the first element in @queue which contains @data, or -1 if no element in @queue contains @data.
 * 
 * Since: 2.4
 **/
gint
g_queue_index (GQueue        *queue,
	       gconstpointer  data)
{
  g_return_val_if_fail (queue != NULL, -1);

  return g_list_index (queue->head, data);
}

/**
 * g_queue_remove:
 * @queue: a #GQueue
 * @data: data to remove.
 * 
 * Removes the first element in @queue that contains @data. 
 * 
 * Since: 2.4
 **/
void
g_queue_remove (GQueue        *queue,
		gconstpointer  data)
{
  GList *link;
  
  g_return_if_fail (queue != NULL);

  link = g_list_find (queue->head, data);

  if (link)
    g_queue_delete_link (queue, link);
}

/**
 * g_queue_remove_all:
 * @queue: a #GQueue
 * @data: data to remove
 * 
 * Remove all elemeents in @queue which contains @data.
 * 
 * Since: 2.4
 **/
void
g_queue_remove_all (GQueue        *queue,
		    gconstpointer  data)
{
  GList *list;
  
  g_return_if_fail (queue != NULL);

  list = queue->head;
  while (list)
    {
      GList *next = list->next;

      if (list->data == data)
	g_queue_delete_link (queue, list);
      
      list = next;
    }
}

/**
 * g_queue_insert_before:
 * @queue: a #GQueue
 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
 * @data: the data to insert
 * 
 * Inserts @data into @queue before @sibling.
Soeren Sandmann's avatar
Soeren Sandmann committed
934 935
 *
 * @sibling must be part of @queue.
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
 * 
 * Since: 2.4
 **/
void
g_queue_insert_before (GQueue   *queue,
		       GList    *sibling,
		       gpointer  data)
{
  g_return_if_fail (queue != NULL);
  g_return_if_fail (sibling != NULL);

  queue->head = g_list_insert_before (queue->head, sibling, data);
  queue->length++;
}

/**
 * g_queue_insert_after:
 * @queue: a #GQueue
 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
 * @data: the data to insert
 *
 * Inserts @data into @queue after @sibling
Soeren Sandmann's avatar
Soeren Sandmann committed
958 959
 *
 * @sibling must be part of @queue
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
 * 
 * Since: 2.4
 **/
void
g_queue_insert_after (GQueue   *queue,
		      GList    *sibling,
		      gpointer  data)
{
  g_return_if_fail (queue != NULL);
  g_return_if_fail (sibling != NULL);

  if (sibling == queue->tail)
    g_queue_push_tail (queue, data);
  else
    g_queue_insert_before (queue, sibling->next, data);
}

/**
 * g_queue_insert_sorted:
 * @queue: a #GQueue
 * @data: the data to insert
 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
 *     called with two elements of the @queue and @user_data. It should
 *     return 0 if the elements are equal, a negative value if the first
 *     element comes before the second, and a positive value if the second
Matthias Clasen's avatar
Matthias Clasen committed
985
 *     element comes before the first.
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
 * @user_data: user data passed to @func.
 * 
 * Inserts @data into @queue using @func to determine the new position.
 * 
 * Since: 2.4
 **/
void
g_queue_insert_sorted (GQueue           *queue,
		       gpointer          data,
		       GCompareDataFunc  func,
		       gpointer          user_data)
{
  GList *list;
  
  g_return_if_fail (queue != NULL);

  list = queue->head;
  while (list && func (list->data, data, user_data) < 0)
    list = list->next;

  if (list)
    g_queue_insert_before (queue, list, data);
  else
    g_queue_push_tail (queue, data);
}
1011 1012 1013

#define __G_QUEUE_C__
#include "galiasdef.c"