aboutsummaryrefslogtreecommitdiff
path: root/src/sort.c
blob: c82fe0107453c2216624799e93265710c2a990e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
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
/* grecs - Gray's Extensible Configuration System
   Copyright (C) 2007-2016 Sergey Poznyakoff

   Grecs is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Grecs 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with Grecs. If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include "grecs.h"

struct node_list {
	struct grecs_node *head, *tail;
};

static void
node_list_init(struct node_list *list, struct grecs_node *node)
{
	if (node) {
		list->head = node;
		while (node->next)
			node = node->next;
		list->tail = node;
	} else
		list->head = list->tail = NULL;
}

static void
node_list_add(struct node_list *list, struct grecs_node *node)
{
	node->next = NULL;
	node->prev = list->tail;
	if (list->tail)
		list->tail->next = node;
	else
		list->head = node;
	list->tail = node;
}

static void
node_list_join(struct node_list *a, struct node_list *b)
{
	if (!b->head)
		return;
	b->head->prev = a->tail;
	if (a->tail)
		a->tail->next = b->head;
	else
		a->head = b->head;
	a->tail = b->tail;
}

static void
_qsort_nodelist(struct node_list *list,
		int (*compare)(struct grecs_node const *,
			       struct grecs_node const *))
{
	struct grecs_node *cur, *middle;
	struct node_list high_list, low_list;
	int rc;
	
	if (!list->head)
		return;
	cur = list->head;
	do {
		cur = cur->next;
		if (!cur)
			return;
	} while ((rc = compare(list->head, cur)) == 0);

	/* Select the lower of the two as the middle value */
	middle = (rc > 0) ? cur : list->head;

	/* Split into two sublists */
	node_list_init(&low_list, NULL);
	node_list_init(&high_list, NULL);
	for (cur = list->head; cur; ) {
		struct grecs_node *next = cur->next;
		cur->next = NULL;
		if (compare(middle, cur) < 0)
			node_list_add(&high_list, cur);
		else
			node_list_add(&low_list, cur);
		cur = next;
	}

	if (!low_list.head)
		low_list = high_list;
	else if (high_list.head) {
		/* Sort both sublists recursively */
		_qsort_nodelist(&low_list, compare);
		_qsort_nodelist(&high_list, compare);
		/* Join both lists in order */
		node_list_join(&low_list, &high_list);
	}
	/* Return the resulting list */
	list->head = low_list.head;
	list->tail = low_list.tail;
}

struct grecs_node *
grecs_nodelist_sort(struct grecs_node *node,
		    int (*compare)(struct grecs_node const *,
				   struct grecs_node const *))
{
	struct node_list list;
	node_list_init(&list, node);
	_qsort_nodelist(&list, compare);
	return list.head;
}

void
grecs_tree_sort(struct grecs_node *node,
		int (*compare)(struct grecs_node const *,
			       struct grecs_node const *))
{
	if (node && node->down) {
		node->down = grecs_nodelist_sort(node->down, compare);
		for (node = node->down; node; node = node->next)
			grecs_tree_sort(node, compare);
	}
}

Return to:

Send suggestions and report system problems to the System administrator.