~ubuntu-branches/debian/sid/glib2.0/sid

« back to all changes in this revision

Viewing changes to glib/glist.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-05-08 06:25:57 UTC
  • mfrom: (1.27.14) (3.1.181 experimental)
  • Revision ID: package-import@ubuntu.com-20130508062557-i7gbku66mls70gi2
Tags: 2.36.1-2
Merge experimental branch, upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
#include "glist.h"
34
34
#include "gslice.h"
 
35
#include "gmessages.h"
35
36
 
36
37
#include "gtestutils.h"
37
38
 
97
98
/**
98
99
 * g_list_previous:
99
100
 * @list: an element in a #GList.
100
 
 * @Returns: the previous element, or %NULL if there are no previous
101
 
 *           elements.
102
101
 *
103
102
 * A convenience macro to get the previous element in a #GList.
 
103
 *
 
104
 * Returns: the previous element, or %NULL if there are no previous
 
105
 *          elements.
104
106
 **/
105
107
 
106
108
/**
107
109
 * g_list_next:
108
110
 * @list: an element in a #GList.
109
 
 * @Returns: the next element, or %NULL if there are no more elements.
110
111
 *
111
112
 * A convenience macro to get the next element in a #GList.
 
113
 *
 
114
 * Returns: the next element, or %NULL if there are no more elements.
112
115
 **/
113
116
 
114
117
#define _g_list_alloc()         g_slice_new (GList)
117
120
 
118
121
/**
119
122
 * g_list_alloc:
120
 
 * @Returns: a pointer to the newly-allocated #GList element.
121
123
 *
122
124
 * Allocates space for one #GList element. It is called by
123
125
 * g_list_append(), g_list_prepend(), g_list_insert() and
124
126
 * g_list_insert_sorted() and so is rarely used on its own.
 
127
 *
 
128
 * Returns: a pointer to the newly-allocated #GList element.
125
129
 **/
126
130
GList*
127
131
g_list_alloc (void)
417
421
  return list1;
418
422
}
419
423
 
 
424
static inline GList*
 
425
_g_list_remove_link (GList *list,
 
426
                     GList *link)
 
427
{
 
428
  if (link == NULL)
 
429
    return list;
 
430
 
 
431
  if (link->prev)
 
432
    {
 
433
      if (link->prev->next == link)
 
434
        link->prev->next = link->next;
 
435
      else
 
436
        g_warning ("corrupted double-linked list detected");
 
437
    }
 
438
  if (link->next)
 
439
    {
 
440
      if (link->next->prev == link)
 
441
        link->next->prev = link->prev;
 
442
      else
 
443
        g_warning ("corrupted double-linked list detected");
 
444
    }
 
445
 
 
446
  if (link == list)
 
447
    list = list->next;
 
448
 
 
449
  link->next = NULL;
 
450
  link->prev = NULL;
 
451
 
 
452
  return list;
 
453
}
 
454
 
420
455
/**
421
456
 * g_list_remove:
422
457
 * @list: a #GList
433
468
               gconstpointer  data)
434
469
{
435
470
  GList *tmp;
436
 
  
 
471
 
437
472
  tmp = list;
438
473
  while (tmp)
439
474
    {
441
476
        tmp = tmp->next;
442
477
      else
443
478
        {
444
 
          if (tmp->prev)
445
 
            tmp->prev->next = tmp->next;
446
 
          if (tmp->next)
447
 
            tmp->next->prev = tmp->prev;
448
 
          
449
 
          if (list == tmp)
450
 
            list = list->next;
451
 
          
 
479
          list = _g_list_remove_link (list, tmp);
452
480
          _g_list_free1 (tmp);
453
 
          
 
481
 
454
482
          break;
455
483
        }
456
484
    }
462
490
 * @list: a #GList
463
491
 * @data: data to remove
464
492
 *
465
 
 * Removes all list nodes with data equal to @data. 
466
 
 * Returns the new head of the list. Contrast with 
467
 
 * g_list_remove() which removes only the first node 
 
493
 * Removes all list nodes with data equal to @data.
 
494
 * Returns the new head of the list. Contrast with
 
495
 * g_list_remove() which removes only the first node
468
496
 * matching the given data.
469
497
 *
470
498
 * Returns: new head of @list
497
525
  return list;
498
526
}
499
527
 
500
 
static inline GList*
501
 
_g_list_remove_link (GList *list,
502
 
                     GList *link)
503
 
{
504
 
  if (link)
505
 
    {
506
 
      if (link->prev)
507
 
        link->prev->next = link->next;
508
 
      if (link->next)
509
 
        link->next->prev = link->prev;
510
 
      
511
 
      if (link == list)
512
 
        list = list->next;
513
 
      
514
 
      link->next = NULL;
515
 
      link->prev = NULL;
516
 
    }
517
 
  
518
 
  return list;
519
 
}
520
 
 
521
528
/**
522
529
 * g_list_remove_link:
523
530
 * @list: a #GList
566
573
 * <note><para>
567
574
 * Note that this is a "shallow" copy. If the list elements 
568
575
 * consist of pointers to data, the pointers are copied but 
569
 
 * the actual data is not.
 
576
 * the actual data is not. See g_list_copy_deep() if you need
 
577
 * to copy the data as well.
570
578
 * </para></note>
571
579
 *
572
580
 * Returns: a copy of @list
574
582
GList*
575
583
g_list_copy (GList *list)
576
584
{
 
585
  return g_list_copy_deep (list, NULL, NULL);
 
586
}
 
587
 
 
588
/**
 
589
 * g_list_copy_deep:
 
590
 * @list: a #GList
 
591
 * @func: a copy function used to copy every element in the list
 
592
 * @user_data: user data passed to the copy function @func, or #NULL
 
593
 *
 
594
 * Makes a full (deep) copy of a #GList.
 
595
 *
 
596
 * In contrast with g_list_copy(), this function uses @func to make a copy of
 
597
 * each list element, in addition to copying the list container itself.
 
598
 *
 
599
 * @func, as a #GCopyFunc, takes two arguments, the data to be copied and a user
 
600
 * pointer. It's safe to pass #NULL as user_data, if the copy function takes only
 
601
 * one argument.
 
602
 *
 
603
 * For instance, if @list holds a list of GObjects, you can do:
 
604
 * |[
 
605
 * another_list = g_list_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
 
606
 * ]|
 
607
 *
 
608
 * And, to entirely free the new list, you could do:
 
609
 * |[
 
610
 * g_list_free_full (another_list, g_object_unref);
 
611
 * ]|
 
612
 *
 
613
 * Returns: a full copy of @list, use #g_list_free_full to free it
 
614
 *
 
615
 * Since: 2.34
 
616
 */
 
617
GList*
 
618
g_list_copy_deep (GList *list, GCopyFunc func, gpointer user_data)
 
619
{
577
620
  GList *new_list = NULL;
578
621
 
579
622
  if (list)
581
624
      GList *last;
582
625
 
583
626
      new_list = _g_list_alloc ();
584
 
      new_list->data = list->data;
 
627
      if (func)
 
628
        new_list->data = func (list->data, user_data);
 
629
      else
 
630
        new_list->data = list->data;
585
631
      new_list->prev = NULL;
586
632
      last = new_list;
587
633
      list = list->next;
590
636
          last->next = _g_list_alloc ();
591
637
          last->next->prev = last;
592
638
          last = last->next;
593
 
          last->data = list->data;
 
639
          if (func)
 
640
            last->data = func (list->data, user_data);
 
641
          else
 
642
            last->data = list->data;
594
643
          list = list->next;
595
644
        }
596
645
      last->next = NULL;
1084
1133
 * GCompareFunc:
1085
1134
 * @a: a value.
1086
1135
 * @b: a value to compare with.
1087
 
 * @Returns: negative value if @a &lt; @b; zero if @a = @b; positive
1088
 
 *           value if @a > @b.
1089
1136
 *
1090
1137
 * Specifies the type of a comparison function used to compare two
1091
1138
 * values.  The function should return a negative integer if the first
1092
1139
 * value comes before the second, 0 if they are equal, or a positive
1093
1140
 * integer if the first value comes after the second.
 
1141
 *
 
1142
 * Returns: negative value if @a &lt; @b; zero if @a = @b; positive
 
1143
 *          value if @a > @b.
1094
1144
 **/
1095
1145
GList *
1096
1146
g_list_sort (GList        *list,
1116
1166
 * @a: a value.
1117
1167
 * @b: a value to compare with.
1118
1168
 * @user_data: user data to pass to comparison function.
1119
 
 * @Returns: negative value if @a &lt; @b; zero if @a = @b; positive
1120
 
 *           value if @a > @b.
1121
1169
 *
1122
1170
 * Specifies the type of a comparison function used to compare two
1123
1171
 * values.  The function should return a negative integer if the first
1124
1172
 * value comes before the second, 0 if they are equal, or a positive
1125
1173
 * integer if the first value comes after the second.
 
1174
 *
 
1175
 * Returns: negative value if @a &lt; @b; zero if @a = @b; positive
 
1176
 *          value if @a > @b.
1126
1177
 **/
1127
1178
GList *
1128
1179
g_list_sort_with_data (GList            *list,