1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* gnome-vfs-list-sort.c - GList sorting function for the GNOME |
---|
3 | Virtual File System. |
---|
4 | |
---|
5 | Copyright (C) 1999 Free Software Foundation |
---|
6 | |
---|
7 | The Gnome Library is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU Library General Public License as |
---|
9 | published by the Free Software Foundation; either version 2 of the |
---|
10 | License, or (at your option) any later version. |
---|
11 | |
---|
12 | The Gnome Library is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | Library General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Library General Public |
---|
18 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
19 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
20 | Boston, MA 02111-1307, USA. |
---|
21 | |
---|
22 | Author: Sven Oliver <sven.over@ob.kamp.net> |
---|
23 | Modified by Ettore Perazzoli <ettore@comm2000.it> to let the compare |
---|
24 | functions get an additional gpointer parameter. |
---|
25 | */ |
---|
26 | |
---|
27 | #ifdef HAVE_CONFIG_H |
---|
28 | #include <config.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <glib.h> |
---|
32 | |
---|
33 | #include "gnome-vfs.h" |
---|
34 | #include "gnome-vfs-private.h" |
---|
35 | |
---|
36 | /* This is the same as the GLIB implementation of `g_list_sort()', but |
---|
37 | uses GnomeVFSListCompareFunc instead of GCompareFunc, which lets us |
---|
38 | pass an additional gpointer parameter to the sorting function. */ |
---|
39 | |
---|
40 | static GList * |
---|
41 | gnome_vfs_list_sort_merge (GList *l1, |
---|
42 | GList *l2, |
---|
43 | GnomeVFSListCompareFunc compare_func, |
---|
44 | gpointer data) |
---|
45 | { |
---|
46 | GList list, *l, *lprev; |
---|
47 | |
---|
48 | l = &list; |
---|
49 | lprev = NULL; |
---|
50 | |
---|
51 | while (l1 && l2) { |
---|
52 | if (compare_func (l1->data, l2->data, data) < 0) { |
---|
53 | l->next = l1; |
---|
54 | l = l->next; |
---|
55 | l->prev = lprev; |
---|
56 | lprev = l; |
---|
57 | l1 = l1->next; |
---|
58 | } else { |
---|
59 | l->next = l2; |
---|
60 | l = l->next; |
---|
61 | l->prev = lprev; |
---|
62 | lprev = l; |
---|
63 | l2 = l2->next; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | l->next = l1 ? l1 : l2; |
---|
68 | l->next->prev = l; |
---|
69 | |
---|
70 | return list.next; |
---|
71 | } |
---|
72 | |
---|
73 | GList * |
---|
74 | gnome_vfs_list_sort (GList *list, |
---|
75 | GnomeVFSListCompareFunc compare_func, |
---|
76 | gpointer data) |
---|
77 | { |
---|
78 | GList *l1, *l2; |
---|
79 | |
---|
80 | if (!list) |
---|
81 | return NULL; |
---|
82 | if (!list->next) |
---|
83 | return list; |
---|
84 | |
---|
85 | l1 = list; |
---|
86 | l2 = list->next; |
---|
87 | |
---|
88 | while ((l2 = l2->next) != NULL) { |
---|
89 | if ((l2 = l2->next) == NULL) |
---|
90 | break; |
---|
91 | l1 = l1->next; |
---|
92 | } |
---|
93 | |
---|
94 | l2 = l1->next; |
---|
95 | l1->next = NULL; |
---|
96 | |
---|
97 | return gnome_vfs_list_sort_merge |
---|
98 | (gnome_vfs_list_sort (list, compare_func, data), |
---|
99 | gnome_vfs_list_sort (l2, compare_func, data), |
---|
100 | compare_func, |
---|
101 | data); |
---|
102 | } |
---|
103 | |
---|